function log(l){
	try {
		console.log(l);
	} 
	catch (e) {}
}

String.prototype.trim = function(){
	var x = this;
	x = x.replace(/^\s*(.*)/, "$1");
	x = x.replace(/(.*?)\s*$/, "$1");
	return x;
}

var gapaCore = {
	// debug
	debug: false,
	
	cookie_options: {
		path: '/'
	},
	
	logger: function(value, title){
		msg = title + " : " + value;
		if (this.debug) {
			try {
				console.log(msg);
			} 
			catch (e) {}
		}
	},
	
	// keyvisual
	closedKeyvisualHeight: '20px',
	openedKeyvisualHeight: '267px',
	halfsizedKeyvisualHeight: '150px',
	keyvisualAnimTempo: 600,
	animationEasing: 'swing',
	relocateTimeout: 800,
	
	closeKeyvisual: function(locationHref){
		if (typeof(locationHref) != "string") {
			locationHref = null;
		}
		
		$('#coreBottom').fadeOut('slow');
		$('#coreImage').animate({
			height: this.closedKeyvisualHeight
		}, this.keyvisualAnimTempo, this.animationEasing, function(){
			$('#coreImage').css({
				'visibility': 'hidden'
			});
			setTimeout("gapaCore.relocate('" + locationHref + "')", this.relocateTimeout);
		});
	},
	
	openKeyvisual: function(){
		$('#coreImage').animate({
			height: this.openedKeyvisualHeight
		}, this.keyvisualAnimTempo, this.animationEasing);
	},
	
	relocate: function(locationHref){
		if (gapaCore.debug) {
			return false;
		}
		try {
			window.location = locationHref;
		} 
		catch (e) {
		}
	},
	
	halfsizeKeyvisual: function(locationHref, skipAnimation){
		if (typeof(locationHref) != "string") {
			locationHref = null;
		}
		$('#coreTeaser').animate({
			height: 0,
			opacity: 0
		}, 500, 'swing');
		$('#coreBottom').css({
			'visibility': 'visible'
		});
		$('#coreBottom').fadeIn('slow')
		$('#coreImage').css({
			'visibility': 'visible'
		});
		if (!skipAnimation) {
			$('#coreImage').animate({
				height: this.halfsizedKeyvisualHeight
			}, this.keyvisualAnimTempo, this.animationEasing, setTimeout("gapaCore.relocate('" + locationHref + "')", this.relocateTimeout));
		}
		else {
			gapaCore.relocate(locationHref);
		}
	},
	
	setFontSize: function(size){
		$('body').removeClass('moreAccessable');
		$('body').removeClass('moreAccessableBig');
		$('body').removeClass('moreAccessableBigger');
		$('body').removeClass('moreAccessableReset');
		
		$('body').addClass('moreAccessableReset');
		
		if (size != 'Normal') {
			$('body').removeClass('moreAccessableReset');
			$('body').addClass('moreAccessable');
			newClass = 'moreAccessable' + size;
			$('body').addClass(newClass);
		}
		
		$.cookie('gapa_accessibility_size', size, gapaCore.cookie_options);
		
		var iFrame = $('#shopFrame');
		var iFrameSrc = iFrame.attr('src');
		var postMsg = 'moreAccessable' + size;
		
		if (iFrame.get(0)) {
			$.postMessage(postMsg, iFrameSrc, iFrame.get(0).contentWindow);
		}
		
	},
	
	setContrast: function(value){
		$('body').toggleClass('moreContrast');
		
		if ($('body').hasClass('moreContrast')) {
			$.cookie('gapa_accessibility_contrast', 'on', gapaCore.cookie_options);
		}
		else {
			$.cookie('gapa_accessibility_contrast', 'off', gapaCore.cookie_options);
		}
		
		var iFrame = $('#shopFrame');
		var iFrameSrc = iFrame.attr('src');
		var postMsg = $('body').hasClass('moreContrast') ? 'moreContrast' : 'noMoreContrast';
		
		if (iFrame.get(0)) {
			$.postMessage(postMsg, iFrameSrc, iFrame.get(0).contentWindow);
		}
		
	}
}


var PaginationControl = {

	pages: [],
	
	pagelinks: [],
	
	activepage: 0,
	
	createPagination: function(){
	
		this.pages = $("div.contentRight:not(.noPaginatedContent) div.contentInnerTwoCol.paginatedContent");
		
		if (this.pages.length <= 0) {
			return;
		}
		
		$("div.contentRight:not(.noPaginatedContent) div.contentInnerTwoCol.paginatedContent:first").prepend('<a name="pagingTop"></a>');
		
		$("div.contentRight:not(.noPaginatedContent) div.contentInnerTwoCol.paginatedContent:not(:first)").hide();
		var pagination = $('<li class="previous off">«</li>');
		
		// return;
		
		$("ul.pagination").html(pagination);
		
		for (var i = 0; i < this.pages.length; i++) {
			// <li class="active"><a href="#pagingTop">1</a>|</li>
			
			var link = $('<li><a href="#pagingTop">' + (i + 1) + '</a></li>');
			
			if (i == 0) {
				link.addClass("active");
			}
			
			if (i < this.pages.length - 1) {
				$("a", link).after("|");
			}
			
			$("a", link).attr('page', i).click(function(){
				PaginationControl.gotoPage($(this).attr('page'));
			});
			
			this.pagelinks[i] = link;
			
			$("ul.pagination").append(link);
		}
		
		var nextLink = $('<li class="next"><a href="#pagingTop">»</a></li>');
		
		$("a", nextLink).attr('page', i).click(function(){
			PaginationControl.gotoPage(PaginationControl.activepage + 1);
		});
		
		$("ul.pagination").append(nextLink);
	},
	
	gotoPage: function(target){
		log("Target: " + target);
		if (target < 0) 
			target = 0;
		if (target > this.pages.length - 1) 
			target = this.pages.length - 1;
		
		var newPage = null;
		
		try {
			newPage = this.pages[target];
		} 
		catch (e) {
			return;
		}
		
		this.activepage = parseInt(target);
		
		// Update prev/next links
		var nextLink = $('<a href="#pagingTop">»</a>');
		var prevLink = $('<a href="#pagingTop">«</a>');
		
		if (target == 0) {
			if (!$("ul.pagination .previous").hasClass("off")) {
				$("ul.pagination .previous").addClass("off");
			}
			
			prevLink = "«";
		}
		else {
			$("ul.pagination .previous").removeClass("off");
			prevLink.click(function(){
				PaginationControl.gotoPage(PaginationControl.activepage - 1);
			});
		}
		
		if (target == this.pages.length - 1) {
			if (!$("ul.pagination .next").hasClass("off")) {
				$("ul.pagination .next").addClass("off");
			}
			
			nextLink = "»";
		}
		else {
			$("ul.pagination .next").removeClass("off");
			nextLink.click(function(){
				PaginationControl.gotoPage(PaginationControl.activepage + 1);
			});
		}
		
		$("ul.pagination .next").html(nextLink);
		$("ul.pagination .previous").html(prevLink);
		
		
		for (var i = 0; i < this.pagelinks.length; i++) {
			if (i == this.activepage) {
				if (!$(this.pagelinks[i]).hasClass("active")) {
					$(this.pagelinks[i]).addClass("active")
				}
			}
			else {
				$(this.pagelinks[i]).removeClass("active")
			}
		}
		
		try {
			$("div.contentInnerTwoCol.paginatedContent").hide();
			$(newPage).show();
		} 
		catch (e) {
		}
		
	}
	
	
};

// on document ready
$(document).ready(function(){

	// toolbar
	// todo: move to a own class/object
	var toolbarfadeInTime = 350;
	var toolbarSlideDownTime = 250;
	
	$("#serviceArea ul li span a").bind("mouseover", function(e){
	
		var tmpObj1 = $(this).parent().parent();
		
		tmpObj1.parent().find('li .title').hide();
		tmpObj1.find('.title').css('opacity', 1);
		tmpObj1.find('.title').animate({
			opacity: 'show'
		}, {
			queue: false,
			duration: toolbarfadeInTime
		});
		
		return false;
		
	});
	$("#serviceArea ul li span a").bind("mouseout", function(e){
	
		var tmpObj1 = $(this).parent().parent();
		
		itemHasActiveClassName = tmpObj1.attr('class').split(' ').slice(-1) == 'active';
		
		if (!itemHasActiveClassName) {
			tmpObj1.find('.title').hide();
			
			tmpObj2 = tmpObj1.parent().find('li.active .title');
			tmpObj2.css('opacity', 1);
			tmpObj2.animate({
				opacity: 'show'
			}, {
				queue: false,
				duration: toolbarfadeInTime
			});
		}
		
		return false;
		
	});
	$("#serviceArea ul li span a").bind("click", function(e){
	
		tmpObj1 = $(this).closest('li');
		
		tmpArr = tmpObj1.attr('class').split(' ');
		itemHasActiveClassName = tmpArr.slice(-1) == 'active';
		
		if (!itemHasActiveClassName) {
			tmpObjX = tmpObj1.parent().find('li');
			tmpObjX.find('.content').stop();
			tmpObjX.find('.title').stop();
			tmpObjX.find('.content').hide();
			tmpObjX.find('.title').hide();
			tmpObjX.removeClass('active');
			
			tmpObj1.addClass('active');
			tmpObj1.find('.content').css('opacity', 1);
			tmpObj1.find('.content').css('height', 'auto');
			if (!tmpObj1.find('> div').hasClass('shop') && !tmpObj1.find('> div').hasClass('watchlist')) {
				tmpObj1.find('.content').animate({
					opacity: 'show',
					height: 'show'
				}, {
					queue: false,
					duration: toolbarSlideDownTime
				});
			}
			else {
				tmpHref = $(this).attr('href');
				tmpObj1.find('.content').animate({
					opacity: 'show',
					height: 'show'
				}, toolbarSlideDownTime, function(){
					window.location = tmpHref;
				});
			}
			
			tmpObj1.find('.title').css('opacity', 1);
			tmpObj1.find('.title').show();
		}
		else {
			tmpObj1.removeClass('active');
			tmpObj1.find('.content').css('opacity', 1);
			tmpObj1.find('.content').css('height', 'auto');
			tmpObj1.find('.content').animate({
				opacity: 'hide',
				height: 'hide'
			}, {
				queue: false,
				duration: toolbarSlideDownTime
			});
			
			tmpObj1.find('.title').css('opacity', 1);
		}
		
		return false;
	});
	$("#serviceArea ul li .content .close a").bind("click", function(e){
	
		tmpObj1 = $(this).closest('li');
		
		itemHasActiveClassName = tmpObj1.attr('class').split(' ').slice(-1) == 'active';
		
		if (itemHasActiveClassName) {
			tmpObj1.removeClass('active');
			tmpObj1.find('.content').css('opacity', 1);
			tmpObj1.find('.content').css('height', 'auto');
			tmpObj1.find('.content').animate({
				opacity: 'hide',
				height: 'hide'
			}, {
				queue: false,
				duration: toolbarSlideDownTime
			});
			
			tmpObj1.find('.title').css('opacity', 1);
			tmpObj1.find('.title').hide();
		}
		
		return false;
		
	});
	
	
	// SKI-WM FORM
	if ($("div.form #fOption1").attr('checked') == false) {
		$("div.form .fOption1 .hideIt").hide();
		$("div.form .fOption1 .open").show();
		$("div.form .fOption1 .close").hide();
	}
	else {
		$("div.form .fOption1 .open").hide();
		$("div.form .fOption1 .close").show();
	}
	if ($("div.form #fOption2").attr('checked') == false) {
		$("div.form .fOption2 .hideIt").hide();
		$("div.form .fOption2 .open").show();
		$("div.form .fOption2 .close").hide();
	}
	else {
		$("div.form .fOption2 div.open").hide();
		$("div.form .fOption2 div.close").show();
	}
	$("div.form .openOption1").bind("click", function(e){
		$("div.form #fOption1").attr('checked', 'checked');
		$("div.form .fOption1 .hideIt").show();
		$("div.form .fOption2 .hideIt").hide();
		$("div.form .fOption1 .open").hide();
		$("div.form .fOption1 .close").show();
		$("div.form .fOption2 .open").show();
		$("div.form .fOption2 .close").hide();
	});
	$("div.form .openOption2").bind("click", function(e){
		$("div.form #fOption2").attr('checked', 'checked');
		$("div.form .fOption2 .hideIt").show();
		$("div.form .fOption1 .hideIt").hide();
		$("div.form .fOption2 .open").hide();
		$("div.form .fOption2 .close").show();
		$("div.form .fOption1 .open").show();
		$("div.form .fOption1 .close").hide();
	});
	$("div.form .close").bind("click", function(e){
		$("div.form #fOption1").attr('checked', '');
		$("div.form #fOption2").attr('checked', '');
		$("div.form .fOption1 .hideIt").hide();
		$("div.form .fOption2 .hideIt").hide();
		$("div.form .fOption1 .open").show();
		$("div.form .fOption1 .close").hide();
		$("div.form .fOption2 .open").show();
		$("div.form .fOption2 .close").hide();
	});
	
	
	
	
	
	
	
	// pagination control
	PaginationControl.createPagination();
	
	/*
	 * CORE START TEASER
	 */
	$("#coreTeaser div.inner1").each(function(i){
		$(this).css({
			'opacity': 1
		});
	});
	$("#coreTeaser div.inner2").each(function(i){
		$(this).css({
			'opacity': 0
		});
	});
	$("#coreTeaser div.inner1").mouseover(function(){
		$(this).addClass('hoverState');
	});
	$("#coreTeaser div.inner1").mouseout(function(){
		$(this).removeClass('hoverState');
	});
	$("#coreTeaser div.inner2 p.more").mouseover(function(){
		$(this).parent().addClass('hoverState');
	});
	$("#coreTeaser div.inner2 p.more").mouseout(function(){
		$(this).parent().removeClass('hoverState');
	});
	$("#coreTeaser div.inner2 h1 a").mousedown(function(){
		startTeaserObj = $(this).parent().parent().parent();
		inner1Obj = startTeaserObj.find('div.inner1');
		inner2Obj = $(this).parent().parent();
		
		inner2Obj.stop();
		inner2Obj.animate({
			opacity: 'hide'
		}, {
			queue: false,
			duration: gapaCore.keyvisualAnimTempo * 0.75
		});
		
		inner1Obj.stop();
		inner1Obj.animate({
			opacity: 1
		}, {
			queue: true,
			duration: gapaCore.keyvisualAnimTempo * 0.75
		});
		
		startTeaserObj.stop();
		startTeaserObj.animate({
			width: '126px'
		}, {
			queue: false,
			duration: gapaCore.keyvisualAnimTempo * 0.75,
			easing: gapaCore.animationEasing
		});
		
		return false;
	}).click(function(){
		return false;
	});
	$("#coreTeaser div.inner1").mousedown(function(){
		$("#coreTeaser div.inner2").each(function(i){
			inner1Obj = $(this).parent().find('div.inner1');
			
			$(this).stop();
			$(this).parent().stop();
			inner1Obj.stop();
			
			checkOpacityInner1 = inner1Obj.css('opacity');
			if (checkOpacityInner1 != 1) {
				inner1Obj.animate({
					opacity: 1
				}, {
					queue: true,
					duration: gapaCore.keyvisualAnimTempo * 0.5
				});
			}
			
			checkOpacity = $(this).css('opacity');
			if (checkOpacity != 0) {
				$(this).animate({
					opacity: 'hide'
				}, {
					queue: false,
					duration: gapaCore.keyvisualAnimTempo * 0.5,
					easing: gapaCore.animationEasing
				});
			}
			
			checkWidth = $(this).parent().css('width');
			if (checkWidth != '126px') {
				$(this).parent().animate({
					width: '126px'
				}, {
					queue: false,
					duration: gapaCore.keyvisualAnimTempo * 0.5,
					easing: gapaCore.animationEasing
				});
			}
		});
		
		$(this).stop();
		$(this).next().stop();
		$(this).parent().stop();
		
		$(this).next().css({
			opacity: 0,
			'display': 'block'
		});
		$(this).next().animate({
			opacity: 1
		}, {
			queue: false,
			duration: gapaCore.keyvisualAnimTempo,
			easing: gapaCore.animationEasing
		});
		
		$(this).parent().animate({
			width: '342px'
		}, {
			queue: false,
			duration: gapaCore.keyvisualAnimTempo,
			easing: gapaCore.animationEasing
		});
		
		$(this).animate({
			opacity: 0
		}, {
			queue: false,
			duration: gapaCore.keyvisualAnimTempo * 0.5
		});
		
		return false;
	});
	
	// mainNavItem keyvisual animation
	$("a.mainNavItem").click(function(){

		navId = $(this).attr("id")
		teaserId = navId + "Teaser";
		gapaCore.logger(navId, 'navid');
		gapaCore.logger(teaserId, 'teaserid');

		skipHalfAnimation = $('#coreImage').height() > 150 || $('#coreImage').height() == 20 ? false : true;
		gapaCore.halfsizeKeyvisual(this.href, skipHalfAnimation);
		$("div.mainTeaser").hide();
		$("#" + teaserId).show();
		return false;
	});
	
	// doorpageItem keyvisual animation
	$("ul.doorPage a").click(function(){
		gapaCore.closeKeyvisual(this.href);
		return false;
	});
	
	// language switcher
	/*
	 $("#languageSwitcher").change(function() {
	 var langid = $("#languageSwitcher option:selected").val();
	 if (langid >= 0) {
	 location = 'index.html?a-Common_global-n_NewLanguageId=' + langid + '&button-Common_global-change_language=';
	 }
	 });
	 */
	// page functions
	$("li.print a").click(function(){
		window.print();
	});
	
	$("li.recommendSite a").click(function(){
		recommendPage();
		return false;
	});
	
	$("li.wishlist a").click(function(){
		$(this).parents("form:first").submit();
	});
	
	$('a.deleteAll').click(function(){
		return confirm("Möchtest Du wirklich alle Artikel löschen?");
	});
	
	
	
	// Bildarchiv: Zoom-Funktion
	$('p.zoom a').click(function(){
		var popup = $(this).parent().parent().find("img[popup]").attr('popup');
		
		if (popup && popup.length > 0) {
			tb_show(null, popup, false);
		}
		
		return false;
	});
	
	// Lupe verstecken, wenn kein Popup angegeben ist
	$('p.zoom').each(function(){
		var img = $(this).parent().find("img[popup]");
		if (!img || img.length <= 0) {
			$(this).hide();
		}
	});
	
	// Accessability Features
	$('div.accessibilityFeaturesFont a#Normal,div.accessibilityFeaturesFont a#Big,div.accessibilityFeaturesFont a#Bigger').click(function(elem){
		gapaCore.setFontSize($(this).attr('id'));
		//this.blur();
		return false;
	});
	
	$('div.accessibilityFeaturesContrast a#contrast').click(function(){
		gapaCore.setContrast();
		//this.blur();
		return false;
	});
	
	if ($.cookie('gapa_accessibility_size')) {
		gapaCore.setFontSize($.cookie('gapa_accessibility_size'));
	}
	
	if ($.cookie('gapa_accessibility_contrast') && $.cookie('gapa_accessibility_contrast') == "on") {
		gapaCore.setContrast("on");
	}
	
	$('#shopFrame').load(function(){
		var iFrame = $('#shopFrame');
		var iFrameSrc = iFrame.attr('src');
		var iFramePostMsg = '';
		
		if ($.cookie('gapa_accessibility_size')) {
			iFramePostMsg += 'moreAccessable' + $.cookie('gapa_accessibility_size');
		}
		if ($.cookie('gapa_accessibility_contrast') && $.cookie('gapa_accessibility_contrast') == "on") {
			iFramePostMsg += ';moreContrast';
		}
		
		setTimeout(function(){
			$.postMessage(iFramePostMsg, iFrameSrc, iFrame.get(0).contentWindow);
		}, 100);
	});
	
	
	// poor man's download autostart
	/*
	 var dllink = $('a.downloadBildarchiv').attr('href');
	 if (dllink) {
	 window.location = dllink;
	 }
	 */
});

// Fehler beim Merkzettelversand
function handleSendMailError(){
	window.setTimeout(function(){
		document.getElementById('basketSendMail').style.display = 'block';
		document.getElementById('basketSendMail').style.opacity = 1;
		window.location = '#ReCaptcha';
	}, 50);
}

