var l = function(x){console.log(x);};
if(typeof window != 'undefined') {
	window.Aiwa = new Object ;
}else{Aiwa = new Object;}

Aiwa.$ = function(arg,dom){
	return Aiwa.$.prototype.init(arg,dom);
};
Aiwa.$.prototype.author = 'Aiwa-20090713';
// toLowerCase
Aiwa.$.prototype.init = function(arg,dom){
	
	this.length = 0;
	var cl = arguments.callee ;
	//args is node ;
	if(arg.nodeType){
		this[this.length] = arg;
		this.length ++ ;
		return this ;
	}
	//string 
	if(typeof arg == 'string'){
		if(typeof dom == 'undefined'){
			dom = new Array;
			dom.push(document);
		}
		if(dom.nodeType){
			var tmp = new Array;
			tmp.push(dom);
			dom = tmp;
		}
		if((typeof dom).toLowerCase() == 'object'){
			var indx = 0;
			var tmp = new Array;
			while(typeof dom[indx] != 'undefined'){
				tmp.push(dom[indx]);
				indx ++ ;
			}
			dom = tmp ;
		}
		
		while(arg.match(/\s\s/)){
			arg = arg.replace(/\s\s/g,' ');
		}
		arg = arg.split(' ');
	}
	///////////////
	
	if(arg.length==1) {
		if(/.*?#(\w*)$/.test(arg[0])){ //'#xxx' by id ;
			this[0] = document.getElementById(RegExp.$1);
			this.length = 1 ;
			return this ;
		}
		
		for(var k=0;k<dom.length;k++){
			if(arg[0].indexOf('.')>0){
				
				var b = arg[0].split('.');
				var c = dom[k].getElementsByTagName(b[0]);
				for(var i=0;i<c.length;i++){
					if(c[i].className.indexOf(b[1])>=0){
						this[this.length] = c[i];
						this.length ++ ;
					}
				}
			}else {
				var c = dom[k].getElementsByTagName(arg[0]);
				for(var i=0;i<c.length;i++){
					this[this.length] = c[i];
					this.length ++ ;
				}
			}
		}
		return this ;
	}
	
	if(arg.length >1){
		var arr = new Array ;
		var str0 = arg[0];
		arg.shift();
		if(/.*?#(\w*)$/.test(str0)) return new cl(arg,Aiwa.$(str0));
		
		for(var k=0;k<dom.length;k++){
			if(str0.indexOf('.')>0){
				var b = str0.split('.');
				var c = dom[k].getElementsByTagName(b[0]);
				for(var i=0;i<c.length;i++){
					if(c[i].className.indexOf(b[1])>=0) arr.push(c[i]);
				}
			}else {
				var c = dom[k].getElementsByTagName(str0);
				for(var i=0;i<c.length;i++){
					arr.push(c[i]);
				}
			}
		}
		return new cl(arg,arr);
	}
	return this ;
};

Aiwa.$.prototype.first = function(){
	return Aiwa.$(this[0]);
};
Aiwa.$.prototype.last = function(){
	return Aiwa.$(this[this.length-1]);
};

Aiwa.$.prototype.css = function(k,v){
	if(typeof k =='string' && typeof v == 'undefined'){
		return this[0].style[k];
	}
	
	if((typeof k).toLowerCase() == 'object'){
		for(var i=0;i<this.length;i++){
			for(var j in k){
				this[i].style[j] = k[j];
			}
		}
		return this ;
	}
	if(typeof v == 'string' && typeof v == 'string'){
		for(var i=0;i<this.length;i++){
			this[i].style[k] = v;
		}
		return this ;
	}
	return this ;
};


Aiwa.$.prototype.html = function(hm){
	if(typeof hm == 'undefined') return this[0].innerHTML;
	if(typeof hm == 'string'){
		for(var i=0;i<this.length;i++){
			this[i].innerHTML = hm;
		}
		return this;
	}
	return this ;
};
////////////////////////////////////////////////////////////////////////////////
var _callxmlhttp=function(type,url,query,callback){
	var xmlhttp=null;
	if(window.XMLHttpRequest){
		xmlhttp=new XMLHttpRequest();
	}
	else if(window.ActiveXObject) {
		xmlhttp=new ActiveXObject("MSXML2.XMLHTTP.3.0");
		if(!xmlhttp){
			xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		if(!xmlhttp){
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	if(xmlhttp){
		xmlhttp.open(type,((type=="POST") ? url : url+"?"+query),true);
		xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4){
				if(xmlhttp.status==200){
					callback(xmlhttp.responseXML);
				}
				else{
				}
			}
			else{
			}
		};
		xmlhttp.send((type=="POST") ? query : null);
	}
	else{
	}
};
//杨稍辉

function showProList(doc){ // Ajax请求回调函数 ;
	var docStr = doc.getElementsByTagName('root')[0].firstChild.nodeValue;
	//2467,2467_1_50X50.jpg,保谷（HOYA) 55mm UV(N)HMC,90.0,1|  数据类型
	docStr = docStr.replace(/\|$/g,'');
	if(docStr.length<=0){  //如果数据为空，显示无商品层
		Aiwa.$('#merchs').css('display','none');
		Aiwa.$(Aiwa.$('div.home_cart')[0]).css('display','block');
		return ;
	}
	Aiwa.$('div.home_cart_products').css({display:''});
	var proList = docStr.split('|');
	var proListDiv = Aiwa.$(Aiwa.$('#merchs div')[0]);
	proListDiv.html('');
	var allMoney = 0;
	var allId = '';
	var allListCount = 0;
	
	proListDiv.html('');
	//Aiwa.$(Aiwa.$('div.home_cart')[0]).css('display','none');
	//Aiwa.$('#merchs').css('display','block');
	
	for(var i=0;i<proList.length;i++){ //循环显示
		var tmpArray =  proList[i].split(',');
		if(tmpArray[3]!='a'){
			allMoney += tmpArray[3] * tmpArray[4];
		}
		allId += ''+tmpArray[0]+',';
		var isGift = tmpArray[3]; //价格，<=0为赠品 
		 
		if(isGift != 'a') allListCount -= (-tmpArray[4]); //如果是赠品,不算再商品件
		var str= '<div style="width:360px;height:50px;margin:0;padding:5px 0px; border-bottom:1px solid #ddd;text-align:left">';
			str +='<div style="width:50px; height:50px; float:left;margin-left:5px;display:inline;"><img title="'+tmpArray[2]+'" src='+jsCtx+'/product_image/'+tmpArray[1]+' " width="50" height="50" border="0" /></div>';
			str +='<div style="height:50px; line-height:25px;width:185px;float:left; margin-left:5px; display:inline;overflow:hidden;">';
				//如果是赠品，则加赠品样式的字  
				 
				if(isGift == 'a') str += '<strong class="gift" style="color:#FF6600;">[赠]</strong>';
				str +=''+tmpArray[2]+'';
			str +='</div>';
			str +='<div style="width:110px;float:left;margin-left:5px;display:inline;height:50px;color:#FF6600">';
				str +='<div style="width:110px; height:25px; text-align:right; line-height:25px;">';
				if(isGift >0) str += '￥'+tmpArray[3]+' 元 ';
				str += 'x '+tmpArray[4]+'</div>';
				str +='<div style="width:110px; height:25px; text-align:right; line-height:25px;">';
				//如果本是赠品，则加删除
				if(isGift >0) str += '<a style="color:#5A3E2E;" href="'+jsCtx+'/shopping/viewShoppingCart.do?m=forwardDelete&id='+tmpArray[0]+'" >删除</a>';
			str +='</div></div></div>';
			//console.log(str);
		proListDiv.html(proListDiv.html()+str);
	}
	Aiwa.$(Aiwa.$('#merchs span')[0]).html('共 '+ allListCount+' 件商品  金额总计：'+allMoney+'  元');
}

function deleteProId(id){
	_callxmlhttp('POST',jsCtx+'/shopping/smallCart.do','method=getAllproducts?id='+id,showProList);
};

function getO(id){
     return document.getElementById(id);
}
var h=null;
//open window
var showTipPro = 0;


function showShop(str){
	showTipPro = 0;
	if(str.length <=0) showTipPro = 2;
	if(Aiwa.$('#dzshoppingcat').css('display')!='none') return ; //如果弹出层显示，直接返回
	Aiwa.$('div.home_cart_products').css({display:'none'});
	_callxmlhttp('POST',jsCtx+'/shopping/smallCart.do','method=getAllproducts',showProList); //Ajax请求,showProList为回调函数
	
	clearInterval(h);
   var shopnumtop=getO("shopnumtop");
   var shopnumdown=getO("shopnumdown");
   var dzshoppingcat=getO("dzshoppingcat");
   var cs=dzshoppingcat.style;
   if(str!=''){
       cs.position="absolute";
       cs.zIndex=9999;
       cs.display="block";
       var xy=new varXY();
       switch(str){
          case 'down':{
             xy.returnXY(shopnumdown);
             cs.left=xy.coorX -85 +"px"; //设置尾部小购物车的left
             cs.top=xy.coorY-dzshoppingcat.clientHeight+5+"px";  //设置尾部小购物车的top
             break;
          }
          case 'ontop':{
             xy.returnXY(shopnumtop);
             cs.left=xy.coorX - 85 +"px";  //设置头部小购物车的left
             cs.top=xy.coorY - (-10)+"px"; ////设置头部小购物车的top
         
             break;
          }
       }
   }
}
//prepare close window
function overShop(index){
	if(typeof index == 'undefined') {
		//showTipPro =0 ;
	}else{
		showTipPro = 0;
	}
   	h=setTimeout(closeDzshoppingcat,500);
}
//close window
function closeDzshoppingcat(){
  var dzshoppingcat=getO("dzshoppingcat");
  if(showTipPro<=1){
  	dzshoppingcat.style.display="none";
  }
  clearTimeout(h);
}
//return window object top and left
function varXY(){
   this.coorX=0;
   this.coorY=0;
}
 varXY.prototype.returnXY=function(object){
     if(object.offsetParent){
       while(object.offsetParent){
	       this.coorY+=new Number(object.offsetTop);
		   this.coorX+=new Number(object.offsetLeft);
		   object=object.offsetParent;
	   }
   }
 }
 //add Merchandise
 function addMerchandise(){
     // Merchandise of Div
     var merch=getO("merchs");
 }