var d = document;
var l=window.location.href;
var f=l.substring(l.lastIndexOf('/')+1);
var timer=false;

var flash_param={wmode:'transparent',menu:false,allowFullscreen: "true",scale:'noscale'};
var flashvars = {};

var lang=window.location.href.match(/\.com\/([a-z]{2})\//);
if(lang && lang.length>1 && lang[1]!='')
	lang=lang[1];
else
	lang='en';

var dictionary=[];

dictionary['en']=[];
dictionary['en']['home']='Home';
dictionary['en']['lightboxClose']='Close X';

dictionary['sc']=[];
dictionary['sc']['home']='主页';
dictionary['sc']['lightboxClose']='关闭 X';

dictionary['de']=[];
dictionary['de']['home']='Home';
dictionary['de']['lightboxClose']='Schlie&#223;en X';

dictionary['es']=[];
dictionary['es']['home']='Inicio';
dictionary['es']['lightboxClose']='Cerrar X';

dictionary['fr']=[];
dictionary['fr']['home']='Page d’accueil';
dictionary['fr']['lightboxClose']='Proximité X';

dictionary['it']=[];
dictionary['it']['home']='Home';
dictionary['it']['lightboxClose']='Close X';

dictionary['pt']=[];
dictionary['pt']['home']='Início';
dictionary['pt']['lightboxClose']='Fechar X';

dictionary['jp']=[];
dictionary['jp']['home']='ホーム';
dictionary['jp']['lightboxClose']='閉める X';



jQuery.fn.center = function () {

	var top=( $(window).height() - this.height() ) / 2+$(window).scrollTop();
	var left=( $(window).width() - this.width() ) / 2+$(window).scrollLeft();

    this.css("position","absolute");
    this.css("top", top + "px");
    this.css("left", left + "px");
    return this;

};


jQuery.fn.cleanOuterHTML = function() {
	return $("<p/>").append(this.eq(0).clone()).html();
};


String.prototype.trunc=function(n){
	return this.substr(0,n-1)+(this.length>n?'...':'');
};


String.prototype.toMonth=function(){
	var months=[];
	var monthsShort='Jan;Feb;Mar;Apr;May;Jun;Jul;Aug;Sep;Oct;Nov;Dec';
	monthsShort=monthsShort.split(';');
	for(var i=0;i<monthsShort.length;i++)
		months[monthsShort[i]]=i;
	if(months[this]!==undefined)
		return months[this];
	return -1;
};


$(function(){

	/* main nav */
	$('#header .nav a').hover(function(){

		$('#menu .popup').hide();

		var target=this.className;
		var position=$(this).offset();
		var css={
			top:position.top-5,
			left:position.left
		};

		$('#menu .'+target).css(css).show();

		/* IE6 select box fix */
		if (target=="company" && $.browser.msie && parseInt($.browser.version)<=6)
			$('#units').css('visibility','hidden');

	},function(){
		timer=setTimeout(function(){
			$('#menu .popup').hide();
		},10);
	});

	$('#menu .popup').hover(function(){
		if(timer)
			clearTimeout(timer);
	},function(){
		var obj=this;
		timer=setTimeout(function(){
			$(obj).css('top','-1000px');

			/* IE6 select box fix */
			if ($.browser.msie && parseInt($.browser.version)<=6)
				$('#units').css('visibility','visible');

		},10);
	});

	
	lightbox.ini();


	/* breadcrumb */
	if(typeof levelArray=='undefined'){

		var breadcrumb=[];
		breadcrumb[0]='<a href="/'+lang+'/">'+dictionary[lang]['home']+'</a>';

		var h1=$('h1');
		if(h1.length>0){

			var obj;
			var id=h1.html().replace('&amp;','&');
			id=id.replace('&#39;',"'");

			if($.browser.msie && id.indexOf("'")>-1){
				$('#leftNav a').each(function(){
					if(this.innerHTML==id){
						obj=$(this);
					}
				});
			}else{
				obj=$("#leftNav a[id=\""+id+"\"]");
			};

			obj.addClass('active');

			if(obj.next().length>0 && obj.next()[0].nodeName.toLowerCase()=='div'){
				obj.addClass('expand');
				obj.next().show();
			};

			if(obj.parent().length>0 && obj.parent()[0].className.indexOf('platform')>-1){
				$(obj.parent()[0]).show();
				$(obj.parent()[0]).prev().addClass('expand');
			};

		};

	};
	
	
	/* languages */
	langSwitch.ini();

});


var langSwitch={};

langSwitch.languages=[];
langSwitch.languages['en']='English';
langSwitch.languages['sc']='简体中文';
langSwitch.languages['de']='Deutsch';
langSwitch.languages['fr']='français';
langSwitch.languages['it']='Italiano';
langSwitch.languages['jp']='日本語';
langSwitch.languages['es']='español';
langSwitch.languages['pt']='Português';

langSwitch.ini=function(){
	
	$('#language-selector a').each(function(){
		var language=this.className.match(/ (en|sc|de|fr|it|jp|es|pt)/i);
		if(language && language.length>0){
			language=language[1];
			this.href=window.location.href.replace(/\/(en|sc|de|fr|it|jp|es|pt)\//i,'/'+language+'/');
		}
	});
}


/*
langSwitch.ini=function(){
	langSwitch.trigger=$('#langSwitch');
	langSwitch.gen();
	langSwitch.trigger.hover(langSwitch.show,langSwitch.hide);
}

langSwitch.gen=function(){

	langSwitch.popup=d.createElement('div');
	langSwitch.popup.id='langSwitchPopup';

	var link,i,position;

	for(i in langSwitch.languages){
		link=d.createElement('a');
		link.className=i;
		link.innerHTML=langSwitch.languages[i];
		link.href=window.location.href.replace(/\/(en|sc|de|fr|it|jp|es|pt)\//i,'/'+i+'/');
		langSwitch.popup.appendChild(link);
	}

	$('body').append(langSwitch.popup);

	$(langSwitch.popup).hover(function(){
		if(langSwitch.timer)
			clearTimeout(langSwitch.timer);
	},langSwitch.hide).hide();

}

langSwitch.show=function(){
	if(langSwitch.timer)
		clearTimeout(langSwitch.timer);
	position=langSwitch.trigger.offset();
	$('#langSwitchPopup').css({
		top:position.top+15,
		left:position.left+langSwitch.trigger.width()+15-($(langSwitch.popup).width()+20)
	}).show();
}

langSwitch.hide=function(){
	langSwitch.timer=setTimeout(function(){
		$('#langSwitchPopup').hide();
	},200);
}
*/


var loading={};
loading.overlay=function(){

	var w_width=$(document).width();
	var w_height=$(document).height();

	if($.browser.msie){
		if(parseInt($.browser.version)==6)
			w_width=w_width-scrollbarWidth()-8;
		else if(parseInt($.browser.version)==8)
			w_width=w_width-scrollbarWidth()-5;
	}

	loading.remove();
	$('<div id="overlay" />').appendTo('body').css({
		'width':w_width,
		'height':w_height
	});

}
loading.start=function(){
	loading.overlay();
	setTimeout(function(){
		$('<div id="loading"/>').appendTo('body').css({
			'width':'32px',
			'height':'32px',
			'z-index':1000
		}).append('<img id="loadingImg" src="/common/images/loading.gif"/>').center();
	},10);
	setTimeout(function(){
		document.title='reloading'
		$('#loadingImg').attr('src','/common/images/loading.gif?'+Math.random());
	},200);
}
loading.remove=function(){
	$('#overlay, #loading').remove();
}


var lightbox={};
lightbox.status=0;
lightbox.contentId=false;
lightbox.selector='a[rel=lightbox]';
lightbox.width=706;
lightbox.height=560+30;
lightbox.ini=function(){

	$(lightbox.selector).click(function(e){

		e.preventDefault();
		lightbox.status=1;
		lightbox.overlay();

		$('<div id="lightbox" />').appendTo('body').css({
			'width':lightbox.width,
			'height':lightbox.height,
			'z-index':99
		}).append('<div class="heading"><div class="title"></div><div class="close">'+dictionary[lang]['lightboxClose']+'</div></div>').center();

		$('#lightbox .close').click(lightbox.close);

		if(lang == 'sc'){
			if (this.title == "Inquiry and Distribution Availability")
				this.title = "咨询和经销途径";
		}

		if(this.title)
			$('#lightbox .title').html(this.title);

		/* launch flv player */
		if(this.href.indexOf('.mp4')>-1){

			var srcPath=this.href.substring(this.href.indexOf('#')+1);
			$('#lightbox').append('<div id="lightbox_flash" />');

			if(iPx()){

				window.location=srcPath;
				lightbox.close();

			}else{

				flashvars={
					'skinPath':'/johnsonelectric/common/images/swf/SkinUnderPlayStopSeekFullVol.swf',
					'srcPath':srcPath,
					'videoW':706,
					'videoH':560
				};
				swfobject.embedSWF('/johnsonelectric/common/images/swf/flvPlayer.swf', 'lightbox_flash', "100%", "100%", '7.0.0', '', flashvars, flash_param);

			};

			$(window).resize(lightbox.relocate);

		}else{

			$('#lightbox').append('<div id="lightbox_content" />');

			if(this.href.indexOf('#')>-1){

				var id=this.href.substring(this.href.indexOf('#')+1);
				lightbox.contentId=id;
				var $content=$('#'+id);
				$('#lightbox').width($content.width()).height($content.height()+50).center();
				$content.appendTo('#lightbox_content').show();

				if(id=='inquiry_popup'){
					$(window).scroll(lightbox.relocateShift).resize(lightbox.relocateShift);
					lightbox.relocateShift();
				}else{
					$(window).unbind('scroll').resize(lightbox.relocate);
				};

			};

		}

	});

};

lightbox.open=function(id,title){

		lightbox.status=1;
		lightbox.overlay();

		$('<div id="lightbox" />').appendTo('body').css({
			'width':lightbox.width,
			'height':lightbox.height,
			'z-index':99
		}).append('<div class="heading"><div class="title"></div><div class="close">'+dictionary[lang]['lightboxClose']+'</div></div>').center();

		$('#lightbox .close').click(lightbox.close);

		if(lang == 'sc'){
			if (title == "Inquiry and Distribution Availability")
				title = "咨询和经销途径";
		}


		$('#lightbox .title').html(title);
		$lightbox=$('#lightbox').append('<div id="lightbox_content" />');
		lightbox.contentId=id;
		var $content=$('#'+id);

		$lightbox.width($content.width()).height($content.height()+50).center();
		if(parseInt($lightbox.css('top'))<0)
			$lightbox.css('top','0px');

		$content.appendTo('#lightbox_content').show();

		if(id=='inquiry_popup'){
			$(window).scroll(lightbox.relocateShift).resize(lightbox.relocateShift);
			lightbox.relocateShift();
		}else{
			$(window).unbind('scroll').resize(lightbox.relocate);
		}

}


lightbox.relocate=function(){
	if(lightbox.status==1){
		lightbox.overlay();
		var $lightbox=$('#lightbox').center();
		if(parseInt($lightbox.css('top'))<0)
			$lightbox.css('top','0px');
	};
}

lightbox.relocateShift=function(){
	if(lightbox.status==1){
		lightbox.overlay();
		if(iPx())
			var top=$(window).scrollTop()+30;
		else
			var top=$(window).scrollTop()+100;
		var $lightbox=$('#lightbox').center().css('top',top);
		if(parseInt($lightbox.css('top'))<0)
			$lightbox.css('top','0px');
	};
}

lightbox.overlay=function(){

	var w_width=$(document).width();
	var w_height=$(document).height();

	if($.browser.msie){
		if(parseInt($.browser.version)==6)
			w_width=w_width-scrollbarWidth()-8;
		else if(parseInt($.browser.version)==8)
			w_width=w_width-scrollbarWidth()-8;
	}

	$('#overlay').remove();
	$('<div id="overlay" />').appendTo('body').css({
		'width':w_width,
		'height':w_height
	}).click(lightbox.close);

	/* IE6 select box fix */
	if ($.browser.msie && parseInt($.browser.version)<=6){
		$('select').css('visibility','hidden');
		$('.popup select').css('visibility','visible');
	};

}

lightbox.close=function(){

	if(lightbox.contentId!=false){
		$('#'+lightbox.contentId).hide().appendTo('body');
		lightbox.contentId=false;
	};

	$('#overlay, #lightbox').remove();

	/* IE6 select box fix */
	if ($.browser.msie && parseInt($.browser.version)<=6)
		$('select').css('visibility','visible');

	lightbox.status=0;

}


function replaceSwfWithEmptyDiv(targetID){
	var el = document.getElementById(targetID);
	if(el && el.nodeName.toLowerCase()!='div'){
		var div = document.createElement("div");
		el.parentNode.insertBefore(div, el);
		swfobject.removeSWF(targetID);
		div.setAttribute("id", targetID);
	};
}


function scrollbarWidth() {
    var div = $('<div style="width:50px;height:50px;overflow:hidden;position:absolute;top:-200px;left:-200px;"><div style="height:100px;"/></div>');
    /* Append our div, do our calculation and then remove it */
    $('body').append(div);
    var w1 = $('div', div).innerWidth();
    div.css('overflow-y', 'scroll');
    var w2 = $('div', div).innerWidth();
    $(div).remove();
    return (w1 - w2);
}


function getParameterByName(name){
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( l );
	if( results == null )
		return "";
	else{
		try{
			return decodeURIComponent(results[1].replace(/\+/g, " "));
		}catch(e){
			return unescape(results[1].replace(/\+/g, " "));
		}
	}
}


function sortChronological(a, b){
	var dateA = new Date(a.period);
	var dateB = new Date(b.period);
	return dateB - dateA ;
}


/* je legacy */
var browserName=navigator.appName;
var browserVer=parseInt(navigator.appVersion);
var version="";
var msie4=(browserName=="Microsoft Internet Explorer"&&browserVer>=4);
if((browserName=="Netscape"&&browserVer>=3)||msie4||browserName=="Konqueror"||browserName=="Opera"){version="n3";}else{version="n2";}
function blurLink(theObject){if(msie4){theObject.blur();}}
function decryptCharcode(n,start,end,offset){n=n+offset;if(offset>0&&n>end){n=start+(n-end-1);}else if(offset<0&&n<start){n=end-(start-n-1);}
return String.fromCharCode(n);}
function decryptString(enc,offset){var dec="";var len=enc.length;for(var i=0;i<len;i++){var n=enc.charCodeAt(i);if(n>=0x2B&&n<=0x3A){dec+=decryptCharcode(n,0x2B,0x3A,offset);}else if(n>=0x40&&n<=0x5A){dec+=decryptCharcode(n,0x40,0x5A,offset);}else if(n>=0x61&&n<=0x7A){dec+=decryptCharcode(n,0x61,0x7A,offset);}else{dec+=enc.charAt(i);}}
return dec;}
function linkTo_UnCryptMailto(s){location.href=decryptString(s,-2);}


function checkPocketIE(){
	if(location.pathname.indexOf('current_vacancies') > 0)
		return false;

	var pda_user_agent_list = new Array("2.0 MMP", "240320", "AvantGo","BlackBerry", "Blazer",
			"Cellphone", "Danger", "DoCoMo", "Elaine/3.0", "EudoraWeb", "hiptop", "IEMobile", "KYOCERA/WX310K", "LG/U990",
			"MIDP-2.0", "MMEF20", "MOT-V", "NetFront", "Newt", "Nintendo Wii", "Nitro", "Nokia",
			"Opera Mini", "Opera Mobi",
			"Palm", "Playstation Portable", "portalmmm", "Proxinet", "ProxiNet",
			"SHARP-TQ-GX10", "Small", "SonyEricsson", "Symbian OS", "SymbianOS", "TS21i-10", "UP.Browser", "UP.Link",
			"Windows CE", "WinWAP", "Android", "iPhone", "iPod", "iPad", "Windows Phone");
	var pda_app_name_list = new Array("Microsoft Pocket Internet Explorer");

	var user_agent = navigator.userAgent.toString();
	for (var i=0; i<pda_user_agent_list.length; i++) {
		if (user_agent.indexOf(pda_user_agent_list[i]) >= 0) {
			return true;
		}
	};
	var appName = navigator.appName.toString();
	for (var i=0; i<pda_app_name_list.length; i++) {
		if (user_agent.indexOf(pda_app_name_list[i]) >= 0) {
			return true;
		}
	};



	return false;
}


function iPx(){
	if((navigator.userAgent.match(/iPhone/i)) ||  (navigator.userAgent.match(/iPod/i)) ||  (navigator.userAgent.match(/iPad/i)))
		return true;
	return false;
}

/* hide size rows if it is product listing page and is performing application filter */
if (
	getParameterByName('app') != '' &&
	getParameterByName('segment') != '' &&
	window.location.href.indexOf('/en/') > -1 &&
	window.location.href.indexOf('/stepper-motors/') == -1 &&
	window.location.href.indexOf('synchronous-motors.html') == -1 &&
	window.location.href.indexOf('switches') == -1
) {
	document.write('<style type="text/css">');
	document.write('div.table table.list tr{ display:none; }');
	if($.browser.msie && parseInt($.browser.version, 10) <9 )
		document.write('div.table table.list tr.header{ display:block; }');
	else
		document.write('div.table table.list tr.header{ display:table-row; }');
	document.write('</style>');
}
