/**
 * @package Valkyrie CMS
 * @version 1.0
 * @author djarlaks
 * @copyright wasd.su
 */
$(document).ready(function(){w_=$(document).width();h_=$(document).height();Init()});function obj(id){return $("#"+id.toString())}function div(id){return $("#div"+id)}function get(b,c,d,e,f)/*url,data,id,noReWrite*/{div(d).fadeTo('normal','0.5');$.ajax({type:"POST",url:b,data:c,success:function f(z){g="";div(d).fadeTo('normal','1');if(e)g=div(d).html();div(d).html(g+z)}})}function size(id,x,y){if(x!=0)div(id).width(eval(x));if(y!=0)div(id).height(eval(y))}function move(id,x,y){if(eval(x)<0)x=10;div(id).css("left",eval(x));div(id).css("top",eval(y))}function cen(id){if(id==null)return w_/2;else return befx(id)+w(id)/2}function mid(id){if(id==null)return h_/2;else return befy(id)+h(id)/2}function befx(id){return div(id).position().left}function aftx(id){return befx(id)+w(id)}function befy(id){return div(id).position().top}function afty(id){return befy(id)+h(id)}function w(id){return div(id).width()}function h(id){return div(id).height()}function fade(f){if(div(0)[0]==undefined){$('body').append('<div id="div0" style="opacity:0;background:black"></div>');size(0,w_,h_);div(0).fadeTo(600,0.5,function(){eval(f)})}else{eval(f);div(0).fadeTo(600,0,function(){div(0).remove()})}}

function base64_decode( data ) {	// Decodes data encoded with MIME base64
	// 
	// +   original by: Tyler Akins (http://rumkin.com)


	var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	var o1, o2, o3, h1, h2, h3, h4, bits, i=0, enc='';

	do {  // unpack four hexets into three octets using index points in b64
		h1 = b64.indexOf(data.charAt(i++));
		h2 = b64.indexOf(data.charAt(i++));
		h3 = b64.indexOf(data.charAt(i++));
		h4 = b64.indexOf(data.charAt(i++));

		bits = h1<<18 | h2<<12 | h3<<6 | h4;

		o1 = bits>>16 & 0xff;
		o2 = bits>>8 & 0xff;
		o3 = bits & 0xff;

		if (h3 == 64)	  enc += String.fromCharCode(o1);
		else if (h4 == 64) enc += String.fromCharCode(o1, o2);
		else			   enc += String.fromCharCode(o1, o2, o3);
	} while (i < data.length);

	return enc;
}

function getCookie(name) {
	var matches = document.cookie.match(new RegExp(
	  "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
	))
	return matches ? decodeURIComponent(matches[1]) : undefined 
}

function setCookie(name, value, props) {
	props = props || {}
	var exp = props.expires
	if (typeof exp == "number" && exp) {
		var d = new Date()
		d.setTime(d.getTime() + exp*1000)
		exp = props.expires = d
	}
	if(exp && exp.toUTCString) { props.expires = exp.toUTCString() }

	value = encodeURIComponent(value)
	var updatedCookie = name + "=" + value
	for(var propName in props){
		updatedCookie += "; " + propName
		var propValue = props[propName]
		if(propValue !== true){ updatedCookie += "=" + propValue }
	}
	document.cookie = updatedCookie

}

function deleteCookie(name) {
	setCookie(name, null, { expires: -1 })
}


function addtocart(i,c){if(typeof(c)=='undefined')c=1;$.post('/js.php',{'d':'{"cart":{}}','count':c,'add_item':i},function(d){set_cart(d);refresh_cart()})}
function delfromcart(i,c){if(typeof(c)=='undefined')c=1;$.post('/js.php',{'d':'{"cart":{}}','count':c,'del_item':i},function(d){set_cart(d);refresh_cart()})}
function set_cart(d){deleteCookie('cart');setCookie('cart',d)}
function formatString(str, sym) {
 var s = str.charAt(0);
 var l = str.length;
 var i;
 for (i = 1; i < l; i++)
 {
  if ((l - i) % 3 == 0)
   s += sym;
  s += str.charAt(i);
 }
 return s;
}
function makeString(value) {
 var ost = value % 10;
 var txt = value + ' товар';
 if (ost == 0 || ost > 4 || (parseInt(value / 10) % 10) == 1)
  txt += 'ов';
 else if (ost > 1)
  txt += 'а';
 return txt;
}
function Rubles(value)
{
 var s = 'Рубл';
 var ost = value % 10;
 if (ost == 0 || ost > 4 || (parseInt(value / 10) % 10) == 1)
  s += 'ей';
 else if (ost > 1)
  s += 'я';
 else
  s += 'ь';
 return s;
}
function refresh_cart(){var ck=getCookie('cart');if(typeof(ck)!='undefined'&&ck.length>0){
eval('cart='+base64_decode(ck));var value=0;var counts=0;
for(var i in cart){counts+=parseInt(cart[i][3]);value+=parseInt(cart[i][3])*parseInt(cart[i][2])}
var Scounts = makeString(counts);
var str = $('div.cart').html();
var s = str.substr(0, str.indexOf('<span'));
 s += '<span class="strong">' + counts.toString() + '</span> Т' + Scounts.substr(Scounts.indexOf('о')) + 
'<br/>' + '<span class="strong">' + formatString(value.toString(), ',') + '</span> ' + Rubles(value);
 $('div.cart').html(s);
}}
