function center(id, pos) {
	var t = $('#' + id);
    t.css("position",pos);
    t.css("top", ( $(window).height() - t.height() ) / 2 + "px");
    t.css("left", ( $(window).width() - t.width() ) / 2 + "px");
}

function getXMLHTTPObject(){
	try { return new XMLHttpRequest(); } catch(e) {}
	try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch (e) {}
	try { return new ActiveXObject("MSXML2.XMLHTTP.3.0"); } catch (e) {}
	try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
	alert('Your browser doesn\'t suport XMLHttpRequest!');
	return null;
}

function ajaxCall(url, handler)
{
	var x = getXMLHTTPObject();
	var count = arguments.length;
	var id = 0;
	if(count > 2)
	{
		id = arguments[2];
	}
	if(x)
	{
		x.onreadystatechange=function(rr)
		  {
		  if (x.readyState==4 && x.status==200)
		    {
			  response = x.responseText;
			  if(count < 3) 
			  {
				  handler(response);
			  }
			  else
			  {
				  handler(response, id);
			  }
		    }
		  }
		x.open("GET",url,true);
		x.send();
	}
}

function setMessage(mess, type)
{
	$(".system_messages").stop(true, true);
	$(".system_messages").html(mess);
	$(".system_messages").removeClass("error").removeClass("warning").removeClass("success").addClass(type);
	$(".system_messages").slideDown().delay(3000).slideUp();
}

processChangedCart = function(r)
	{
		if(r == "0")
		{
			alert("An error has occurred!");
		}
		else if(r == "false")
		{
			window.location = "/compare_cart/?action=logout";
		} else if(r == "true")
		{
			var resp = confirm("You've added additional items to your compare cart, do you wish to save these before logging out?");
			if(resp)
			{
				save_cart();
			} else {
				window.location = "/compare_cart/?action=logout";
			}
		}
	}

processSaveCart = function(r)
{
	if(r == "0")
	{
		alert("An error has occurred!");
	}
	else if(r == "not_logged_in")
	{
		setMessage("You are not logged in!", "error");
	} else if(r == "empty")
	{
		setMessage("You are trying to save an empty cart!", "warning");
	} else if(r == "ok")
	{
		setMessage("Cart saved successfully!", "success");
	}
	$('#popup_body').fadeOut();
}

processClearCart = function(r)
	{
		if(r == "ok")
		{
			setMessage("Compare Cart cleared!", "success");
		}
		$('#popup_body').fadeOut();
		$('#compare-page').slideDown();
		$('#compare-page').remove();
		$('#compare-page-p').hide();
		$('#compare-page-p').html('The Compare Cart is Empty! Please use the search form or browse through our products and add them to this Compare Cart by clicking the "+ Compare" buttons in the listing.');
		$('#compare-page-p').slideDown();
	}

processClearCartSmall = function(r)
{
	if(r == "ok")
	{
		ajaxCall("/compare_cart/?action=compare_right_box", function(r){
			$("#compare-cart").html(r);
		});
		$('input.comparison_box').attr('checked', false);
	}
}

processAddProduct = function(r, id)
	{
		if(r == "found")
		{
			setMessage("Item already in compare cart!", "warning");
		} else if(r == "no_id")
		{
			setMessage("No Item ID specified!", "error");
		} else if(r == "ok")
		{
			setMessage("Item added to compare cart!", "success");
		}
		ajaxCall("/compare_cart/?action=compare_right_box", function(r, id){
			$("#compare-cart").html(r);
			if(id == -1)
			{
				$('#popup_body').fadeOut();	
				$('img.comparison_add').unbind("click").bind("click", productDetailRemove);
				$('img.comparison_add').attr("src", "/images/comparison_remove.gif").attr("alt", "Remove item from cart").attr("title", "Remove item from cart");
				$('img.comparison_add').removeClass("comparison_add").addClass("comparison_remove");	
			} else {
				$('#al' + id).remove();
			}
		}, id);
	}

processRemoveProduct = function(r, id)
{
	if(r == "found")
	{
		setMessage("Item is not in compare cart!", "warning");
	} else if(r == "no_id")
	{
		setMessage("No Item ID specified!", "error");
	} else if(r == "ok")
	{
		setMessage("Item removed from compare cart!", "success");
	}
	ajaxCall("/compare_cart/?action=compare_right_box", function(r, id){
		$("#compare-cart").html(r);
		if(id == -1)
		{
			$('#popup_body').fadeOut();
			$('img.comparison_remove').unbind("click").bind("click", productDetailAdd);
			$('img.comparison_remove').attr("src", "/images/comparison_add.gif").attr("alt", "Add item to cart").attr("title", "Add item to cart");			
			$('img.comparison_remove').removeClass("comparison_remove").addClass("comparison_add");
		} else {
			$('#al' + id).remove();
		}
	}, id);
}

processAddFeaturedProduct = function(r, id)
{
	if(r == "found")
	{
		setMessage("Item already in compare cart!", "warning");
	} else if(r == "no_id")
	{
		setMessage("No Item ID specified!", "error");
	} else if(r == "ok")
	{
		setMessage("Item added to compare cart!", "success");
	}
	ajaxCall("/compare_cart/?action=compare_right_box", function(r, id){
		$("#compare-cart").html(r);
		$('img#f'+id).unbind("click").bind("click", productDetailRemove);
		$('img#f'+id).attr("src", "/images/comparison_remove.gif").attr("alt", "Removing product comparison.").attr("title", "Removing product comparison.");
		$('img#f'+id).removeClass("comparison_add").addClass("comparison_remove");
		$('#popup_body').fadeOut();	
	}, id);
}

processRemoveFeaturedProduct = function(r, id)
{
	if(r == "found")
	{
		setMessage("Item is not in compare cart!", "warning");
	} else if(r == "no_id")
	{
		setMessage("No Item ID specified!", "error");
	} else if(r == "ok")
	{
		setMessage("Item removed from compare cart!", "success");
	}
	ajaxCall("/compare_cart/?action=compare_right_box", function(r, id){
		$("#compare-cart").html(r);
		$('img#f'+id).unbind("click").bind("click", productDetailAdd);
		$('img#f'+id).attr("src", "/images/comparison_add.gif").attr("alt", "Adding product comparison.").attr("title", "Adding product comparison.");
		$('img#f'+id).removeClass("comparison_remove").addClass("comparison_add");
		$('#popup_body').fadeOut();
	}, id);
}


processLoadCart = function(r, type)
{
	if(r == "no_login")
	{
		setMessage("You are not logged in!", "error");
	} else if (r == "ok")
	{
		if(type == "small")
		{
			ajaxCall("/compare_cart/?action=compare_right_box", function(r){
				$("#compare-cart").html(r);
			});			
		} else {
			renderList();
		}
		$('#popup_body').fadeOut();
	}
}

function renderList()
{
	var slots = $("#items_across_select").val();
	ajaxCall("/compare_cart/?action=render_compare_list&slots=" + slots, function(r){
		ajaxCall("/compare_cart/?action=get_compare_list_product_number", function(r1){
			var nslots = parseInt(r1);
			if(nslots == 0)
			{ 
				$("#items_across_select").childre().remove()
			} else {
				$("#items_across_select option:gt(" + parseInt((nslots - parseInt(1))) + ")").remove();
			}
			if(slots > nslots){
				$("#items_across_select :last-child").attr("selected","selected");
			}
		});
		if($("#compare-page").length > 0)
		{
			$("#compare-page").replaceWith(r);
		} else {
			$("#compare-page-p").replaceWith(r);
		}
	});
}

processSoartedCart = function()
{
	renderList();
	$('#popup_body').fadeOut();
}

productDetailRemove = function(){
	var id = this.id.substr(1);
	center('popup_body', 'fixed');
	$('#popup_body').fadeIn();
	if(this.id == ('f'+id))
	{	
		ajaxCall('/compare_cart/?action=product_remove_compare&id=' + id, processRemoveFeaturedProduct, id);
	} else {
		ajaxCall('/compare_cart/?action=product_remove_compare&id=' + id, processRemoveProduct, -1);
	}
}

productDetailRemoveSmall = function(id){
	center('popup_body', 'fixed');
	$('#popup_body').fadeIn();
	ajaxCall('/compare_cart/?action=product_remove_compare&id=' + id, processRemoveFeaturedProduct, id);
}

function productCompareRemove(id) {
	center('popup_body', 'fixed');
	$('#popup_body').fadeIn();
	ajaxCall('/compare_cart/?action=product_remove_compare&id=' + id, function(r){
		if(r == "found")
		{
			setMessage("Item is not in compare cart!", "warning");
		} else if(r == "no_id")
		{
			setMessage("No Item ID specified!", "error");
		} else if(r == "ok")
		{
			setMessage("Item removed from compare cart!", "success");
		}
		renderList();
		$('#popup_body').fadeOut();		
	});
}

productDetailAdd = function(){
	var id = this.id.substr(1);
	center('popup_body', 'fixed');
	$('#popup_body').fadeIn();
	if(this.id == ('f'+id))
	{
		ajaxCall('/compare_cart/?action=product_add_compare&id=' + id, processAddFeaturedProduct, id);
	} else {
		ajaxCall('/compare_cart/?action=product_add_compare&id=' + id, processAddProduct, -1);
	}
}


function save_cart()
{
	center('popup_body', 'fixed');
	$('#popup_body').fadeIn();
	ajaxCall("/compare_cart/?action=save_compare", processSaveCart);
}

function logoutUserCompare()
{
	ajaxCall("/compare_cart/?action=is_cart_changed", processChangedCart);
}

function product_clear_compare()
{
	center('popup_body', 'fixed');
	$('#popup_body').fadeIn();
	ajaxCall("/compare_cart/?action=product_clear_compare", processClearCart);
}

function product_clear_compare_small()
{
	ajaxCall("/compare_cart/?action=product_clear_compare_small", processClearCartSmall);
}

function loadCompareCartAjax(type)
{
	center('popup_body', 'fixed');
	$('#popup_body').fadeIn();
	ajaxCall("/compare_cart/?action=load_compare_ajax", processLoadCart, type);
}

function sortCart(field)
{
	center('popup_body', 'fixed');
	$('#popup_body').fadeIn();	
	ajaxCall("/compare_cart/?action=load_compare_ajax_sorted&field="+field, processSoartedCart);
}

function moveItem(id, dir)
{
	center('popup_body', 'fixed');
	$('#popup_body').fadeIn();	
	ajaxCall("/compare_cart/?action=move_item&item_id="+id+"&dir="+dir, processSoartedCart);
}

$(document).ready(function(){
	$("input.comparison_box").click(function(){
		var id = this.id.substr(1);
		var gif = $('<img src="/images/add-ajax-loader.gif" alt="ajax loader" id="al' + id + '" />');
		gif.insertAfter(this);
		if(this.checked) 
		{
			ajaxCall('/compare_cart/?action=product_add_compare&id=' + id, processAddProduct, id);
		} else {			
			ajaxCall('/compare_cart/?action=product_remove_compare&id=' + id, processRemoveProduct, id);
		}
	});
	$('img.comparison_add').click(productDetailAdd);
	$('img.comparison_remove').click(productDetailRemove);
	
	$('div[id^="sysmess"]').delay(5000).slideUp();
});
