$(document).ready(function() {
    
    setFooter();
    openInNewWindow();
    init();
});

$(window).load(function() {
    if ($("#login,#registerform,#forgotpassword").length > 0) {
            $("#forgotpassword").fancyForm();
            $("#login").fancyForm();
            $("#registerform").fancyForm();
        }

    if ($("#newpassword").length > 0) {
            $("#newpassword").fancyForm();
        }    
    
    
    $(".switchForgotPassword").click(function() {
        switchForgotPassword();
    });
    createIframeBox(); // Create fancy Thickbox
    setBoxes(); // Set the height of every box
    //$("img[@src$=png], .box .top .topleft").pngfix({ repeatMethod: "crop" });
    if ($.browser.msie && $.browser.version < 7) {
        $(document).pngFix();
    }
});

$(window).resize(function() {
    setFooter();
});

function openInNewWindow() {
	$("a[rel='blank']").each(function() {
		$(this).attr({
			href: "javascript:window.open('" + $(this).attr("href") + "');void(0);", 
			title: "Link opent in een nieuw venster"
		});
	});
}

function switchForgotPassword() {
    $(".visible").fadeOut("slow", function() {
        $(this).removeClass("visible");
        $(this).addClass("hidden");    
    });
    $(".hidden").fadeIn("slow", function() {
        $(this).removeClass("hidden");
        $(this).addClass("visible");
    });
}


function setBoxes() {
    $(".box").each(function() {
        setDimensionsBox($(this));
    });
}

function setFooter() {
    if($(window).height() > 725) {
        $("#footer").addClass("big");
        $("#footer").css("left", (($(window).width() - 992) / 2) + 1)
        
    }
    else {
        $("#footer").removeClass("big");
        $("#footer").css("left", "")
    }
}

function setDimensionsBox(boxElement) {
    $(boxElement).find(".topleft").css("width", ($(boxElement).width() - 17));
    $(boxElement).find(".bottomleft").css("width", ($(boxElement).width() - 17));
    $(boxElement).find(".top").css("width", $(boxElement).width());
    $(boxElement).find(".bottom").css("width", $(boxElement).width());
    $(boxElement).find(".right").css("height", $(boxElement).find(".container").height());
    $(boxElement).find(".right").css("height", $(boxElement).find(".container").height());
    $(boxElement).find(".left").css("height", $(boxElement).find(".container").height());
    //$(boxElement).find(".container").css("height", $(boxElement).find(".container").height());
    $(boxElement).find(".container").css("width", ($(boxElement).width() - 20));
    
    if ($(boxElement).hasClass("blue") == false) {
        if ($(boxElement).find(".container").height() > 460) {
            if (!$.browser.msie) {
                $(boxElement).find(".container").css("background-image", "url('/include/images/gradient_large.gif')")
            }
            else {
                $(boxElement).find(".container").css("background-image", "url('/include/images/gradient_large.gif')")
                $(boxElement).find("iframe").css("background-image", "url('/include/images/gradient_large.gif')");
                $(boxElement).find("iframe").css("zoom", 1); 
            }
        }
        else if ($(boxElement).find(".container").height() < 460 && $(boxElement).find(".container").height() > 200) {
            if (!$.browser.msie) {
                $(boxElement).find(".container").css("background-image", "url('/include/images/gradient_medium.gif')")
            }
            else {
                $(boxElement).find(".container").css("background-image", "url('/include/images/gradient_medium.gif')")
                $(boxElement).find("iframe").css("background-image", "url('/include/images/gradient_medium.gif')");
                $(boxElement).find("iframe").css("zoom", 1);
            }
        }
        else   {
            if (!$.browser.msie) {
                $(boxElement).find(".container").css("background-image", "url('/include/images/gradient_small.gif')")
            }
            else {
                $(boxElement).find(".container").css("background-image", "url('/include/images/gradient_small.gif')")
                $(boxElement).find("iframe").css("background-image", "url('/include/images/gradient_small.gif')");
                $(boxElement).find("iframe").css("zoom", 1);
            }
        }
    }
}

function createIframeBox(){
		$("#TB_window").prepend('<div class="top"><div class="topleft"></div><div class="topright"></div></div>');
		$("#TB_window").append('<div class="bottom"><div class="bottomleft"></div><div class="bottomright"></div></div>');
		$("#TB_iframeContent").wrap('<div class="middle"><div class="container">');
		$("#TB_window .middle").prepend('<div class="left"></div>');
		$("#TB_window .middle").append('<div class="right"></div>');		
}

function setDimensionsThickBox(boxElement) {
    //alert(boxElement.length);
    $(boxElement).find(".topleft").css("width", ($(boxElement).width() - 17));
    $(boxElement).find(".bottomleft").css("width", ($(boxElement).width() - 17));
    $(boxElement).find(".top").css("width", $(boxElement).width());
    $(boxElement).find(".bottom").css("width", $(boxElement).width());
    $(boxElement).find(".right").css("height", $(boxElement).find(".container").height());
    $(boxElement).find(".left").css("height", $(boxElement).find(".container").height());
    $(boxElement).find(".container").css("width", ($(boxElement).width() - 20));
    if ($.browser.msie && $.browser.version < 7) {
        $("a.close").css("margin", "0");
    }
}

function showThickbox()	{
	/* Vraag zonder image (bonusvraag) */
	//tb_show('Thickbox Title', 'question_intro_noimg.html?TB_iframe=true&height=auto&width=900&modal=true', null);
	/* Vraag met image*/
	//tb_show('Thickbox Title', 'question_intro_img.html?TB_iframe=true&height=auto&width=900&modal=true', null);
	
	// Calculate iframe width
	if($("#TB_window").length>0){
		$("iframe").attr("allowtransparency","true");
		var iframeWidth = $("iframe").css("width");
		iframeWidth = iframeWidth.substr(0, 3);
		$("#TB_window iframe").css("width", iframeWidth - 19);
	}
}



function thickboxGrow(height) {
	$("#TB_iframeContent").css("height",height +34);
	setDimensionsThickBox($("#TB_window"));
}

function init() {
    //de focus en blur voor Code
    $("#PageTemplateLoader1_ctl00_cp1_ctl00_CodeTextBox,#PageTemplateLoader1_ctl00_cp1_ctl00_posterCode1_CodeTextBox").blur(function() {
        if ($(this).val() == "") {
            $(this).val("Code");
        }
    });

    $("#PageTemplateLoader1_ctl00_cp1_ctl00_CodeTextBox,#PageTemplateLoader1_ctl00_cp1_ctl00_posterCode1_CodeTextBox").focus(function() {
        if ($(this).val() == "Code") {
            $(this).val("");
        }
    });

}


function calcHeight() {
    if ($(".questionanswered").length > 0) {
        thickboxGrow($(".questionanswered").height());
    }
    if ($(".question").length > 0) {
        thickboxGrow($(".question").height());
    }

}

function bliep(tar) {
    alert(tar + ": " + $("iframe").height());
}