$(document).ready(function(){

//close all selects
$(document).click(function(){
   $('.select .box .list').slideUp('fast');
});

//select click
$('.select .arr, .select .box').livequery('click', function(){
   var $thisSelect = $(this).parent().children('.box').children('.list');
      $('.select .box .list').slideUp('fast');
      if ($thisSelect.css('display') == 'block') $thisSelect.slideUp();
      else $thisSelect.slideDown();
      return false;
   });

//select check
$('.select .box .list a').livequery('click', function(){
   var $val = $(this).attr('value');
      $(this).parents('.select').children('input').val($val);
      $(this).parents('.box').children('a.selected').text($(this).text());
      $(this).parent().slideToggle();
      return false;
});


//radio check
$('.radio a').click(function(){
   var $val = $(this).attr('value');
      $(this).parents('.radio').children('input').val($val);
      $(this).parents('.radio').children('a').removeClass('sel');
      $(this).addClass('sel');
      return false;
   });


//adv click open select
$('a.open_types_sub').click(function(){
      $('#calc_types_sub .select .box .list').slideToggle();
      return false;
   });
   
   
//radio type select
$('.radio#calc_types_r a').click(function(){
   var $type = $('input#form_type').val();
   
   if ($type == 'Design' || $type == 'Druck') {

		$.ajax({
	   	url: '/index.php?ajax=calc',
   		data: 'act=show_products&type='+$type,
   		beforeSend: function(){
//      		$(".tree").show();
   		},
   		complete: function(){
//      		$(".tree").hide();
				$('#calc_types_sub').slideDown();
   		},
   		success: function ($jqueryOutput) {
            $('.select#form_product .box .list').replaceWith($jqueryOutput);
            $('.select#form_product .box .list').slideDown();
	   	}
		});
	}
	else {
	   $('#personal_order .in').show();
	   $('#personal_order .thanks').hide();
	   $('#personal_order .tree').hide();
		formClear('#personal_order form');

		$('#calc_types_sub').slideUp();
		$('#calc_load_bl .anim').animate({marginLeft: "-1304px"}, 700);
	}
	
      return false;
});


//product select
$('.select#form_product .box .list a').livequery('click', function(){
   var $val = $(this).parents('.select').children('input').val();

   $('#calc_load_bl .anim').animate({marginLeft: "-652px"}, 700, function(){

		$('#calc_postprocess_text').fadeIn();
		
		$('#calc_features_ajax').slideUp('fast', function () {

		$.ajax({
	   	url: '/index.php?ajax=calc',
   		data: 'act=show_features&product_id='+$val,
   		beforeSend: function(){
				$('#calc_features_selects .tree').show();
   		},
   		complete: function(){
      		$('#calc_features_selects .tree').hide();
   		},
   		success: function ($jqueryOutput) {//alert($jqueryOutput);
            $('#calc_features_ajax').html($jqueryOutput);
				$('#calc_features_ajax').slideDown();
	   	}
		});
		});

	});

   $(".calc_r .in_bl#simple").animate({right: '-253px'}, 350,
		function(){
		   $(".calc_r .in_bl#cart").animate({right: '-5px'}, 350);
		}
	);
   

      return false;
});


//feature select
$('#calc_features_selects .select .box .list a:not(.selected)').livequery('click', function(){
   var $productId = $('input[name=form_product]').val(),
		 $val = $(this).parents('.select').children('input').val(),
		 $selectedLink = $(this).parents('.box').children('a.selected'),
		 $featureName = $selectedLink.attr('default');

		if ($val != 0) $selectedLink.prepend($featureName + ': ');
		else $selectedLink.html($featureName);

		//clear styles
		$('#count_button .price').hide();
		$('#count_button .link').fadeIn();
		$('#tocart_button').removeClass('sel');


		if($('#calc_features_selects .select .box a.req').parents('.select').children('input[value=0]').length == 0) calcPrice();


		$currentVals = $('#calc_features_selects .select input[value!=0]');

		$('#calc_features_selects .select .box .list a').show();

		if ($currentVals.length != 0) {
			$.getJSON('/index.php?ajax=calc&act=features_reload&product_id='+ $productId +'&'+ $currentVals.serialize(),
				function(data){
	            $.each(data.exceptions[0], function(i,item){//alert(item);
	               if (item != $val) $('#calc_features_selects .select .box .list a[value=' + item + ']').hide();
	            });
			});
		}

      return false;
});


//Count button click
$('a#count_button').click(function(){
   calcPrice();
   return false;
});


//Add_to_cart button click
$('a#tocart_button').click(function(){

   var $productId = $('input[name=form_product]').val(),
	$featureIds = '',
	$price = $('#count_button .price .num').text();

	$('input[name="feature[]"]').each(function(i){
		$featureIds += this.value;
		if (i != $('input[name="feature[]"]').length - 1) $featureIds += '-';
	});

   if ($(this).hasClass('sel')) {
		$('.cart_count').text(parseInt($('.cart_count#cart_count_main').text()) + 1);
		$('#cart_view_button .cart_count').css('color', '#ffffff');
		$('#cart_view_button .cart_count').animate({marginLeft: '-35px', fontSize: '50px', lineHeight: '50px'}, 200,
			function(){
				$('#cart_view_button .cart_count').animate({marginLeft: '-15px', fontSize: '12px', lineHeight: '17px'}, 150,
					function(){$('#cart_view_button .cart_count').css('color', '#4d4d4d');});
			}
		);
   	$('#tocart_button').removeClass('sel');

		$.ajax({
	   	url: '/index.php?ajax=cart',
   		data: 'act=add_to_cart&product_id=' + $productId + '&feature_ids=' + $featureIds + '&price=' + $price,
   		beforeSend: function(){
//				$('#calc_features_selects .tree').show();
   		},
   		complete: function(){
//      		$('#calc_features_selects .tree').hide();
   		},
   		success: function ($jqueryOutput) {

	   	}
		});

	}

   return false;
});


//Edit_cart button click
$('a#editcart_button').click(function(){

   var $productId = $('input[name=form_product]').val(),
	$featureIds = '',
	$price = $('#count_button .price .num').text(),
	$item_id = $(this).attr('item_id');

	$('input[name="feature[]"]').each(function(i){
		$featureIds += this.value;
		if (i != $('input[name="feature[]"]').length - 1) $featureIds += '-';
	});

   if ($(this).hasClass('sel')) {
   	$('#editcart_button').hide();

		$.ajax({
	   	url: '/index.php?ajax=cart',
   		data: 'act=edit_cart&item_id=' + $item_id + '&product_id=' + $productId + '&feature_ids=' + $featureIds + '&price=' + $price,
   		beforeSend: function(){
//				$('#calc_features_selects .tree').show();
   		},
   		complete: function(){
//      		$('#calc_features_selects .tree').hide();
   		},
   		success: function ($jqueryOutput) {
            $('a#cart_view_button').click();
	   	}
		});

	}

   return false;
});


$('a#cart_view_button').click(function(){

   $(".calc_r .in_bl#cart").animate({right: '-253px'}, 350,
		function(){
		   $(".calc_r .in_bl#cart_view").animate({right: '-5px'}, 350);
		}
	);

   $("#calc_bl").animate({marginLeft: '-700px'}, 350,
		function(){
		  $("#cart_bl").animate({marginLeft: '0px'}, 350);
		}
	);
   loadCart();

   return false;
});


$('a#back_tocalc_button').click(function(){

   $(".calc_r .in_bl#cart_view").animate({right: '-253px'}, 350,
		function(){
		   $(".calc_r .in_bl#cart").animate({right: '-5px'}, 350);
		}
	);

   $("#cart_bl").animate({marginLeft: '-700px'}, 350,
		function(){
		  $("#calc_bl").animate({marginLeft: '0px'}, 350);
		}
	);

   return false;
});


//MAIN!
});


function calcPrice () {

	if($('#calc_features_selects .select .box a.req').parents('.select').children('input[value=0]').length != 0) {
		alert('Bitte alle *Pflichtfelder ausfullen');
		return false;
	}

	var $calcStr = $('#calc_features_selects .select input').serialize();

		$.ajax({
	   	url: '/index.php?ajax=calc',
   		data: 'act=calc_price&'+$calcStr,
   		beforeSend: function(){
//				$('#calc_features_selects .tree').show();
   		},
   		complete: function(){
//      		$('#calc_features_selects .tree').hide();
   		},
   		success: function ($jqueryOutput) {//alert($jqueryOutput);
            $('#count_button .link').hide();
            $('#count_button .price').fadeIn().html($jqueryOutput);
            if ($jqueryOutput.indexOf('Nein') == -1) {
					/*$('#tocart_button').css('display','hidden');
					$('#tocart_button').animate({right: '-0px'}, 50, function(){$('#tocart_button').fadeIn();});*/
					$('#tocart_button').fadeOut(50);$('#tocart_button').fadeIn(100);
					$('#tocart_button').addClass('sel');}
	   	}
		});


}

