function Set_Cookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) expires = expires * 1000 * 60 * 60 * 24;
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function Get_Cookie( check_name ) {
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false;
	
	for ( i = 0; i < a_all_cookies.length; i++ ) {
		a_temp_cookie = a_all_cookies[i].split( '=' );
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
		if ( cookie_name == check_name ) {
			b_cookie_found = true;
			if ( a_temp_cookie.length > 1 ) {
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	
	if ( !b_cookie_found ) return null;
}

function updatecart(id,price) {
	var cook = Get_Cookie('cart');
	if( (cook&&!cook.match(id)) || !cook ) {
		// куки
		if(document.getElementById('ringsize')) var razm = document.getElementById('ringsize').value; else var razm = 0;
		Set_Cookie('cart', cook+' '+id+':'+razm, 7, '/');
		// блок с ценником
		var container = document.getElementById('cartprice');
		container.innerHTML = number_format((parseInt(container.innerHTML.substr(0,container.innerHTML.length).replace(/ /, ""))+price), 0, '.', ' ');
		// блок с именованием количества в корзине
		var cook = Get_Cookie('cart'); var total = 0;
		if(cook) {
			var sz = cook.split(" ");
			for(var i=0; i<=sz.length;i++) { if(!parseInt(sz[i])) continue; total++; }
			//total++;
		}
		document.getElementById('buynum').innerHTML = total+' товар'+endof(total);
		document.getElementById('sp'+id).style.visibility = 'hidden';
	}
}

function number_format( number, decimals, dec_point, thousands_sep ) {
    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "." : dec_point;
    var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}

function endof(str) {
	if (str%10==0||str%10>=5) return 'ов';
	else if (str%10>=2&&str%10<=4) return 'а';
	else return '';
}


function faker(id) {
	var dd = document.getElementById(id).style.display;
	if(dd=='block') document.getElementById(id).style.display = 'none'; else document.getElementById(id).style.display = 'block';
}

function buyselect(id) {
	var pl = document.getElementById("placeorder"); var hide = document.getElementById("d_hide");
	if(id.value) {
		if(pl) { pl.className = 'buy fl opacity100'; }
		if(id.value=='hold') hide.style.display = 'block';
		else hide.style.display = 'none';
	}
	else { if(pl) { pl.className = 'buy fl opacity30'; } hide.style.display = 'none'; }
}

function checkselect() {
	if(document.getElementById("placeorder").className=='buy fl opacity30') { alert('Пожалуйста выберите место получения товара!'); return false; }
}

function makereplace() {
	var inputs = document.forms[0].getElementsByTagName('input');
	var spans = document.forms[0].getElementsByTagName('span');
	for(var i = 0; i <= inputs.length; i++) {
		if(!inputs[i]) continue;
		if(inputs[i].type == 'hidden' && inputs[i].className == 'chggme' ) inputs[i].type = 'text';
		if(spans[i].className == 'ml20') { 
			spans[i].style.display = 'none';
			insertafter(document.createElement('br'), spans[i]);
		}
	}
	for (i=getDocumentHeight(); i>=206; i--) { self.scroll(1,i); }
}

function insertafter(newChild, refChild) { 
	refChild.parentNode.insertBefore(newChild,refChild.nextSibling); 
}

function getDocumentHeight() {
	return (document.body.scrollHeight > document.body.offsetHeight)?document.body.scrollHeight:document.body.offsetHeight;
}

$(document).ready(function(){
		/* CONFIG */
			
		xOffset = 93;
		yOffset = 30;
			
		/* END CONFIG */
		$("a.preview").hover(function(e){
			this.t = this.title;
			this.title = "";	
			var c = (this.t != "") ? "<br/>" + this.t : "";
			$("body").append("<p id='preview'><img src='"+ this.rel +"' alt='Image preview' />"+ c +"</p>");								 
			$("#preview")
				.css("top",(e.pageY - xOffset) + "px")
				.css("left",(e.pageX + yOffset) + "px")
				.fadeIn("fast");				
	    },
		function(){
			this.title = this.t;	
			$("#preview").remove();
	    });	
		$("a.preview").mousemove(function(e){
			$("#preview")
				.css("top",(e.pageY - xOffset) + "px")
				.css("left",(e.pageX + yOffset) + "px");
		});			
});
