// Cookie Variablen
var options = { path: '/', expires: 2 };

function displayShoppingBasket(){
	var articles = $.cookie('articles');
	var tmp = new Array();
	if(articles&&articles.length>0)
		tmp = articles.split(';');
	if(tmp[0] && tmp[0].length>1)
		$('.Warenkorb').html('<a href="index.php?id=10">Warenkorb: '+tmp.length+' Artikel</a>');
	else
		$('.Warenkorb').html('&nbsp;');
	var html = '';
	var price = 0;
	var parts = null;
	for(var i=0;i<tmp.length;i++){
	   parts = tmp[i].split('|');
	   if(parts[0].length>1){
		   price += Number(parts[1]);
		   html +=  '<div id="item_'+i+'">'+parts[0]+' '+parts[1]+' € <a href="#" class="remove_item" reL="item_'+i+'" item="'+parts[0]+'"><img src="/fileadmin/template/img/delete.gif" border="0" /></a></div>';
	   }
	}
	if(html.length<2)
		html = 'Keine Artikel ausgewählt.';
	$('#shopping_basket').html(html);
	if(tmp && $.isArray(tmp))
		articles = tmp.join("\n");
	$('#mailformbestellung').html(articles);
	$('#mailformgesamtpreis').attr('readonly','true');
	$('#mailformgesamtpreis').attr('value',price.toFixed(2)+' €');
	var vat = ((price/107)*7).toFixed(2);
	$('#mailformmehrwertsteuer').attr('readonly','true');
	$('#mailformmehrwertsteuer').attr('value',vat+' €');
	$('.remove_item').unbind();
	$('.remove_item').bind('click',function(e){		
		var articles = $.cookie('articles');
		var tmp = articles.split(';');
		var parts = null;
		var articlesNew = '';
		var alreadyRemoved = false;
		for(var i=0;i<tmp.length;i++){
	  		parts = tmp[i].split('|');
	  		if(!alreadyRemoved){
	  			if($(this).attr('item')!=parts[0]){	
		  			if(articlesNew.length>0)
		  			  articlesNew += ';'
	  				articlesNew += parts[0]+'|'+parts[1];
	  			}else
	  				alreadyRemoved = true;
	  		}else{
	  			if(articlesNew.length>0)
		  		  articlesNew += ';'
	  			articlesNew += parts[0]+'|'+parts[1];
	  		}
	   	}
	   	$.cookie('articles',articlesNew,options);
	   	displayShoppingBasket();
	   	return false;
	});
}

$(document).ready(function(){
	$("#mailformbestellung").hide();
	$("#confirm_dialog").dialog({autoOpen: false, buttons: {"Ok": function() {$(this).dialog("close");}}});
	$("#confirm_dialog").dialog('option', 'title', 'Artikel');
	$("#confirm_dialog").dialog('option', 'width', 460);
	$("#confirm_dialog").dialog('option', 'modal', true);

	$('.order_link').bind('click', function(e){
		var price = $(e.target).parent().find('b').html().substring(2);
		var article = $(e.target).attr('title')+'|'+price;
		var articles = '';
//		if(!$.cookie('articles') || $.cookie('articles').search(article)==-1){
			if($.cookie('articles')&&$.cookie('articles').length>0){
				articles = $.cookie('articles');
				articles = articles+';';
			}
			articles = articles+article;
			var tmp = articles.split(';');
			$.cookie('articles',articles,options);
		
			$('.Warenkorb').html('<a href="index.php?id=10">Warenkorb: '+tmp.length+' Artikel</a>');
//		}
		$("#confirm_dialog").dialog('option', 'buttons', {"Schließen": function() {$(this).dialog("close");return false;} , "Zum Warenkorb": function(){window.location='index.php?id=10';return false;}});
	 	$("#confirm_dialog").html("Der Artikel wurde zum Warenkorb hinzugefügt.");
		$("#confirm_dialog").dialog('open');
		return false;	
	});

	if($.cookie('articles')){
		displayShoppingBasket();		
	}else
		$('#shopping_basket').html('Keine Artikel ausgewählt.');
	
	$('.ad-thumb-list li>a img').each(function(){
		if($(this).parent().next().find('.gallery_desc').html())
			$(this).attr('longdesc',$(this).parent().next().find('.gallery_desc').html());
	});
	$('.remove').remove();

var galleries = $('.ad-gallery').adGallery({
  loader_image: '../img/loader.gif',
  width: 600, // Width of the image, set to false and it will read the CSS width
  height: 400, // Height of the image, set to false and it will read the CSS height
  thumb_opacity: 0.7, // Opacity that the thumbs fades to/from, (1 removes fade effect)
                      // Note that this effect combined with other effects might be resource intensive
                      // and make animations lag
  start_at_index: 0, // Which image should be displayed at first? 0 is the first image
  animate_first_image: false, // Should first image just be displayed, or animated in?
  animation_speed: 400, // Which ever effect is used to switch images, how long should it take?
  display_next_and_prev: true, // Can you navigate by clicking on the left/right on the image?
  display_back_and_forward: true, // Are you allowed to scroll the thumb list?
  scroll_jump: 0, // If 0, it jumps the width of the container
  slideshow: {
    enable: true,
    autostart: false,
    speed: 5000,
    start_label: 'Start',
    stop_label: 'Stop',
    stop_on_scroll: true, // Should the slideshow stop if the user scrolls the thumb list?
    countdown_prefix: '(', // Wrap around the countdown
    countdown_sufix: ')',
  },
  effect: 'slide-hori', // slide-hori or 'slide-vert', 'resize', 'fade', 'none' or false
  enable_keyboard_move: true, // Move to next/previous image with keyboard arrows?
  cycle: false, // If set to false, you can't go from the last image to the first, and vice versa
  // All callbacks has the AdGallery objects as 'this' reference
  callbacks: {
    // Executes right after the internal init, can be used to choose which images
    // you want to preload
    init: function() {
      // preloadAll uses recursion to preload each image right after one another
      this.preloadAll();
    }
  }
});

});