// JavaScript Document
var minHeight=520;
var minWidth;
var pageSize;
var size;
var imgSize;
var imgListArr=Array();
var imgPath='/images/';
var imgType='.jpg';
var doImgRun={o:null};
imgListArr['imghome']=Array('home1','home2','home3','home4','home5','home6','home7');
imgListArr['imgFindUsContact']='findus2';
imgListArr['imgimgstoreLocator']='findus1';
imgListArr['imgstore']=Array('findus1','findus2');
imgListArr['imgmedia']='media1';
imgListArr['imgmedia_news']='media1';
imgListArr['imgmedia_more']='media1';
imgListArr['imgmedia_detail']='media1';
imgListArr['imgmedia_contact']='media1';
imgListArr['imgabout']=Array('about1','about2','about3','about4');
imgListArr['imgabout_aboutcochine']='about1';
imgListArr['imgabout_FoundingStory']='about2';
imgListArr['imgabout_Sustainability']='about4';
imgListArr['imgabout_Quality']='about3';
imgListArr['imgfragrances']=Array('frag1','frag2','frag3','frag4');
imgListArr['imgfragrances_agarwoodamber']='frag1';
imgListArr['imgfragrances_VietnameseRoseDelent']='frag2';
imgListArr['imgfragrances_WaterHyacinthLimeBlo']='frag3';
imgListArr['imgfragrances_WhiteJasmineandGarde']='frag4';
imgListArr['imgproducts']=Array('products1','products2','products3','products4');
imgListArr['imgproducts_scentedcandle']='products1';
imgListArr['imgproducts_ReedDiffuserDiffuseu']='products2';
imgListArr['imgproducts_FineLiquidHandWashSa']='products3';
imgListArr['imgproducts_HandBodyLotionBaumep']='products4';
imgListArr['imgagarwoodamber']=Array('f2');
var loadImgArr=Array('welcome.gif','titleBg.gif','home_off.png','home_on.png','about_off.png','about_on.png','fragrnaces_off.png','fragrnaces_on.png','products_off.png','products_on.png','media_off.png','media_on.png','contact_off.png','contact_on.png');
var qwe=0;
//imgListArr['imgHome']=Array('home5');
var cook = new CookieClass(); 
if(!$('body').find('.bodyRoot').length && $('body').attr('id')!='flash'){
loadImg(loadImgArr);
}
$(document).ready(function(){
try{
	if($('body').attr('id')=='flash'){
		var so = new SWFObject("loading.swf", "mymovie", "1000", "600", "8", "#fff9e3");
	   	so.addParam("quality", "low");
	   	so.addParam("wmode", "transparent");
	   	so.addParam("salign", "t");
		if(imgListArr['imghome'].length){
			for(var i=0; i<imgListArr['imghome'].length;i++){
				var vari='image'+parseInt(i+1);
				var val=imgPath+imgListArr['imghome'][i]+'_'+getImgType(getPageSize())+imgType;
				so.addVariable(vari, val);
			}
		}
	   	so.write("flashcontent");
	}else{
		$("body").css("display", "none");
		$("body").fadeIn(1500);
		if($('body').find('.bodyRoot').length){
			loadBg2(imgListArr['imghome']);
		}else{
			if( $('#header').find('a').length>1){
				$('#header').find('a').hide();
			}
			//if(!jQuery.browser.msie){
				
			//}	
			
			menuRo();
			menuRo_();
			resize_();
			if(imgListArr[$('#imgBox').attr('class')] && imgListArr[$('#imgBox').attr('class')].constructor != window.Array ){ 
				cook.setCookie('section',imgListArr[$('#imgBox').attr('class')]);
			}
			loadBg(imgListArr[$('#imgBox').attr('class')]);
		  if($('#box11').length){
			 $('#contentBg').fadeTo(10,0);
			 $('#box11').fadeTo(10,0);
			}
			$('#wrap').show();
			setTimeout('openbox()',3000);
			//newsList();
			$('.tool').find('a').each(function(){
				if($(this).find('img').length>1){
					$(this).hover(function(){
						$(this).find('img').eq(0).show();
						$(this).find('img').eq(1).hide();	
					},function(){
						$(this).find('img').eq(0).hide();
						$(this).find('img').eq(1).show();
					})
				}
			})
		}
	}
}catch(err){}
})
$(window).resize(function() {
	 resize_();
});

$(function(){
	try{
		$('.scroll-pane').jScrollPane({showArrows:true});	
	}catch(err){}
		
});

function checkFash(){
	var hasFlash=0;　　　　//是否安装了flash
	var flashVersion=0;　　//flash版本
	
	if(document.all)
	{
	var swf = new ActiveXObject('ShockwaveFlash.ShockwaveFlash'); 
	if(swf) {
	hasFlash=1;
	VSwf=swf.GetVariable("$version");
	flashVersion=parseInt(VSwf.split(" ")[1].split(",")[0]); 
	}
	}else{
	if (navigator.plugins && navigator.plugins.length > 0)
	{
	var swf=navigator.plugins["Shockwave Flash"];
	if (swf)
		{
	hasFlash=1;
		   var words = swf.description.split(" ");
		   for (var i = 0; i < words.length; ++i)
	{
			 if (isNaN(parseInt(words[i]))) continue;
			 flashVersion = parseInt(words[i]);
	}
	}
	}
	}
	//return {f:hasFlash,v:flashVersion};
	if(!hasFlash){
		window.location ="/cochine.html";
		return false;
	}
}
function jsHtml(key){
	$('#contentBg').fadeTo(10,0); 
	if($('#contentLon2').width())
		$('#contentBg').width($('#contentLon2').width());
	if($('#contentLon2').height())
		$('#contentBg').height($('#contentLon2').height());
	/*
	$(document).ready(function(){
		if($('#contentLon2').width())
			$('#contentBg').width($('#contentLon2').width());
		if($('#contentLon2').height())
			$('#contentBg').height($('#contentLon2').height());
		$('#header_a').click(function(){
			$('#box1').html(mc1);
			$(function(){
				$('.scroll-pane').jScrollPane({showArrows:true});			
			  });
			$('#contentBg').fadeTo("slow",0.95); 
			$('#box1').fadeIn("slow"); 
			thisabc();
			return false;
		
	});*/
		
	$('#close').click(function(){
		$('#box1').fadeTo("slow",0); 
		$('#contentBg').fadeTo("slow",0); 
		return false;
	});
	$('#header > a').click(function(){
		$('#contentBg').fadeTo("slow",0.95); 
		$('#box1').fadeTo("slow",1); 
		return false;
		
	});
	if(key=='store') jsHtmlStore();
	if(key='media_news'){
		jsHtmlMedia();
		mediaRecent();
	}
}

function jsHtmlMedia(){
	$('#m_media_news').click(function(){
		$('#m_media_more').attr('class','');
		$('#m_media_news').attr('class','');
		$('#m_media_contact').attr('class','');
		$(this).attr('class',$(this).attr('id'));
		$('#infos').remove();
		$('#msg').remove();
		$('#box3').remove();
		$('#yearMenu').remove();
		mediaRecent();
		return false;
	});
	$('#m_media_more').click(function(){
		$('#m_media_more').attr('class','');
		$('#m_media_news').attr('class','');
		$('#m_media_contact').attr('class','');
		$(this).attr('class',$(this).attr('id'));
		mediaMore(year__,0);
		return false;
	});
	$('#m_media_contact').click(function(){
		$('#newsBox').remove();
		$('#m_media_more').attr('class','');
		$('#m_media_news').attr('class','');
		$('#m_media_contact').attr('class','');
		$(this).attr('class',$(this).attr('id'));
		$('#infos').remove();
		$('#msg').remove();
		$('#box3').remove();
		$('#yearMenu').remove();
		$('#menuMedia').after(contact);
		return false;
		//mediaRecent();
	});
}

function mediaMore(index,l){
	var tt='/media_news/'+index+'/en'+(parseInt(l)+1)+'/index.phtml';
	$('#box3').remove();
	$('#newsBox').remove();	
	$('#yearMenu').remove();		
	$('#infos').remove();
	$('#msg').remove();

	$.get(tt,
		function(data){
			//$('#menuMedia').after(URLdecode(data));
			$('#menuMedia').after(URLdecode(data));
			//$('#menuMedia').after('a');
			newsList();
			yearMenu();
			yearPage();
			mediaMorea();
			if($.browser.msie ){
			$('#menuMedia').parent().hide();
			$('#menuMedia').parent().show();
			}
	    }
	); 
}



function yearPage(){
	$('#yearPage').find('a').click(function(){
		mediaMore($(this).attr('index'),$(this).attr('l'));	
		return false;
	});
}

function yearMenu(){
	$('#yearMenu').find('a').click(function(){
		mediaMore($(this).attr('value'),0);									
		return false;
	});
}

function mediaMorea(){
	$('.newsList').find('a').click(function(){
		$.get($(this).attr('value'),
			function(data){
				$('#box3').remove();
				$('#newsBox').remove();	
				$('#yearMenu').remove();
				$('#menuMedia').after(URLdecode(data));
				newsList();
				mediaMorea();
				yearMenu();
			}
		); 
		return false;
	});
}

function mediaRecent(index){
try{
	if(index=='' || index==null)
	index=0;
	else if(parseInt(index)>=recent.length)
	index=0;
	$('#newsBox').remove();
	$('#menuMedia').after(URLdecode(recent[index]));
	
	$(function(){
	$('.scroll-pane').jScrollPane({showArrows:true});			
	});
	newsList();
	mediaRecentLink();
}catch(err){}
}

function newsList(){
	$('.newsList').hover(function(){
		 if($(this).attr('class')!="newsList sel"){
		 	$(this).find('.newsListI').css({'background-color':'#6c6c64','color':'#ffffff'});
			$(this).find('.newsListI').find('a').css({'background-color':'#6c6c64','color':'#ffffff'});
		 }
	},function(){
		 if($(this).attr('class')!="newsList sel"){
		 	$(this).find('.newsListI').css({'background-color':'#ffffff','color':'#6c6c64'});
		 	$(this).find('.newsListI').find('a').css({'background-color':'#ffffff','color':'#6c6c64'});
		 }
	});
}

function mediaRecentLink(){
	$('.newsList').find('a').click(function(){
		mediaRecent($(this).attr('value'));
		return false;
	});
}

function mediaMenu(){
	
}


function jsHtmlStore(){
	
	$('#m_find_contact').click(function(){
		$('#oo').hide();
		$('#infos').remove();
		$('#msg').remove();
		$('#m_find_store').attr('class','');
		$('#m_find_contact').addClass('m_find_contact');
		$('#oo').after(contact);
		return false;
	});
	$('#m_find_store').click(function(){
		getPageTxt();
		return false;
	});
	$('#subSubMenu>.ssmenu').click(function(){
		getPageTxt(null,$('#subSubMenu>.ssmenu').index(this));
		return false;
	});	
	getPageTxt();
}


function getPageTxt(val,index,l){
	$('#oo').show();
	$('#msg').remove();
	$('#m_find_store').addClass('m_find_store');
	$('#m_find_contact').attr('class','');
	if(val=='' || val==null) val=txtArr;
	if(!val.constructor==Array){
		return false;
	}
	if(index=='' || index==null || parseInt(index)>=val.length){
		index=0;
	}else{
		index=parseInt(index);
	}
	$('#subSubMenu > .ssmenu').attr('class','ssmenu');
	$('#subSubMenu > .ssmenu').eq(index).addClass($('#subSubMenu > .ssmenu').eq(index).attr('id'));
	var txt=val[index];
	if(l=='' || l==null  || parseInt(l)>=txt.length){
		l=0;
	}else{
		l=parseInt(l);
	}
	
	if(txt.length>0){
		var p='<div id="pageList">Page ';
		for(var o=0;o<txt.length;o++){
			if(o==l){
				p+='<span>'+parseInt(o+1)+'</span>';
				$('#infos').remove();
				$('#oo').after(URLdecode(txt[o]));
			}else{
				p+='<a href="javascript:getPageTxt(null,'+index+','+o+')">'+parseInt(o+1)+'</a>';
			}
		}
		p+='</div>';
		if(txt.length>1){
			$('#pageNo').html(p);
			//$('#oo').show();
		}else{
			$('#pageNo').html('');
			//$('#oo').hide();
		}
		
	}
	
	//return [URLdecode(txt[l]),index,txt.length,(l+1)];
}

function openbox(){
  if($('#box1').css('display')=='none'){
    $('#contentBg').fadeTo("slow",0.95); 
    $('#box1').fadeIn("slow"); 
  }
  if($('.noshow').length){
  	$('#contentBg').fadeTo("slow",0.95); 
    $('.noshow').fadeTo("slow",1); 
  }
  if($('#box11').length){
    $('#contentBg').fadeTo("slow",0.95); 
    $('#box11').fadeTo("slow",1); 
  }
}

function pageshow(key){
	//alert(key);
	$("body").css("display", "none");

}

function menuRo_(){
	$('.tool > div > a').hover(function(){
	  	$(this).find('.pdBtn_on').css('display','block');
	 	$(this).find('.pdBtn_off').css('display','none');
	},
	function(){
	  	$(this).find('.pdBtn_on').css('display','none');
		$(this).find('.pdBtn_off').css('display','block');
	}); 
}


function menuRo(){
	//if($(".sbmboxs").length){
		$(".sbmboxs").each(function(){
			if($(this).attr('id')!='sbmbox_contact'){
				$(this).css('left',(0-parseInt(parseInt($(this).css('width'))+28-parseInt($(this).parent().css('width')))/2)+'px');
				//alert($(this).width());
				//alert($(this).html());
			}
		
		});
		
		$(".menuBox").hover(function(){
			$(this).find('.sbmboxs').find('*').show();						 
			$(this).find('.sbmboxs').show();
		},
		function(){
			$(this).find('.sbmboxs').find('*').hide();
			$(this).find('.sbmboxs').hide();
		}); 
	//}
}


function loadBg(imgArr){
	try{
    	if(doImgRun.o==null){
			imgRunGo();
		}
	var imgValue;
		if(imgArr ==''){ 
			return false;
		}else if(imgArr.constructor == window.Array ){
			imgValue=imgArr[$('#imgBox').find('img').length];
			imgUrl=imgPath+imgValue+'_'+getImgType(getPageSize())+imgType;
		}else{
			imgUrl=imgPath+imgArr+'_'+getImgType(getPageSize())+imgType;
			imgValue=imgArr;
		}
		
		var im=new Image();
		im.src=imgUrl;
		$(im).load(function(){
			
			if(cook.getCookie('section')==imgValue){
				$('#imgBox').find('img').hide();
				$('#imgBox').append('<img src="'+imgUrl+'" value="'+imgValue+'" style="width:100%;"/>');
			}else if($('#imgBox').find('img').length==0){
				$('#imgBox').append('<img src="'+imgUrl+'" value="'+imgValue+'" style="width:100%;"/>');				
			}else{
				$('#imgBox').append('<img src="'+imgUrl+'" value="'+imgValue+'" style="width:100%;display:none"/>');
			}
				
			resize_();
			if(imgArr.constructor == window.Array && $('#imgBox').find('img').length<imgArr.length){
				loadBg(imgArr);
			}else if(imgArr.constructor == window.Array && $('#imgBox').find('img').length==imgArr.length){
				
				if(doImgRun.o==null){
					if($('#imgBox').find('img:visible').length==0){
            			$('#imgBox').find('img').eq(0).show();
					}else if($('#header').find('a').length==$('#imgBox').find('img').length){
						$('#header').find('a').hide();
						$('#header').find('a').eq($('#imgBox').find('img').index($('#imgBox').find('img:visible'))).show();
					}
					//if($('#headerHome').length){
					//setTimeout('imgRunGo()',3500);
					//}else{
						//alert('s');
						//
					//}
				}
			}
		});
		if(jQuery.browser.msie){
			im.src=imgUrl;
			//getSize();
		}
	}catch (e) {
		return false;
	}
}


function loadBg2(imgArr){
	try{
    var imgValue;
		if(imgArr ==''){ 
			return false;
		}else if(imgArr.constructor == window.Array ){
			imgValue=imgArr[$('.bodyRoot').find('img').length];
			imgUrl=imgPath+imgValue+'_'+getImgType(getPageSize())+imgType;
		}else{
			imgUrl=imgPath+imgArr+'_'+getImgType(getPageSize())+imgType;
			imgValue=imgArr;
		}
		
		var im=new Image();
		im.src=imgUrl;
		$(im).load(function(){
			$('.bodyRoot').append('<img src="'+imgUrl+'" value="'+imgValue+'" />');
			if(imgArr.constructor == window.Array && $('.bodyRoot').find('img').length<imgArr.length){
				loadBg2(imgArr);
			}else if(imgArr.constructor == window.Array && $('.bodyRoot').find('img').length==imgArr.length){
			//	window.location.href='/home/en/';	
				window.location.href='http://'+window.location.host+"/home/en/";
			//	alert($('.bodyRoot').find('img').length+'--'+imgArr.length);
			}
		});
		if(jQuery.browser.msie){
			im.src=imgUrl;
			//getSize();
		}
	}catch (e) {
		return false;
	}
}

function imgRunGo(){
	doImgRun.start('imgRun()',6000);
}

function loadImg(imgArr){
	try{
		var img={};
		for(var i=0;i<imgArr.length;i++){
			img[i]=new Image();
			img[i].src=imgPath+imgArr[i];
		}
		
	}catch (e) {
		return false;
	}
}

function imgRun(){
	if($('#imgBox').find('img').length>1){
		var l=$('#imgBox').find('img').length
		var i=$('#imgBox').find('img').index($('#imgBox > img:visible'));
		var n=i+1;
		if(n==1){
			if($('#headerHome > div').length && $('#headerHome > div').css('display')=='none'){
        $('#headerHome > div').fadeIn(1000);
				$('#headerHome > div').fadeTo(1000,0.80);
			}
		}
		if(n==l) n=0;
		if($('#header').find('a').length>1){
      var l1=$('#header').find('a').length
      var i1=$('#header').find('a').index($('#header > a:visible'));
      var n1=i1+1;
      if(n1==l1) n1=0;
			$('#header').find('a').eq(i1).fadeOut(1000);
			$('#header').find('a').eq(n1).fadeIn(1000);
		}
		$('#imgBox').find('img').eq(i).fadeOut(1000);
		$('#imgBox').find('img').eq(n).fadeIn(1000);
		doImgRun.start('imgRun()');
	}
}

function resize_(){
try{
	pageSize=getPageSize();
	minWidth=$('#wrap').width();
	minHeight=$('.header').height()+$('.content').height()+$('.footer').height();
	$('#imgBox').find('img').each(function(){
		//if(imgPath+$(this).attr('value')+'_'+getImgType(size)+imgType!=$(this).attr('src'))
		$(this).attr('src',imgPath+$(this).attr('value')+'_'+getImgType([(pageSize[0]>minWidth?pageSize[0]:minHeight),pageSize[1]])+imgType);
	});
	var h=0;
	
	if(pageSize[0]>minWidth){
		$('#imgBox').css({'width':'100%'});
		$('#logo').css('display','none');
		$('#wLogo').css({'display':'block'});
	}else{
		$('#logo').css('display','block');
		$('#wLogo').css('display','none');
		$('#imgBox').width(minWidth);
		if($.browser.safari){
			h=15
		}else if($.browser.mozilla ){
			h=16
		}
	}
	if(pageSize[1]>minHeight){
		$('#imgBox').height(pageSize[1]-h);
		$('#wrap').height(pageSize[1]-h);
	}else{
		$('#imgBox').height(minHeight-h);
		$('#wrap').height(minHeight-h);
	}
	if($('#imgBox > img').length){
		imgSize= Array($('#imgBox > img').width(),$('#imgBox > img').height());
		if(imgSize[1]>minHeight && imgSize[1]<pageSize[1]){
			$('#wrap').height(imgSize[1]);
			setTimeout('resize__()',1);
		}else if(imgSize[1]<minHeight && imgSize[1]!=0){
			$('#wrap').height(minHeight-h);
		}
	}
	$('.content').css('top',(($('#wrap').height()+$('.header').height()-$('.footer').height())/2)+'px');
	$('#wLogo').css('top',parseInt($('#wrap').height()-144)+'px');
}catch(err){}
}

function resize__(){
	if($('#imgBox > img').height()-$('#wrap').height()<10 && $('#imgBox > img').height()!=$('#wrap').height()){
		//$('#wrap').height($('#imgBox > img').height());
		$('.content').css('top',(($('#wrap').height()+$('.header').height()-$('.footer').height())/2)+'px');
	}
}

function getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	arrayPageSize = [w,h];
	return arrayPageSize;
}

function getImgType(i){
	try{
		if((i[0]-1024)<=0){
			return 'S';
		}else if((i[0]-1180)<=0){
			return 'S';
		}else if((i[0]-1280)<=0 && (i[1]-620)<=0){
			return 'MC'
		}else if((i[0]-1280)<=0 && (i[1]-660)<=0){
			return 'M';
		}else if((i[0]-1280)<=0 && (i[1]-800)<=0){
			return 'M';
		}else if((i[0]-1280)<=0){
			return 'MB';
		
		}else{
			return 'L';
		}
	}catch (e) {
		return 'L';
	}
}

doImgRun.start=function(s,t){
	if(s=='') return false;
	if(parseInt(t)<1 || t=='' || t==null ) t=7000;
	doImgRun.o=setTimeout(s,t);
};
doImgRun.stops=function(){
	if(doImgRun.o==null) return false;
	window.clearTimeout(doImgRun.o);
	doImgRun.o=null;
}

function CookieClass(){ 

this.expires = 0 ; 
this.path = "/"; 
this.domain = ""; 
this.secure = false; 

this.setCookie = function(name,value){ 
   var str = name+"="+escape(value); 
   if (this.expires>0){
    var date=new Date(); 
    var ms=this.expires * 60 * 1000; 
    date.setTime(date.getTime()+ms); 
    str+="; expires="+date.toGMTString(); 
   } 
   if(this.path!="")str+="; path="+this.path; 
   if(this.domain!="")str+="; domain="+this.domain; 
   if(this.secure!="")str+="; true"; 

   document.cookie=str; 
} 

this.getCookie=function(name){ 
   var cookieArray=document.cookie.split("; "); 
   var cookie=new Object(); 
   for(var i=0;i<cookieArray.length;i++){ 
    var arr=cookieArray[i].split("="); 
    if(arr[0]==name)return unescape(arr[1]); 
   } 
   return ""; 
} 

this.deleteCookie=function(name){ 
   var date=new Date(); 
   var ms= 1 * 1000; 
   date.setTime(date.getTime() - ms); 
   var str = name+"=no; expires=" + date.toGMTString(); 
   document.cookie=str; 
} 

this.showCookie=function(){ 
   alert(unescape(document.cookie)); 
} 

} 


function URLdecode(str) {
    var ret = "";
    for(var i=0;i<str.length;i++) {
        var chr = str.charAt(i);
        if(chr == "+") {
            ret += " ";
        }else if(chr=="%") {
            var asc = str.substring(i+1,i+3);
            if(parseInt("0x"+asc)>0x7f) {
                ret += decodeURI("%"+ str.substring(i+1,i+9));
                i += 8;
            }else {
                ret += String.fromCharCode(parseInt("0x"+asc));
                i += 2;
            }
        }else {
            ret += chr;
        }
    }
    return ret;
}
