﻿function initPage(type)
{
	preloadImages('images/template/blue_bg.gif', 'images/template/green_bg.gif', 'images/template/menu/menu_bg.gif', 'images/template/menu/menu_1_bg.png', 'images/template/menu/menu_2_bg.png', 'images/template/menu/menu_3_bg.png', 'images/template/menu/menu_4_bg.png', 'images/template/menu/menu_5_bg.png', 'images/template/menu/menu_6_bg.png', 'images/template/shadow_right.gif', 'images/template/shadow_bottom.gif', 'images/template/shadow_bottom_left.gif', 'images/template/shadow_bottom_right.gif', 'images/template/menu/tx_aktiviteter_active.gif', 'images/template/menu/tx_motion_active.gif', 'images/template/menu/tx_energi_active.gif', 'images/template/menu/tx_mad_active.gif', 'images/template/menu/tx_vaegt_active.gif', 'images/template/menu/tx_til_laereren_active.gif');
	
//	$("#page, #container").vjustify();
	
	//$("#shadow_right").css("height", $("#container").innerHeight()+"px");
			
    /* Level col divs */	
    //$("#center_col, #right_col").vjustify();
    //$("#left_col body").vjustify();
    
    //$("#left_col_bg img").height = $("#left_col").height;
	
	//$("#left_col_bg img").height = $("body").innerHeight() - $("#top").outerHeight();
	
	//$("#left_col").height = $(window).innerHeight();// - $("#top").outerHeight();
	
    if(type == "subpage1" || type == "subpage2")
    {
        /* Menu */
        var menu_id = $("#menu-items > a.active").attr("id");
        /*    
        if(menu_id != undefined || menu_id != null)
        {
            if(menu_id == "menu_1") { $("#menu-items > a.active img").attr("src", "images/template/menu/tx_aktiviteter_active.gif"); }
			if(menu_id == "menu_2") { $("#menu-items > a.active img").attr("src", "images/template/menu/tx_motion_active.gif"); }
			if(menu_id == "menu_3") { $("#menu-items > a.active img").attr("src", "images/template/menu/tx_energi_active.gif"); }
			if(menu_id == "menu_4") { $("#menu-items > a.active img").attr("src", "images/template/menu/tx_mad_active.gif"); }
			if(menu_id == "menu_5") { $("#menu-items > a.active img").attr("src", "images/template/menu/tx_vaegt_active.gif"); }
			if(menu_id == "menu_6") { $("#menu-items > a.active img").attr("src", "images/template/menu/tx_til_laereren_active.gif"); }
        } */ 
        
		/* Bubble popup */
		/*
        $("#bubble-question .bubble-link a").toggle(
            function()
            {
                $("#bubble-answer").fadeIn("slow");            
            },
            function()
            {
                $("#bubble-answer").fadeOut("slow");
            }
            
        )
		
		$("#bubble-answer").hide();
		*/
    }
    
    if(type == "subpage1")
    {     
        $(".folding-list .folding-item").mouseover(
            function()
            {
                $(this).addClass("bg");
            }
        )
        
        $(".folding-list .folding-item").mouseout(
            function()
            {
                if($(this).hasClass("bg"))
                {
                   $(this).removeClass("bg");
                }
            }
        )        
    }
	
	if(type == "subpage2")
	{
		var _id = $.query.get("id");
		
		$("p.amb-text").hide();
		
		if(_id)
		{			
		}
		else
		{
			_id = 1;
		}
		
		if(_id)
		{
			$(".amb-menu a[href='?id="+_id+"']").addClass("active");
			$("p.amb-text[id='"+_id+"']").show();			
		}
			
		if(_id == 1) { $(".amb-menu-part3").after("<img style='position: absolute; bottom: -280px; right: -50px;' src='images/amb/chris_big.gif'/>"); }
		if(_id == 2) { $(".amb-menu-part3").after("<img style='position: absolute; bottom: -280px; right: -50px;' src='images/amb/kronprinsen_big.gif'/>"); }
		if(_id == 3) { $(".amb-menu-part3").after("<img style='position: absolute; bottom: -280px; right: -50px;' src='images/amb/lotte_friis_big.gif'/>"); }
		if(_id == 4) { $(".amb-menu-part3").after("<img style='position: absolute; bottom: -280px; right: -50px;' src='images/amb/sindballe_big.gif'/>"); }
		if(_id == 5) { $(".amb-menu-part3").after("<img style='position: absolute; bottom: -280px; right: -50px;' src='images/amb/tine_big.gif'/>"); }
		if(_id == 6) { $(".amb-menu-part3").after("<img style='position: absolute; bottom: -280px; right: -50px;' src='images/amb/cyron_big.gif'/>"); }
		if(_id == 7) { $(".amb-menu-part3").after("<img style='position: absolute; bottom: -280px; right: -50px;' src='images/amb/mikkel_big.gif'/>"); }
		if(_id == 8) { $(".amb-menu-part3").after("<img style='position: absolute; bottom: -280px; right: -50px;' src='images/amb/zanka_big.gif'/>"); }
		if(_id == 9) { $(".amb-menu-part3").after("<img style='position: absolute; bottom: -280px; right: -50px;' src='images/amb/trine_big.gif'/>"); }
		if(_id == 10) { $(".amb-menu-part3").after("<img style='position: absolute; bottom: -280px; right: -50px;' src='images/amb/nicklas_big.gif'/>"); }
		
	}
	
	if(type == "frontpage")
	{		
		var rn = Math.floor(Math.random()*10) + 1;		
		
		//rn = 8;		
		
		$(".amb_list").css("left", "25px");
		$(".amb_list").show();
		
		if(rn == 1) // Chris
		{
			$(".amb_text").not(".amb_text[id='amb1']").hide();
			
			$(".amb_text[id='amb1']").css("width", "120px");
			
			switchImage("ambPicture", "images/amb/chris.gif");
			
			$("#ambPicture").css("position", "absolute").css("top", "72px").css("right", "0px");
			
			//$("#amb img").after("<img style='position: absolute; bottom: 4px; right: 0px;' src='images/amb/chris.gif' width='113' height='128' />");
		}
		else
		if(rn == 2) // Kronprinsen
		{
			$(".amb_text").not(".amb_text[id='amb2']").hide();
			
			$(".amb_text[id='amb2']").css("width", "100px");
			
			switchImage("ambPicture", "images/amb/kronprinsen.gif");
			
			$("#ambPicture").css("position", "absolute").css("top", "5px").css("right", "9px");
		
//			$("#amb img").after("<img style='position: absolute; bottom: 7px; right: 9px;' src='images/amb/kronprinsen.gif' width='115' height='189' />");
		}
		else
		if(rn == 3) // Lotte
		{
			$(".amb_text").not(".amb_text[id='amb3']").hide();
		
			switchImage("ambPicture", "images/amb/lotte_friis.gif");
			
			$("#ambPicture").css("position", "absolute").css("top", "93px").css("right", "3px");
			
			//$("#amb img").after("<img style='position: absolute; bottom: 4px; right: 3px;' src='images/amb/lotte_friis.gif' width='155' height='107' />");
		}
		else
		if(rn == 4) // Sindballe
		{
			$(".amb_text").not(".amb_text[id='amb4']").hide();
			
			$(".amb_text[id='amb4']").css("width", "110px");
		
			switchImage("ambPicture", "images/amb/sindballe.gif");
			
			$("#ambPicture").css("position", "absolute").css("top", "-12px").css("right", "0px");
		
			//$("#amb img").after("<img style='position: absolute; bottom: 0px; right: 0px;' src='images/amb/sindballe.gif' width='112' height='211' />");
		}		
		else
		if(rn == 5) // Tine
		{
			$(".amb_text").not(".amb_text[id='amb5']").hide();
			
			$(".amb_text[id='amb5']").css("width", "120px");
		
			switchImage("ambPicture", "images/amb/tine.gif");
			
			$("#ambPicture").css("position", "absolute").css("top", "69px").css("right", "8px");
		
			//$("#amb img").after("<img style='position: absolute; bottom: 3px; right: 8px;' src='images/amb/tine.gif' width='101' height='132' />");
		}
		else
		if(rn == 6) // Cyron
		{
			$(".amb_text").not(".amb_text[id='amb6']").hide();
			
			$(".amb_text[id='amb6']").css("width", "120px");
		
			switchImage("ambPicture", "images/amb/cyron.gif");
			
			$("#ambPicture").css("position", "absolute").css("top", "15px").css("right", "-8px");
		
			//$("#amb img").after("<img style='position: absolute; bottom: 3px; right: 8px;' src='images/amb/cyron.gif' width='116' height='184' />");
		}
		else
		if(rn == 7) // Mikkel
		{
			$(".amb_text").not(".amb_text[id='amb7']").hide();
			
			$(".amb_text[id='amb7']").css("width", "120px");
		
			switchImage("ambPicture", "images/amb/mikkel.gif");
			
			$("#ambPicture").css("position", "absolute").css("top", "-14px").css("right", "0px");
		
			//$("#amb img").after("<img style='position: absolute; bottom: 3px; right: 0px;' src='images/amb/mikkel.gif' width='108' height='214' />");
		}
		else
		if(rn == 8) // Zanka
		{
			$(".amb_text").not(".amb_text[id='amb8']").hide();
			
			$(".amb_text[id='amb8']").css("width", "120px");
		
			switchImage("ambPicture", "images/amb/zanka.gif");
			
			$("#ambPicture").css("position", "absolute").css("top", "-3px").css("right", "-8px");
		
			//$("#amb img").after("<img style='position: absolute; bottom: 3px; right: -8px;' src='images/amb/zanka.gif' width='118' height='204' />");
		}
		else
		if(rn == 9) // Trine
		{
			$(".amb_text").not(".amb_text[id='amb9']").hide();
			
			$(".amb_text[id='amb9']").css("width", "120px");
		
			switchImage("ambPicture", "images/amb/trine.gif");
			
			$("#ambPicture").css("position", "absolute").css("top", "-16px").css("right", "0px");
		
			//$("#amb img").after("<img style='position: absolute; bottom: 3px; right: 0px;' src='images/amb/trine.gif' width='115' height='216' />");
		}
		else
		if(rn == 10) // Nicklas
		{
			$(".amb_text").not(".amb_text[id='amb10']").hide();
			
			$(".amb_text[id='amb10']").css("width", "120px");
		
			switchImage("ambPicture", "images/amb/nicklas.gif");
			
			$("#ambPicture").css("position", "absolute").css("top", "-11px").css("right", "-8px");
		
			//$("#amb img").after("<img style='position: absolute; bottom: 3px; right: -8px;' src='images/amb/nicklas.gif' width='109' height='210' />");
		}		
	}
	
	/* PNG fix */
    $.ifixpng('images/template/pixel.gif');
    $("img[src$='.png']").ifixpng();
}

jQuery.fn.vjustify=function() {
    var maxHeight=0;
    this.each(function(){
        if (this.offsetHeight>maxHeight) {maxHeight=this.offsetHeight;}
    });
    this.each(function(){
        $(this).height(maxHeight + "px");
        if (this.offsetHeight>maxHeight) {
            $(this).height((maxHeight-(this.offsetHeight-maxHeight))+"px");
        }
    });
};


function calcUseOfEnergy()
{
    var badminton = parseInt($("#energyUse input#tx_energiforbrug_badminton").val(), 10);
    var basketball = parseInt($("#energyUse input#tx_energiforbrug_basketball").val(), 10);
    var bordtennis = parseInt($("#energyUse input#tx_energiforbrug_bordtennis").val(), 10);
    var cykling = parseInt($("#energyUse input#tx_energiforbrug_cykling").val(), 10);
    var dans = parseInt($("#energyUse input#tx_energiforbrug_dans").val(), 10);
    var fodbold = parseInt($("#energyUse input#tx_energiforbrug_fodbold").val(), 10);
    var gang = parseInt($("#energyUse input#tx_energiforbrug_gang").val(), 10);
    var golf = parseInt($("#energyUse input#tx_energiforbrug_golf").val(), 10);
    var gymnastik = parseInt($("#energyUse input#tx_energiforbrug_gymnastik").val(), 10);    
    var hockey = parseInt($("#energyUse input#tx_energiforbrug_hockey").val(), 10);
    var haandbold = parseInt($("#energyUse input#tx_energiforbrug_haandbold").val(), 10);
    var legudenfor = parseInt($("#energyUse input#tx_energiforbrug_legudenfor").val(), 10);
    var loeb = parseInt($("#energyUse input#tx_energiforbrug_loeb").val(), 10);
    var ridning = parseInt($("#energyUse input#tx_energiforbrug_ridning").val(), 10);
    var rulleskojtelob = parseInt($("#energyUse input#tx_energiforbrug_rulleskojtelob").val(), 10);
    var sove = parseInt($("#energyUse input#tx_energiforbrug_sove").val(), 10);
    var svomning = parseInt($("#energyUse input#tx_energiforbrug_svomning").val(), 10);
    var tegnelaese = parseInt($("#energyUse input#tx_energiforbrug_tegnelaese").val(), 10);
    var tennis = parseInt($("#energyUse input#tx_energiforbrug_tennis").val(), 10);
    var trampolinspring = parseInt($("#energyUse input#tx_energiforbrug_trampolinspring").val(), 10);
    var tvcomputerspil = parseInt($("#energyUse input#tx_energiforbrug_tvcomputerspil").val(), 10);
    
    $("#energyUse span#time").text(badminton + basketball + bordtennis + cykling + dans + fodbold + gang + golf + gymnastik + hockey + haandbold + legudenfor + loeb + ridning + rulleskojtelob + sove + svomning + tegnelaese + tennis + trampolinspring + tvcomputerspil);
    
    badminton = Math.round(badminton * 14.108);
    basketball = Math.round(basketball * 14.108);
    bordtennis = Math.round(bordtennis * 12.540);
    cykling = Math.round(cykling * 25.080);
    dans = Math.round(dans * 14.108);
    fodbold = Math.round(fodbold * 21.945);
    gang = Math.round(gang * 10.973);
    golf = Math.round(golf * 14.108);
    gymnastik = Math.round(gymnastik * 12.540);
    hockey = Math.round(hockey * 25.080);
    haandbold = Math.round(haandbold * 25.080);
    legudenfor = Math.round(legudenfor * 15.675);
    loeb = Math.round(loeb * 25.080);
    ridning = Math.round(ridning * 12.540);
    rulleskojtelob = Math.round(rulleskojtelob * 21.945);
    sove = Math.round(sove * 2.822);
    svomning = Math.round(svomning * 25.080);
    tegnelaese = Math.round(tegnelaese * 3.135);
    tennis = Math.round(tennis * 21.945);
    trampolinspring = Math.round(trampolinspring * 14.108);
    tvcomputerspil = Math.round(tvcomputerspil * 3.135);
    
    $("#energyUse input#tx_energiforbrug_badminton").parent().next("td").text(badminton);
    $("#energyUse input#tx_energiforbrug_basketball").parent().next("td").text(basketball);
    $("#energyUse input#tx_energiforbrug_bordtennis").parent().next("td").text(bordtennis);
    $("#energyUse input#tx_energiforbrug_cykling").parent().next("td").text(cykling);
    $("#energyUse input#tx_energiforbrug_dans").parent().next("td").text(dans);
    $("#energyUse input#tx_energiforbrug_fodbold").parent().next("td").text(fodbold);
    $("#energyUse input#tx_energiforbrug_gang").parent().next("td").text(gang);
    $("#energyUse input#tx_energiforbrug_golf").parent().next("td").text(golf);
    $("#energyUse input#tx_energiforbrug_gymnastik").parent().next("td").text(gymnastik);
    $("#energyUse input#tx_energiforbrug_hockey").parent().next("td").text(hockey);
    $("#energyUse input#tx_energiforbrug_haandbold").parent().next("td").text(haandbold);
    $("#energyUse input#tx_energiforbrug_legudenfor").parent().next("td").text(legudenfor);
    $("#energyUse input#tx_energiforbrug_loeb").parent().next("td").text(loeb);
    $("#energyUse input#tx_energiforbrug_ridning").parent().next("td").text(ridning);
    $("#energyUse input#tx_energiforbrug_rulleskojtelob").parent().next("td").text(rulleskojtelob);
    $("#energyUse input#tx_energiforbrug_sove").parent().next("td").text(sove);
    $("#energyUse input#tx_energiforbrug_svomning").parent().next("td").text(svomning);
    $("#energyUse input#tx_energiforbrug_tegnelaese").parent().next("td").text(tegnelaese);
    $("#energyUse input#tx_energiforbrug_tennis").parent().next("td").text(tennis);
    $("#energyUse input#tx_energiforbrug_trampolinspring").parent().next("td").text(trampolinspring);
    $("#energyUse input#tx_energiforbrug_tvcomputerspil").parent().next("td").text(tvcomputerspil);
    
    $("#energyUse span#use").text(badminton + basketball + bordtennis + cykling + dans + fodbold + gang + golf + gymnastik + hockey + haandbold + legudenfor + loeb + ridning + rulleskojtelob + sove + svomning + tegnelaese + tennis + trampolinspring + tvcomputerspil);
}

function checkTip13()
{
	var correctAnswers = 0;
	
	var count = 0;
	
	count += validateAnswer("q1");
	count += validateAnswer("q2");
	count += validateAnswer("q3");
	count += validateAnswer("q4");
	count += validateAnswer("q5");
	count += validateAnswer("q6");
	count += validateAnswer("q7");
	count += validateAnswer("q8");
	count += validateAnswer("q9");
	count += validateAnswer("q10");
	count += validateAnswer("q11");
	count += validateAnswer("q12");
	count += validateAnswer("q13");
	
	if(count < 13)
	{
	    alert("Husk at vælge et svar til alle spørgsmålene!");
	}
	else
	{
	    correctAnswers += checkAnswer("q1", "x");
	    correctAnswers += checkAnswer("q2", "1");
	    correctAnswers += checkAnswer("q3", "1");
	    correctAnswers += checkAnswer("q4", "x");
	    correctAnswers += checkAnswer("q5", "2");
	    correctAnswers += checkAnswer("q6", "x");
	    correctAnswers += checkAnswer("q7", "x");
	    correctAnswers += checkAnswer("q8", "x");
	    correctAnswers += checkAnswer("q9", "x");
	    correctAnswers += checkAnswer("q10", "2");
	    correctAnswers += checkAnswer("q11", "1");
	    correctAnswers += checkAnswer("q12", "x");
	    correctAnswers += checkAnswer("q13", "1");
	    
	    $("span#tips-correct-answers").text(correctAnswers);	    
	    $("table.tipsresult").show("fast");
	}	
}

function checkAnswer(question, correct_answer)
{    
    var count = 0;
    
	$("#form_tip13 input:radio[name='"+question+"']").each(	
		function()
		{			
			if($(this).parent().hasClass("wrongresult"))
			{
				$(this).parent().removeClass("wrongresult");
			}			

			if($(this).attr('value') == correct_answer && $(this).is(":checked"))
			{
				count = count + 1;							
				$(this).parent().siblings(".qbox").children("span.question-help").hide("fast");
			}
			else
			if($(this).is(":checked"))
			{
				$(this).parent().addClass("wrongresult");				
				$(this).parent().siblings(".qbox").children("span.question-help").show("fast");
			}			
		}
	);	
	
	return count;
}

function validateAnswer(question)
{
	var count = 0;
	
	$("#form_tip13 input:radio[name='"+question+"']").each(	
		function()
		{			
			if($(this).is(":checked"))
			{
				count = count + 1;
			}			
		}
	);
	
	return count;	
}

// Energibehov

function energibehovStep(step)
{
//	$("#form_energibehov")
	
	var isValid = true;
	
	var gender = $("#form_energibehov input:radio[name='gender']:checked").val();	
	var age = $("#form_energibehov #ageSelecter").val();
	var weight = $("#form_energibehov input:text[name='weight']").val();
	var activity = $("#form_energibehov input:radio[name='activity']:checked").val();	
	
	if(gender){ $("#invalidGender:visible").hide();	} else { $("#invalidGender").show("fast"); isValid = false; }	
	if(age && age > 0) { $("#invalidAge:visible").hide(); } else { $("#invalidAge").show("fast"); isValid = false; }
	if(weight > 0) { $("#invalidWeight:visible").hide(); } else { $("#invalidWeight").show("fast"); isValid = false; }
	if(activity) { $("#invalidActivity:visible").hide(); } else { $("#invalidActivity").show("fast"); isValid = false; }	
	
	var male = new Array();
	var female = new Array();
	
	male[10] = new Array(250,285,315);
	male[11] = new Array(235,265,295);
	male[12] = new Array(220,250,280);
	male[13] = new Array(210,235,265);
	male[14] = new Array(205,230,265);
	male[15] = new Array(195,220,255);
	male[16] = new Array(190,215,240);
	male[17] = new Array(185,210,235);
	
	female[10] = new Array(220,250,280);
	female[11] = new Array(200,230,255);
	female[12] = new Array(190,215,240);
	female[13] = new Array(180,200,225);
	female[14] = new Array(165,190,210);
	female[15] = new Array(160,180,205);
	female[16] = new Array(155,180,200);
	female[17] = new Array(155,175,195);
	
	var arMeal1 = new Array();
	var arMeal2 = new Array();
	var arMeal3 = new Array();
	
	arMeal1[1] = 0.20;
	arMeal1[2] = 0.225;
	arMeal1[3] = 0.25;
	
	arMeal2[1] = 0.25;
	arMeal2[2] = 0.30;
	arMeal2[3] = 0.35;
	
	arMeal3[1] = 0.25;
	arMeal3[2] = 0.30;
	arMeal3[3] = 0.35;
	
	var bmr = 0;
	var kjres = 0;
		
	if(isValid)
	{
		$("#step2").show("fast");
		if(gender == "male")
		{
			bmr = Math.round(male[age][activity-1]*weight);
			$("#dailyNeed").text(bmr);
		}
		if(gender == "female")
		{
			bmr = Math.round(female[age][activity-1]*weight);
			$("#dailyNeed").text(bmr);
		}
		
		$("#dailyEnergy").show("fast");
		
		if(step == 2)
		{
			var meal1 = $("#form_energibehov input:radio[name='meal1']:checked").val();	
			var meal2 = $("#form_energibehov input:radio[name='meal2']:checked").val();	
			var meal3 = $("#form_energibehov input:radio[name='meal3']:checked").val();	
			
			if(meal1 && meal2 && meal3)
			{
				// $bmr - ($bmr* $meal_arr['meal1'][$this->piVars['meal1']]) - ($bmr* $meal_arr['meal2'][$this->piVars['meal2']]) - ($bmr* $meal_arr['meal3'][$this->piVars['meal3']])
				kjres = Math.round(bmr - (bmr * arMeal1[meal1]) - (bmr * arMeal2[meal2]) - (bmr * arMeal3[meal3]));
				
				$("#eatFor").text(kjres);
				
				$("#availMeal").show("fast");
				
				$("#invalidMeal:visible").hide("fast");
			}
			else
			{
				$("#availMeal:visible").hide();
				$("#invalidMeal").show("fast");
			}
		}
	}
	else
	{
		$("#step2:visible").hide();
	}
}

// Energibehov end

function switchImage(imgName, imgSrc) 
{
  if (document.images)
  {
    if (imgSrc != "none")
    {
      document.images[imgName].src = imgSrc;	
	}
  }
}

function preloadImages()
{
  if(document.images)
  {
    if(!document.imageArray) document.imageArray = new Array();

	var i,j = document.imageArray.length, args = preloadImages.arguments; 

    for(i=0; i<args.length; i++)
    {
      if (args[i].indexOf("#")!=0)
      {
        document.imageArray[j] = new Image;
        document.imageArray[j++].src = args[i];
      }
    }
  }
}

// Example:
// randomImage(['0.gif',50,50,'1.gif',25,25,'2.gif',50,25]); 

// * Dependencies * 
// this function requires the following snippet:
// JavaScript/Randomizers/randomNumber

function randomImage(imgArr)
{
  var imgSrc, imgW, imgH, r;
  r = randomNumber(imgArr.length / 3)-1;
  
  imgSrc = imgArr[r * 3];
  imgW = imgArr[(r * 3)+1];
  imgH = imgArr[(r * 3)+2];

  document.write('<img src='+imgSrc+' width='+imgW+' height='+imgH+' />');
}

