	app = {
			actualTeaser:"1",	
	newsletter: {
	  toggle: function() {
	    $('#newsletter_subscribe').toggle();
  	  $('#newsletter_unsubscribe').toggle();
  	  return false;
	  }
	},
	groessen: {
	  toggle: function() {
		$('#groessen_massnehmen').toggle();
	  	$('#groessen_tabellen').toggle();
	  	return false;
	  }
	},
	shadowbox: {
	  togglePrintButton: function() {
	    if ($("iframe#shadowbox_content").length) {
        $("#shadowbox_nav a#shadowbox_nav_print").show();
      } else {
        $("#shadowbox_nav a#shadowbox_nav_print").hide();
      }
	  },
	  print: function() {
	    if (shadowbox_content) {
	      shadowbox_content.print();
	    }
	  },
	  basket: function() {
	    Shadowbox.open({
	      player: 'html',
	      title: 'Sie haben den folgenden Artikel<br />in den Warenkorb gelegt',
	      content: $("#shadowBoxContent").html(),
	      width: 605,
	      height: 605
	    }, {
	      enableKeys: false,
	      handleOversize: 'none'
	    });
	  }
	},
    initHomepage: function() {
		
		if($('#picSlider').length>0) {
			app.initSlider();
		}
		if($('#teaser_1').length>0) {
			var timer = false;
			app.refreshTeaser();
			app.initTeaserNavigation();
		}	
	},	
	initSlider: function() {

		$("#teaser_1").fadeIn("slow");
		$("#teaser_left_1").fadeIn("slow");
		
		var laenge=0;
		$(".blocks li").each(function(){
		    laenge+=$(this).width();
		})
		laenge+=200;
		$(".blocks").css("width",laenge+"px");
		
		$('#picSlider').jScrollHorizontalPane({ showArrows: true,
            scrollbarHeight: 10,
            arrowSize: 35,
            dragMaxWidth: 100}
		);
		$(".slider-product img").unbind("mouseover");
		$(".slider-product img").unbind("mouseout");
		$(".slider-product img").mouseover(function(){
			if($(this).width()<131) {
			$(this).animate({width:'172px',height:'130px',opacity:0},100, function() {
			      $(this).animate({width:'214px',height:'130px',opacity:1},100, function() {}).attr("src",$(this).attr("src").replace("small","large"));
			  });
			}
			//Fallback ohne Animation 
			//$(this).attr("src",$(this).attr("src").replace("small","large")).css("width","214px").css("height","130px");

		});

		$(".slider-product img").mouseout(function(){
				$(this).animate({width:'172px',height:'130px',opacity:0.25},100, function() {
					   $(this).animate({width:'130px',height:'130px',opacity:1},100, function() {}).attr("src",$(this).attr("src").replace("large","small"));
				});
			//Fallback ohne Animation
			//$(this).attr("src",$(this).attr("src").replace("large","small")).css("width","130px").css("height","130px");
				
		});
	},
	refreshTeaser: function() {
		timer = window.setTimeout("app.switchTeaser('f',true);app.refreshTeaser()", 5000);	
	},
	stopTeaser: function() {
		if(timer) {
			window.clearTimeout(timer);
		}				
	},	
    initTeaserNavigation: function() {
		$('.teaserStart').mouseout(function(){
			$("#prevTeaser,#nextTeaser").hide();
			app.refreshTeaser();
		});	
		$('.teaserStart').mouseover(function(){
			$("#prevTeaser,#nextTeaser").show();
			app.stopTeaser();
		});	
		$('#prevTeaser,#nextTeaser').mouseover(function(){
			$("#prevTeaser,#nextTeaser").show();
			app.stopTeaser();
		});	
		
		$("#prevTeaser").click(function(){
			app.switchTeaser("b",false);
		});
		$("#nextTeaser").click(function(){
			app.switchTeaser("f",false);
		});
		
	},
    switchTeaser: function(dir,initial) {
		nr=app.actualTeaser;
		if (dir=="b") {
			nr--;			
		} else {
			nr++;
		}	
		if(nr<1) {
			nr=$(".slideshow").length;			
		}
		if(nr>$(".slideshow").length) {
			nr=1;
		}
	
		if(initial) {
			$("#teaser_left_"+app.actualTeaser+",#teaser_"+app.actualTeaser).fadeOut("slow",function() {
				$("#teaser_left_"+nr+",#teaser_"+nr).fadeIn("slow");
			});
		} else {
			$("#teaser_left_"+app.actualTeaser+",#teaser_"+app.actualTeaser+",#prevTeaser,#nextTeaser").fadeOut("slow",function() {
				$("#teaser_left_"+nr+",#teaser_"+nr+",#prevTeaser,#nextTeaser").fadeIn("slow");
			});			
		}	
		app.actualTeaser=nr;
	}
};

$(document).ready(function(){

	app.initHomepage();

	if ( jQuery('#bodyteaserbelt-carousel').size() == 1 ) {
		jQuery('#bodyteaserbelt-carousel').jcarousel({
			scroll: 1
		});
	}
	
	// Hightlight navigation
	$("#navimain > li.hl").hover(
		function(){$(this).children('a').children('img').fadeTo(250,0.0);},
		function(){$(this).children('a').children('img').fadeTo(250,1.0);}
	);
	
	// Replace default input texts
  $('input.rpl').each(function(){
    $(this).toggleDefaultValue();
  });
	
	// Printlinks
  $('a.print').click(function(){
    window.print();
    return false;
  });
	
	// Newsletter
  $('.newsletterbox .content_tabs a').click(function(){
    app.newsletter.toggle();
  });

	// Groessentabellen
  $('.groessenbox .content_tabs a').click(function(){
	  app.groessen.toggle();
  });
  $('.groessenbox .groessen_link a').click(function(){
	  app.groessen.toggle();
  });
  $('.groessenbox .massnehmen_link a').click(function(){
	  app.groessen.toggle();
  });
  
  $('.content.contentbox.accountbox input.registerNew').live('click', function(e) {
	  $(".content.contentbox.accountbox img.loading").fadeIn('slow');
  });
  
  $('form input.next').live('click', function(e) {
	 
	 var $form = $(this).closest("form");
	 $form.find('.loading').fadeIn('slow');		 
	 
	 var radiologinchoice = $("#checkoutlogin .radio:checked").val();
	 if(radiologinchoice==null){
	 
		 e.preventDefault();
	
		 if (!$form.data("submitting")) {
			 $form.data("submitting", true);
			 $("input.flow-action").attr('name', $(this).attr('name'));
			 $form.submit();
			 $form.find('.bestellung_abschicken').removeClass('bestellung_abschicken').addClass('bestellung_abschicken_inactive');
		 }
	 }
  });
  
	// Disabled options
  disableOptions();

  testRestoreBasketLayer();
  
  if (typeof Shadowbox !== 'undefined') {
    Shadowbox.init({
      overlayOpacity: 0.5,
      onFinish: app.shadowbox.togglePrintButton
    });
  }
function initFullscreen() {
	var init = function() {
		imgwidth=this.width;
		imgheight=this.height;
		fullwidth=$( "#fullscreenContainer" ).width();
		fullheight=$( "#fullscreenContainer" ).height();

		xoffset=imgwidth-fullwidth;
		yoffset=imgheight-fullheight;
		$("#fullscreenimage").css("top","0px");
		$("#fullscreenimage").css("left",-(xoffset/2)+"px");
		$(document).keypress(function(event) {
			if (event.keyCode=27) {
				$('#fullscreenschliessen').trigger('click');
			}
		});
		$( "#fullscreenimage" ).bind('mousewheel', function(event, delta) {
		    //console.log(delta);
    		imgheight=$( "#fullscreenimage" ).height();
    		fullheight=$( "#fullscreenContainer" ).height();
    		yoffset=imgheight-fullheight;
		    y=$("#fullscreenimage").position().top;
		    y=y+(delta*5);
	    	$("#fullscreenimage").css("top",y+"px");
		    if (y>2) {
		    	$("#fullscreenimage").css("top","0px");
		    } else if (y<-yoffset) {
		    	$("#fullscreenimage").css("top",-yoffset+"px");
    		} else {    		
		    	$("#fullscreenimage").css("top",y+"px");
    		}	
		});
		
		if($( "#fullscreenContainer" ).width() < $("#fullscreenimage").width()) {    		
    		$("#fullscreenimage").draggable();
        	$( "#fullscreenimage" ).bind("drag", function(event, ui) {
        		x=ui.position.left;   
        		y=ui.position.top;
        		imgwidth=$( "#fullscreenimage" ).width();
        		imgheight=$( "#fullscreenimage" ).height();
        		fullwidth=$( "#fullscreenContainer" ).width();
        		fullheight=$( "#fullscreenContainer" ).height();
        		xoffset=imgwidth-fullwidth;
        		yoffset=imgheight-fullheight;
        		
        		//console.log("x: "+x+" | y: "+y+" | imgwidth: "+imgwidth+" | imgheight: "+imgheight+" | fullwidth: "+fullwidth+" | fullheight: "+fullheight+" | xoffset: "+xoffset+" | yoffset: "+yoffset);
        		
        		if (x<-xoffset) {
            		ui.position.left=-xoffset;   
        		}    		
        		if (x>0) {
            		ui.position.left=0;   
        		}    		
        		if (y>0) {
            		ui.position.top=0;   
        		}    		
        		if (y<-yoffset) {
            		ui.position.top=-yoffset;   
        		}    		
       		});
    	}
		
		$("#fullscreenContainer #loadingScreen").fadeOut("slow");
	};
	
	$("#fullscreenContainer #loadingScreen").fadeIn("fast");
	setTimeout( function(){ $("#fullscreenContainer #loadingScreen").fadeOut("slow") },5000);
	
	var src;
	try {
		src = $("#productimage #mainImg").attr("src").replace("520","1500");
	}
	catch(err){
		
	}
	
	
	$("#fullscreenimage").bind("load",init);
	$("#fullscreenimage").attr("src", src);
}

	function initZoom() {
		
    	$("#zoomimage").draggable();
		
    	$( "#zoomimage" ).bind( "drag", function(event, ui) {
    		x=ui.position.left;   
    		y=ui.position.top;
    		imgwidth=$( "#zoomimage" ).width();
    		imgheight=$( "#zoomimage" ).height();
    		zoomwidth=$( "#productzoom" ).width();
    		zoomheight=$( "#productzoom" ).height();
    		xoffset=imgwidth-zoomwidth;
    		yoffset=imgheight-zoomheight;
    		if (x<-xoffset) {
        		ui.position.left=-xoffset;   
    		}    		
    		if (x>0) {
        		ui.position.left=0;   
    		}    		
    		if (y>0) {
        		ui.position.top=0;   
    		}    		
    		if (y<-yoffset) {
        		ui.position.top=-yoffset;   
    		}    		
   		});
		
		src=$("#zoomimage").attr("src");
		if($("#zoomimage").width()==75) {
			//src_1500=src.replace(/75/,"1500");			
			//$("#zoomimage").attr("src",src_1500);
		}
		imgwidth=1500;
		imgheight=$( "#zoomimage" ).height();
		zoomwidth=$( "#productzoom" ).width();
		zoomheight=$( "#productzoom" ).height();
		xoffset=imgwidth-zoomwidth;
		yoffset=imgheight-zoomheight;

		if(xoffset>0 && yoffset>0){		
			$("#zoomimage").css("top",(-yoffset/2)+"px");
			$("#zoomimage").css("left",(-(xoffset/2)-150)+"px");
		}
		
	}
 
  	$( "#zoomimage" ).bind("load",	initZoom);
    $('.zoomoeffnen').live('click', function() {
    
    	var zoombutton_url = $("#zoomview").css("background-image");
    	var zoombutton_url_neu = zoombutton_url.replace(/bttn_zoom_oeffnen/g, "bttn_zoom_schliessen");
    	$("#zoomview").css("background-image",zoombutton_url_neu);
    	
    	if($( "#zoomimage" ).attr("src")==null){
    		var src = $("#productimage #mainImg").attr("src").replace("520","1500");
    		$( "#zoomimage" ).attr("src",src);
    	}
    	
    	$("#productzoom").show();
    	$("#zoomview").attr("style","");
    	$("#zoomview").removeClass( "zoomButton" ).removeClass("zoomoeffnen").addClass( "zoomschliessen" ).addClass( "zoomButtonClose" );
    	return false;
    });
    
    $('.zoomschliessen').live('click', function() {
    	var zoombutton_url = $("#zoomview").css("background-image");
    	var zoombutton_url_neu = zoombutton_url.replace(/bttn_zoom_schliessen/g, "bttn_zoom_oeffnen");
    	$("#zoomview").css("background-image",zoombutton_url_neu);
    	$("#productzoom").hide();
    	$("#zoomview").attr("style","");
    	$("#zoomview").addClass( "zoomButton" ).addClass("zoomoeffnen").removeClass( "zoomschliessen" ).removeClass( "zoomButtonClose" );
    	return false;
    });    
    

    $('#productimage').live('click', function() {
		$('#fullscreen').trigger('click');
    	return false;
    });

    $('#fullscreen, .productimage, .fullscreenButton').live('click', function() {
    	initFullscreen();
    	$("#fullscreenContainer").fadeIn(1000,"linear");
    	
		$("#overall").css("overflow","hidden");
		$("#overall").css("height","0px");
		$("#overall").css("width","0px");
    	return false;
    });




	function xoffset() {
		imgwidth=$( "#fullscreenimage" ).width();
		fullwidth=$( "#fullscreenContainer" ).width();
		return imgwidth-fullwidth;
	}

	function yoffset() {
		imgheight=$( "#fullscreenimage" ).height();
		fullheight=$( "#fullscreenContainer" ).height();
		return imgheight-fullheight;
	}


    
    $('#fullscreenschliessen').live('click', function() {
    	$("#fullscreenContainer").hide();
    	var zoombutton_url = $("#zoomview").css("background-image");
    	if(zoombutton_url!=null) {
    		var zoombutton_url_neu = zoombutton_url.replace(/bttn_zoom_schliessen/g, "bttn_zoom_oeffnen");
    	}
    	$("#zoomview").css("background-image",zoombutton_url_neu);
    	$("#productzoom").hide();
    	$("#zoomview").removeClass( "zoomschliessen" )
    	$("#zoomview").addClass( "zoomoeffnen" )
		$("#overall").css("overflow","visible")
		$("#overall").css("height","100%")
		$("#overall").css("width","81.75em")
		$(document).unbind("keypress");
		$("#fullscreenimage").unbind("mousewheel");
    	return false;
    });    



    $('.changeImg').live('click', function() {
		src=$(this).attr("src");
		if($(this).width()==75) {
			src_330=src.replace(/75/,"520");			
			src_1500=src.replace(/75/,"1500");			
			src_2000=src.replace(/75/,"2000");			
		} else {
			src_330=src.replace(/90/,"520");			
			src_1500=src.replace(/90/,"1500");			
			src_2000=src.replace(/90/,"2000");			
		}
		
		
		$("#productimage img").attr("src",src_330);
		$("#zoomimage").attr("src",src_1500);
		if(this.parentNode!=null && this.parentNode.rel=="fullscreen") {
			$("#fullscreenimage").attr("src",src_1500);
			initFullscreen();
		}

		return false;
    });    
    
       
    

    
});

function disableOptions() {
  $('select').each(function(){
  
  	try{
  
    this.rejectDisabled = function(){
      if (this.options[this.selectedIndex].disabled){
        if (this.lastSelectedIndex) {
          this.selectedIndex = this.lastSelectedIndex;
        } else {
          var first = $(this).children('option:not(:disabled)').get(0);
          this.selectedIndex = first ? first.index : 0;
        }
      } else {
        this.lastSelectedIndex = this.selectedIndex;
      }
    };
    this.rejectDisabled();
    this.lastSelectedIndex = this.selectedIndex;
    $(this).children('option[disabled]').each(function(){
      $(this).css('color', '#aba099');
    });
    $(this).change(function() {
      this.rejectDisabled();
    });
    
    }
    catch(e)
    {	
    	;
    }
    
  });
}


/** Custom Plugins 
		example:  $("#selector").realOffsetTop()
*/
$.fn.realOffsetTop = function() {
  var elem = $(this).get(0);
  var realOffset = elem.offsetTop;
	while (elem.offsetParent) {
		elem = elem.offsetParent;
   	realOffset += elem.offsetTop;
	}
  return realOffset;
};

/**	toggle default value of input field 
		example to place in $(document).ready(): 
		$('input#searchValue').toggleDefaultValue();
*/
$.fn.toggleDefaultValue = function(default_value) {
	var defval = default_value || $(this)[0].defaultValue;
	$(this).focus( function() {
		if (this.value == defval) {
		  this.value = "";	
		}
	});	
	$(this).blur( function() {
		if (this.value.replace(/\s/, "") === "") {
			this.value = defval; 
		} 
	});
};

/** Popups */
function popup( url, optionen, popupName ) {
  popupWindow = window.open( url, popupName, optionen );
  if ( popupWindow ) {
    if (popupWindow.focus) {
      popupWindow.focus();
    }
  }
  return false;
}

function defaultPopup( url, name ) {
  popup( url, 'width=550,height=350,toolbar=0,menubar=0,resizable=1,scrollbars=1,status=1,left=200,top=10', name );
  return false;
}

function sizePopup(url, name, width, height, top, left ) {
  popup(url, 'width='+width+',height='+height+',toolbar=0,menubar=0,resizable=1,scrollbars=1,status=1,left='+left+',top='+top+'', name );
}

/** helpers */
function isIE() {
	return $.browser.msie;
} 

// AJAX cityLookup BEGINN
function getCityByPostalCode(postalCode) {
  if (null != postalCode && "" != postalCode) {
   	var origCity = $("select#city").val();
    $.ajax({
        	url: "_/postalCode/" + postalCode + "/lookupCity.html",
            dataType: 'json',
            error: function(msg1, msg2, msg3) {},
            success: function(data) {
				if(data.cityList.length == 1){
				  if($("select#city")) {
				  	var input = '<input type="text" class="text" name="city" id="city" value="city" readonly="readonly"/>';
				  	$("div#cityContainer").html(input);
				  }
			      $.each(data.cityList, function(i,item) {
			      	$("input#city").val(item);	
			      });
			      
				}	
				else if(data.cityList.length > 1){
				  var select = '<select class="city" name="city" id="city">';
				  var options = '';
			      $.each(data.cityList, function(i,item) {
					options += '<option value="' + item + '"'
					if (item==origCity) {
						options += ' selected="selected"';
					}
					options += '>' + item + '</option>';
				  });
				  select += options;
				  select += '</select>';
				  $("div#cityContainer").html(select);
				}
			}
    });
  }
}
// AJAX cityLookup END

function removeFailedColorthumbs(){

	$('#colorthumbs_container img.colorthumb').each(function() {
	if((typeof this.naturalWidth != "undefined" &&
			this.naturalWidth == 0 ) 
			|| this.readyState == 'uninitialized' ) {
			$(this).parent().remove();
		}
	});
}

function addFlatToBasket( variationId, promoInfo, title ) {
	$(".Popup").animate({opacity:0},500, function(){$(".Popup").hide();});

	$.ajax({
		url:"${rc.contextPath}/addVariationToBasket.html", 
		dataType:'xml',  
		data: {variation_id:variationId, count: 1, promotionInfo:promoInfo },
		error: function(msg1, msg2, msg3) {
			//console.log(msg1,msg2,msg3)
		}, 
		success: function(data) {
			Shadowbox.open({
				player: 'html',
				title: title,
				content: $("#shadowBoxContent").html(),
				width: 605,
				height: 605
			}, {
				enableKeys: false,
				handleOversize: 'none',
				onClose: function() { document.location.reload(); }
			});
		}
	});

	return false;
}


$(window).bind('load', removeFailedColorthumbs);

function sendEmailForNotification() {
    var promoItemId = $('#promoItemId').val();
    var variationId = $('#variationId').val();
    var email = $('#emailInput').val();
	$.ajax({
          url:"backinstockform.html",
       	  type: 'POST', 
          dataType:'html',  
          data: {promoItemId:promoItemId, variationId:variationId, email:email},
          success: function(data) {
              $('#shadowbox_content').html(data);
          }
    });
}

function testRestoreBasketLayer(){
	var previousBasketExists = $("meta[property=rf:last-basket]");
	if (previousBasketExists.length > 0) {
		var $restoreLayer = $('<div id="restore_basket_layer"/>');
		$("#overall").append($restoreLayer);
		var $spaceLeft = ($("#overall").width() - $("#restore_basket_layer").width()) / 2;
		$("#restore_basket_layer").css("left", $spaceLeft);
		$("#restore_basket_layer").delay(500).fadeIn();
		$("#restore_basket_layer .buttonBar a.delete").live("click", function(){
			$.get($("a.delete").attr("data-url"));
			closeRestoreBasketLayer();
			if (tracker55) {
				tracker55.fb("Abgebrochener_Warenkorb", "loeschen", "Click_Button");
			}
		});
		$("#restore_basket_layer .buttonBar a.keepInBasket").live("click", function(){
			$.get($("a.keepInBasket").attr("data-url"));
			closeRestoreBasketLayer();
			if (tracker55) {
				tracker55.fb("Abgebrochener_Warenkorb", "im_Warenkorb_behalten", "Click_Button");
			}
		});
		$("#restore_basket_layer #header a.keepInBasket").live("click", function(){
			closeRestoreBasketLayer();
			if (tracker55) {
				tracker55.fb("Abgebrochener_Warenkorb", "schliessen", "Click_Button");
			}
		});
		$.get($("meta[property=rf:last-basket]").attr("content"));
		$("#restore_basket_layer").live("click", function(event){
			event.stopPropagation();
		});
		$("body").live('click',function (e) {
			if(!e.isPropagationStopped())
				closeRestoreBasketLayer();
		});
	}
}


function closeRestoreBasketLayer(){
	$("#restore_basket_layer").fadeOut();
    $("body").unbind('click',closeRestoreBasketLayer);
}

