﻿function searchTrucks() {
	if($("#homeSearchTrucksBox").height() == 50) {
		$("#homeSearchPlantBox").height(50);
		$("#homeSearchVansBox").height(50);
		$("#homeAdvancedSearchLink").fadeOut(50);
		$("#homeSearchTrucksBox").animate({"height": 200}, {duration: 200 });
		$("#homeQuickSearchBox").fadeOut(100);
	} else {
		$("#homeSearchTrucksBox").animate({"height": 50}, {duration: 200 });
		$("#homeQuickSearchBox").fadeIn(600);
		$("#homeAdvancedSearchLink").fadeIn(800);
	}
}
function searchPlant() {
	if($("#homeSearchPlantBox").height() == 50) {
		$("#homeSearchTrucksBox").height(50);
		$("#homeSearchVansBox").height(50);
		$("#homeAdvancedSearchLink").fadeOut(50);
		$("#homeSearchPlantBox").animate({"height": 200}, {duration: 200 });
		$("#homeQuickSearchBox").fadeOut(100);
	} else {
		$("#homeSearchPlantBox").animate({"height": 50}, {duration: 200 });
		$("#homeQuickSearchBox").fadeIn(600);
		$("#homeAdvancedSearchLink").fadeIn(800);
	}
}
function searchVans() {
	if($("#homeSearchVansBox").height() == 50) {
		$("#homeSearchTrucksBox").height(50);
		$("#homeSearchPlantBox").height(50);
		$("#homeAdvancedSearchLink").fadeOut(50);
		$("#homeSearchVansBox").animate({"height": 200}, {duration: 200 });
		$("#homeQuickSearchBox").fadeOut(100);
	}  else {
		$("#homeSearchVansBox").animate({"height": 50}, {duration: 200 });
		$("#homeQuickSearchBox").fadeIn(600);
		$("#homeAdvancedSearchLink").fadeIn(800);
	}
}
//Functions for home page keyword search
function clearKeywords() {
	v = document.getElementById("keywordQuickSearch").value;
	if(v == 'Type here for quick search'){
		document.getElementById("keywordQuickSearch").value = '';
	}
}
function checkKeywords() {
	v = document.getElementById("keywordQuickSearch").value;
	if(v == ''){
		document.getElementById("keywordQuickSearch").value = 'Type here for quick search';
	}
}
function checkKeywordSubmit() {
	v = document.getElementById("keywordQuickSearch").value;
	if(v!='' && v!='Type here for quick search') {
		document.keywordSearch.submit();
	}
}
//Functions for advanced search keywords
function clearAdvKeywords() {
	v = document.getElementById("keywordQuickSearch").value;
	if(v == 'Enter Keywords here...'){
		document.getElementById("keywordQuickSearch").value = '';
	}
}
function checkAdvKeywords() {
	v = document.getElementById("keywordQuickSearch").value;
	if(v == ''){
		document.getElementById("keywordQuickSearch").value = 'Enter Keywords here...';
	}
}
function checkAdvKeywordSubmit() {
	v = document.getElementById("keywordQuickSearch").value;
	if(v=='Enter Keywords here...') {
		document.getElementById("keywordQuickSearch").value = '';
		document.advSearch.submit();
	}
}
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

//Favourites
function addFavourite(stockId) {
	favNum = getNextFavourite(stockId);
	if(favNum==99) {
		alert('This vehicle is already in your favourites.');
		return;
	}
	if(favNum>8) {
		alert('Your favourites are full.\n Please clear your favourites to continue adding vehicles.');
		return;
	}
	createCookie("fav"+favNum, stockId, 7);
	//animate
	stock = document.getElementById(stockId);
	fav = document.getElementById("fav"+favNum);
	toPos = $("#fav"+favNum).offset();
	curPos = $("#"+stockId).offset();
	flyingImage = document.createElement('img');
	flyingImage.style.position = 'absolute';
	document.body.appendChild(flyingImage);
	flyingImage.style.left = curPos.left + 'px';
	flyingImage.style.top = curPos.top + 'px';
	flyingImage.id = "flyingImage";
	img = document.getElementById(stockId).src;
	flyingImage.src = img;
	flyingImage.style.display='block';
	flyingImage.style.width = stock.offsetWidth + 'px';
	flyingImage.style.height = stock.offsetHeight + 'px';
	$('#flyingImage').animate({
    left: toPos.left,
    top: toPos.top,
		height: '47px',
		width: '63px'
  }, 600, function() {
    // Animation complete.
		fav.innerHTML = '<a href="stockdetail.asp?stockId='+stockId+'" class="stockFavouritesItemLink"><img class="stockFavouritesItemLink" id="favImg'+favNum+'" src="'+img+'" width="63" height="47" border=0 /></a>';
		$("#flyingImage").remove();
  });	
}

function getNextFavourite(stockId) {
	//first check stock num not already in favourites
	for(x=1; x<10; x++) {
		a = readCookie("fav"+x);
		if(a==stockId) {
			return 99;
			exit;
		}
	}
	for(x=1; x<10; x++) {
		a = readCookie("fav"+x);
		if(a == null | a=='') {
			return x;
			x=10;
		}
	}
}
function clearFavourites() {
	c = confirm("Are you sure you wish to clear your Favourites ?");
	if(c==true) {
		for(x=1; x<9; x++) {
			eraseCookie("fav"+x);
			$("#favImg"+x).animate({"opacity": 0}, {duration: 400 });
			$("#fav"+x).innerHTML = '';
		}
	}
}
//bookmark
function bookmark_us(url, 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);
}

//Language
function changeLanguage() {
	lang = $("#selectLanguage").val();
	switch(lang)
	{	
		case "en":
			img = "English";
			str = "Translate these details";
			break;
		case "ar":
			img = "Arabic";
			str = "ترجمة هذه التفاصيل";
			break;
		case "fi":
			img = "Finnish";
			str = "K&#228;&#228;nn&#228; n&#228;m&#228; tiedot";
			break;
		case "pl":
			img = "Polish";
			str = "Przelozenia tych informacji";
			break;
		case "pt":
			img = "Portuguese";
			str = "Traduzir estes detalhes";
			break;
		case "es":
			img = "Spanish";
			str = "Traducir estos detalles";
			break;
		case "sv":
			img = "Swedish";
			str = "&#214;vers&#228;tta dessa uppgifter";
			break;
		case "ru":
			img = "Russian";
			str = "Перевести эти подробности";
			break;
	}
	
	//Set cookies for chosen Language
	createCookie("langPrev", readCookie("langCode"), 365);
	createCookie("langCode", lang, 365);
	createCookie("langFlag", img, 365);
	createCookie("langTranslate", escape(str), 365);
	//Update page
	if(lang != "en") {
		$("#langFlag").attr("src", "images/"+img+".png");
		$(".langTranslate").html(str);
		//homepage
		if($("#homeContainerRightText").length != 0) {
			translateHome();
		}
		if($("#contactUsText").length != 0) {
			translateContact();
		}
		if($("#aboutUsContainerRightText").length != 0) {
				translateAbout();
		}
	} else {
		location.reload(true);
	}
}

