if(typeof(cmp)=='undefined')
{
	var cmp = 1;
}


$(document).ready(function (){
	/*
	 @COMPARE
	*/
	$(".compare a").click(function (){
		// add to compare list
		if(cmp>3)
			return false;
		add_cmp(this);
		return false;
	});
	function add_cmp(object)
	{
		var par = $(object).parent();
		par.css('display', 'none');// hide compare button
		par.parent().find(".comparing").css('display', '');// show comparing button
		var obj = $("."+cmp+"_cmp").html(par.parent().parent().html());// copy html to compare list
		obj.find(".compare, .comparing").css('display', 'none');// in comparing list hide cmp buttons
		obj.find(".price").css('display', 'none');// im comparing list hide price
		obj.find(".compare_item_del").css('display', '');// show delete button
		cmp++;
		var cid = parseInt(par.parent().attr('class'));//alert(cid);
		show_cmp_btn();
		ajax_add_cmp(cid);// send add ajax request
		
		
		// event listener
		$(".compare a").unbind("click");
		$(".compare a").click(function (){
			// add to compare list
			if(cmp>3)
				return false;
			add_cmp(this);
			return false;
		});
	}
	
	$(".comparing a").click(function (){
		// remove from compare list
		del_cmp(this);
		return false;
	});
	function del_cmp(object)
	{
		var par = $(object).parent();
		par.css('display', 'none');// hide comparing button
		par.parent().find(".compare").css('display', '');// show compare button
		var class_name = par.parent().attr("class");// get conatainer class attribute
		var obj = $(".product_comparing_items").find("."+class_name).parent();// find the object in comparing list
		obj.html($(".compare_item_free").html());// remove it
		var cid = parseInt(obj.attr('class'));// replacing all items to left (if needed)
		for(var i=cid; i<cmp-1;i++)
		{
			$("."+i+"_cmp").html($("."+(i+1)+"_cmp").html());
		}
		$("."+i+"_cmp").html($(".compare_item_free").html());
		cmp--;
		show_cmp_btn();
		ajax_del_cmp(parseInt(class_name));// send del ajax request
		
		// event listener
		$(".comparing a").unbind("click");
		$(".comparing a").click(function (){
			// remove from compare list
			del_cmp(this);
			return false;
		});
	}
	
	show_cmp_btn(); // onload show compare button if there are more then 2 elements in compare list
	
	
	
	/*
	 @PHONE VIEW
	*/
	$("a.color_item").click(function (){
		var color = $(this).parent().attr('title');
		if($(".color_"+color).hasClass("color_activ"))
			return false;
		var img_src = $(".color_"+color+" div:first a").attr('href');
		$(".color_sel").removeClass("color_sel");
		//$(".color_container").removeClass("color_activ");
		$(this).addClass("color_sel");
		var w = $(".prod_img_big img").width();
		var h = $(".prod_img_big img").height();
		$(".prod_img_big").width(w).height(h);
		$(".color_activ").fadeOut('', function (){
			$(this).removeClass("color_activ");
		});
		$(".prod_img_big img").fadeOut('', function (){
			$(this).attr("src", img_src);
			$(".color_"+color).fadeIn().addClass("color_activ");
			$(this).load(function (){
				$(this).fadeIn();
			});
		});
		
		return false;
	});
	
	$(".prod_img_thumb a").click(function (){
		var href = $(this).attr('href');
		var href_cur = $(".prod_img_big img").attr("src");
		if(href_cur==href)
			return false;
		var w = $(".prod_img_big img").width();
		var h = $(".prod_img_big img").height();
		$(".prod_img_big").width(w).height(h);
		$(".prod_img_big img").fadeOut('', function (){
			//$(this).hide();
			$(this).attr("src", href);
			//$(this).hide();
			$(this).load(function (){
				$(this).fadeIn();
			});
		});
		return false;
	});
	
	
	// search form
	var already_blured=false;
	$(".searchfield").focus(function (){
		if(!already_blured)
		{
			$(this).val('');
			already_blured=true;
		}
	}).blur(function (){
		if($(this).val()=='')
		{
			$(this).val(search_label);
			already_blured=false;
		}
	});
});

function del_func(obj) {
	// remove from compare list
	var cmp_obj = $(obj).parent().parent();// compare list object
	var obj2 = $(".product_items").find("."+cmp_obj.attr('class'));// container class
	obj2.find(".comparing").css('display', 'none');// hide comparing btn
	obj2.find(".compare").css('display', '');// hide compare btn
	obj = cmp_obj.parent();
	obj.html($(".compare_item_free").html());// remove the compare item
	var cid = parseInt(obj.attr('class'));// replacing all items to left (if needed)
	for(var i=cid; i<cmp-1;i++)
	{
		$("."+i+"_cmp").html($("."+(i+1)+"_cmp").html());
	}
	$("."+i+"_cmp").html($(".compare_item_free").html());
	cmp--;
	show_cmp_btn();
	ajax_del_cmp(parseInt(cmp_obj.attr("class")));// send del ajax request
	return false;
};

function ajax_add_cmp(id)
{
	var url = self.location.href;
	$.post(url, { ajax: "true", add_cmp_item: id } );
}

function ajax_del_cmp(id)
{
	var url = self.location.href;
	$.post(url, { ajax: "true", del_cmp_item: id } );
}

function show_cmp_btn()
{
	if(cmp>2)
	{
		$(".compare_button").slideDown();
	}
	else
	{
		$(".compare_button").slideUp();
	}
}

