app = {
	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'
	    });
	  }
	}
};

$(document).ready(function(){

	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();
  });

	// Disabled options
  disableOptions();
	
  if (typeof Shadowbox !== 'undefined') {
    Shadowbox.init({
      overlayOpacity: 0.5,
      onFinish: app.shadowbox.togglePrintButton
    });
  }
});

function disableOptions() {
  $('select').each(function(){
    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();
    });
  });
}


/** 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").attr("value", 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