$(document).ready(function(){
	var isDropDownOpened = false;
	var isMouseOverDropD = false;
	var clickOnce = 0;
	var LanguageOptions = $('#LanguageOptions');
	var LanguageDropDown_Select = $('#LanguageDropDown_Select');
	var LanguageOptions = $('#LanguageOptions');
	var Options = $('#LanguageOptions .Options');
	var selectedOption = $('#LanguageOptions .Selected');
	var selectedLanguage = $('#selectedLanguage');
	var emailThisPage = $('#emailThisPage');
	
	
	emailThisPage.attr("href", emailThisPage_URL);
	
	//highlightCurrentLang();
	
	LanguageDropDown_Select.bind('click', function(){
		checkLanDropD_state();
	});
	
	Options.bind('click', function(evt){
		var idName = this.id;
		var idNameHash = "#"+idName;
		var idObj = $(idNameHash);
		selectedLanguage.attr("value", idObj.attr('domain'));
		LanguageOptions.fadeOut(100, function(){isDropDownOpened = false;});
		LanguageDropDown_Select.html(idObj.html());
		//change actual language currentlang variable
		//switchLanguage(idObj.attr('domain'));
		location.href = "http://" + idObj.attr("redirectURL");
		
		Options.removeClass("Selected");
		idObj.addClass("Selected");
	});
	
	function checkLanDropD_state(){
		if(isDropDownOpened == false){
			LanguageOptions.fadeIn(100, function(){isDropDownOpened = true;});
			
			LanguageDropDown_Select.bind("mouseleave", function(e){
			$(document).bind("click", function(e){
				checkLanDropD_state();
				
				$(document).unbind("click");
				LanguageDropDown_Select.unbind("mouseleave");
			});
		});
			isMouseOverDropD = true;
		}
		else{
			LanguageOptions.fadeOut(100, function(){isDropDownOpened = false;});
			isMouseOverDropD = false;
		}
	}
	
	function delayCloseDnav(){
		LanguageOptions.fadeOut(100, function(){isDropDownOpened = false;});
	}
	
	function switchLanguage(lang){
		var currentHost = window.location.host;
		//var currentHost = window.location.href;
		var lang = lang;
		if(lang !== 'English'){
			switch(lang){
				case 'UK English':
					lang = 'uk';
					break;
				case 'German':
					lang = 'german';
					break;
				case 'Chinese':
					lang = 'chinese';
					break;
				case 'Tech Products':
					lang = 'tech';
					break;
			}
			var newLangURL = currentHost.replace(/fabreeka|\w*\.fabreeka/, lang+'.fabreeka');
			window.location = 'http://'+newLangURL;
			//window.location = newLangURL;
		}
		else{
			var newLangURL = currentHost.replace(/fabreeka|\w*\.fabreeka/, 'fabreeka');
			window.location = 'http://'+newLangURL;
			//window.location = newLangURL;
		}
	}	
	
	function highlightCurrentLang(){
		var currentURL = window.location.href;
		var currentLangURL = currentURL.match(/chinese|uk|german|tech/);
		if(currentLangURL !== null)
			$('#'+currentLangURL+'').addClass('Selected');
		else
			$('#eng').addClass('Selected');	
	}
});

function bookmarksite(){
	var url = window.location.href;
	var title = document.title;
	if (window.sidebar){ // firefox
		window.sidebar.addPanel(title, url, "");
	}
	else if(window.opera && window.print){ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	} 
	else if(document.all){// ie
		window.external.AddFavorite(url, title);
	}
}
function initSiteCredit(){
	$("#initLink").fadeOut("normal");
	$("#CreditsLink").animate( { right:"0" }, { duration:1000 } );
	setTimeout("revertSiteCredit()",7000);
}

function revertSiteCredit(){
	$("#CreditsLink").animate( { right:"-375" }, 1000, "linear", function(){$("#initLink").fadeIn("normal");} );
}

function checkBox(img, checkbox){	
	if ($(checkbox).attr("checked")) {
		$(img).attr("src", "/graphics/"+currentLang+"/Global/checkBoxOff.gif");
		$(checkbox).attr("checked", false)
    } else {
		$(img).attr("src", "/graphics/"+currentLang+"/Global/checkBoxOn.gif");
		$(checkbox).attr("checked", true)
    }
}

function searchInputEnterKey(evt, value){
	evt = (evt) ? evt : ((event) ? event : null);
	if (evt) {
		if(evt.keyCode == 13){
			var string= $('#SearchByName').val();	
			window.location = '/Products&searchstring='+string;
		}
	}
}

function loadSlideShow(lang){
	var imgCount;
	switch(lang){
		case 'tech':
			imgCount = 7;
			break;
		case 'german':
			imgCount = 10;
			break;
		case 'chinese':
			imgCount = 10;
			break;
		case 'uk':
			imgCount = 10;
			break;
		case 'eng':
			imgCount = 10;
			break;
	}
	rotateImages(imgCount);
}

var imgNumber = 1;
function rotateImages(imgCount){
	$.timer(5000, function (timer) {
		$("#slideImg"+imgNumber).fadeOut("slow", function(){ 
			if(imgNumber == imgCount){
				imgNumber = 0;	
			}
			imgNumber++;
			$("#slideImg"+imgNumber).fadeIn("normal");
		});
	});	
}

function toggleEventItems(elementID, classTotoggle){
	var elem = $(elementID);
	var classToDo = $(classTotoggle);
	classToDo.toggle();
}





