
$.extend({
    isset: function(_var) {
        return (_var !== undefined);
    }
});




$(function($) {
	$.merkzettel = {
		list:{},
		
		findById:function(id) {
			/*
			for(e in this.list) {
				if(this.list[e].id == id) {
					this.list[e].index = e;
					return this.list[e];
				}
			}
			*/
			if($.isset(this.list['e'+id])) {
				return this.list['e'+id];
			}
			
			return false;
		},
		
		addRemoveItem:function(id, url, img, title, description, sku, price) {
			if(!this.findById(id)) {
				this.addItem(id, url, img, title, description, sku, price);
			} else {
				this.removeItem(id);
				$("#addItem"+id).fadeIn('fast');
			}
		},
		
		updateItem:function(obj) {
			if(o = this.findById(obj.id)) {
				for(e in obj) { o[e] = obj[e]; }
				this.list['e'+obj.id] = o;
			}
		},
		
		addItem:function(id, url, img, title, description, sku, price) {
			if($.isset(id) || $.isset(url) || $.isset(img) || $.isset(title) || $.isset(description) || $.isset(sku) || $.isset(price)) {
				//if(!this.findById(id)) {
					this.list['e'+id] = {
						id:id,
						url:url,
						img:img,
						title: title,
						description:description,
						sku:sku,
						price:price,
						num:1
					};
					this.store();
					this.drawList();
					this.updateBadge();
				//}
				$("#addItem"+id).fadeOut('fast');
			} else {
				alert('all parameter required');
			}
		},
		
		removeItem:function(id) {
			if(this.findById(id)) {
				delete this.list['e'+id];
			}
			$("#addItem"+id).fadeIn('fast');
			this.store();
			this.drawList();
			this.updateBadge();
		},
		
		items:function () {
			var i=0;
			for(e in $.merkzettel.list) { i++; }
			return i;
		},
		
		updateBadge:function() {
			var _this = this;
			$('.merkzettel .numberItems').each(function(i,e) {
				this.innerHTML = _this.items()+' Artikel gemerkt';
			});
		},
		
		drawList:function () {
			$('#merkzettellist').html('');
			
			
			
			if(this.items() > 0) {
				var counter = 0;
				var total = 0;
				for(x in this.list) {
					var classname = '';
					
					if(counter != this.items() - 1)
						classname = 'class="listitemborder"';
					
					
					var e = this.list[x];
					
					if(typeof e.price == 'undefined') { e.price = this.list[x].price = 1.00; }
					if(typeof e.amount == 'undefined') { e.amount = this.list[x].amount = 1; }
					
					
					total += e.price * e.amount;
					
					$('#merkzettellist').append(
						$(['<li ',classname,'>', 
						'<a href="',e.url,'"><img src="', e.img,'" /></a>',
						'<div class="box">',
						'	<span class="title">', e.amount, ' x ',unescape(e.title),' <span style="float:right"> á ',Number(e.price).toFixed(2),' € </span></span> ',
						'	<span class="sku">Artikel-Nr: ', e.sku,'</span>',
						'	<div class="edit">',
						'		<div class="minus" rel="',e.id,'" >Anzahl: -</div>',
						'		<div class="plus" rel="',e.id,'" >+</div>',
						'		<a href="',e.url,'" class="link clear">anzeigen</a>',
						'		<div class="remove" rel="',e.id,'">entfernen</div>',
						'	</div>',
						'</div>',
						'</li>'].join('')));
					counter++;
					
					
					
				}
				
				$('#merkzettellist').append('<li class="totalSum">Summe: '+total.toFixed(2)+' €</li>');
					
			} else {
				$('#merkzettellist').append('Es befinden sich keine Artikel auf Ihrem Merkzettel');
			}
			$('#merkzettellist .remove').each(function(i,e) { $(e).click(function() { $.merkzettel.removeItem($(e).attr('rel')); })});
			$('#merkzettellist .plus').each(function(i,e) { $(e).click(function() { $.merkzettel.increase($(e).attr('rel')); })});
			$('#merkzettellist .minus').each(function(i,e) { $(e).click(function() { $.merkzettel.decrease($(e).attr('rel')); })});
		},
		
		
		
		increase:function(id) {
			if(e = this.findById(id)) {
				this.list['e'+id].amount++;
				this.store();
				this.drawList();
			}
		},
		
		decrease:function(id) {
			if(e = this.findById(id)) {
				if(e.amount <= 1) return;
				this.list['e'+id].amount--;
				this.store();
				this.drawList();
			}
		},
		
		drawListPrint:function () {
			$('#merkzettellistprint').html('');
			
			if(this.items() > 0) {
				var counter = 0;
				for(x in this.list) {
					
				
					var classname = '';
					if(counter != this.items() - 1)
						classname = 'class="listitemborder"';
					
					var e = this.list[x];
					
					if(typeof e.price == 'undefined') { e.price = this.list[x].price = 1.00; }
					if(typeof e.amount == 'undefined') { e.amount = this.list[x].amount = 1; }
					
										
					$('#merkzettellistprint').append(
						$(['<li ',classname,'>',
						'<a href="',e.url,'"><img src="', e.img,'" /></a>',
						'<div class="box">',
						'	<span class="title">', e.title,'</span>',
						'	<span class="sku">Artikel-Nr: ', e.sku,'</span>',
						'	<div class="libox">Einzelpreis:</div><div class="libox">Menge:</div><div class="libox">Summe:</div> ',
						'	<div class="clear"></div> ',
						'	<div class="libox price">', Number(e.price).toFixed(2),'</div><div class="libox menge">', e.amount,'</div>	<div class="libox priceTotal">', Number(e.price * e.amount).toFixed(2),'</div>',
						'	<div class="edit"><a href="',e.url,'" class="link">anzeigen</a>',
						'	<div class="remove" rel="',e.id,'">entfernen</div></div>',
						'</div>',
						'</li>'].join('')));
					counter++;
				}
			} else {
				$('#merkzettellistprint').append('Es befinden sich keine Artikel auf Ihrem Merkzettel');
			}
			$('#merkzettellistprint .remove').each(function(i,e) { $(e).click(function() { $.merkzettel.removeItem($(e).attr('rel')); })});
		},
		
		open:function() {
			this.drawList();
			$('#merkzettelpage').fadeIn('fast');
		},

		openprint:function() {
			this.drawList();
			$('#merkzettelpage').fadeIn('fast');
		},

		
		close:function() {
			$('#merkzettelpage').fadeOut('fast');

		},
		
		store:function() {
			$.cookie('mymerkzettel', JSON.stringify(this.list), { expires: 365, path: '/' });
		}
	}
	
	// load existing list
	if($.cookie('mymerkzettel') !== null) {
		$.merkzettel.list = JSON.parse($.cookie('mymerkzettel'));
	}
	
		
	// inject some html to show the merkzettel
	$('#cntr').append('<div id="merkzettelpage" ><div class="inner"><div id="merkzettel_header"><img src="/fileadmin/sconto/images/merkzettel_header.gif" alt="Merkzettel" /></div><div id="merkzettelprint"><a href="/merkzettel.html" target="_blank"><img src="/fileadmin/sconto/images/merkzettel_print.gif" alt="Drucken" /></a></div><div id="merkzettelclose"><img src="/fileadmin/sconto/images/merkzettel_close.gif" alt="Schlie&szlig" /></div><ul id="merkzettellist"> </ul><div id="merkzettel_footer"><img src="/fileadmin/sconto/images/adw_sconto_footer.gif" alt="Sconto - M&ouml;bel sofort" /></div><div style="clear:both;"></div></div></div>');
	
	$.merkzettel.updateBadge();
	
	$('.merkzettel .button, .merkzettel .numberItems').each(function(i,e) { $(e).click(function() { $.merkzettel.open(); }); });
	$('#merkzettelclose').click($.merkzettel.close);

});


