$j=jQuery.noConflict();
var pathname = window.location.pathname;

function changeLanguage(lang){
	var ow = top.location.href;
	checkDefaultLang(ow, lang);
}

function checkDefaultLang(ow, lang){
	var dflang = window.location.pathname;
	var langList = "/" + lang + "/";
	ow = ow.replace("#","");	
	if(dflang.search("/sc/")> -1){
		ow = ow.replace("/sc/",langList);
	}else if (dflang.search("/en/")> -1){	
		ow = ow.replace("/en/",langList);
	}else{
		if(dflang.search("/tc/")> -1){
			ow = ow.replace("/tc/",langList);
		}else{
			ow = ow + langList.substr(1, langList.length-1);
		}
	}
	location.href=ow;
}

function getShopInfo(url, location, city, lang){	
	$j('#mapLoader').fadeIn('slow');
	$j('#map').load(url+ '/shop-data.php?lang=' +lang+ '&location=' +location+ '&city=' +city).hide().fadeIn('slow');
}  
function closeShop(){	
	$j('#mapLoader').fadeOut('slow');
	$j('#map').empty().fadeOut('slow');
}

function gotoHomepage(){	
	if(pathname.indexOf("/sc/") > -1) {
		location.href= "/sc/";
	}else if(pathname.indexOf("/tc/") > -1){
		location.href= "/tc/";
	}else if(pathname.indexOf("/en/") > -1){
		location.href= "/en/";
	}
}

function adjustFooter(){
	// Adjust Footer Alignment
	wH = $j(window).height(); // window height
	hH = $j('#header').height(); // header height
	nH = $j('.fixWidth').height(); // top navigation height
	bH1 = $j('#homesildertccontainer').height(); // home banner height	
	bH2 = $j('#homesildersccontainer').height(); // home banner height	
	bH3 = $j('#homesilderencontainer').height(); // home banner height
	mH = $j('#main').height(); // main body content height
	hbH = $j('#homeBottom').height(); // home bottom height	
	fH = $j('#colophon').height(); // footer height	

	totalHeight = hH + nH + bH1 + bH2 + bH3 + mH + hbH + fH;
	
//	alert(totalHeight);
//	aH = 679;
	$j('#footer').css('height', wH - totalHeight - 2);
}

// Check images loaded
$j.fn.imagesLoaded = function(callback){
  var elems = this.filter('img'),
      len   = elems.length,
      blank = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
  elems.bind('load',function(){
      if (--len <= 0 && this.src !== blank){ callback.call(elems,this); }
  }).each(function(){
     // cached images don't fire load sometimes, so we reset src.
     if (this.complete || this.complete === undefined){
        var src = this.src;
        // webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f
        // data uri bypasses webkit log warning (thx doug jones)
        this.src = blank;
        this.src = src;
     }  
  }); 
  return this;
};

// Home Rotator Logo After Loaded show.
$j(document).ready(function(){
	$j('#homeLogoRotator').hide();
	$j('.hlrg img').imagesLoaded(function() { 
		$j('#preload').remove();
		$j(this).fadeIn(); 
		$j(this).resize({maxWidth: 90 }); 
		$j('#homeLogoRotator img').attr('alt', '').attr('title', '');
		$j('#homeLogoRotator').css('display', 'block');
    });
	// Adjust Footer Alignment
	adjustFooter(5);
	$j(window).bind('resize', function () { 
		adjustFooter();
	});
});


// Top Header Logo Resizing and Show.
$j(document).ready(function(){
	$j('#site-title img').hide();
	$j('#site-title img').imagesLoaded(function() { 
		$j(this).fadeIn(); 
		$j(this).resize({maxWidth: 293 });
    });
});

// Show Brands Logo.
$j(document).ready(function(){
	$j('.brandsTable img').hide();
	$j('.brandsTable img').each(function() {
		$j(this).imagesLoaded(function() { 
			$j(this).fadeIn(); 
		});
    });
});

$j(function($) {
	// Show instant date time
	var currentDate = new Date()
	var month = currentDate.getMonth() + 1
	var day = currentDate.getDate()
	var year = currentDate.getFullYear()
	
	if((pathname.indexOf("/sc/") > -1 ) || (pathname.indexOf("/tc/") > -1 )) {
		$j('.jdate').html(year+"年"+month+"月"+day+"日");
	}else{
		$j('.jdate').html(year+"-"+month+"-"+day);
	}
    $j('.jclock').jclock();
});

$j(function(){
	$j(".menu ul li:first").addClass("firstSep"); // Top Nav First Button left line
	$j("ul#menu-ow_footer  li:last").addClass("lastFooterNav"); // Bottom Footer Last link remove the line
	$j("#sitenews div.news:last").addClass("noDotline"); // Home News last news remove dot line
	$j("#investmentnews div.news:last").addClass("noDotline"); // Home News last news remove dot line
	$j(".breadcrumb ul li:last").addClass("noSep"); // Breadcrumb remove last bg
	$j(".navigationBlock ul li:first").addClass("noline"); // Left Nav remove first line
	$j(".entry-content .attachmentLink:first").addClass("attachmentLinkNoline"); // remove template PDF top line first line
	$j(".entry-content .newsLinkList .padding:first").addClass("newsLinkListNoline"); // remove template Category List first line	
	$j(".documentIcons").prev().css('margin-bottom', '0px'); // remove space of the file icon top
	$j(".sitemap li:first").css("border", "0"); // remove template Category List first line	
	
	// Images Alt & Title
	$j('.brandsTable img').attr('alt', '').attr('title', ''); // remove all Brands alt and title
	$j('.gallery-icon a.lightbox').attr('alt', '').attr('title', ''); // remove post photogallery alt and title	
	$j('.gallery-icon a.lightbox img').attr('alt', '').attr('title', ''); // remove post photogallery alt and title	
	$j('.shopThumb img').attr('alt', '').attr('title', ''); // remove Taxonomy-brand alt and title		
	
	// Cheeky way to disable site navigation first level link
	$j('#menu-item-292 a:first').css('cursor', 'default');
	$j('#menu-item-292 a:first').click(function(e) {
		e.preventDefault();
	});
	$j('#menu-item-59 a:first').css('cursor', 'default');
	$j('#menu-item-59 a:first').click(function(e) {
		e.preventDefault();
	});
	$j('#menu-item-282 a:first').css('cursor', 'default');
	$j('#menu-item-282 a:first').click(function(e) {
		e.preventDefault();
	});
	$j('#menu-item-88 a:first').css('cursor', 'default');
	$j('#menu-item-88 a:first').click(function(e) {
		e.preventDefault();
	});
	$j('#menu-item-91 a:first').css('cursor', 'default');
	$j('#menu-item-91 a:first').click(function(e) {
		e.preventDefault();
	});
	$j('#menu-item-138 a:first').css('cursor', 'default');
	$j('#menu-item-138 a:first').click(function(e) {
		e.preventDefault();
	});		
	
	// 媒體室
	$j('#menu-item-313 a:first').css('cursor', 'default');
	$j('#menu-item-313 a:first').click(function(e) {
		e.preventDefault();
	});	
	//財務報告
	$j('#menu-item-2724 a:first').css('cursor', 'default');
	$j('#menu-item-2724 a:first').click(function(e) {
		e.preventDefault();
	});	
	
	// Cheeky way to disable site left navigation first level link
	// 媒體室
	$j('.page-item-294 a:first').css('cursor', 'default');
	$j('.page-item-294 a:first').click(function(e) {
		e.preventDefault();
	});	
	
	// 財務報告
	$j('.page-item-2720 a:first').css('cursor', 'default');
	$j('.page-item-2720 a:first').click(function(e) {
		e.preventDefault();
	});	

	// Hide / Show Left Nav
//	$j(".leftNav ul.children").hide(); // default hide all children first.
//	$j(".leftNav .current_page_item ul").show(); // show the selected current sub level
	
	// Home News Section Buttons
	$j('#btnSiteNews').mouseover(function() {
		$j('#sitenews').css('display','block');
		$j('#investmentnews').css('display','none');
		$j('#btnSiteNews').addClass('selected');
		$j('#btnInvestNews').removeClass('selected');
	});
	$j('#btnInvestNews').mouseover(function() {
		$j('#sitenews').css('display','none');
		$j('#investmentnews').css('display','block');
		$j('#btnSiteNews').removeClass('selected');
		$j('#btnInvestNews').addClass('selected');
	});
		
	if(pathname.indexOf("/sc/") > -1) {
		$j("#langSC").addClass("selected");
	}else if(pathname.indexOf("/tc/") > -1){
		$j("#langTC").addClass("selected");
	}else if(pathname.indexOf("/en/") > -1){
		$j("#langEN").addClass("selected");
	}else{
		$j("#langTC").addClass("selected");	
	}
	
	$j("#langTC").click(function() {
	  changeLanguage('tc');
	});
	$j("#langSC").click(function() {
	  changeLanguage('sc');
	});
	$j("#langEN").click(function() {
	  changeLanguage('en');
	});
	// Go to Home top Logo Link
	$j("#gotohome").click(function() {
	  gotoHomepage();
	});
	
});

$j(function(){
	// Home Brands logo rotator
	var delay = 2500;
	// for each list item in
	// the rotator ul, generate
	// show a random list item
	$j('#homeLogoRotator > li').each(function(){
	// save images in an array
	var $imgArr = $j(this).children();
	// show a random image
	$imgArr.eq(Math.floor(Math.random()*$imgArr.length)).show();
	});
	// run the changeImage function after every (delay) miliseconds
	setInterval(function(){
	changeImage();
	},delay);
	
	function changeImage(){
	// save list items in an array
	var $liArr = $j('#homeLogoRotator > li');
	// select a random list item
	var $currLi = $liArr.eq(Math.floor(Math.random()*$liArr.length));
	// get the currently visible image
	var $currImg = $j('.hlri:visible', $currLi);
	if ($currImg.next().length == 1) {
		var $next = $currImg.next();
	} else {
		var $next = $j('.hlri:first', $currLi);
	}
	$currImg.fadeOut(1500);
	$next.fadeIn(1500);
	}
});

