function initInputAlt() {
	/* show description layer for forms */
	$('input[type!=image][alt]').focus(function() {

		var position    = $(this).offset();
		var pv_top      = position.top;

		if($(this).attr('alt').length > 0) {

                        // @since 2010/03/26 fixed #1459 body being flooded with description divs
                        $('div.descwrapper').remove();

                        $('body').append('\
				<div class="descwrapper" style="top: ' + pv_top + 'px;">\n\
					<div class="descr">'+
						$(this).attr('alt')
					+'</div>\n\
				</div>'
			);

			$('div.descwrapper').show();
		}

	});

	/* hide description layer for forms */
	$('input[alt]').blur(function() {
		$('div.descwrapper').remove();
	});
}

/* load the program teaser */
function loadProgramTeaser() {

	var url = $('#showteaser').attr('action');
	var showId = '';

	if ($('#showteaser .showteaser_day').val() == 'tomorrow') {
		showId = $('#showteaser_showId_tomorrow').val();
	} else {
		showId = $('#showteaser_showId_today').val();
	}
	url = url + '/showId/' + showId;

	$.ajax({
		type: "GET",
		url:  url,
		beforeSend: function(){
		},
		success: function(data){
			$('#program-teaser .bd').html(data);
			$("#program-teaser .bd").css("top", "0px");
			$('.program-teaser #sl-top').addClass('inactive');
			$('.program-teaser #sl-bottom').removeClass('inactive');
		}
	});

}


function isIE() {
    if(navigator.userAgent.match(/MSIE \d\.\d+/))
        return true;
    return false;
}

function productNavigationWorkaround(){
    width = new Array(71, 71, 126, 122, 104, 71, 126, 115, 81, 103);
    var count = 0;
    if (isIE7) {
        $('#product-navigation li').each(function() {
            $(this).css('width', width[count]);
            $('a' , this).css('width', width[count]);
            $('a > span' , this).css('width', width[count]);
            $('a > span' , this).css('background', 'none');

            if($(this).hasClass('first') && $(this).hasClass('active')){
                $('a' , this).before('<img class="beginning" src="lib/portal/css/img/product-navigation-active-first.png" />');
            }
			else if($(this).hasClass('last') && $(this).hasClass('active')){
                $('a' , this).before('<img class="closure" src="lib/portal/css/img/product-navigation-active-last.png" />');
            }
			else if(!$(this).hasClass('last')){
                $('a' , this).after('<img class="border" src="lib/portal/css/img/product-navigation-li.png" />');
            }

            count ++;
        });
    }
}

function zIndexWorkaround() {
    if(isIE()) {
        var zIndexNumber = 1000;
	    $('div').each(function() {
	        $(this).css('zIndex', zIndexNumber);
	        zIndexNumber -= 10;
	    });

		$('.shoppingcart').css('zIndex', 1000);
		$('.iframewrapper').css('zIndex', 1000);
		$('.previewwrapper').css('zIndex', 1000);
		$('.pr-discount-double').css('z-index', 1000);
		$('.pr-discount-single').css('z-index', 1000);
    }
}

$(document).ready(function() {

    zIndexWorkaround();
    productNavigationWorkaround();
	initInputAlt();
	
	/* make complete shoppingcart teaser clickable */
	$('#branding .shoppingcart').click(function() {
		window.location = $(this).attr('href');
	});

	/* make complete shoppingcart teaser clickable */
	$('#branding .shoppingcart').click(function() {
		window.location = $(this).attr('href');
	});

 	/* Tab loading */
	$('a.tabcontainer').click(function() {

 		$('#ladeAnimation').show();

 		// Define default product parameter
 		var productIdParameter = '';

 		// Check if actual product id set
 		if (typeof(flexFunctions.actualProductId) != 'undefined') {
			productIdParameter += '&productDetails=' + flexFunctions.actualProductId;
		}

 		var id = $(this).attr('id');
 		$.get(
 			$(this).attr('href') + productIdParameter,
 			{},
 			function(data){

 				$('ul.tabbed li').removeClass('active');
 				$('ul.tabbed li#li_' + id).addClass('active');
	 			$('#tabContent').html(data);
	 			$('#ladeAnimation').hide();
	 		}
	 	);
		return false;

 	});

	/* toogle delivery fieldsets */
	$('input[type="radio"][name="Lieferoption"]').focus(function() {
		switch(this.value) {
			case 'on':
				$("#deploymentAddressDHL").css('display','none');
				$("#deploymentAddress").css('display','none');
			break;
			case 'LieferadressePrivat':
				$("#deploymentAddressDHL").css('display','none');
				$("#deploymentAddress").css('display','block');
			break;
			case 'LieferadresseStation':
				$("#deploymentAddressDHL").css('display','block');
				$("#deploymentAddress").css('display','none');
			break;
		}
	});

	$('.popup320250').click(function() {
		window.open($(this).attr('href'),$(this).val(),"width=320,height=250,scrollbars=yes,resizable=yes,statusbar");
		return false;
	});

	/****************************/
	/* Sales Promotion Tracking */
	/****************************/

	// get campaign from hash
	if (window.location.hash.indexOf('#wid=') == 0) {
		sptWidNew = window.location.hash.substr(5);
	}

	// set given campaign
	if (sptWidNew.length==9) {

		var sptCampaign = new Array();
		sptCampaign['wid'] = sptWidNew;
		sptCampaign['lifetime'] = sptWidLifeTime;
		sptCampaign['highprio'] = 0;

		// walk configured campaigns
		for (var i=0; i<sptWidCampaigns.length; i++) {
			if (sptWidNew==sptWidCampaigns[i]['wid']) {

				// set configured campaigns
				sptCampaign['lifetime'] = sptWidCampaigns[i]['lifetime'];
				sptCampaign['highprio'] = sptWidCampaigns[i]['highprio'];
				break;
			}
		}

		// check if campaign has higher or equal prio
		if (sptCampaign['highprio']==1 || sptWidCurrentPrio==0) {

			// set new campaign
			var p1  = window.location.hostname.lastIndexOf(".");
			var tld = window.location.hostname.substring(p1+1);
			var sub = window.location.hostname.substring(0,p1);
			var p2  = sub.lastIndexOf(".");
			var dom = window.location.hostname.substring(p2+1);
			$.cookie(sptWidCookieName, sptCampaign['wid'], { domain: dom, path: '/', expires: parseInt(sptCampaign['lifetime']) });
		}
	}

	// move program teaser to bottom
	$(".program-teaser #sl-bottom").click(function(){

		if ($(".program-teaser #sl-bottom").hasClass('inactive')) {
			return false;
		}
		if ($(".program-teaser #sl-bottom").hasClass('click')) {
			return false;
		}

		$(".program-teaser #sl-bottom").addClass('click');

		var position = $("#program-teaser .bd").position();
		var view_height = $("#program-teaser .bd").height() + 8;
		var top = view_height;

		count = $("#program-teaser .bd .program-teaser-pr").length;
		height = count * 90;

		if (height <= (-1 * position.top) + 2 * view_height) {
			top = height - (view_height + (-1 * position.top));
		}

		$("#program-teaser .bd").animate({"top": "-="+top+"px"}, "slow", 'linear', function() {
			$(".program-teaser #sl-bottom").removeClass('click');
		});

		if (height <= (-1 * position.top) + 2 * view_height) {
			$(".program-teaser #sl-bottom").addClass('inactive');
			$(".program-teaser #sl-top").removeClass('inactive');
		} else {
			$(".program-teaser #sl-top").removeClass('inactive');
		}

	});

	// move program teaser to top
	$(".program-teaser #sl-top").click(function(){

		if ($(".program-teaser #sl-top").hasClass('inactive')) {
			return false;
		}
		if ($(".program-teaser #sl-top").hasClass('click')) {
			return false;
		}

		$(".program-teaser #sl-top").addClass('click');

		var position = $("#program-teaser .bd").position();
		var view_height = $("#program-teaser .bd").height() + 8;
		var top = view_height;

		count = $("#program-teaser .bd .program-teaser-pr").length;
		height = count * 90;

		if (height + position.top < view_height) {
			top = height + position.top - view_height;
		}

		if ((position.top + top) > -90) {
			$("#program-teaser .bd").animate({"top": "0px"}, "slow", 'linear', function() {
				$(".program-teaser #sl-top").removeClass('click');
			});
			$(".program-teaser #sl-top").addClass('inactive');
			$(".program-teaser #sl-bottom").removeClass('inactive');
		} else {
			$("#program-teaser .bd").animate({"top": "+="+top+"px"}, "slow", 'linear', function() {
				$(".program-teaser #sl-top").removeClass('click');
			});
			$(".program-teaser #sl-bottom").removeClass('inactive');
		}

	});

	$(".showteaser_day").change(function() {
		if ($(".showteaser_day").val() == 'tomorrow') {
			var selectSub = $('#showteaser_showId_today option:selected').text().substring(0, 6);
			$('#showteaser_showId_tomorrow option').each(function(index) {

				if ($(this).text().substring(0, 6) == selectSub) {
					$('#showteaser_showId_tomorrow').val($(this).val());
					return;
				}
  			});
			$('#showteaser_showId_today').hide();
			$('#showteaser_showId_tomorrow').show();
		} else {
			var selectSub = $('#showteaser_showId_tomorrow option:selected').text().substring(0, 6);
			$('#showteaser_showId_today option').each(function(index) {

				if ($(this).text().substring(0, 6) == selectSub) {
					$('#showteaser_showId_today').val($(this).val());
					return;
				}
  			});
			$('#showteaser_showId_tomorrow').hide();
			$('#showteaser_showId_today').show();
		}

		loadProgramTeaser();
	});

	$(".showteaser_show").change(function() {
		loadProgramTeaser();
	});

	$('.program-link-navi .morelink').click(function() {

		var showId = $(this).attr('rel');
		if ($(".showteaser_day").val() == 'tomorrow') {
			$('#showteaser_showId_tomorrow').val(showId);
		} else {
			$('#showteaser_showId_today').val(showId);
		}
		loadProgramTeaser();

		return false;
	});


	/**********************/
	/* Debugging Handling */
	/**********************/

	if (self.location.href.search(/[a-z]{2,3}\.dev\./) == -1) {
		if (typeof self.console == 'undefined' || typeof console.firebug == 'undefined') {
			var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
			self.console = {};
			for (var i = 0, len6 = names.length; i < len6; ++i) {
		    	self.console[names[i]] = function() {}
			}
		}
	}
	else {
		if (typeof self.console == 'undefined') {
			var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
			self.console = {};
			for (var i = 0, len7 = names.length; i < len7; ++i) {
				self.console[names[i]] = function() {
					for (var i = 0, len8 = arguments.length; i < len8; i++) {
						alert(arguments[i]);
					}
				}
			}
		}
	}


});
