//var baseUrl = "http://192.168.0.100/fake_shop/";
var baseUrl = "http://www.fakeblondie.com/";

FlipBook = Class.create();
FlipBook.prototype = {
    initialize: function(fb_images) {
        this.fb_images = fb_images;
		this.currentImage = 0;
    },
    prevImage: function(){
		if(this.currentImage > 0)
			this.currentImage--;

		this.showImage();
	},
	nextImage: function() {
		if(this.currentImage < (this.fb_images.length - 1))
			this.currentImage++;

		this.showImage();
	},
	showImage: function() {
		$("currentImageNumber").innerHTML = this.currentImage + 1;
		$("inner_product_image").style.background = "url(" + this.fb_images[this.currentImage] + ")";
	}
}

Event.observe(window, 'load', initMouseOvers);

function initMouseOvers() {
	['viewcart', 'go', 'downlad', 'add_to_cart', 'paynow', 'update'].each(function(s) {
		if($(s))
			initMouseOver(s);
	});
}

function initMouseOver(imageId) {
	var normalImage = $(imageId).src;
	var overImage = normalImage.split(".");
	overImage.pop();
	overImage = overImage.join(".") + "_over.gif";
	Event.observe(imageId, 'mouseover', function(e) {this.src = overImage});
	Event.observe(imageId, 'mouseout', function(e) {this.src = normalImage});	
}

function productSearch() {
	if($F("searchinput").length > 0) {
		window.location = baseUrl + "products/" + escape($F("searchinput"));
	}
	return false;
}

function addToCart(productId) {
	new Ajax.Request(baseUrl + 'cart/addToCart/' + productId, {
		requestHeaders: {Accept: 'application/json'},
		onSuccess: function(transport) {
			var json = transport.responseText.evalJSON();
			$("numitems").innerHTML = json.items > 0 ? json.items + " items": "0 items";
			$("amount").innerHTML = "€" + json.amount_formated;
			if(json.itemsOfTheCurrent > 0 && $("quantity")) {
				var quantity = parseInt($("quantity").innerHTML.split(" ")[0]);
				// If more items have been added than in quantity, dim the button
				if(json.itemsOfTheCurrent >= quantity) {
					var dimmedButton = document.createElement("img");
					dimmedButton.src = baseUrl + "img/sold_out_btn.gif";
					$("addToCart").parentNode.replaceChild(dimmedButton, $("addToCart"))
				}
			}
		}
	});
}

function updateCart() {
	new Ajax.Request(baseUrl + 'cart/updateCart/', {
		requestHeaders: {Accept: 'application/json'},
		postBody: $("cart_form").serialize(),
		onSuccess: function(transport) {
			var json = transport.responseText.evalJSON();
			$("numitems").innerHTML = json.items > 0 ? json.items + " items": "0 items";
			$("amount").innerHTML = "€" + json.amount_formated;
			$("total").innerHTML = "Total: €" + json.amount;
			$("total_no_shipping").value = json.amount;
			recalculateSubTotals();
			updateWithShipping();
		}
	});
}

function removeItem(id) {
	new Ajax.Request(baseUrl + 'cart/removeItem/' + id, {
		requestHeaders: {Accept: 'application/json'},
		postBody: $("cart_form").serialize(),
		onSuccess: function(transport) {
			var json = transport.responseText.evalJSON();
			$("numitems").innerHTML = json.items > 0 ? json.items + " items": "0 items";
			$("amount").innerHTML = "€" + json.amount_formated;
			$("total").innerHTML = "Total: €" + json.amount;
			$("total_no_shipping").value = json.amount;

			$("row_" + id).remove();
			updateWithShipping();
		}
	});
}

function recalculateSubTotals() {
	var rows = $("cart_table").getElementsByTagName('tr');
	
	for(var i = 1; i < rows.length; ++i) {
		var price = rows[i].getElementsByClassName("price")[0].innerHTML.substr(1);
		var subtotal = rows[i].getElementsByClassName("subtotal")[0];
		var quantity = rows[i].getElementsByClassName("quantity")[0].value;

		subtotal.innerHTML = "€" + (quantity * price);
	}
}

function updateWithShipping() {
	switch($F("shipping")) {
		case "0":
			var shipping = 0;
			break;
		case "1":
			var region = "Germany";
			var shipping = 0.75;
			break;
		case "2":
			var region = "Europe";
			var shipping = 1;
			break;
		case "3":
			var region = "outside of Europe";
			var shipping = 2.5;
	}

	$("total_with_shipping").value = eval($F("total_no_shipping")) + shipping;
	$("itemsum").innerHTML = "Items: €" + $F("total_no_shipping");
	$("totalsum").innerHTML = "Total: €" + $F("total_with_shipping");

	if($F("shipping") > 0)
		$("shipsum").innerHTML = "Regular shipping to " + region + ": €" + shipping;
	else
		$("shipsum").innerHTML = "Please select shipping to calculate total cost";
}

function checkout() {
	if($F("shipping") > 0)
		window.location = baseUrl + 'cart/checkOut/' + $F("shipping");
	else {
		alert("Please select shipping region");
		$("shipping").activate();
	}

	/*
	new Ajax.Request(baseUrl + 'cart/checkOut/' + $F("shipping"), {
		requestHeaders: {Accept: 'application/json'},
		onSuccess: function(transport) {
			var json = transport.responseText.evalJSON();
			window.location = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=" . json.token;
		}
	});
	*/
}

function placeOrder() {
	if($F("address").length > 1) {
		$("mainform").submit();
	} else {
		alert("An address is required to place an order!")
		$("address").activate();
	}
}