var selected_options = new Array();
selected_options["color"] 			= "black 070";
selected_options["orientation"] 	= "standard";


$(function() {
	//wall color
	$(".wall2 ul li a").click(function() {
		sel_wall_color(this);
		return false;
	});
	
	//shape color
	$(".wall1 ul li a").click(function() {
		sel_shape_color(this);
		return false;
	});
	
	//choose size
	$("#sizes-prices a").click(function() {
		$("#sizes-prices a").parent().removeClass("sel");
		$(this).parent().addClass("sel");
		return false;
	});
	

	//update the price
	update_price_interval = setInterval(function() {
		selected_options["article_id"] 	= $("#article_id").text();
		selected_options["article_title"] 	= $("#article_title").text();
		if( $("input#quantity").val() === "0" ) $("input#quantity").val("1");
		selected_options["quantity"] 	= $("input#quantity").val();
		selected_options["size"] 	= $("#sizes-prices li.sel em.size").text();
		selected_options["price"] 	= $("#sizes-prices li.sel em.price").text();
		
		var my_price = selected_options["quantity"] * selected_options["price"];
		
		$(".total").html(my_price + " &euro;");
	}, 200);
	
	
	//add to cart on click
	$("a#add-to-cart").click(function() {
		$(this).attr("disabled", "disabled");
		$(this).fadeTo("fast", 0.5);
		
		update_cart(true);
		
		return false;
	});
	
	//change the quantity
	$(".options a.plus").click(function() { quantity_inc(1); return false; });
	$(".options a.minus").click(function() { quantity_inc(-1); return false; });
	
	//choose first size
	$("#sizes-prices a:eq(0)").click();
	
	//update cart
	update_cart(false);
	
});

function empty_cart() {
	//add to cart
	$.ajax({
		 type: 	"POST"
		,url: 	"/cart_empty.php"
		,success: function(ret) {
			//update cart
			update_cart(false);
			
			show_notice("Cart emptied.");
		} //end success()
		
	});
} //end empty_cart()

function update_cart(add) {
	if( add ) {
		mdata = {
			 article_id: 	selected_options["article_id"]
			,article_title: selected_options["article_title"]
			,price: 		selected_options["price"]
			,quantity: 		selected_options["quantity"]
			,size: 			selected_options["size"]
			,orientation: 	selected_options["orientation"]
			,color: 		selected_options["color"]
		}
	} else {
		mdata = '';
	} //end if

	//add to cart
	$.ajax({
		 type: 	"POST"
		,url: 	"/cart.php"
		,dataType: "json"
		,data: 	 mdata
		,success: function(ret) {
			$("a#add-to-cart").removeAttr("disabled");
			$("a#add-to-cart").fadeTo("fast", 1);
			
			//write result
			yhtml = "<em>Quantity:</em> " + ret.total_quantity + "<br />";
			yhtml += "<em>Total:</em> " + ret.total_price + " EUR<br />";
			yhtml += '<a href="#" id="empty-cart" onclick="empty_cart(); return false;">Empty cart</a>';
			if( $("#shopping-cart").length > 0 ) checkout_btn = "";
			else checkout_btn = '<a href="/checkout">checkout</a>';
			$("#nav2 .c2").html(checkout_btn + '<span>' + yhtml + '</span>');
			
			if(add) show_notice("Added to cart.");
			//alert("Added to cart.");
		} //end success
		,error: function(ret) {
			$("a#add-to-cart").removeAttr("disabled");
			$("a#add-to-cart").fadeTo("fast", 1);
			//alert(ret);
		} //end error()
		
	});
}

function show_notice(txt) {
	$(".options .notice").html(txt);
	$(".options .notice").fadeIn(600);
	setTimeout(function() {
		$(".options .notice").fadeOut(1200);
	}, 3000);
}


function sel_wall_color(el) {
	var selected_color = $(el).attr("rel");
	selected_color = "#" + selected_color.substring(1);
	
	//select element
	$(".wall2 ul li a").removeClass("sel");
	$(el).addClass("sel");
	
	//change the color
	$("#ozadje").css("backgroundColor", selected_color);
	
} //end sel_wall_color()

function sel_shape_color(el) {
	var selected_color = $(el).attr("rel");
	selected_color = "#" + selected_color.substring(1);
	
	//select element
	$(".wall1 ul li a").removeClass("sel");
	$(el).addClass("sel");
	
	//load new color shape
	$("#ozadje img").attr("src", "/dekoracije/dek_1/red.png");
	
	//set selected value
	//selected_options["color"] = $(el).attr("rel");
	
} //end sel_wall_color()


function set_color(col) {
	//alert("EN: " + col);
	selected_options["color"] = col;
}

function set_orientation(col) {
	selected_options["orientation"] = col;
}


function quantity_inc(inc) {
	if( $("input#quantity").val() * 1 ) {
		end_val = ( $("input#quantity").val() * 1) + inc;
		if( end_val < 1 ) end_val = 1;
		$("input#quantity").val( end_val )
	} else {
		$("input#quantity").val("0");
	} //end if
} //end quantity_inc()




