$(document).ready(function(){

	/**
	 * Javascript to be run once the DOM has been loaded.
	 *
	 * ONLY used on choose tariff page.
	 *
	 */

	/* IE6 compatible alignment */
	$("div.showOptions div.column").equalHeights();
	$(".showOptions .last div.product-price").css({
		position:"absolute"
	});

	/* Set equal height to various tariff overview boxes */
	$("#plan-overview .plan-top").equalHeights();
	$("#plan-overview .plan-availability").equalHeights();
	$("#plan-overview .plan-cellphone-price").equalHeights();
	$("#plan-overview .plan-extra").equalHeights();
	$("#plan-overview .plan-billingIncrement").equalHeights();
	$("#plan-overview .plan-discounts").equalHeights();
	$("#plan-overview .price .priceTitle").equalHeights();
	$("#plan-overview .price .fixedPrice").equalHeights();
	$("#plan-overview .price .connectionFee").equalHeights();
	$("#plan-overview .price .monthlyDevicePrice").equalHeights();
	$("#plan-overview .price .tariffduration").equalHeights();
	$("#plan-overview .price strong").equalHeights();
	$("#plan-overview .plan-announcement ul:first-child").equalHeights();
	$("#plan-overview .plan-announcement ul:nth-child(2)").equalHeights();
	$("#plan-overview .plan-announcement ul:nth-child(3)").equalHeights();
	$("#plan-overview .plan-announcement ul:nth-child(4)").equalHeights();
	$("#plan-overview .plan-announcement ul:nth-child(5)").equalHeights();
	$("#plan-overview .plan-announcement .summary").equalHeights();
    $("#plan-overview .total-price .checklist").equalHeights();
    $("#plan-overview .total-price .priceTag").equalHeights();
    $("#plan-overview .plan-announcement").equalHeights();
    $("#plan-overview .plan-promotion").equalHeights();
    $(".vvl .conclusion div.divided").equalHeights();
	$("#plan-overview .plan-goodies").equalHeights();

    
    $(".vvl .conclusion").css({
    	height: $(".vvl .conclusion div.divided").css("min-height")
    });

	/* Align former price info to the bottom of the announcement column */
	$("#plan-overview .plan-announcement").css({
		/* Setting the height is needed only for FF2 */
		height:$("#plan-overview .plan-announcement").css("min-height"),
		position:"relative"
	});
	$("#plan-overview .plan-announcement div").css({
		/* Setting the width is needed only for IE */
		width:$("#plan-overview .plan").width(),
		position:"absolute",
		bottom:"0px",
		left:"0px"
	});

	/* Calculating the min-height for the inner div must be the last action, */
	/* as it's height may be changed by the operations before */
	$("#plan-overview .plan-announcement div").equalHeights();

	/* Fix for the 1px displacement bug */
	$("div.plan-goodies select").css("margin-bottom","-1px");

	/* Disable tariff selection buttons in case of prolongation login form being displayed */
	if($("div#enterMsisdnDialog, div#enterMsisdnDialogPremium, div#selectCurrentTariffDialog").length > 0) {
		$("div#plan-overview div.actions a.plan-button-choose").each(function() {
	        $(this).inlinePopup({
	            isDiv : true,
	            targetUrl : "#prolongationDialogInfoPopupLoginFirst",
	            isButtonBarTop : false,
	            isButtonBarBottom : false,
	            isPrintButtons : false,
	            isRefocussing : false
	        });
	        $(this).bind("click", function() {
	            if (!($.browser.msie && $.browser.version.substr(0,1) == "6")) {
	                document.location.href = '#contractDialogBox';
	            }
	        });
		});
	}

	/* Avoid possible tariff form submission once BTX has been clicked (especially SuperflatFamily) */
    $("form#businessTransactionForm input").bind("click", function() {
        $("div#plan-overview a.plan-button-choose").hide();
    });



	/*
	 *
	 * Make upper part of tariff columns fully clickable.
	 * Lower part MUST not be, since it has it's own interaction elements.
	 *
	 *
	 */
    $("div#plan-overview div.plan div.recommendation-indicator," +
    		"div#plan-overview div.plan div.selected-indicator," +
    		"div#plan-overview div.plan div.prolongation-indicator," +
    		"div#plan-overview div.plan div.plan-details," +
    		"div#plan-overview div.plan div.plan-top," +
    		"div#plan-overview div.plan div.plan-availability," +
    		"div#plan-overview div.plan div.plan-goodies," +
    		"div#plan-overview div.plan div.plan-announcement").each(function() {
		$(this).css("cursor", "pointer");
    	$(this).bind("click", function() {
    		$(this).parent("div.plan").find("div.total-price a.plan-button-detail").parents("form").submit();
        });
    }).bind("mouseover", function() {
    	$(this).parents("div.plan").addClass("hovered");
    }).bind("mouseout", function() {
    	$(this).parents("div.plan").removeClass("hovered");
    });



	/*
	 *
	 * ButtonSelection for BTX/Goodie selection.
	 *
	 */

	// Enable fancy button layout for all according radio buttons
    $("form fieldset.buttonSelection div.buttonField").each(function() {

        // Mark area to be js-enabled version
        $(this).parents("fieldset.buttonSelection").addClass("buttonSelectionDynamic");

	    // Check whether option is selected and mark DIVs accordingly
        var classSelected = ($(this).children("label").children("input").attr("checked") ? 'selected' : '');
        var infoExists = ($(this).next("div.infoField").text() != '');
        $(this).addClass(classSelected);
        if (infoExists) {
        	$(this).next("div.infoField").addClass(classSelected);
        }

        // Add fancy button, arrow and hide label/radio-selection
        $(this).children("label").hide();
        $(this).append('<a href="#" class="aButton bgComprehensive ' + classSelected + '"><span><b>' + $(this).children("label").text() + '</b></span></a>');
        if (infoExists) {
            $(this).append('<span class="arrow"></span>');
        }

    });

    // Hide all info fields, except for selected option one's
    $("form fieldset.buttonSelection div.infoField").hide();
    $("form fieldset.buttonSelection div.infoField.selected").show();

    // Hide all arrows, except for selected option one's
    $("form fieldset.buttonSelection div.buttonField span.arrow").hide();
    $("form fieldset.buttonSelection div.buttonField.selected span.arrow").show();

    // Bind form submission to buttons and hide old-fashioned submit button
    $("form fieldset.buttonSelection div.submitField").hide();
    $("form fieldset.buttonSelection div.buttonField a.aButton").bind("click", function() {

        $(this).prev("label").children("input[@type=radio]").attr('checked', true);
        $(this).parents("form").submit();

    });

    // Bind mouseover event to buttons for showing info fields
    $("form fieldset.buttonSelection div.buttonField").bind("mouseover", function() {
    	// Hide all info and button fields
        $(this).siblings("div.infoField").hide();
        $(this).siblings("div.buttonField").children("span.arrow").hide();
    	// Display current info field in case it is not empty
    	if ($(this).next("div.infoField").text()) {
	        $(this).next("div.infoField").show();
	        $(this).children("span.arrow").show();
    	}
    });
    $("form fieldset.buttonSelection div.buttonField").bind("mouseout", function() {
    	// Hide all info fields except for DEFAULT SELECTED one
        $(this).siblings("div.infoField").hide();
        $(this).siblings("div.infoField.selected").show();
    	// Hide all arrows except for DEFAULT SELECTED one
        $(this).parent().children("div.buttonField").children("span.arrow").hide();
        $(this).parent().children("div.buttonField.selected").children("span.arrow").show();
    });

    // Display feature. Whole feature has been hidden in JSP to avoid flash of fallback-version
    // in case of page loading too slow.
    $("form fieldset.buttonSelection").show();

    // Set height of info fields to size of largest info field
    var maxHeight = 0;
    $("form fieldset.buttonSelection div.infoField").each(function() {
    	if ($(this).height() > maxHeight) {
    		maxHeight = $(this).height();
    	}
    });
    $("form fieldset.buttonSelection div.infoField").css("height", maxHeight);
	$("form fieldset.buttonSelection").css("height", maxHeight);

    // Set size of overall buttonSelection at least to largest infoField
	$("form fieldset.buttonSelection").equalHeights();

	/*
	 *
	 * Prolongation prepopulation forms
	 *
	 */

//	// hide manual fields, if prepopulated list is available but show them in error case
//	if ($("select#prepopulatedMsisdn").length) {
//		var selectedValue= $(this).find(":selected").val();
//		if (selectedValue == "-2") {
//			$("div.error").parent().show();
//		} else {
//			$("#VorwahlFS, #RufnummerFS").hide();
//		}
//	}

	// if javascript is enabled show change dialog
	if ($("div.changeNumber").length) {
		$("div.selectNumber").hide();
		$("div.changeNumber").show();
	}

	// on click show select box
	$("div.selectNumber div.actions").hide();
	$("a#changeNumber").bind("click", function() {
	  $("div.changeNumber").hide();
	  $("div.selectNumber").fadeIn(800);
	 });

	// if javascript is enabled show change dialog
	//$("div.selectNumber").hide();
	//$("div.changeNumber").show();

	// on click show select box
	$("a#changeNumber").bind("click", function() {
		$("div.changeNumber").hide();
		$("div.selectNumber").fadeIn(800);
	});
	
	
	 // bind onChange to select box
	 $("div.selectNumber select").bind("change", function () {
	  $("form#SelectMsisdn").submit();
	 });

	 //
	 // TARIFFRAMP ONLY ACTIONS
	 //
	 
	 // hide the collapse button when the panel can not be opened
	 if ($('#actionRHN table tbody tr').length <= 2){
		 $('#actionRHN div.collapser').css('display','none');
	 }
	 
	 // hide all items from the RHN aka Step-By-Step, except for the cart-button
	 $('#actionRHN tr').not('.cart').css('display','none');
	 
	 // now bring back all items which start with a number, normaly only 'Handy', 'Tariff' and 'Options'
	 $('#actionRHN .number').parents('tr').css('display','');
	 
	 // also, move the cart-button to a hidden div which is only used on this page (tarifframp)
	 $('#actionRHN .cart td').children().appendTo($('.cartHolder'));
	
	 // get the size of the teaser image and safe it for all the heights and important functions
	 var trTeaserImageSize = $('.tariffRampTeaser img').height() -1;
	 // set the size of the RHN to fit the teaser-banner
	 $('#actionRHN fieldset').css({ height: trTeaserImageSize + "px" });
	 
	 // by this class we know that we are on the tarifframp page
	 $('#page-tariff-overview').addClass('tarifframp');

	 // hide/collapse the RHN when mouse over for tarifframp
	 $('#actionRHN #collapserButton').bind('click', function() {
		 
		 if ($(this).hasClass('iconUp')){
			$(this).addClass('iconDown').removeClass('iconUp');

			// get the cart-button back to position
			$('#actionRHN fieldset').removeClass('cartPadding');
			// remove the anchor
			$('#actionRHN fieldset').removeClass('activeHover');
			// hide all elements on the RHN
			$('#actionRHN tr').not('.cart').css('display','none');
			// again, bring back the numbered items
			$('#actionRHN span.number').parents('tr').css('display','');	 		
	 	} else {
			$(this).addClass('iconUp').removeClass('iconDown');

			// a anchor so we know the popup is active
			$('#actionRHN fieldset').addClass('activeHover');
			// bring back all hidden items
			$('#actionRHN tr').css('display','');
			// ugly hack to move the cart-button to the correct location
			if ($('#actionRHN').height() > (trTeaserImageSize + 20)){
				 $('#actionRHN fieldset').addClass('cartPadding');
			}
	 	}
		$(this).blur();
		return false;
	 });
	 
	 
	 /* Click handler to switch from warning message to menu display */
	$('#switch-to-menu').click(function(){
		$('#warning-message').toggle();
		$('#menu-structure').toggle();
		return false;
	});
	
	$("#plan-overview .plan-goodies").equalHeights();
	
});
