jQuery.noConflict();

/*
jQuery(document).ready(function() {
	jQuery("a.buy").click(function() {
		jQuery("div#checkLocation").css({
			left: jQuery(this).offset().left,
			top: jQuery(this).offset().top
		}).show();
		return false;
	});
});
*/

jQuery(document).ready(function() {
	/*
	jQuery(window).scroll(function() {
		if (checkLocation) {
			jQuery("div#checkLocation").css({
				left: boxPosition().left,
				top: boxPosition().top
			});
		}
	});
	*/
	jQuery("input#ort").val('Fyll i ort');
	
	
	function changeBoxPosition() {
		jQuery("div#checkLocation").css({
			left: boxPosition().left,
			top: boxPosition().top
		});
	}
	
	function changeBackgroundSize() {
		jQuery("div#checkLocationBackground").css({
			width: 0,
			height: 0
		}).css({
			width: jQuery(document).width(),
			height: jQuery(document).height()
		});
	}
	
	function boxPosition() {
		var x = (jQuery(window).scrollLeft() + jQuery(window).width() / 2) - (jQuery("div#checkLocation").width() / 2);
		var y = (jQuery(window).scrollTop() + jQuery(window).height() / 2) - (jQuery("div#checkLocation").height() / 2);

		return {"left":x,"top":y};
	}
	
	function closeBox() {
		jQuery("div#state1, div#state2, div#state3, div#state4").hide();
		jQuery("div#state2 p.body u").empty();
		jQuery("div#state3 p.body b, div#state4 p.body b").empty();
		jQuery("div#checkLocation").hide();
		jQuery("div#checkLocationBackground").hide();
		jQuery(window).unbind("scroll", changeBoxPosition);
		jQuery(window).unbind("resize", changeBoxPosition);
		jQuery(window).unbind("resize", changeBackgroundSize);
		jQuery(document).unbind("keyup", checkFormButton);
		jQuery(document).unbind("keydown", checkEscape);
		jQuery("div#checkLocationBackground").unbind("click", closeBox);
	}
	order = "";
	
	jQuery("a.buyFresh").click(function() {
		order = jQuery(this).attr("href");
		jQuery("a.govidare").attr("href", order);
		jQuery("input#ort").val('Fyll i ort');
		jQuery("div#state1").show();
		changeBoxPosition();
		changeBackgroundSize();
		jQuery("div#checkLocation").show();
		jQuery("div#checkLocationBackground").show();
		jQuery("input#ort").focus();
		jQuery(window).bind("scroll", changeBoxPosition);
		jQuery(window).bind("resize", changeBoxPosition);
		jQuery(window).bind("resize", changeBackgroundSize);
		jQuery(document).bind("keyup", checkFormButton);
		jQuery(document).bind("keydown", checkEscape);
		jQuery("div#checkLocationBackground").bind("click", closeBox);
		
		return false;
	});
	
	function checkEscape(e) {
		if (e.which == 27) {
			closeBox();
		}
	}
	
	jQuery("a#closeCheckLocation").click(function() {
		jQuery("a#buttonOrt").removeClass('active');
		closeBox();
		return false;
	});
	
	linkbutton = false;
	
	function checkFormButton() {
		if (jQuery("input#ort").val() != 'Fyll i ort' && jQuery("input#ort").val().length >= 2) {
			jQuery("a#buttonOrt").addClass('active');
			linkbutton = true;
		} else {
			linkbutton = false;
			jQuery("a#buttonOrt").removeClass('active');
		}
	}
	
	jQuery("a#buttonOrt").click(function() {
		findCities();
		return false;
	});
	
	function findCities() {
		if (linkbutton) {
			jQuery("div#state2 p u").empty();
			
			tmphost = window.location.host.toString();
			tmpurl = "http://" + tmphost + "/includes/misc/getcity.asp?char=" + jQuery("input#ort").val();
			
			jQuery.ajax({
				type: "GET",
				url: tmpurl,
				contentType: "application/json; charset=ISO-8859-1",
				dataType: "json",
				success: function(data) {
					if (!data.error) {
						if (data.orter[0]) {
							jQuery.each(data.orter, function() {
								jQuery("div#state2 p u").append("<a href=\"#\" ort=\""+this.ort+"\" butik=\""+this.butik+"\" id=\""+this.id+"\" ok=\""+this.ok+"\" class=\"cityLink\">" + this.ort + " (" + this.butik + ")</a><br />");
							});
							jQuery("div#state1").hide();
							jQuery("div#state2").show();
						} else {
							jQuery("div#state4 p.body b").append(jQuery("input#ort").val());
							jQuery("div#state1").hide();
							jQuery("div#state4").show();
						}
					} else {
						alert('Ett databasfel har uppståt, var god försök igen senare');
					}
				},
				error: function(x,e) {
					//alert("Url:" + this.url);
					//alert("Url:" + tmpurl);
					//window.open(this.url,"Test");
					alert("Error: " + x.status + x.statusText + " - " + e.Message);
					//alert("Error: " + e);
				}
			});
		}
	}
	
	jQuery("input#ort").keyup(function(e) {
		if (e.keyCode == 13) {
			findCities();
		}
	});
	
	jQuery("input#ort").focus(function() {
		if (jQuery(this).val() == 'Fyll i ort') {
			jQuery(this).val('');
		}
	});
	
	jQuery("input#ort").blur(function() {
		if (jQuery(this).val() == '') {
			jQuery(this).val('Fyll i ort');
		}
	});
	
	jQuery("div#checkLocation a.cityLink").live("click", function() {
		checkLocation(jQuery(this).attr("ort"),jQuery(this).attr("butik"),jQuery(this).attr("id"));
		return false;
	});

	function checkLocation(ort, butik, id) {
		jQuery("div#state3 p.body b").append(ort + " (" + butik + ")");
		jQuery("a#buttonBuy").attr("href", order + "&CityID=" + id);
		jQuery("div#state3").show();

		jQuery("div#state2").hide();
	}

	
});