function productsEvenHeigths(){
	$('.product_images').css({
		height : 'auto'
	});
	$('.product_info').css({
		height : 'auto'
	});
	if($('.product_images').height() > $('.product_info').height()){
		$('.product_info').height($('.product_images').height());
	} else {
		$('.product_images').height($('.product_info').height());
	}
}

function saveAndCheckout(url) {
    // Leere Felder prüfen
    var fields = $('div.gravur_product').children('div#gravur_hide_wrap');
    var inp = fields.children('div.gravur_product_single').children('div').children('input');

    var errors = false;

    inp.each(function() {
	if($(this).val() == '') {
	    errors = true;
	}
    });

    if(errors == true) {
	var checkit = confirm('Sie haben nicht alle Gravurfelder ausgefüllt. Sollte dies beabsichtigt sein, klicken Sie bitte auf OK um mit der Bestellung fortzufahren. Andernfalls klicken Sie bitte auf Abbrechen um die Eingabe zu korrigieren.');
	if(checkit == false) {

	} else {
	    $('#cart').append('<input type="hidden" value="1" name="checkout_kasse" />');
	    $('#cart').append('<input type="hidden" value="' + url + '" name="cont_url" />');
	    $('#cart').submit();
	}
    } else {
	$('#cart').append('<input type="hidden" value="1" name="checkout_kasse" />');
	$('#cart').append('<input type="hidden" value="' + url + '" name="cont_url" />');
	$('#cart').submit();
    }

    errors = false;
    checkit = null;
}

// Drucktexte aktualisieren
function updatePrint() {

}

function startConfigurator() {
    fenster = window.open('http://smilebox.de/configurator/?meta={"1-zeilig":[{"x":0,"y":50}],"2-zeilig":[{"x":0,"y":45},{"x":0,"y":55}],"3-zeilig":[{"x":0,"y":35},{"x":0,"y":45},{"x":0,"y":55}],"4-zeilig":[{"x":0,"y":40},{"x":0,"y":45},{"x":0,"y":50},{"x":0,"y":55}]}&image=http://smilebox.de/image/cache/schieferqu-konfig-444x314.jpg&id=353&front=3-zeilig', "Popupfenster", "width=700,height=600,resizable=yes");
    fenster.focus();
    return false;
}

function switchConfig(rel) {
    if($('.' + rel).css('display') == 'none') {
	$('.' + rel).slideDown('slow');
    } else {
	$('.' + rel).slideUp('slow', function() { $(this).css('display', 'none'); });
    }
}

function fancyBoxes(){
	$('.box').each(function(){
		if(!$(this).hasClass('pimped')){
			$(this).append('<img class="corner_ul" src="catalog/view/theme/smilebox/image/corner_ul.gif" />');
			$(this).append('<img class="corner_ur" src="catalog/view/theme/smilebox/image/corner_ur.gif" />');
			$(this).append('<img class="corner_ll" src="catalog/view/theme/smilebox/image/corner_ll.gif" />');
			$(this).append('<img class="corner_lr" src="catalog/view/theme/smilebox/image/corner_lr.gif" />');
			$(this).addClass('pimped');
		}
	});

	$('.button').each(function(){
		if(!$(this).hasClass('pimped')){
			$(this).append('<img class="button_ul active_button" src="catalog/view/theme/smilebox/image/button_ul.gif" />');
			$(this).append('<img class="button_ur active_button" src="catalog/view/theme/smilebox/image/button_ur.gif" />');
			$(this).append('<img class="button_ll active_button" src="catalog/view/theme/smilebox/image/button_ll.gif" />');
			$(this).append('<img class="button_lr active_button" src="catalog/view/theme/smilebox/image/button_lr.gif" />');
			// Grau
			$(this).append('<img class="button_ul inactive" src="catalog/view/theme/smilebox/image/button_ul_grau.gif" />');
			$(this).append('<img class="button_ur inactive" src="catalog/view/theme/smilebox/image/button_ur_grau.gif" />');
			$(this).append('<img class="button_ll inactive" src="catalog/view/theme/smilebox/image/button_ll_grau.gif" />');
			$(this).append('<img class="button_lr inactive" src="catalog/view/theme/smilebox/image/button_lr_grau.gif" />');
		
			$(this).addClass('pimped');
		}
	});
}

function calculatePrice(){
	var options = [];
	$('.product_option option:selected').each(function(){
		options.push(escape($(this).attr('rev')));
	});
	$('#calc_price').load('helpers/real_price.php', {
		base_price : escape($('#base_price').val()),
		quantity : $('#quantity').val(),
		options : options.join(';')
	});
}

function configuratorLoaded(){

}

$(function(){

	$('#saveChanges').click(function() {
	    var order_id = $(this).attr('rel');

	    var data = {
		"gravuren": {}
	    };

	    // Seiten
	    $('.prod_page').each(function() {
		var page_id = $(this).attr('rel');
		var article_id = $(this).parent().parent().attr('rel');

		if(data["gravuren"][article_id] == undefined) {
		    data["gravuren"][article_id] = {};
		    data["gravuren"][article_id][page_id] = [];
		} else {
		    data["gravuren"][article_id][page_id] = [];
		}

		// Zeilen
		$(this).children('div').each(function() {
		    data["gravuren"][article_id][page_id].push($(this).children('input').eq(0).val());
		});
	    });
	    $.ajax({
		method: 'GET',
		url: 'helpers/update_order.php',
		data: 'data=' + encodeURIComponent(JSON.stringify(data)) + '&id=' + order_id,
		success: function(html) {
		    alert('Die Änderungen wurden erfolgreich gespeichert.');
		},
		error: function() {
		    alert('Beim speichern der Änderungen ist ein Fehler aufgetreten, bitte versuchen Sie es später wieder.');
		}
	    });
	});

	$('.categories_nav > ul').addClass('level-1');
	$('.categories_nav > ul > li').addClass('level-1-li');
	$('.categories_nav > ul > li > a').addClass('level-1-a');
	$('.categories_nav > ul > li > ul').addClass('level-2');
	$('.categories_nav > ul > li > ul > li').addClass('level-2-li');
	$('.categories_nav > ul > li > ul > li > a').addClass('level-2-a');
	$('.categories_nav > ul > li > ul > li > ul').addClass('level-3');
	$('.categories_nav > ul > li > ul > li > ul > li').addClass('level-3-li');
	$('.categories_nav > ul > li > ul > li > ul > li > a').addClass('level-3-a');
	
	$('.categories_nav').show();
  
	var box = 1;
  
	$('#homepage_content > p > strong').each(function(){
		if(box < 4){
			var el = $('#box' + box).children('.box').children('.inner');
			el.append('<h3>' + $(this).html() + '</h3>');
			el.append($(this).parent('p').next('p').html());
			el.append('<p>' + $(this).parent('p').next('p').next('p').children('a').html() + '</p>');
			//console.log($(this).parent('p').next('p').next('p').children('a').attr('href'));
			el.append('<a class="button small" href="' + $(this).parent('p').next('p').next('p').children('a').attr('href') + '">mehr...</a>');
			el.append('<div class="break"></div>');
			el.append('<div class="break"></div>');
		}
		box++;
	});

	$('.categories_nav > ul > li').prepend('<img class="normal" src="catalog/view/theme/smilebox/image/menu_tab_left.gif" />');
	$('.categories_nav > ul > li').append('<img class="normal" src="catalog/view/theme/smilebox/image/menu_tab_right.gif" />');
	$('.categories_nav > ul > li').prepend('<img class="hover" style="display:none;" src="catalog/view/theme/smilebox/image/menu_tab_active_left.gif" />');
	$('.categories_nav > ul > li').append('<img class="hover" style="display:none;" src="catalog/view/theme/smilebox/image/menu_tab_active_right.gif" />');
  
	$('.categories_nav > ul > li > a').mouseenter(function(){
		$(this).parent('li').children('img.normal').hide();
		$(this).parent('li').children('img.hover').show();
		$(this).parent('li').css( {
			backgroundPosition : 'bottom'
		} );
		if($(this).parent('li').children('ul').length > 0){
			$(this).parent('li').children('ul').slideDown(150);
		}
	});
  
	$('.categories_nav > ul > li > ul > li > a').mouseenter(function(){
		if($(this).parent('li').children('ul').length > 0){
			$(this).parent('li').children('ul').fadeIn(150);
		}
	});
  
	$('.categories_nav > ul > li > ul > li').mouseleave(function(){
		if($(this).children('ul').length > 0){
			$(this).children('ul').fadeOut(150);
		}
	});
  
	$('.categories_nav > ul > li > a').mouseleave(function(){
		if(!$(this).hasClass('active')){
			$(this).parent('li').children('img.hover').hide();
			$(this).parent('li').children('img.normal').show();
			$(this).parent('li').css( {
				backgroundPosition : 'top'
			} );
		}
	});
  
	$('.categories_nav > ul > li').mouseleave(function(){
		if($(this).children('ul').length > 0){
			$(this).children('ul').slideUp(150);
		}
	});
  
	$('.categories_nav > ul > li > a.active').each(function(){
		$(this).parent('li').children('img.normal').hide();
		$(this).parent('li').children('img.hover').show();
		$(this).parent('li').css( {
			backgroundPosition : 'bottom'
		} );
	});
  
	fancyBoxes();
  
	$('#header .categories_nav > ul > li > ul').append('<img class="corner_ll" src="catalog/view/theme/smilebox/image/corner_ll.gif" />');
	$('#header .categories_nav > ul > li > ul').append('<img class="corner_lr" src="catalog/view/theme/smilebox/image/corner_lr.gif" />');
  
	$('.product_images .inner a:first').show();
  
	$('.product_images_nav .left').click(function(e){
		e.preventDefault();
		var el = $('.product_images .inner a:visible');
		el.fadeOut(150);
		if(el.prev('a').length > 0){
			el.prev('a').fadeIn(150);
		} else {
			$('.product_images .inner a:last').fadeIn(150);
		}
		productsEvenHeigths();
	});
  
	$('.product_images_nav .right').click(function(e){
		e.preventDefault();
		var el = $('.product_images .inner a:visible');
		el.fadeOut(150);
		if(el.prev('a').length > 0){
			el.prev('a').fadeIn(150);
		} else {
			$('.product_images .inner a:first').fadeIn(150);
		}
		productsEvenHeigths();
	});
  
	productsEvenHeigths();
  
	$('#product > ul > li > select').change(function(){
		calculatePrice();
		var r = new RegExp($(this).children('option:selected').attr('rel'), "ig");
		$('.product_images > .inner > a > img').each(function(){
			if($(this).attr('src').search(r) != -1){
				$('.product_images > .inner > a').hide();
				$(this).parent('a').show();
			}
		});
	});
  
	var ship_quantity = [1, 10, 50, 100];
	var ship_time = [7, 14, 21, 28];
  
	$('#quantity').keyup(function(){
		calculatePrice();
		days = ship_time[0];
		for(var i in ship_quantity){
			if($(this).val() > ship_quantity[i]){
				days = ship_time[i];
			}
		}
		$('#ship_days').html('<div class="info">' + days + ' Tage Anfertigungszeit' + '<br><small>ab Zahlungseingang</small></div>');
		$('#ship_days').show();
		
	});
  
	/*
	$('#product > ul > li.gravur > select').change(function(e){
		if($('#product > ul > li.gravur').length > 0
			&& $('#product > ul > li.gravur > select').children('option:selected').attr('rel') != 'ohne gravur'
			&& $('#product > ul > li.gravur > select').children('option:selected').attr('rel') != 'keine'){
			$('#add_to_cart').hide();
			$('#configure').show();
			$('#liefer').show();
		} else {
			$('#add_to_cart').show();
			$('#configure').hide();
			$('#liefer').hide();
		}
		productsEvenHeigths();
	});
	*/
  
	$('#product > ul > li.druckkosten > select').change(function(e){
		if($('#product > ul > li.druckkosten').length > 0
			&& $('#product > ul > li.druckkosten > select').children('option:selected').attr('rel') != 'ohne druck'){
			$('#free_text').show();
		} else {
			$('#free_text').hide();
		}
		productsEvenHeigths();
	});
  /*
	if($('#product > ul > li.gravur').length > 0
		&& $('#product > ul > li.gravur > select').children('option:selected').attr('rel') != 'ohne gravur'
		&& $('#product > ul > li.gravur > select').children('option:selected').attr('rel') != 'keine'){
		$('#add_to_cart').hide();
		$('#configure').show();
		$('#liefer').show();
		productsEvenHeigths();
	}
  */
	if($('#product > ul > li.druckkosten').length > 0
		&& $('#product > ul > li.druckkosten > select').children('option:selected').attr('rel') != 'ohne druck'){
		$('#free_text').show();
		productsEvenHeigths();
	}

	/*if($('#product > ul > li.helium').length > 0 && $('#product > ul > li.helium > select.product_option').children('option:selected').attr('rel') != 'nicht gefüllt'){
		$('#free_text').show();
		productsEvenHeigths();
	}*/

	$('#product > ul > li.helium > select.product_option').change(function() {
	    if($(this).attr('rel') != 'nicht gefüllt') {
		$('#date_pick').show();
		productsEvenHeigths();
	    } else {
		$('#date_pick').hide();
	    }
	});
  /*
	$('#configure').click(function(e){
		e.preventDefault();
		var opts = [];
		$('#product > ul > li > select').each(function(){
			opts.push([ $(this).attr('name'), $(this).val() ]);
		});
		$('#content').prepend('<div id="configurator"></div>');
		var img = '';
		var color = 'bright';
	 
		$('.product_images .inner a img').each(function(){
			if($(this).attr('src').search(/konfig.+/) != -1){
				img = $(this).attr('src');
				if($(this).attr('src').search(/dark.+/) != -1){
					color = 'dark';
				}
			}
		});
		*/
		/*
	 if($('.product_images .inner a').length > 1){
		img = $('.product_images .inner a:first img').attr('src');
	 }
	 */
		/*
		var front = $('#product > ul > li.gravur > select > option:selected').attr('rel');
	 
	 
		if($('#product > ul > li.vorderseite').length > 0
			&& $('#product > ul > li.vorderseite > select').children('option:selected').attr('rel') != 'ohne gravur'
			&& $('#product > ul > li.vorderseite > select').children('option:selected').attr('rel') != 'keine'){
			front = $('#product > ul > li.vorderseite > select > option:selected').attr('rel');
		}
	 
		var back = '';
	 
	 
		if($('#product > ul > li.rückseite').length > 0
			&& $('#product > ul > li.rückseite > select').children('option:selected').attr('rel') != 'ohne gravur'
			&& $('#product > ul > li.rückseite > select').children('option:selected').attr('rel') != 'keine'){
			back = $('#product > ul > li.rückseite > select > option:selected').attr('rel');
		}
	 
		$('#configurator').load('configurator/index.php', {
			image : img,
			front : front,
			color : color,
			back : back,
			name : $('#product_title').html(),
			quantity : $('#quantity').val(),
			meta : $('#meta').val(),
			opts : $.toJSON(opts),
			id : $('#product_id').val()
		}, configuratorLoaded);
	});
	

	$('.pauschale > .button').live('click', function (e) {
		e.preventDefault();
		var me = $(this).parent('.pauschale');
		$.ajax({
			type: 'post',
			url: 'index.php?route=module/cart/callback',
			dataType: 'html',
			data: me.children(':input'),
			success: function (html) {
				$('#module_cart .inner').html('<h3>Warenkorb</h3><hr>' + html);
				me.append('<div style="clear:both;"><small>Das Produkt wurde in Ihren Warenkorb gelegt.</small></div>');
			},
			complete: function () {
			}
		});
	});
	*/
	
	/*
	$('#add_to_cart, #configure').live('click', function (e) {
		e.preventDefault();
		$.ajax({
			type: 'post',
			url: 'index.php?route=module/cart/callback',
			dataType: 'html',
			data: $('#product :input'),
			success: function (html) {
				$('#module_cart .inner').html('<h3>Warenkorb</h3><hr>' + html);
			},	
			complete: function () {
			}			
		});			
	});
	
	$('#add_to_cart').live('click', function (e) {
		if($('#free_text').length > 0 && $('#free_text > p > textarea').val()){
			$.post("configurator/save_freetext.php", {product : $('#product_title').html(),txt : $('#free_text > p > textarea').val()});
		}
		var oldHtml = $(this);
		$(this).after('<a id="add_to_cart_ok" class="button"><small>Das Produkt wurde in Ihren Warenkorb gelegt.</small></div>');
		$(this).remove();
		window.setTimeout(function(){
			$('#add_to_cart_ok').after(oldHtml);
			$('#add_to_cart_ok').remove();
		}, 3000);
		fancyBoxes();
	});
	*/
	
	$('a#add_to_cart').click(function(e){
		e.preventDefault();
		$("form#product").submit();
	});
	
	
	var catC = 0;
	$('.categories_nav > ul > li').each(function(){
		var me = $(this);
		var el = new jQuery('<div class="col20 cat" id="cat'+ catC +'"></div>');
		el.append('<strong style="font-size:12px;color:#ff9000;">' + me.children('li a').html() + '</strong>');
		el.append('<ul></ul>');
		el.children('ul').append(me.children('li ul').html());
		$('#footer_categories > .inner').append(el);
		//$('#cat'+catC+' > ul > li > a:first').css('font-weight', 'bold');
		catC++;
	});
  
	$('div.inner > div:last').css('border', 'none');
  
	$('#footernav > .inner > div:last').css('border', 'none');
	$('#footernav > .inner > div:last').css('margin', '0px');
	$('#footernav > .inner > div:last').css('padding', '0px');
  
	$('#footer_categories > .inner').append('<div class="break"></div>');
	
	$('a').each(function(){
		if(!$(this).attr('href')){
			$(this).attr('href', 'javascript:;');
		}
	});
  
  
	// Normale Buttons
	$('.button:not(a#configure, a#add_to_cart, .arrow)').mouseenter(function() {
		$(this).css('background', '#5d5d5d none repeat scroll 0 0');
		var children = $(this).children();
		if(children.length > 0) {
			$(children).each(function() {
				if($(this).hasClass('active_button')) {
					$(this).removeClass('active_button');
					$(this).addClass('inactive');
				} else if($(this).hasClass('inactive')) {
					$(this).removeClass('inactive');
					$(this).addClass('active_button');
				}
			});
		}
	});
  
	$('.button:not(a#configure, a#add_to_cart, .arrow)').mouseleave(function() {
		$(this).css('background', '#FF9000 none repeat scroll 0 0');
		var children = $(this).children();
		$(children).each(function() {
			if($(this).hasClass('inactive')) {
				$(this).removeClass('inactive');
				$(this).addClass('active_button');
			} else if($(this).hasClass('active_button')) {
				$(this).removeClass('active_button');
				$(this).addClass('inactive');
			}
		});
	});
  
	// Detail-Buttons
	$('.arrow').mouseenter(function() {
		$(this).css('background', '#5d5d5d url(catalog/view/theme/smilebox/image/button_arrow_grau.gif) no-repeat scroll 90% 52%');
		var children = $(this).children();
		if(children.length > 0) {
			$(children).each(function() {
				if($(this).hasClass('active_button')) {
					$(this).removeClass('active_button');
					$(this).addClass('inactive');
				} else if($(this).hasClass('inactive')) {
					$(this).removeClass('inactive');
					$(this).addClass('active_button');
				}
			});
		}
	});
  
	$('.arrow').mouseleave(function() {
		$(this).css('background', '#FF9000 url(catalog/view/theme/smilebox/image/button_arrow.gif) no-repeat scroll 90% 52%');
		var children = $(this).children();
		$(children).each(function() {
			if($(this).hasClass('inactive')) {
				$(this).removeClass('inactive');
				$(this).addClass('active_button');
			} else if($(this).hasClass('active_button')) {
				$(this).removeClass('active_button');
				$(this).addClass('inactive');
			}
		});
	});
  
	$('#add_to_cart, #configure').mouseenter(function() {
		$(this).css('background', '#5d5d5d url(catalog/view/theme/smilebox/image/button_buy_grey.gif) no-repeat scroll 5% 50%');
		var children = $(this).children();
		if(children.length > 0) {
			$(children).each(function() {
				if($(this).hasClass('active_button')) {
					$(this).removeClass('active_button');
					$(this).addClass('inactive');
				} else if($(this).hasClass('inactive')) {
					$(this).removeClass('inactive');
					$(this).addClass('active_button');
				}
			});
		}
	});
  
	$('#add_to_cart, #configure').mouseleave(function() {
		$(this).css('background', '#ff9000 url(catalog/view/theme/smilebox/image/button_buy.gif) no-repeat scroll 5% 50%');
		var children = $(this).children();
		if(children.length > 0) {
			$(children).each(function() {
				if($(this).hasClass('active_button')) {
					$(this).removeClass('active_button');
					$(this).addClass('inactive');
				} else if($(this).hasClass('inactive')) {
					$(this).removeClass('inactive');
					$(this).addClass('active_button');
				}
			});
		}
	});
/*
	$('ul.level-2').each(function() {
	    $(this).children('li:first').before('<li class="level-2-li"><a href="http://smilebox.de/index.php?route=information/information&information_id=8">Hilfe</a></li>');
	 });*/
});
