var widgets = {
    
	addToCart: function(shop_id, product_id, price, obj, weight, volume, package_amount) {

		var amount = 1;
		product_id = Number(product_id);

      	if (!isNaN(shop_id)&&(shop_id>0)&&!isNaN(product_id)&&(product_id>0)&&!isNaN(price)&&!isNaN(amount)&&(amount>0)) {      
          // Cookies
          var total = readCookie('CART_TOTAL_'+shop_id);
          var cart = unescape(readCookie('CART_'+shop_id));
          
          // Cart split
          var cart_split = cart.split(';');
          var cart_new = "", cart_item_split, found = false, total_amount = 0;
          
          // Parse total values   
          if (typeof total!="number") total = Number(total);
          if (typeof weight!="number") weight = Number(weight);
          if (typeof volume!="number") volume = Number(volume); 
          if (typeof package_amount!="number") package_amount = Number(package_amount);
          
          // Apply common cookies value merge for each of additional(optional) cookies value types
          mergeCartCookieItem('CART_WEIGHT_'+shop_id, product_id, weight, true);
          mergeCartCookieItem('CART_VOLUME_'+shop_id, product_id, volume, true);
          mergeCartCookieItem('CART_PACKAGE_AMOUNT_'+shop_id, product_id, package_amount, true);
          
          // Do the main loop to encounter price and product amount values 
          for (var i=0; i < cart_split.length; i++){

             cart_item_split = cart_split[i].split("=");

             if (cart_item_split.length == 2){
                 
                if (!found&&cart_item_split[0] == product_id) {

                     //total = total - price*cart_item_split[1];
                     total = Number(total) + Number(price*amount);
                     total_amount = Number(amount)+Number(cart_item_split[1]);
                     found = true;

                     if (cart_new!="") cart_new = cart_new + ";";                     
                     cart_new = cart_new + product_id + "=" + total_amount;

                } else {
                     
                     if (cart_new!="") cart_new = cart_new + ";";                     
                     cart_new = cart_new + cart_item_split[0] + "=" + cart_item_split[1];

                }
               
             }

        }

        // Product is new to cart 
        if (!found) {
            if (cart_new!="") cart_new = cart_new + ";";                     
            cart_new = cart_new + product_id + "=" + amount;
            total = Number(total) + Number(price*amount); 
        }    
        
        // Round total price            
        total = Math.round(total*100)/100;           
        
        // Update cookies             
        createCookie('CART_'+shop_id,cart_new,10); 
        createCookie('CART_TOTAL_'+shop_id,total,10);

        if (document.getElementById('cart_total')) document.getElementById('cart_total').innerHTML = total;
		
		// Display success message  
		var pos = findPos(obj);
		var d = document.createElement("DIV");
		if (d) {
			d.style.position = 'absolute';
			d.innerHTML = 'добавлено...';
			d.style.display = 'block';
			d.className = 'added-to-cart';
			d.style.left = (pos.x+obj.offsetWidth) + 'px';
			d.style.top = (pos.y+obj.offsetHeight) + 'px';
			document.body.appendChild(d);
	
			window.setTimeout(function(){if (d&&d.parentNode)d.parentNode.removeChild(d); delete d;},1500);
	    }	
         
        return true;  
          
          
          
     }

     return false;     
		
	}, 
    
    addToCartWithUpdate: function(shop_id, product_id, price, obj, shopURL, objUpdate, loaderImg, weight, volume, package_amount) {
        if(this.addToCart(shop_id, product_id, price, obj, weight, volume, package_amount)){
            this.updateCart(shopURL, objUpdate, loaderImg);
            return true;
        }
        
        return false;
    },
	
	/*
    * @param mixed updateCont -- optional container where to load an AJAX response from the cart page 
    * note: Uses jQuery
    */
    updateCart : function (shopURL, obj, loaderImg){
        if(!obj) return;
        
        var $obj = $((typeof obj=='string')?'#'+obj:obj);
        
        // Loader image 
        try{
            if(typeof loaderImg == 'string'){
                var src = loaderImg;
                loaderImg = new Image();
                loaderImg.src = src;
                loaderImg.border = 0;
                loaderImg.alt = loaderImg.title = 'Загрузка...';
                loaderImg.className = 'ajax-loader';
            }
          }
          catch(e){}
          if(loaderImg) $obj.empty().append(loaderImg);
        
        
        // Append ? or & to shop URL, if needed
        shopURL += (/\?$/.test(shopURL) ? '' : (/\&$/.test(shopURL) ? '' : '?'));
        shopURL += (/\&$/.test(shopURL) ? '' : (/\?$/.test(shopURL) ? '' : '&'));
        try{
            // Make AJAX request
            $.get(shopURL + 'mode=cart&ajax=1', function(data) {
                $obj.html(data);
            });
        }catch(e){}
    }
	
}

function findPos(obj){

		var result = {};

		result.x = 0;
		result.y = 0;

		if (obj.offsetParent) {
		
			while (obj.offsetParent) {
				result.y += obj.offsetTop;
				result.x += obj.offsetLeft;
				obj = obj.offsetParent;
			}
			
		} else {
			if (obj.x) result.x += obj.x;
			if (obj.y) result.y += obj.y;
		}

		return result;

}

function clientSize() {
		var a = new Object();
		
		a.h = 0; a.w = 0;
	
		if (window.innerHeight >= 0) {
			a.h = window.innerHeight;
			a.w = window.innerWidth;
		} else if (document.documentElement && document.compatMode=='CSS1Compat' /*(!browserController.isMSIE || browserController.isMSIE7)*/) {
			a.h = document.documentElement.clientHeight;
			a.w = document.documentElement.clientWidth;
		} else if (document.body.clientHeight >= 0) {
			a.h = document.body.clientHeight;
			a.w = document.body.clientWidth;
		}
		
		return a;
}

function urlParamReplace(url, kv){
	
	var param_value;
	
	for(param_name in kv){
		param_value = kv[param_name];
	
		if (url.indexOf("&"+param_name+"=")!=-1||url.indexOf("?"+param_name+"=")!=-1) {  
	
			var pattern = param_name + '\\=[^\\&]*';
			var replace = "";
					
			if (param_value!='') {				
					replace=param_name+"="+encodeURIComponent(param_value); 									
					url = url.replace(eval(new String('/([&?])'+pattern+'/').toString()),'$1'+replace);
			} else {
					replace=''; 			
					url = url.replace(eval(new String('/[?]'+pattern+'\\&/').toString()),'?'+replace);
					url = url.replace(eval(new String('/[&?]'+pattern+'/').toString()),replace);	
			}		
		
		} else if (param_value!='') {
					
			if (url.indexOf('?')==-1)
				url = url + "?";
			else 	
				url = url + "&";
			
			url = url + param_name + "=" +encodeURIComponent(param_value); 									
		
		}
	}	
				
	return url;					
}

