/* ---------------------------------------------------------------------
JavaScript for SakuraShops
FileName: common.js
--------------------------------------------------------------------- */


// *** OS
var UA = navigator.userAgent;
var osWin = (UA.indexOf("Win", 0) != -1);
var osMac = (UA.indexOf("Mac", 0) != -1);


// *** browser
var brwOpera = (UA.indexOf('Opera') != -1);
var brwIE = (UA.indexOf('MSIE') != -1) && !brwOpera;
var brwFirefox = (UA.indexOf('Firefox') != -1);
var brwSafari = (UA.indexOf('Safari') != -1);
var brwGecko = (UA.indexOf('Gecko') != -1) && !(UA.indexOf('like Gecko') != -1);
var brwNetscape = (UA.indexOf('Netscape') != -1);
UA = undefined;


// *** method
function DW(htmlSrc){document.write(htmlSrc);}
function DWL(htmlSrc){document.writeln(htmlSrc);}


// *** add preload object
var preLoadObj = new Array();

function addPreLoad(imgObj){
	preLoadObj = preLoadObj.concat(imgObj);
}


// *** image preload
function imgPreLoad(){
	preLoadImg = new Array();
	for(var i = 0; i < preLoadObj.length; i++){
		preLoadImg[i] = new Image();
		preLoadImg[i].src = preLoadObj[i];
	}
}


Event.domReady.add(function() {
  imgPreLoad();
  design();
});



// *** image over
function imgChange(imgObj){
	var imgSrc = imgObj.src;
	var imgStatus = imgSrc.indexOf("_on") != -1;
	if(!imgStatus){
		var P = imgSrc.lastIndexOf(".");
		imgSrc = imgSrc.substr(0, P) + '_on' + imgSrc.substring(P);
	} else {
		imgSrc = imgSrc.replace('_on', '');
	}
	imgObj.src = imgSrc;
}


// *** image auto sizing
function imgAutoSizing(imgObj, maxW, maxH){
	if(osMac && brwIE && !brwOpera) return;
	if(maxH == undefined) maxH = maxW;
	var maxAspectRatio = maxW / maxH;
	var imgDummy = new Image();
	imgDummy.src = imgObj.src;
	var originalW = brwSafari ? imgObj.width : imgDummy.width;
	var originalH = brwSafari ? imgObj.height :  imgDummy.height;
	var originalAspectRatio = originalW / originalH;

	if(originalW < maxW && originalH < maxH){
		imgObj.style.width = originalW + 'px';
		imgObj.style.height = originalH + 'px';
		var styleW = imgObj.style.width;
		styleW = Number(styleW.substr(0, styleW.length - 2));
		var styleH = imgObj.style.height;
		styleH = Number(styleH.substr(0, styleH.length - 2));
		return new Array(styleW, styleH);
	}

	if(originalAspectRatio < maxAspectRatio){
		imgObj.style.width = Math.floor(maxH * originalAspectRatio) + 'px';
		imgObj.style.height = maxH + 'px';
	} else{
		imgObj.style.width = maxW + 'px';
		imgObj.style.height = Math.floor(maxW / originalAspectRatio) + 'px';
	}
	var styleW = imgObj.style.width;
	styleW = Number(styleW.substr(0, styleW.length - 2));
	var styleH = imgObj.style.height;
	styleH = Number(styleH.substr(0, styleH.length - 2));
	
	return new Array(styleW, styleH);
}


// *** display change
function dispChange(nextObj, beforeObj){
	document.getElementById(beforeObj).style.display = 'none';
	document.getElementById(nextObj).style.display = 'block';
}


// *** global cart action
function gnCartOver(obj, status){
	document.getElementById(obj).style.color = status ? '#595959' : '#F23D00';
	document.getElementById(obj).style.textDecoration = status ? 'underline' : 'none';
}

function gnCartJump(obj){
	var nextHref = document.getElementById('gn_btn_obj').href;
	location.href = nextHref;
}

// *** anchor link open
function anchorOpen(targetId, anchorObj){

	var targetObj = document.getElementById(targetId);
	targetObj.style.display = (targetObj.style.display == 'block') ? 'none' : 'block';
	anchorObj.className = (anchorObj.className == 'active') ? '' : 'active';

	return true;
}

// currency change script

function change_currency(id, lang, change) {
  var manager = new CookieManager({shelfLife:30});

  var currency = '';
//  if (id) {
  if (change == '1') {
    currency = $F(id);
//alert("form currency : "+currency);
//alert("lang : "+lang);
  }
  else {
    currency = manager.getCookie('currency')
//alert("cookie : "+currency);
    if (! currency) {
      if (lang == 'chs' || lang == 'cht') {
        currency = 'RMB';
      }
      else if (lang == 'en') {
        currency = 'USD';
      }
      else {
        currency = 'JPY';
      }
    }
//alert("cookie currency : "+currency);
//alert("lang : "+lang);
  }
//alert("currency : "+currency);

// currency change
  if ($('jpyrmb')) {
    if (currency == 'JPY' || currency == 'RMB') {
      $('jpyrmb').style.display = "inline";
      $('jpyusd').style.display = "none";
    }
    else if (currency == 'USD') {
      $('jpyrmb').style.display = "none";
      $('jpyusd').style.display = "inline";
    }
  }
  if ($('jpyrmb1_1')) {
    if (currency == 'JPY' || currency == 'RMB') {
      for (var n = 1; n <= 20; n++) {
        if(! $('jpyrmb'+n+'_1')) break;
        for (var i = 1; i <= 20; i++) {
          if(! $('jpyrmb'+n+'_'+i)) break;
          $('jpyrmb'+n+'_'+i).style.display = "inline";
          $('jpyusd'+n+'_'+i).style.display = "none";
        }
      }
    }
    else if (currency == 'USD') {
      for (var n = 1; n <= 20; n++) {
        if(! $('jpyrmb'+n+'_1')) break;
        for (var i = 1; i <= 20; i++) {
          if(! $('jpyrmb'+n+'_'+i)) break;
          $('jpyrmb'+n+'_'+i).style.display = "none";
          $('jpyusd'+n+'_'+i).style.display = "inline";
        }
      }
    }
  }
//alert('currency : '+currency);
// sync other select
//  if (id == 'goods_currency_select' || id == 'left_menu_currency_select') {
    if ($('left_menu_currency_select')) $('left_menu_currency_select').value = currency;
    if ($('goods_currency_select')) $('goods_currency_select').value = currency;
//  }
//  else if (id == 'cart_currency_select') {
//    if ($('left_menu_currency_select')) $('left_menu_currency_select').value = currency;
    if ($('cart_currency_select')) $('cart_currency_select').value = currency;
//  }

  manager.setCookie('currency', currency);

}





