/*
 * The very first increment of Droppables&Draggables demo. The code is going to
 * be more concise (remove unnecessary code repetitions etc.). And imho the
 * photo management is a good candidate for demonstration of more jQuery UI
 * components (sortables, selectables...). More to come...
 *
 */

jQuery(window).bind('load', function() {
	
	// make images in the gallery draggable
	jQuery('.img_content').draggable({
		helper: 'clone',
		revert: true
	});
	
	jQuery('#recycle_bin').droppable({
		accept: '.in_trash_now',
		activeClass: 'recycle_active',
		drop: function(ev, ui) {
			//alert(jQuery(ui.draggable).parent().text());
			var product_id = jQuery(ui.draggable).attr("id");
			var product_qty = jQuery("#qty_"+product_id).text();
			var product_price = jQuery(ui.draggable).attr("title");

			var total_in_cart = Total_Qty_In_Cart();
			if (product_qty > 1) {
				
				jQuery(ui.draggable).attr("disabled", "disabled");
				jQuery(ui.draggable).ajaxComplete(function () {
					
					jQuery(this).attr("disabled", "");
				});
				
				jQuery.get('product_xmlhttp.php?action=delete&mode=1&pid='+product_id, function(xml) {
					jQuery(xml).find('result').each(function(){
				      var total_price = jQuery("total_price", this).text();
				      var qty = jQuery("qty", this).text();
				      jQuery("#total_price").text(total_price);
						jQuery("#qty_"+product_id).text(qty);
				   });
					
				});
			} else {
				if (total_in_cart > 5) {
					var left_page = jQuery("#left").text();
					var prev_left_page = jQuery(".in_trash_now#"+left_page).prev().attr("id");
					if (prev_left_page != undefined) {
						jQuery(".in_trash_now#"+prev_left_page).show();
						jQuery("#left").text(prev_left_page);
					}
				}
				ui.draggable.remove();
				
				var latest_qty_in_cart = Total_Qty_In_Cart();
				jQuery("#total_item").text(latest_qty_in_cart);
				jQuery.get('product_xmlhttp.php?action=delete&mode=2&pid='+product_id, function(xml) {
					jQuery(xml).find('result').each(function(){
				      var total_price = jQuery("total_price", this).text();
				      jQuery("#total_price").text(total_price);
				   });
				});
			}
		}
	});
	
	jQuery('#cart').droppable({
		accept: '.img_content',
		activeClass: 'active',
		drop: function(ev, ui) {
			var product_price = jQuery(ui.draggable).attr("title");
			var product_id = jQuery(ui.draggable).attr("id");
			var img_location = jQuery("#img_pic_"+product_id).attr("src");
			var img_name = jQuery("#img_pic_"+product_id).attr("title");

			jQuery.get('product_xmlhttp.php?action=add_to_cart&pid='+product_id, function(data) {
				
			});
			//alert(jQuery(".in_trash_now#"+product_id).attr('id'));
			if (jQuery(".in_trash_now#"+product_id).attr('id') == undefined) {
				jQuery(jQuery(ui.draggable)
					.clone()
					.hide()
					.removeClass('img_content')
					.draggable({
						helper: 'clone'
					})
					.addClass('in_trash_now')
					.fadeIn()
				).appendTo("#product_listing");
				
				jQuery(".in_trash_now#"+product_id+" img").attr("src","images/products/"+product_id+"/50_"+img_name);
				
				var hidden_field_product_id = '<input type="hidden" name="hidden_product_id[]" value="'+product_id+'" id="hidden_'+product_id+'" />';
				var hidden_field_qty = '<input type="hidden" name="hidden_qty['+product_id+']" value="1" id="hidden_qty_'+product_id+'" />';
				var append_price = '<div style="float:left;text-align:center;">'+product_price+'</div>';
				var append_operator = '<div style="float:left;text-align:center;" id="operator_'+product_id+'">&nbsp;x&nbsp;</div>';
				var append_qty = '<div style="float:left;display:block;" id="qty_'+product_id+'">1</div>';
				var combined_append = '<div id="ext_'+product_id+'" style="text-align:center;padding-left:10px;margin-top:-7px;">'+hidden_field_product_id+hidden_field_qty+append_price+append_operator+append_qty+'</div>';
				
				jQuery(".in_trash_now#"+product_id).append(combined_append);
				jQuery("#right").text(product_id);
				
				PagingInCart();
			} else {
				
				var prev_qty = jQuery("#qty_"+product_id).text();
				
				var new_qty = eval(prev_qty + '+' + 1);
				jQuery("#qty_"+product_id).text(new_qty);
				jQuery("#hidden_qty_"+product_id).val(new_qty);
				//Calculate_Total();
			}
			
			total = jQuery("#total_price").text();
			var new_total_price = eval(total + '+' + product_price);
			new_total_price = Math.round(eval(new_total_price)*100)/100;
			jQuery("#total_price").text(new_total_price);
		}
	});

});

	function AddToCart(product_id) {
		jQuery.get('product_xmlhttp.php?action=add_to_cart&pid='+product_id, function(data) {
			
		});
		
		jQuery.growlUI('Product Added', 'Thank You');
		
		var t=setTimeout("jQuery.unblockUI();",1500)
		ItemInCart();
	}
	
	function make_it_draggable () {

	
		jQuery('.in_trash_now').draggable({
			helper: 'clone'
		});
		
		// make the trash box droppable, accepting images from the content section only
		
	}
	
	function PagingInCart() {
		var products_arr = new Array();
		
		jQuery("#cart").find("li").each(function() {
			//alert(this.id);
			products_arr.push(this.id);
		});
		
		total_in_cart = count(products_arr);
		
		jQuery("#total_item").text(total_in_cart);
		var page = eval(total_in_cart + '/' + 5);
		jQuery("#page").text(page);
		
		if (total_in_cart > 5) {
			var last_five = eval(total_in_cart + '-' + 5);
			var show_cnt = 1;
			for (var product_cnt = 0; product_cnt < total_in_cart; product_cnt++) {
				if (last_five > product_cnt) {
					jQuery(".in_trash_now#"+products_arr[product_cnt]).hide();
				} else {
					if (show_cnt == 1) {
						jQuery("#left").text(products_arr[product_cnt]);
					}
					jQuery(".in_trash_now#"+products_arr[product_cnt]).show();
					show_cnt ++;
				}
				
				if (product_cnt == eval(total_in_cart + '-' + 1)) {
					var right_button_inactive = products_arr[product_cnt];
					jQuery("#right_button_inactive").text(right_button_inactive);
				}
				
				if (product_cnt == 4) {
					var left_button_inactive = products_arr[product_cnt];
					jQuery("#left_button_inactive").text(left_button_inactive);
				}
			}
		}
	}

	function SlideLeft () {
		var left_page = jQuery("#left").text();
		var right_page = jQuery("#right").text();
		var left_button_inactive = jQuery("#left_button_inactive").text();
		
		var total_in_cart = jQuery("#total_item").text();
		
		if (total_in_cart > 5) {
			if (left_button_inactive != right_page) {
				//alert(left_page);
				var prev_left_page = jQuery(".in_trash_now#"+left_page).prev().attr("id");
				if (prev_left_page != undefined) {
					jQuery(".in_trash_now#"+prev_left_page).show();
					jQuery(".in_trash_now#"+right_page).hide();
					jQuery("#left").text(prev_left_page);
					var right_button = jQuery(".in_trash_now#"+right_page).prev().attr("id");
					jQuery("#right").text(right_button);
				}
			}
		}
	}
		
	function SlideRight () {
		var left_page = jQuery("#left").text();
		var right_page = jQuery("#right").text();
		var right_button_inactive = jQuery("#right_button_inactive").text();
		
		var total_in_cart = jQuery("#total_item").text();
		var left_button = '';
		
		if (total_in_cart > 5) {
			if (right_button_inactive != right_page) {
				var next_right_page = jQuery(".in_trash_now#"+right_page).next().attr("id");
				if (next_right_page != undefined) {
					jQuery(".in_trash_now#"+next_right_page).show();
					jQuery(".in_trash_now#"+left_page).hide();
					jQuery("#right").text(next_right_page);
					var left_button = jQuery(".in_trash_now#"+left_page).next().attr("id");
					jQuery("#left").text(left_button);
				}
			}
		}
	}
	
	function Default_In_Cart () {
		var total_in_cart = 0;
		var products_arr = new Array();
		
		jQuery("#cart").find("li").each(function() {
			products_arr.push(this.id);
		});
		
		total_in_cart = count(products_arr);
		
		for (var i = 0; i < total_in_cart; i++) {
			if (i >= 0 && i < 3) {
				jQuery(".in_trash_now#"+products_arr[i]).show();
			} else {
				jQuery(".in_trash_now#"+products_arr[i]).hide();
			}
		}
	}
	
	function Calculate_Total () {
		var product_price = '';
		var total_price = 0;
		jQuery("#total_price").text('0');
		jQuery("#cart").find("li").each(function() {
			//alert(this.id);
			if (this.id != '') {
				var qty = jQuery("#qty_"+this.id).text();
				product_price = eval(this.title + '*' + qty);
				total = jQuery("#total_price").text();
				total_price = eval(total + '+' + product_price);
				total_price = Math.round(eval(total_price)*100)/100;
			}
		});
		jQuery("#total_price").text(total_price);
	}
	
	function Total_Qty_In_Cart () {
		var total_li = jQuery("#cart li").length;
		var total = eval(total_li + '-' + 1);
		return total;
	}
	
		
	function ClearAll () {
		jQuery.get('product_xmlhttp.php?action=clear_cart', function(data) {
				
		});
		jQuery("#cart .in_trash_now").remove();
		jQuery("#total_price").text('0');
		var total_in_cart = Total_Qty_In_Cart();
		jQuery("#total_item").text(total_in_cart);
		Total_Qty_In_Cart();
		
		jQuery("#total_price").text('0');
		jQuery("#total_item").text('0');
		jQuery("#left_button_inactive").text('0');
		jQuery("#right_button_inactive").text('0');
		jQuery("#left").text('0');
		jQuery("#right").text('0');
	}
	
	function ItemInCart () {
		jQuery.get('product_xmlhttp.php?action=item_in_cart', function(xml) {
			jQuery(xml).find('result').each(function(){
		      var qty = jQuery("qty", this).text();
		      jQuery("#cart_num").text(qty);
		   });
		});
	}
	
	function set_record(total_item, last_id, total_price_in_cart) {
		jQuery("#total_price").text(total_price_in_cart);
		jQuery("#total_item").text(total_item);
		jQuery("#left_button_inactive").text('0');
		jQuery("#right_button_inactive").text('0');
		jQuery("#left").text('0');
		jQuery("#right").text(last_id);
	}