var frontpage = {
	headlines : function()
	{
		// is er de flash plugin geinstaleerd?
		if(_jm.detectPluginVersion("flash"))
		{
			$("#headlines").remove();
			
			$("#header").height(260+76).append('<div class="headlines"></div>');
			$("#main").css({ paddingTop: '15px' });			
		
			$("#header .headlines").jmedia({ 
				version		: "8,0",
				elemType	: "div",
				elemClass	: "headlines"
			}, 
			{
				src			: config.submap + "/fla/headlines.swf",
				width		: 980,
				height		: 260,
				quality		: "best",
				wmode		: "opaque",
				menu		: "false",
				bgcolor		: "#007DB5",
				flashVars	: "&submap=" + config.submap + "&_debug=nee&datamap=" + config.datamap + "&phpsessid=" + config.phpsessid
			});
		}
	},
	
	
	init : function()
	{
		frontpage.zijbalk.wedstrijd();
		tabs.fn.tab_nieuwsbrief();
	},
	
	
	zijbalk	: {
		wedstrijd	: function()
		{
			$("#zijbalk_wedstrijd div.wedstrijd")
				.css("cursor", "pointer")
				.click(function()
				{
					window.location = $("h4 a", this).attr("href");
				});
		}
	}
};

$(function() { frontpage.init(); });

// poll uitslagen tabje met animatie
tabs.fn.tab_poll_uitslagen = function()
{
	poll_tab = $("#interactie ul.tabs a[href=#tab_poll]").parent().addClass("actief");
	
	poll_breedte = $("#tab_poll_uitslagen dl").width();
	
	$("#tab_poll_uitslagen dl dd").each(function()
	{
		procent = Math.round( $(this).text().replace("%", "") );
		breedte = Math.round( (poll_breedte/100) * procent );
	
		$(this).width(0).animate({ width: breedte }, 1500);
	});
}


var fn_nieuwsbrief_before_callback = function()
{
	var melding    	= ''; 
	var focus_op_veld    = ''; 

	String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };
	error_veld = '';
	if (document.getElementById('nieuwsbrief_geslacht').value == "")
	{
		melding += '- Bij Geslacht is niets geselecteerd\n';
		if(focus_op_veld == ''){ focus_op_veld = 'nieuwsbrief_geslacht'; }
		error_veld = 'nieuwsbrief_geslacht';    		    
	}

	String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };
	error_veld='';
	if (document.getElementById('nieuwsbrief_voornaam').value.trim() == "") 
	{
		melding += '- Voornaam is leeg\n';
		if(focus_op_veld == ''){ focus_op_veld = 'nieuwsbrief_voornaam'; } 				
		error_veld = 'nieuwsbrief_voornaam';
	}
		
	String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };
	error_veld='';
	if (document.getElementById('nieuwsbrief_achternaam').value.trim() == "") 
	{
		melding += '- Achternaam is leeg\n';
		if(focus_op_veld == ''){ focus_op_veld = 'nieuwsbrief_achternaam'; } 				
		error_veld = 'nieuwsbrief_achternaam';
	}
								
	error_veld='';		
	if(document.getElementById('nieuwsbrief_emailadres').value != '') 
	{ 
		if (document.getElementById('nieuwsbrief_emailadres').value.search(/^(("[^"\x0D\\]+")|([^\x00-\x20\(\)\<\>\[\]\.\\",;:@]+(\.[^\x00-\x20\(\)\<\>\[\]\.\\",;:@]+)*))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([[a-zA-Z0-9\xC0-\xFF\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/) == -1) 
		{
			melding += '- Emailadres moet een e-mailadres zijn (bv. voornaam.achternaam@domein.nl)\n';
			if(focus_op_veld == ''){ focus_op_veld = 'nieuwsbrief_emailadres'; } 				
			error_veld = 'nieuwsbrief_emailadres';		
		}			
	}
		
	if (document.getElementById('nieuwsbrief_emailadres').value == "") 
	{
		melding += '- Emailadres is leeg\n';
		if(focus_op_veld == ''){ focus_op_veld = 'nieuwsbrief_emailadres'; } 				
		error_veld = 'nieuwsbrief_emailadres';			
	}
										
	if (melding !== "") 
	{ 
		alert('U heeft niet alle velden correct ingevoerd:\n\n' + melding);
		if(focus_op_veld != '') 
		{ 
			if ( document.getElementById(focus_op_veld) ) document.getElementById(focus_op_veld).focus(); 
		} 
		return false; 
	}		
	else
	{
		return true;
	}
};

// nieuwsbrief tab
tabs.fn.tab_nieuwsbrief = function()
{
	$("#tab_nieuwsbrief form").formNaarAjax({
		onSubmit    : fn_nieuwsbrief_before_callback,
		onComplete  : function(response) {
			if		(response == "GOED")
				$("#tab_nieuwsbrief").html('<p>Bedankt voor je aanmelding.</p>');
			else 
			{ 
				if	 (response == "DUBBEL")
					$("#tab_nieuwsbrief").html('<p>Uw emailadres is al aanwezig.</p>');
				else 
					$("#tab_nieuwsbrief").html('<div id=melding><p>Uw emailadres kon niet worden toegevoegd.</p></div>');
			}
		}
	});   	
}

// een 'Graafschap' call om de wedstrijden dynamisch in te laden :)
function switch_wedstrijd(wedstrijd)	{
    $("#zijbalk_wedstrijd").load("home/wedstrijd/" + wedstrijd + "/", null, tabs.fn.tab_wedstrijden_klikbaar_maken);
}


tabs.fn.tab_wedstrijden = function()	{
    document.location = 'selectie/eerste-elftal/wedstrijden/' + document.getElementById("wedstrijd_links_id_wedstrijd").value + '/details.html';
}


tabs.fn.tab_wedstrijden_klikbaar_maken = function() {
    $("#zijbalk_wedstrijd_klikbaar").click(tabs.fn.tab_wedstrijden);
}


$(document).ready(function() {
	tabs.fn.tab_wedstrijden_klikbaar_maken();
	
    $("#stemmen").click(function() {
        if ($("#pollantwoorden > li > input:checked").length == 0)
        {
            alert("Kies een antwoord om op te stemmen.");
            return false;
        }
    });
});



// movieplayer related
function getUpdate(typ, pr1, pr2, pid) 
{
	if(pid != "null") 
	{
		if((typ == "state")&&(pr1 != undefined)) 
		{ 
			currentState = pr1; 
			if(currentState=="1") 	{ 
				$("#live .film_volledig").hide();
			}
			else if(currentState=="3") 	{ 
				$("#live .film_volledig").fadeIn(250);
			}
		}
	}
}

function loadFile(obj) 
{
	$("#live .film_volledig a").attr("href", obj.http_link);
	$("#live .film_volledig").hide();
	
	$("#live .film").flash({
        src             : config.submap + "/fla/mediaplayer.swf",
        width           : 270,
        height          : 196,
        version         : 8,
        allowfullscreen : "true",
		wmode			: "opaque",
        flashVars       : {
            file            : obj.file,
            id              : obj.id,
            image           : obj.image,
            width           : 270,
            height          : 196,
            screencolor     : '0x002947',
            lightcolor      : '0xffffff',
            searchbar       : 'false',
			javascriptid	: obj.id,
			enablejs     	: 'true'
        }
    });
	
	// gare internet explorer bug
	if($.browser.msie)	{
		window.onunload = function()	{
			$("#live .film").remove();
		};
	}
}
