var $j = jQuery.noConflict();

var BrowserDetect = {
	init : function() {
		this.browser = this.searchString(this.dataBrowser)
				|| "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
				|| this.searchVersion(navigator.appVersion)
				|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString : function(data) {
		for ( var i = 0; i < data.length; i++) {
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch
					|| data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			} else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion : function(dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1)
			return;
		return parseFloat(dataString.substring(index
				+ this.versionSearchString.length + 1));
	},
	dataBrowser : [ {
		string :navigator.userAgent,
		subString :"Chrome",
		identity :"Chrome"
	}, {
		string :navigator.userAgent,
		subString :"OmniWeb",
		versionSearch :"OmniWeb/",
		identity :"OmniWeb"
	}, {
		string :navigator.vendor,
		subString :"Apple",
		identity :"Safari"
	}, {
		prop :window.opera,
		identity :"Opera"
	}, {
		string :navigator.vendor,
		subString :"iCab",
		identity :"iCab"
	}, {
		string :navigator.vendor,
		subString :"KDE",
		identity :"Konqueror"
	}, {
		string :navigator.userAgent,
		subString :"Firefox",
		identity :"Firefox"
	}, {
		string :navigator.vendor,
		subString :"Camino",
		identity :"Camino"
	}, { // for newer Netscapes (6+)
				string :navigator.userAgent,
				subString :"Netscape",
				identity :"Netscape"
			}, {
				string :navigator.userAgent,
				subString :"MSIE",
				identity :"Explorer",
				versionSearch :"MSIE"
			}, {
				string :navigator.userAgent,
				subString :"Gecko",
				identity :"Mozilla",
				versionSearch :"rv"
			}, { // for older Netscapes (4-)
				string :navigator.userAgent,
				subString :"Mozilla",
				identity :"Netscape",
				versionSearch :"Mozilla"
			} ],
	dataOS : [ {
		string :navigator.platform,
		subString :"Win",
		identity :"Windows"
	}, {
		string :navigator.platform,
		subString :"Mac",
		identity :"Mac"
	}, {
		string :navigator.platform,
		subString :"Linux",
		identity :"Linux"
	} ]

};

BrowserDetect.init();

$j(document).ready(
	function() {
		/*
		 * main menu effect
		 */
		if (BrowserDetect.browser != "Explorer" || BrowserDetect.version >= 7) {
			
			var menu_items = $j('div.main_menu > ul > li');
		
			menu_items.each( function(i) {
				
				if ($j(menu_items[i]).find('ul').length>0) {
					$j(menu_items[i]).data('original_height', $j(menu_items[i]).find('ul:first').height()+'px');
					$j(menu_items[i]).find('ul:first').hide();
					$j(menu_items[i]).find('ul:first').css({'visibility':'visible', 'height':'1px', 'opacity': 0});
					$j(menu_items[i]).hover(
							function() {
								$j(this).find('ul:first').stop().animate({'height': $j(this).data('original_height'), 'opacity': 1}, 3*parseInt($j(this).data('original_height')));
							}, function() {
								$j(this).find('ul:first').stop();
								$j(this).find('ul:first').css('overflow','hidden');
								$j(this).find('ul:first').animate({'height': '0px', 'opacity': 0}, 3*parseInt($j(this).data('original_height')));
							});
				}
			});
		}

		/*
		 * sliding effect for the home page latest news item
		 */
		var spec_news = $j('.latest_news_item_spec');

		spec_news.each( function(i) {
			$j(spec_news[i]).mouseenter(function() {
				$j(this).find('a .latest_news_item:first').stop(0).animate({'right': '-274px', 'width': '274px'}, 500);
			});
			$j(spec_news[i]).mouseleave(function() {
				$j(this).find('a .latest_news_item:first').stop(0).animate({'right': '-21px', 'width': '250px'}, 500);
			});
		});
		
		/*
		 * bottom right column element
		 */
		var bottom_els = $j('.columns_bottom_el');
		bottom_els.each( function(i) {
			$j(bottom_els[i]).html($j(bottom_els[i]).siblings('.columns_bottom_el_magic:first').html());
		});
		
		/*
		 * sort button
		 */ 
		var sort_buttons = $j('.sort_button');
		sort_buttons.each( function(i) {
			
			if (!$j(sort_buttons[i]).hasClass('back')) {
				$j(sort_buttons[i]).data('original_height', $j(sort_buttons[i]).find('.popup:first').height()+'px');
				$j(sort_buttons[i]).find('.popup:first').hide();
				$j(sort_buttons[i]).find('.popup:first').css({'visibility':'visible', 'height':'1px', 'opacity': 0});

				$j(sort_buttons[i]).find('a:first').click(function() {
					$j(this).siblings('.popup:first').stop().animate({'height': $j(this).parent().data('original_height'), 'opacity': 1}, 1.5*parseInt($j(this).parent().data('original_height')));
					//$j(this).siblings('.popup:first').css('visibility', 'visible'); 
					return false;
				});
				$j(sort_buttons[i]).mouseleave(function() {
					var popup = $j(this).find('a:first').siblings('.popup:first');
					popup.stop();
					popup.css('overflow','hidden');
					popup.animate({'height': '0px', 'opacity': 0}, 1.5*parseInt($j(this).data('original_height')));
					//$j(this).find('a:first').siblings('.popup:first').css('visibility', 'hidden'); 
				});
			}

		});
		
		/*
		 * tip a friend
		 */
		
		$j('#tipafriend').hide();
		
		$j('div.tipafriend').appendTo($j('div.container'));
		
		x = $j('div.header').height() + $j('div.page_content').height()- $j('div.util_menu_outer').height() - 16;
		y = $j('#tipafriend').height();
		z = $j('div.util_menu_outer').height();
	
		variety  = y - x  - 12; 
		bottom2 = 220 - variety;
		
		if(x<y){
			
			//prop = 'min-height';
			//$j('div.container_outer').css(prop, 'auto');
			//$j('div.container').css(prop, 'auto');

			$j('#tipafriend').css("bottom", bottom2);
		}
		
	
		if($j('#tipafriend').height() > $j('div.container_outer').height()){
			
			variety2 = $j('#tipafriend').height() - $j('div.container_outer').height();
			new_height = $j('div.container_outer').height() + variety2 + 3;
			bottom3 = bottom2 + 14;
			
			$j('div.container_outer').css("height", new_height);
			$j('div.container').css("height", new_height);
			$j('#tipafriend').css("bottom", bottom3);
		}
		
        if(bottom2 < 0){
			
			//bottom2 = 0;
			$j('#tipafriend').css("bottom", 0);
		}
	
        if ($j('div.tipafriend').css('position') == 'static'){
        	$j('div.tipafriend').appendTo($j('div.body'));
        	
        }

		
		/*
		 * IE6 fix
		 */
		try {
			document.execCommand("BackgroundImageCache", false, true);
		} catch (e) {}
	});
