﻿var newHeight = -1;
var transactionTab;

$(document).ready(function () {
    //setMenuHover();
    doWalletStuff();
    /*if ($.browser.safari) {
    $(window).load(function() {
    setHeight();
    //setMenuSpacer();
    });
    } else {
    setHeight();
    //setMenuSpacer();
    }
    */
    setHeight();
    if ($.browser.msie && $.browser.version < 7) {
        $(document).pngFix();
        $("#footer .footertext ul li:first").css("border", "none");
        $("input:submit").bind("mouseenter mouseleave", function () {
            $(this).toggleClass("hover");
        });
        $("#buttons li").bind("mouseenter mouseleave", function () {
            $(this).toggleClass("hover");
        });
        $(".topmenu li").css("cursor", "pointer");
        $(".topmenu li").click(function () {
            document.location.href = $(this).find("a").attr("href");
        });
    }
    setMenuClicks();
    setVideos();
    setHalfBlockAlt();
    setAnswerToggle();
    setFancyList();
    setSearchToggle("Typ hier uw zoekterm ...");
    setHomepageFlash("home", "/include/flash/home.swf");
    setHomepageFlash("demo", "/include/flash/demo.swf");
    setLogoClick("/");
    setActiecodePopup();
    setMobileRedirect();
    setSendSMS();
    toolTip();
    $("#buttons li").click(function () {
        document.location.href = $(this).find("a").attr("href");
    });

    //focus on first page input
    $(".content .wallet input:first").focus();

    $('#step1fieldset .button_128').click(function () {
        if (Page_IsValid) {
            tb_show('Minitix Voorwaarden', '#TB_inline?height=300&width=654&inlineId=acceptDiv', false);
            return false;
        }
    });
});

function doAcceptCheck() {
    if (!$('#minitixAcceptTerms').attr('checked')) {
        $('#accept-error').show();
        return false;
    }
}

$(window).resize(function() {
    if ($.browser.mozilla) {
        updateHeight($("body"), fixBG());
    }
});
function doWalletStuff() {

    $('.transactions .description span').textOverflow('...');
    transactionTab = $('#transactiontabs > ul').tabs();
    $(".walletConfig .fullBlock").each(function(i) {
    	if (i % 2 == 0) {
    		$(this).addClass("alt");
    	}
    });
}
function setMenuSpacer() {
	// spacer = 542 - all LI's in blue
	$(".leftCloser, .leftCloserActive").next().css("paddingLeft", 0);
    var liWidth = -120;
    $("#site .content .menu .submenu li").each(function() {
        liWidth += $(this).width() + 40; // 20+20 padding
    });
      
    //console.log('ok ' + liWidth);
    if ($("#site .content .menu .orange").length > 0) {
    	$("#site .content .menu .submenu li.filler").css("width", 676 - liWidth);
    } else {
		$("#site .content .menu .submenu li.filler").css("width", 952 - liWidth);
	}
}
function setMenuHover() {
    // hover function for the blue menu items
	$("#site .content .menu .submenu li").bind("mouseenter mouseleave", function() {
        if ($(this).hasClass("filler") || $(this).hasClass("rightCloser")) {
            // do nothing
        	$(this).css("cursor", "default");

        }
        else if ($(this).hasClass("leftCloser") || $(this).hasClass("leftCloserHover") || $(this).hasClass("leftCloserActive")) {
            // toggle next as well
            if ($.browser.msie && $.browser.version < 7) {
                if ($(this).hasClass("leftCloserHover")) {
                	$("#site .content .menu .submenu li.leftCloserHover").attr("class", "leftCloser");
                } else {
					$("#site .content .menu .submenu li.leftCloser").attr("class", "leftCloserHover");
                }
                $(this).next().toggleClass("hover");
            } else {
                $(this).toggleClass("hover");
                $(this).next().toggleClass("hover");
            }
           } else if ($(this).find("a").text() == "Wat is het" || $(this).find("a").text() == "Overzicht") {
            // toggle previous as well
            $(this).toggleClass("hover");
            if ($.browser.msie && $.browser.version < 7) {
                if ($(this).prev().hasClass("leftCloserHover")) {
                	$("#site .content .menu .submenu li.leftCloserHover").attr("class", "leftCloser");
                } else {
					$("#site .content .menu .submenu li.leftCloser").attr("class", "leftCloserHover");
                }
            } else {
                $(this).prev().toggleClass("hover");
            }
        } else {
            $(this).toggleClass("hover");
        }
    });
    $("#site .content .menu .orange").bind("mouseenter mouseleave", function() {
        $(this).toggleClass("hover");
    });
    if ($("#site .content .menu .submenu li:first").next().hasClass('active')) {
    	$("#site .content .menu .submenu li.leftCloser").addClass("active");
        if ($.browser.msie && $.browser.version < 7) {
        	$("#site .content .menu .submenu li.leftCloser").attr("class", "leftCloserActive");
        }
    }
  
}
function setHeight() {
    // set the heights of the elements
    var questionHeight = 0;
    var questions = 0;
    $(".answer").each(function() {
        if ($(this).height() > questionHeight) questionHeight = $(this).height();
        questions++;
    });
    questionHeight += (questions * 11);
    var leftHeight  = $("#site .content .left").height();
    var rightHeight = $("#site .content .right").height();
    newHeight = Math.max(leftHeight, rightHeight) + 20;
    var isHomepage = false;
    if ($(".homeContent").length > 0) {
    	newHeight = $(".homeContent").height() + 20;
    	isHomepage = true;
    }
	if (newHeight < 410) newHeight = 410;
	newHeight = newHeight + questionHeight;
	updateHeight($("#site .content"), newHeight + 24 + 20 + 25 + 15);
	updateHeight($("#site .content .left"), newHeight);
    updateHeight($("#site .content .left img.block"), newHeight);
    updateHeight($("#site .content .left .white"), newHeight - 18);
    updateHeight($("#site .content .right"), newHeight - 12);
    if (isHomepage) {
    	updateHeight($("#site .content .shadow"), newHeight + 1);
    	updateHeight($("#site .homeContent"), newHeight);
    	updateTop($("#site .content .bottomShadow"), newHeight + 54);
    } else {
		updateHeight($("#site .content .shadow"), newHeight - 18);
		updateTop($("#site .content .bottomShadow"), newHeight + 35);
    }
    if (!$.browser.msie) {
        updateHeight($("body"), fixBG());
    }
    //updateHeight($("body"), newHeight + 200);
    //updateTop($("#footer"), newHeight + 170);
}
function updateHeight(elem, newHeight) {
	//console.log('new ' + newHeight + " " + elem.attr("class"));
    elem.css("height", newHeight);
}
function updateTop(elem, newTop) {
    elem.css("top", newTop);
}
function fixBG() {
    var newHeight = $(window).height();
    if ($(document).height() > newHeight) {
        newHeight = $(document).height();
        return newHeight;
    }
    return newHeight - 10;
}
function setMenuClicks() {
    // make li item's clickable
    $("#site .content .menu li").click(function() {
        if ($(this).hasClass("filler") || $(this).hasClass("rightCloser")) {
            // do nothing
        } else if ($(this).hasClass("leftCloser")) {
            // same as next
            document.location.href = $(this).next().find("a").attr("href");
        } else {
            document.location.href = $(this).find("a").attr("href");
        }
    });
}
function setVideos() {
    var playerURL = "/include/flash/mtv.swf?vid=";
    $(".videopreview .video").each(function() {
        $(this).css('opacity',0.5);
        $(this).mouseover(function() {
            $(this).css('opacity',1);
        });
        $(this).mouseout(function() {
            $(this).css('opacity',0.5);
        });
        $(this).next().mouseover(function() {
            $(this).prev().css('opacity',1);
        });
        $(this).next().mouseout(function() {
            $(this).prev().css('opacity',0.5);
        });
        $(this).click(function() {
            var url = $(this).next().attr("href");
            $("#flash").flash({
                src: playerURL + url,
                height: 273,
                width: 320
            }, { version: 9 });
            $("#flash").unbind("click");
            $("#flash").css("marginTop", 60);
            return false;
        });
        $(this).next().click(function() {
            var url = $(this).attr("href");
            $("#flash").flash({
                src: url,
                height: 273,
                width: 330
            }, { version: 9 });
            $("#flash").unbind("click");
            $("#flash").css("marginTop", 60);
            return false;
        });
    });
    /*
    $("#flash").one("click", function () {
        $(this).flash({
            src: '/include/flash/mtv.swf',
            height: 225,
            width: 320
        }, { version: 9 });
        $("#flash").css("marginTop", 75);
        return false;
    });
    */
}
function setHalfBlockAlt() {
    $(".halfBlock.alt").each(function(i) {
        if (i % 2 == 1) {
            $(this).css("border",0);
        }
    });
}
function setAnswerToggle() {
  $(".question>a").click(function() {
        genericToggleQuestion($(this));
        return false;
  });
  $(".question>img").click(function() {
        genericToggleQuestion($(this));      
        return false;
  });
}
function genericToggleQuestion(obj) {
    if (obj.parents(".question").hasClass("open")) {
        obj.parents(".question").find(".answer").slideUp("slow", function() {
            $(this).parents(".question").removeClass("open");
        });
        obj.parents(".question").find("img").attr("src", "/include/images/icons/plus_icon.gif");
    } else {
        // close opened questions
        obj.parents(".questions").find(".question.open .answer").slideUp("slow", function() {
            $(this).parents(".question").removeClass("open");
        });
        obj.parents(".questions").find(".question.open img").attr("src", "/include/images/icons/plus_icon.gif");
        // now open the question
        obj.parents(".question").find(".answer").slideDown("slow", function() {
            $(this).parents(".question").addClass("open");
        });
        obj.parents(".question").find("img").attr("src", "/include/images/icons/min_icon.gif");
    }
}
function setFancyList() {
    // create styled lists
    $("ol.orangeList li").each(function(i) {
        $(this).prepend("<span class='bullet'>"+(i+1)+"</span>");
    });
    $("ol.orangeList").addClass("fancyList");
}
function setSearchToggle(defaultSearchText) {
    $(".search .searchText").focus(function() {
        if ($(".search .searchText").attr("value") == defaultSearchText) {
            $(".search .searchText").attr("value","");
        }
    });
    $(".search .searchText").blur(function() {
        if ($(".search .searchText").attr("value") == "" ){
            $(".search .searchText").attr("value",defaultSearchText);
        }
    });
}
function setHomepageFlash(div, swf) {
    $("#site .content .left div."+div).flash({
        src: swf+'?itemID='+$("#flashItemID").text(),
        height: 447,
        width: 700
    }, { version: 8 });
    if ($("#site .content .left div."+div)[0] != undefined) {
        newHeight = 447;
        updateHeight($("#site .content"), newHeight + 24 + 20 + 25);
        updateHeight($("#site .content .left"), newHeight);
        updateHeight($("#site .content .left img.block"), newHeight);
        updateHeight($("#site .content .left .white"), newHeight - 10);
        updateHeight($("#site .content .right"), newHeight - 22);
        updateHeight($("#site .content .shadow"), newHeight - 10);
        if ($.browser.mozilla) {
            updateHeight($("body"), fixBG());
        }
        //updateHeight($("body"), newHeight + 200);
        updateTop($("#site .content .bottomShadow"), newHeight + 40 );
    }
}
function setLogoClick(url) {
    $("#footer .image").click(function() {
        document.location.href = url;
    });
}
function setActiecodePopup() {
    if (document.location.href.indexOf('/naar_mijn_portemonnee/aanmelden') != -1) {
		tb_show("","/Templates/Global/actiecode.html?height=120&width=500&modal=true",null);
	}
}
function setMobileRedirect() {
	// /naar_mijn_portemonnee?mobilenumber=
	$(".phoneNumber input:submit").click(function() {
		var input = $(".phoneNumber input:text").val();
		if (isNaN(input) || input.length != 8) {
			$(".phoneNumber input:text").addClass("error");
		} else {
			window.location.href = "/naar_mijn_portemonnee?msisdn=" + $(".phoneNumber input:text").val();
		}
		return false;
	});
	$(".phoneNumber input:text").bind("blur", function() {
		var input = $(".phoneNumber input:text").val();
		if (isNaN(input) || input.length != 8) {
			$(".phoneNumber input:text").addClass("error");
		} else {
			$(".phoneNumber input:text").removeClass("error");
		}
	});
}

function setSendSMS () {
	// http://esi.mobielbetalen.m2active.nl/install_midlet.php?msisdn
	$(".smsNumber input:submit").click(function() {
		var input = $(".smsNumber input:text").val();
		if (isNaN(input) || input.length != 8) {
			$(".smsNumber input:text").addClass("error");
		} else {
			window.location.href = "http://esi.mobielbetalen.m2active.nl/install_midlet.php?msisdn=06" + $(".smsNumber input:text").val();
		}
		return false;
	});
	$(".smsNumber input:text").bind("blur", function() {
		var input = $(".smsNumber input:text").val();
		if (isNaN(input) || input.length != 8) {
			$(".smsNumber input:text").addClass("error");
		} else {
			$(".smsNumber input:text").removeClass("error");
		}
	});
}

function CheckAccept(e, args) {
    args.IsValid = $('.acceptBox input').attr('checked');
}

function ConfirmCancel() {
	return confirm("Weet u zeker dat u uw MiniTix Portemonee in het geheel wil opzeggen?");
}
function ConfirmUnregister() {
	return confirm("Uw MiniTix portemonee blijft bestaan, maar de koppeling met uw mobiele nummer wordt opgeheven? Weet u het zeker?");
}

function toolTip(){
	$('.tooltip').each(function(){
		var text = $(this).attr('title');
		$(this).append('<span id="currentTooltip">' + text + '</span>');
	});
	
	$('.tooltip').bind('mouseenter mouseleave', function(e){
		var element = $(this).find('span');
		if(e.type == 'mouseenter'){
			$(this).attr('title','');
			$(this).find('span').css({
				display: 'block',
				bottom: 15,
				right: 0
			});
		}else{
			$(this).attr('title',$('#currentTooltip').text());
			$(this).find('span').hide();
		}
	});
}