var aldi;
if (!aldi) {
	aldi = {}
};
$.extend(aldi, {
	page: {
		menu: function() {
			
			var $header = $('#header')
				,$main = $('#main');
			
			$('#navigation ul li').hoverIntent({
				over: function() {
					if ($.browser.msie && $.browser.version.slice(0,1) < "8") {
						$header.css('z-index', 5);
					}
					
					var t = $(this);
					t.find('> ul').show();
					
					$('.product-social-links').each(function(){
						if(!$(this).parent('.product-content').length){
							$(this).hide();
						}
					});
				},
				out: function() {
					if ($.browser.msie && $.browser.version.slice(0,1) < "8") {
						$header.css('z-index', '');
					}
					
					var t = $(this);
					t.find('> ul').hide();
				},
				timeout: 150
			});
		},
		lightbox: function() {

			$('#content a.lightbox').fancybox({
				margin: 10,
				overlayColor: '#fff'
			});

			$('.product-detail a.lightbox').fancybox({
				margin: 10,
				overlayColor: '#fff',
				showNavArrows: true,
				onComplete: function(){
					// var thumbs = $('.stage-product ul.pagination').detach().appendTo($('#fancybox-content'));					
				},
				onCleanup: function(){
					// var thumbs = $('#fancybox-content ul.pagination').detach().appendTo($('.stage-product'));
				}
				
			});
			$('a.lightbox.remind').fancybox({
				type: 'iframe',
				width: 930,
				height: '90%',
				margin: 10,
				overlayColor: '#fff'
			});
			$('a.lightbox.recommend').fancybox({
				type: 'iframe',
				width: 930,
				height: '90%',
				margin: 10,
				overlayColor: '#fff'
			});
           
			
			//fuer Feuerwerks SWF
		    $('a.lightbox.swf').fancybox({
				type        :'swf',
		        margin      :0,
		        padding     :10,
		        mode        :'transparent',
                overlayColor:'#fff'
		    });
		}
	},
	modules: {
		accordion : {
			init : function($p_o){
				var o = {
					$o : $p_o
					,ismultiopen : $p_o.hasClass('accordion-multiopen')
					,items : []
				};
				
				(function(){
					o.$o.find('> h3').each(function(){
						var item = aldi.modules.accordion.item($(this), o);
						o.items.push(item);
					});
					
					/*if(!o.ismultiopen){
						o.items[0].open();
					}*/
				}());
				
				return o;
			}
			,item : function($p_o, p_accordion){
				var o = {
					accordion : p_accordion
					,$headline : $p_o
					,$content : $p_o.next()
					,isopened : false
					,open : function(){
						o.isopened = true;
						
						$.each(o.accordion.items, function(i, p_item){
							if(p_item !== o && !p_accordion.ismultiopen){
								p_item.close();
							}
						});
						
						$.scrollTo( o.$headline, 0);
						o.$headline.addClass('active');
						o.$content.show();
					}
					,close : function(){
						o.isopened = false;
						o.$headline.removeClass('active');
						o.$content.hide();
					}
				};
				
				(function(){
					o.close();
					
					o.$headline.bind('click', function(e){
						e.preventDefault();
						e.stopPropagation();
						
						if(o.isopened){
							o.close();
						} else {
							o.open();
						}
					});
					
					/* fuer Kaffeewelt und Teewelt */
					/*if(o.$headline.hasClass("open")){
						o.open();
					}*/
				}());
				
				return o;
			}
		}
		,shoppinglist: function() {
							
			$('.shoppinglist-head a.notes').click(function() {
				var t = $(this),
					n = t.closest('.shoppinglist-head').find('div.notes');
				
				n.find('.indicator').css({
					left: Math.floor(t.closest('li').position().left)
				});
				
				if(n.is(':visible')) {
					n.hide();
				} else {
					n.show();
				}
			});

			$('.shoppinglist-head a.print').click(function(e) {
				var t = $(this),
					m = t.closest('li').find('.print-menu');
				
				e.preventDefault();
				e.stopPropagation();
				
				/* IE 8 Bug beheben */
				if ($.browser.msie && $.browser.version.slice(0,1) == "8") {
					m.css("left","-10px");
					m.css("top","40px");
				}
				
				if(m.is(':visible')) {
					m.hide();
				} else {
					m.show();
				}
			});
			
			$('.shoppinglist-head a.gallery').click(function(e) {
				e.preventDefault();
				$(this).closest('ul').find('li').removeClass('active');
				$(this).closest('li').addClass('active');
				$('.product-tile .image, .product-tile a.delete').show();
				$('.product-tile').css({
					'min-height': '430px'
				});
			});
			$('.shoppinglist-head a.text').click(function(e) {
				e.preventDefault();
				$(this).closest('ul').find('li').removeClass('active');
				$(this).closest('li').addClass('active');
				$('.product-tile .image, .product-tile a.delete').hide();
				$('.product-tile').css({
					'min-height': '1px'
				});
			});
			
			$('.shoppinglist-head .print-menu a.print-wimg').click(function(e) {
				e.stopPropagation();
				$('body').removeClass('print-woimg');
				$('body').addClass('print-wimg');
				self.print();
			});
			$('.shoppinglist-head .print-menu a.print-woimg').click(function(e) {
				e.stopPropagation();
				$('body').removeClass('print-wimg');
				$('body').addClass('print-woimg');
				self.print();
			});
		},
		producttile: function() {
			var $tmp = $('.product-social-links:eq(0)')
				,h = $tmp.outerHeight()
				,w = $tmp.outerWidth();
			
			$('.product-tile,.offer .content').each(function(i) {
				var t = $(this);
				
				var $detaillink = t.find('> h3 > a.detaillink')
					,$sociallinks = t.find('> .product-social-links')
					,$sociallinkshandler = $('<a href="#" class="social">Social Links</a>').appendTo(t)
					,sociallinkshandlerposition = $sociallinkshandler.position()
					,$recommendlink = $sociallinks.find('> a.recommend')
					,$remindlink = $sociallinks.find('> a.remind');				
		
				if ($.browser.msie && $.browser.version.slice(0,1) < "10") {
						if(t.parent().hasClass('warenkorblist')) {
								t.css('z-index', 800);			
						}
				}
				
				//Indicator und Link (...) hinzufügen
				$('<div class="indicator" />').css({
					left: w / 2 - 9
				}).appendTo($sociallinks);
				
				if($recommendlink.length) {
					$recommendlink.attr('href', $recommendlink.attr('href') + "&page=" + $detaillink.attr('href'));
				}
				
				//Positionieren ueber social-link button
				$sociallinks.css({
					top: sociallinkshandlerposition.top - (h + 12),
					left: sociallinkshandlerposition.left - ((w / 2) - (25 / 2))
				});
				
				if(t.parent().hasClass('warenkorblist')) {
					 if($remindlink.length < 1) {
						$sociallinks.css({
							top: sociallinkshandlerposition.top - (h - 18),
							left: sociallinkshandlerposition.left - ((w / 2) - (25 / 2))
						});
					}
				}

				//Add hover states for tile
				t.hover(function() {
					if($detaillink.length ) {
						t.addClass('hover');
					}
				}, function() {
					t.removeClass('hover');
				});
				
				// Handler for click on tile
				t.click(function(e) {
					e.stopPropagation();
					if(($detaillink.length) && (!$(e.target).hasClass('product-social-links'))) {
						document.location = $detaillink.attr('href');
					}
				});
				
				if(t.parent('.warenkorblist').length == 0) {
					t.css({'cursor':'pointer'});
				}
				$sociallinks.css({'cursor':'default'});
				
				//Stop propagation for all links
				t.find('a').click(function(e) { e.stopPropagation(); });
				
				//Handler für Link
				var zindex = 1;
				$sociallinkshandler.click(function(e) {
					var offer = t.closest('.offer'),
						s = t.closest('.product-tile,.offer .content').find('.product-social-links');
					
					e.preventDefault();
					e.stopPropagation();
					
					/* Performance Update IE8 */
					if ($.browser.msie && $.browser.version.slice(0,1) == "8") {
					s.css('behavior','url("./css_images/pie.php");');
					}
					
					if(!s.is(':visible')) {
						if ($.browser.msie && $.browser.version.slice(0,1) < "8") {
							if(offer.length){
								offer.css('z-index', zindex);
								zindex++;
							}
						}
						
						$('.product-tile .product-social-links').hide();
						$('.offer .content .product-social-links').hide();
						s.show();
					} else {
						$('.product-tile .product-social-links').hide();
						$('.offer .content .product-social-links').hide();
						
						if ($.browser.msie && $.browser.version.slice(0,1) < "8") {
							$('.offer').css('z-index', '');
						}
					}
				});
			});
		},
		firstproduct: function(){
			$('.product_01,.offer-1').click(function(){
				document.location = $(this).find('a.detaillink').attr('href');	
			});
		},
		firstpageoffer: function(){
			$('.offers .tipps,.offers .offers-grid').click(function(){
				document.location = $(this).find('a.detaillink').attr('href');	
			}).css({'cursor':'pointer'});
		},
		stage: function() {
			//stage-items are hidden in css. show all before stages are initialized.
			$(".stage .stage-item, .stage-small .stage-item, .stage-product .stage-item").each(function(){
				$(this).show();
			});
			
			$(".stage").not('.stage-small').slides({
				container: 'content',
				preload: true,
				generatePagination: false,
				generateNextPrev: false,
				hoverPause: true,
				play: 5000,
				pause: 1
			});
			$(".stage-small").slides({
				container: 'content',
				preload: true,
				generatePagination: true,
				paginationClass: 'pagination-small',
				generateNextPrev: true,
				hoverPause: true,
				play: 5000,
				pause: 1
			});
			$(".stage-product").slides({
				container: 'content',
				preload: true,
				generatePagination: false,
				generateNextPrev: true
			});
			
			$(".stage-small .pagination-small").each(function() {
				var t 	= $(this),
					li 	= t.find('li'),
					len = li.length;
					
				t.find('li').each(function() {
					var t = $(this);
					t.text(t.text() + ' / ' + len);
				});
			});
			
		},
		tabs: function() {
			$('.tabs').not('.tabs.notabsfunction').each(function(i) {
				var t 	= $(this),
					ol 	= $('<ol />').addClass('clearfix tabs-control tabs-'+i);
				
				if(t.hasClass('detachfirsttab')){
					t.find('h2:first').detach().insertBefore(t).hide();
					t.find('.tab-content:first').detach().insertBefore(t);
				}	
							
				t.find('h2').each(function(j) {
					var t 	= $(this).attr('rel', 'tabs-'+i+'-'+j),
						c 	= t.next('.tab-content'),
						li 	= $('<li />').append(t);
					
					if(j == 0) {
						li.addClass('active');
					} else {
						c.hide();
					}
					
					ol.append(li);
					c.attr('id', 'tabs-'+i+'-'+j);
					
					t.click(function() {
						var t 	= $(this),
							id 	= t.attr('rel');
						
						t.closest('.tabs-control').find('li').removeClass('active');
						t.closest('.tabs').find('.tab-content').hide();
						t.closest('li').addClass('active');
						t.closest('.tabs').find('#'+id).show().closest('li').removeClass('active');						
					});
				});
				
				t.prepend(ol);
			});
		}
	},
    init: function() {
    	//2011-11-11, tr:
    	//init nur fuers Frontend, nicht fuer be sicht!
   	
    	aldi.page.menu();
    	aldi.page.lightbox();
    	aldi.modules.stage();
    	aldi.modules.tabs();
    	
    	$('.accordion').each(function(){
    		aldi.modules.accordion.init($(this));
    	})
    	aldi.modules.producttile();
    	aldi.modules.firstproduct();
    	aldi.modules.shoppinglist();
    	aldi.modules.firstpageoffer();

		$(".reload_captcha").click(function(e) {
			e.preventDefault();
			var random = "./securimage/securimage_show.php?" + Math.random();
	    	$("#suso_img").attr("src",random);
		});
		
		function resetForm(id) {
		    $('#'+id).each(function(){
		    	this.reset();
		    });
		}
		
		$('#navigation ul li').addClass("js");
		

        $('#reset_form').click(function(e) {
        	e.preventDefault();
        	var formid = $(this).closest('form').attr("id");
            resetForm(formid);
        });

    	$('body').click(function() {
			$('.product-tile .product-social-links').hide();
			$('.offer .content .product-social-links').hide()
			$('.print-menu').hide();
		});
		
		$('.linkarrow').each(function() {
			$(this).html($(this).html() + ' &#187;');
		});

		$('.linkarrowback').each(function() {
			$(this).prepend("&#171; ");
		});
		
		/* Performance Update IE8 */
		if ($.browser.msie && $.browser.version.slice(0,1) != "8") {
			$('.price-tag .main').css('behavior','url("./css_images/pie.php");');
			$('.product-social-links').css('behavior','url("./css_images/pie.php");');
		}
		
		if ($.browser.msie && $.browser.version.slice(0,1) == "9") {
		$('body').addClass("ie9");
		}
		
		var browser = jQuery.uaMatch(navigator.userAgent).browser;
		$('body').addClass(browser);

    }
});
$(aldi.init);
