function gets_offers(opt){
	var har1=$(opt).find('option:selected').attr('har1');
	var id=$(opt).find('option:selected').attr('ids');
	var sect_id=$(opt).find('option:selected').attr('section_id');
	var id_val=$(opt).find('option:selected').attr('value');
	if($(opt).find('option:selected').attr('har2')){
		var har2=$(opt).find('option:selected').attr('har2');
	}
	if($(opt).find('option:selected').attr('har3')){
		var har3=$(opt).find('option:selected').attr('har3');
	}
	ajax_offer(sect_id,id,har1,har2,har3,id_val);
}

function ajax_offer(sect_id,id,har1,har2,har3,id_val){
	var url="/catalog/ajax_offer.php?HAR1="+har1+"&HAR2="+har2+"&HAR3="+har3+"&SECTION_ID="+sect_id+"&id="+id+"&id_val="+id_val;
	url = encodeURI(url);
	$('#ajax_result_auth').load(url, function(response, status, xhr) 
	{
		$('#ajax_res').empty();
		$('#ajax_res').html(response);
                Cufon.refresh(".rubl");
		//$("#size").html(har1).css("display", "inline");
		//$("#sizeselect").css("display", "none");
	});
}


function changeOffer(buylink, buytext, har1, har2, price, newprice)
{
       var newpricee = newprice;
       if (!newprice) 
                var newpricee = ' '; 
	$("#p_cg").show();
	$("#catalog_add2cart_link").removeAttr("href").attr({href: buylink}).removeClass("disabled").css("cursor", "pointer").html(buytext);
	$("#size").html(har1).css("display", "inline");
	$("#sizeselect").css("display", "none");
	$("#coveringselect").css("display", "none");
    $("#covering").html(har2).css("display", "inline");;
	$("#price").html(price);
	$("#new-price").html(newpricee);
	$("div.offers-window").hide();
    $("div.overlay").hide();
	$("#coveringselect").find('option').hide();
	$("#coveringselect option[name='"+har1+"']").show();
	$("#coveringselect option").each(function (i) {
		if($(this).attr('name')==har1){
			if($(this).text()==har2){
				$(this).attr("selected","selected");
				var desc=$(this).attr("ref")
				$('#dec_har2').empty();
				$('#dec_har2').text(desc);
			}
		}
	});
	// if($("#coveringselect option[name='"+har1+"']").text()==har2){
		// $("#coveringselect option[name='"+har1+"']").attr("selected","selected");
		// var desc=$("#coveringselect option[name='"+har1+"']").attr("ref")
		// $('#dec_har2').empty();
		// $('#dec_har2').text(desc);
	// }
        Cufon.refresh(".rubl");
	

}
function showOffers(mode)
{
if (mode == "size") {
	$("#size").css("display", "none");
	$("#sizeselect").removeAttr("style").css("display", "inline");}

else if (mode == "covering"){
	$("#covering").css("display", "none");
	$("#coveringselect").removeAttr("style").css("display", "inline");}

}

function deleteWish(lid)
{
var	element = document.getElementById(lid);
if (!element)
		return;

$.getJSON('?action=delete&id='+lid, function(){
$(element).css("display", "none");
  });
$(element).css("display", "none");
return false;

}

function disableAddToCart(elementId, mode, text)
{
	var	element = document.getElementById(elementId);
	if (!element)
		return;
	
	if (mode == "detail")
		$(element).html("<span>" + text + "</span>").toggleClass("disabled")
			.removeAttr("href").unbind('click').css("cursor", "default");
	else if (mode == "list")
		$(element).html(text).removeClass("catalogue-item-buy").addClass("catalogue-item-buy")
			.removeAttr("href").unbind('click').css("cursor", "default"); 
}
function disableAddToWish(elementId, text)
{
	var	element = document.getElementById(elementId);
	if (!element)
		return;
			$(element).html(text).toggleClass("disabled").removeAttr("href").removeAttr("onclick").attr({href: "/personal/wishlist/"});

}

function addToCart(element, imageToFlyId, mode, text)
{
	if (!element || !element.href)
		return;

	var button = $(element);
	if (mode == "detail")
		button.toggleClass("disabled").unbind('click').css("cursor", "default");
	else if (mode == "list")
		button.removeClass("catalogue-item-buy").addClass("catalogue-item-buy").unbind('click').css("cursor", "default");

	$.get(
		element.href + "&ajax_buy=1",
		$.proxy(
			function(data) {

				if (this.mode == "detail")
					this.button.removeAttr("href").html("<span>" + text + "</span>");
				else if (this.mode == "list")
					this.button.removeAttr("href").html(text);

				var imageElement = document.getElementById(this.imageToFlyId);
				if (!imageElement)
				{
					$("#cart_line").html(data);
					return;
				}

				var hoverClassName = "";
				var wrapper = null;
				if (this.mode == "detail")
				{
					hoverClassName = "catalog-detail-hover";
					wrapper = this.button.parents("div.catalogue-detail");
				}
				else if (this.mode == "list")
				{
					hoverClassName = "catalogue-item-hover";
					wrapper = this.button.parents("div.catalogue-item");
				}

				wrapper.unbind("mouseover").unbind("mouseout").removeClass(hoverClassName);

				var imageToFly = $(imageElement);
				var position = imageToFly.position();
				var flyImage = imageToFly.clone().insertBefore(imageToFly);

				flyImage.css({ "position": "absolute", "left": position.left, "top": position.top });
				flyImage.animate({ width: 0, height: 0, left: -50, top: -420 }, 600, 'linear');
				flyImage.data("hoverClassName", hoverClassName);
				flyImage.queue($.proxy(function() {

					this.flyImage.remove();
					$("#cart_line").html(data);
                                        Cufon.refresh(".rubl");

					if (this.wrapper.data("adminMode") === true)
					{
						var hoverClassName = "";
						if (this.mode == "detail")
							hoverClassName = "catalogue-detail-hover";
						else if (this.mode == "list")
							hoverClassName = "catalogue-item-hover";

						this.wrapper.addClass(hoverClassName).bind({
							mouseover: function() { $(this).removeClass(hoverClassName).addClass(hoverClassName); },
							mouseout: function() { $(this).removeClass(hoverClassName); }
						});
					}

				}, {"wrapper" : wrapper, "flyImage" : flyImage, "mode": this.mode}));

			}, { "button": button, "mode": mode, "imageToFlyId" : imageToFlyId }
		)
	);

	return false;
}

function disableAddToCompare(elementId, text)
{
	var	element = document.getElementById(elementId);
	if (!element)
		return;

	$(element)
		.removeClass("catalogue-item-compare").addClass("catalogue-item-compare")
		.text(text)
		.unbind('click').removeAttr("href")
		.css("cursor", "default");

	return false;
}

function addToCompare(element, text)
{
	if (!element || !element.href) 
		return;

	var href = element.href;
	var button = $(element);

	button.removeClass("catalogue-item-compare").addClass("catalogue-item-compare").unbind('click').removeAttr("href").css("cursor", "default");

	$.get(
		href + '&ajax_compare=1&backurl=' + decodeURIComponent(window.location.pathname),
		$.proxy(
			function(data) {

				var compare = $("#compare");
				compare.html(data).removeAttr("class");
				
				this.text(text);

				if (compare.css("display") == "none") {
					compare.css({ "display": "block", "height": "0", "float": "left" });
					compare.animate({ "height": "29px" }, 300);
				}
			}, button
		)
	);
	
	return false;
}

function addToWishList(element, text)
{
	if (!element || !element.href) 
		return;

	var href = element.href;
	var button = $(element);

	button.removeAttr("href").removeAttr("onclick").attr({href: "/personal/wishlist/"});

	$.get(
		href + '&ajax_wish=1&backurl=' + decodeURIComponent(window.location.pathname),
		$.proxy(
			function(data) {

				
				this.text(text);

				if (wishlist.css("display") == "none") {
					wishlist.css({ "display": "block", "height": "0" });
					wishlist.animate({ "height": "22px" }, 300);
				}
			}, button
		)
	);
	
	return false;
}


function set_region(code){
$.postJSON('/include/regions.php', {act: 'set_region', region: code}, function(res){
alert(res.region);
});
}
