﻿// JavaScript Document

$(document).ready(function() {
	//shadowboxInit();
	//Autocomplete functionality and assigning the action attribute to Form
	if($('#searchfield').length || $("#cse-search-box").length) {
		autoCompleteTags("searchfield");
		$("#cse-search-box").attr("action", "/" + getSelectedLanguage() +$("#cse-search-box").attr("action"));
	};

	if($("#logoLink")) {
		$("#logoLink").attr("href", "/" + getSelectedLanguage() +$("#logoLink").attr("href"));
	}
	if($("#externalLink")) {
		$("#externalLink").attr("href", "/" + getSelectedLanguage() +$("#externalLink").attr("href"));
	}

	if($("#buynowlink")){
		
	}
	var billboardContainer = $("#billboardContainer", $("#content"));
	if(billboardContainer){
		if($("#billboardNav", $("#billboardContainer")).children().length < 1){
			$('.pics').cycle({cleartypeNoBg: true, fx: 'fade', pager: '#billboardNav', speed: 600, pause: 1, timeout: 7000 });
		}
	}
	 
	// Check if the page is being shown in SiteEdit mode (Site edit mode means the page is being loaded as an iframe)
	if(window.location != window.parent.location && $('#billboardContainer').length) {
		$('#billboardContainer .pics a').removeAttr('href');
	} 
	if(location.search){
		//var prevLang = getUrlVars(location.search)["prev"];
		var prevLang = getPrev(location.search);
		if(prevLang){ 
			switch(prevLang.toUpperCase()){
				case "CN":
					showFootNotice("关闭信息", "翻译页面正进行更新，非常抱歉所引起的不便。",'/images/Picture-32.png');
				break;
				case "DE":
					showFootNotice("Meldung Schließen", "Unsere lokalen Seiten werden momentan überarbeitet. Bitte entschuldigen Sie die Unannehmlichkeiten.",'/images/Picture-32.png');
				break;
				case "JP":
					showFootNotice("メッセージを閉じる", "現在、このページは更新中です。ご不便をお掛けし大変申し訳ありません。",'/images/Picture-32.png');
				break;
				case "ES": 
					showFootNotice("Cerrar Mensaje","Estamos actualizando las páginas traducidas. Disculpe las molestias.",'/images/Picture-32.png');
				break;
				case "FR":
					showFootNotice("Fermer le message", "Les pages traduites sont en cours de mise à jour. Veuillez nous excuser pour tout désagrément.",'/images/Picture-32.png');
				break;
				case "IT":
					showFootNotice("Messaggio Chiuso", "Le pagine tradotte stanno subendo un aggiornamento. Chiediamo scusa per qualsiasi inconvenienza causata.", '/images/Picture-32.png');
				break;
				default:
					showFootNotice("Close Message", "The translated pages are currently undergoing an update. We apologize for any inconvenience caused.",'/images/Picture-32.png');
				break;	
			}
		}
	}
	
	
	
	//check if the buy now button is added to the page
	var btBuyNow = $(".left_pane_35 .padding5 input");
	if(btBuyNow){
		//get the value of the product status
		var prodStatus = $(".font_green.font_italic span span", $(".left_pane"));
		var txtProdStatus = jQuery.trim(prodStatus.text());
		//translate the buy now button
		btBuyNow.attr('src', "/images/buyNow-" + getSelectedLanguage() +".jpg");
		switch(txtProdStatus){
			case "Discontinued.":
			case "Discontinued":
				prodStatus.css('color', '#009907');
			case "Coming Soon":
				btBuyNow.css('display', 'none');
			break;
		}
	}
	
	//check if the Ecomlinks buy now box is added to the page
	var btBuyNowEc = $(".left_pane_35 .padding5 #FromBN_BuyNowOutput");
	if(btBuyNowEc){
		//get the value of the product status
		var prodStatus = $("#prodStatusHolder span", $(".left_pane"));
		var prodStatusSE = $("#prodStatusHolder span span", $(".left_pane"));
		var txtProdStatus = jQuery.trim(prodStatus.text());
		var txtProdStatusSE = jQuery.trim(prodStatusSE.text());
		switch(txtProdStatus){
			case "Discontinued.":
			case "Discontinued":
				prodStatus.css('color', '#009907');
			case "Coming Soon":
				btBuyNowEc.css('display', 'none');
			break;
			}
	    switch(txtProdStatusSE){
			case "Discontinued.":
			case "Discontinued":
				prodStatus.css('color', '#009907');
			case "Coming Soon":
				btBuyNowEc.css('display', 'none');
			break;	
		} 
	}
	
	//apply ellipsis
	var toCut = $('.content_thumbnails_02 .font_gray_small');
	if(toCut){
		toCut.each(function (i){ $(this).ellipsis({lines:7}); });
	}

});
function getPrev(url){
	var start = url.indexOf("prev=");
	if(start != -1){
		return url.slice(start + 5, start + 7);
	}
}

// Read a page's GET URL variables and return them as an associative array.
function getUrlVars(url)
{
    var vars = [], hash;
    var hashes = url.slice(url.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

function shadowboxInit(){
	// Initialize Shadowbox
    Shadowbox.init({ handleOversize: "drag", players: ["swf", "html"], overlayOpacity: 0.8});

	// Sets up the content build inside of the shadowbox
	$("a[rel*='shadowbox']").mouseup(function(){buildProductsPOPUP($(this).attr("title"));});

	// Set popup icons to the off state 
	$('.textNav .navi .blindNav .unitPng').hide();
	
	// Attach the mouseover events to the popup icons
	$('.textNav').bind("mouseenter",function(){
		$(this).children(".navi").children(".blindNav").children(".unitPng").slideDown(200);
 	}).bind("mouseleave",function(){ 
		$(this).children(".navi").children(".blindNav").children(".unitPng").slideUp(200);
 	});
}

function clearText(id){
	var textInput = document.getElementById(id);
	if(textInput.value == textInput.defaultValue) {
		textInput.value = '';
	}
}

function resetDefault(id) {
	var textInput = document.getElementById(id);
	if(textInput.value == '') {
		textInput.value = textInput.defaultValue;
	}
}

function gotobuynowpage() {
	var buynowlink = '/' + getSelectedLanguage() + '/Buy-Now/index.aspx';
	window.open(buynowlink, "_self");

}	
function getSelectedLanguage(){
	return document.location.href.split("/")[3];
}

function autoCompleteTags(id){
	$.get('/' + getSelectedLanguage() +'/Web.sitemap.xml', function(data) {
		var tags = new Array();	
		$(data).find('siteMapNode').each(function(){
			var linkUrl = $(this).attr('url');
			if (linkUrl.toLowerCase().indexOf("/products/") != -1) {
				var modelName = $(this).attr('modelName');
				var title = $(this).attr('title'); 
				if(title){
					tags.push({text:title, url:linkUrl});
				}
				if(modelName){
					tags.push( {text:modelName, url:linkUrl} );
				}
  		    }
  		});
		$("#"+id).autocomplete(tags, {matchContains: true, formatItem: function(item) { return item.text;}}).result(function(event, item) {
			location.href = item.url;
		});
	});
}

function showFootNotice(buttonLabel, message, imageURL){

	//when the window is resize the disclaimer box will resize
	$(window).resize(function() {
		var pos=parseInt($(window).scrollTop())+parseInt($(window).height());
		$('#disclaimer_box').css("bottom",0);
		$('#disclaimer_box').show(); //display the disclaimer box
	}); 
	var pos=parseInt($(window).scrollTop())+parseInt($(window).height());
	$('#disclaimer_box').css("bottom",0);
	$('#disclaimer_box').show(); //display the disclaimer box
	
	$('#disclaimer_box').html("<div id='close_disclaimer' style='float:right; cursor:pointer; display:inline; padding-top:10px;'>&#187 " + buttonLabel + "<\/div> <div style='float:left;disply:inline; width:90%;'><img src='" + imageURL + "' style='float:left; padding-top:10px;'\/><p id='dsclText'>"+ message + "<\/p><\/div>");
	
	//when the close text at right corner of the disclaimer box is clicked
	$('#close_disclaimer').click(function(){
	  	//the disclaimerbox gets scrool down with top property and gets hidden with zero opacity
		$('#disclaimer_box').animate({opacity:0 }, 700, 'linear', function(){$(this).css("width", 0);});
		setCookie('show_disclaimer','no',1); //cookies is set to no value
	});

}
