/*!
 * Mohltied jQuery JavaScripts
 *
 * Copyright 2010, mey-R-delius
 * Dual licensed under GPL Version 2 licenses.
 *
 * Date: Mon Sep 13 2010
 */

$(document).ready(function() {

	/* register all category sliders */
	$(".cat-head").each(function(i, el) {
		$(el).click(function() {
			var cat_id = extractId(el.id);
			if($(el).hasClass("open")){
				closeCat(cat_id);
			}else{
				openCat(cat_id);
				closeCatsExcept(cat_id);
			}
		});
	});

	/* register all manufacturer sliders */
	$(".manu-head").each(function(i, el) {
		$(el).click(function() {
			var prod_id = extractId(el.id);
			toggleManuInfo(prod_id);
			var manu_head = $("#manu-head_"+prod_id);
			if($(el).hasClass("open")){
				manu_head.removeClass("open");
				manu_head.addClass("closed");
			}else{
				manu_head.removeClass("closed");
				manu_head.addClass("open");
			}
		});
	});
	
	/* register all select inputs */
	$("select.size").each(function(i, el) {
		$(el).change(function() {
			recalcPrice(el);
		});
	});

	/* register all to cart buttons */
	$("input.cart-add-button").each(function(i, el) {
		$(el).click(function(event) {
			event.preventDefault();
			addToCart(el);
		});
	});
	
	/* register images for lightbox */
	$("a[rel*='lightbox']").lightBox({
		imageLoading: '../images/lightbox-ico-loading.gif',
		imageBtnClose: '../images/closelabel.gif',
		imageBtnPrev: '../images/prevlabel.gif',
		imageBtnNext: '../images/nextlabel.gif',
		imageBlank: '../images/lightbox-blank.gif',
		overlayOpacity: '0.7',
		txtImage: 'Bild',
		txtOf: 'von'
	});
});

function extractId (stringId) {
	return stringId.split("_")[1];
}

function toggleManuInfo(prod_id) {
	$("#manu-body_"+prod_id).slideToggle("fast");
}
	
function openCat(cat_id){
	$("#cat-body_"+cat_id).slideDown("slow");
	var cat_head = $("#cat-head_"+cat_id);
	cat_head.removeClass("closed");
	cat_head.addClass("open");
}

function closeCat(cat_id){
	$("#cat-body_"+cat_id).slideUp("slow");
	var cat_head = $("#cat-head_"+cat_id);
	cat_head.removeClass("open");
	cat_head.addClass("closed");
}

function closeCatsExcept(cat_id){
	$(".cat-head").each(function(i,el) {
		tmp_id = extractId(el.id);
		if(tmp_id != cat_id){
			closeCat(tmp_id);
		}
	});
}

function changeImages(){
	side = !side;
	
	if (side) {
		fadeOutEl = $("#diashow-img-right");
		fadeInEl = $("#diashow-img-left");
	} else {
		fadeOutEl = $("#diashow-img-left");
		fadeInEl = $("#diashow-img-right");
	}
	
	// rausfaden mit callback
	fadeOutEl.fadeTo( fadeDelay, 0, function() {
		nextImg(fadeOutEl);
	});

	// simples reinfaden
	fadeInEl.fadeTo( fadeDelay, 1 );
	
}

function nextImg(el){
	idx++;
	if(idx >= images.length){
		idx = 0;
	}
	el.attr("src", '../media/dia_show/'+images[idx]);
}

function recalcPrice ( el ) {
	var prodId = extractId(el.id);
	var sizeVal = $('#size_'+prodId).val();
	$('#price_'+prodId).load('../includes/price.php', { prod: prodId, size: sizeVal });
}

function addToCart( el ) {
	var prodId = extractId(el.id);
	var sizeId = $('#size_'+prodId).val();
	var optId = $('#opt_'+prodId).val();
	var amount = $('#amount_'+prodId).val();
	jQuery.ajax({url: '../shop/modify_cart.php', data: { cmd: "add", prod: prodId, size: sizeId, opt: optId, amount: amount }, success: function(){
		refreshCart();
	}});
}

function removeFromCart( el ) {
	var cartItemId = extractId(el.id);
	jQuery.ajax({url: '../shop/modify_cart.php', data: { cmd: "del", item: cartItemId }, success: function(){
		refreshCart();
	}});
}

function removeFromFullCart( el ) {
	var cartItemId = extractId(el.id);
	jQuery.ajax({url: '../shop/modify_cart.php', data: { cmd: "del", item: cartItemId }, success: function(){
		refreshFullCart();
	}});
}

function refreshCart() {
	$('#cart').load('../includes/cart.php');
}

function refreshFullCart() {
	$('#cart').load('../includes/cart_full.php');
}

function goToOrder() {
	location = '../shop/order.php';
}


