/*
 * JTip
 * By Cody Lindley (http://www.codylindley.com)
 * Under an Attribution, Share Alike License
 * JTip is built on top of the very light weight jquery library.
 */

//on page load (as soon as its ready) call JT_init
//JQ(document).ready(JT_init);
jparams = new Object ();

function JT_init(){
	       //JQ("a.jTip")
		   //.click(function(){JQ('#JT').remove();JT_show(this.href,this.id,this.name);return false;})
           //.click(function(){return false});	   
}
function showBuyTip(tipurl,tipid,tipname){
	JQ('#JT').remove();
	JT_show(tipurl,tipid,tipname);
	return false;
}

function JT_show(url,linkId,title){
	if(title == false)title="&nbsp;";
	var de = document.documentElement;
	var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var hasArea = w - getAbsoluteLeft(linkId);
	var clickElementy = getAbsoluteTop(linkId) - 3; //set y position
	
	var queryString = url.replace(/^[^\?]+\??/,'');
	jparams = parseQuery( queryString );
	if(jparams['width'] === undefined){jparams['width'] = 200};
	
	if(hasArea>((jparams['width']*1)+75)){
		JQ("body").append("<div id='JT' style='width:"+jparams['width']*1+"px'><div id='JT_arrow_left'></div><div id='JT_close_left'>"+title+"<img style='cursor:pointer;position:relative;left:125px;top:2px;' onclick='closeTip()' src='/images/closeit.gif' /></div><div id='JT_copy'><div class='JT_loader'><div></div></div>");//right side
		var arrowOffset = getElementWidth(linkId) + 11;
		var clickElementx = getAbsoluteLeft(linkId) + arrowOffset; //set x position
	}else{
		JQ("body").append("<div id='JT' style='width:"+jparams['width']*1+"px'><div id='JT_arrow_right' style='left:"+((jparams['width']*1)+1)+"px'></div><div id='JT_close_right'>"+title+"<img style='cursor:pointer;position:relative;left:125px;top:2px;' onclick='closeTip()' src='/images/closeit.gif' /></div><div id='JT_copy'><div class='JT_loader'><div></div></div>");//left side
		var clickElementx = getAbsoluteLeft(linkId) - ((jparams['width']*1) + 15); //set x position
	}
	
	JQ('#JT').css({left: clickElementx+"px", top: clickElementy+"px"});
	JQ('#JT').show();
	var url2 = "/commons/cart/cart.html"
	JQ('#JT_copy').load(url2,{test:true},function(){setSongInfo()});
	//JQ('#JT_copy').html(JQ('.hiddentip').html())
	//setSongInfo()
}
function setSongInfo(){
	var p = new Number(jparams['count'])
	document.getElementById("jtname").innerHTML="歌曲名称："+jparams['jtname']
	document.getElementById("jtstar").innerHTML="歌手："+jparams['jtstar']
	document.getElementById("jtprice").innerHTML="价格："+jparams['count']+"*0.99 RMB "
	try{
		//document.getElementById("jtprice2").innerHTML=readPrice(p*0.99)
	}catch(exception){
		
	}
}


function getElementWidth(objectId) {
	x = document.getElementById(objectId);
	return x.offsetWidth;
}

function getAbsoluteLeft(objectId) {
	// Get an object left position from the upper left viewport corner
	o = document.getElementById(objectId)
	oLeft = o.offsetLeft            // Get left position from the parent object
	while(o.offsetParent!=null) {   // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent    // Get parent object reference
		oLeft += oParent.offsetLeft // Add parent left position
		o = oParent
	}
	return oLeft
}

function getAbsoluteTop(objectId) {
	// Get an object top position from the upper left viewport corner
	o = document.getElementById(objectId)
	oTop = o.offsetTop            // Get top position from the parent object
	while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent  // Get parent object reference
		oTop += oParent.offsetTop // Add parent top position
		o = oParent
	}
	return oTop
}

function parseQuery ( query ) {
 	var Params = new Object ();
   //if ( ! query ) return Params; // return empty object
   var Pairs = query.split(/[;&]/);
   for ( var i = 0; i < Pairs.length; i++ ) {
      var KeyVal = Pairs[i].split('=');
      if ( ! KeyVal || KeyVal.length != 2 ) continue;
      var key = unescape( KeyVal[0] );
      var val = unescape( KeyVal[1] );
      val = val.replace(/\+/g, ' ');
      Params[key] = val;
   }
   return Params;
}

function blockEvents(evt) {
              if(evt.target){
              evt.preventDefault();
              }else{
              evt.returnValue = false;
              }
}

function closeTip(){
	JQ('#JT').remove();
}

//JTIP相关逻辑
function addToCartTip(){
	addToCart(jparams['id'],jparams['typeid'],jparams['count'],jparams['stype']);
	closeTip()
}

function buy(){
	clientinfo = getClientinfo()
	var requesturl = "http://pay.aigomusic.com/cnpay/payAndDownload.jsp"
	var p="portalid=27&totalmoney="+jparams['count']+"&shoppingCart="+jparams['id']+"-"+jparams['typeid']+"-"+jparams['count']+"-"+jparams['stype']
	var i = getCookie("AIGOMUSICLGTOKEN");
	if (i==""){
		//JQ('.tipinfo').html('您还没有登陆，请先登陆<br />')
		document.getElementById("tipinfo").innerHTML='您还没有登陆，请先登陆<br />'
		document.getElementById("tiplogin").style.display="block";
		//JQ('.tiplogin').show();
		//JQ('.tipsrcurl').val(encodeURIComponent(requesturl+"?"+p+"&clientinfo="+clientinfo))
		document.getElementById("tipsrcurl").value=encodeURIComponent(requesturl+"?"+p+"&clientinfo="+clientinfo)
		return;
	}
	closeTip()
	window.open(requesturl+"?"+p+"&clientinfo="+clientinfo)
}
function errorHandler(errorMessage, url, line){
	//alert(errorMessage);
	return true;
}	
onerror = errorHandler;
//得到客户端信息
function getClientinfo(){
	return netObj.GetSystemInfo();
}