// ================================================
// | Globalt funktionsbibliotek för www.timbro.se |
// |                                              |
// | © Timbro / Stiftelsen Fritt Näringsliv 2008  |
// ================================================

// ---------------------------------------------------------
// Funktioner för Apsis Nyhetsbrev
// (c) Apsis AB
// ---------------------------------------------------------
function CorrectEmailaddress(SubscriberForm){
if (SubscriberForm.pf_Email.value.length > 255)
{
alert("Ange som mest 255 tecken i \"E-postadress\" Fält.");
SubscriberForm.pf_Email.focus();
return (false);
 }
if (SubscriberForm.pf_Email.value == "")
{
alert("Ange värdet för \"E-postadress\" Fält.");
SubscriberForm.pf_Email.focus();
return (false);
 }
if (SubscriberForm.pf_Email.value.length < 7)
{
alert("Ange minst 7 tecken i \"E-postadress\"Fält.");
SubscriberForm.pf_Email.focus();
return (false);
 }
pf_Email=SubscriberForm.pf_Email.value
at=pf_Email.indexOf("@");
lastat=pf_Email.lastIndexOf("@");
dot=pf_Email.lastIndexOf(".");
if(at<1||at!=lastat||dot<at){
alert("Din e-postadress är inte rätt. Försök igen.");
return false;}
}

// ---------------------------------------------------------
// init () - funktion som anropas av sidans onload-händelse
// 
// Version 0.1 - 080508
// 
// ---------------------------------------------------------
function init ()
{	
	setPageHeight ();

	// ------------------------------------------------
	// Parametrar för JQuery-bildspelet på första sidan
	// ------------------------------------------------
	try 
	{
	$.fn.cycle.defaults.speed   = 900;
	$.fn.cycle.defaults.timeout = 6000;
	}
	catch (err) {}
	
	try 
	{
		// ------------------------------------------------------------
		// Ange id:t på elementet som innehåller element för bildspelet
		// ------------------------------------------------------------
		$(function() {
			$('#fade')
			.before('<div id="nav">') 
			.cycle({pager: '#nav'});
		});
		
		var nav = document.getElementById('nav');
		
		nav.style.marginLeft = 893 - nav.offsetWidth + "px";
	}
	catch (err) { }
	
		// wait for the DOM to be loaded 
		$(function() { 
			// bind 'myForm' and provide a simple callback function 
			try {
				$('#tipsa-van-formular').ajaxForm(function() {     
					alert("Tipset skickat!"); 
					toggleTellAFriend ();
					document.getElementById('tipsa-van-formular').reset(); 
					});
			}
			catch (err) { 
				//alert ("erreur!"); 
			}
				
		});
	
	try
	{
		 jQuery('#mycarousel').jcarousel({
		        visible: 6
		    });
		    
		 jQuery('#mycarousel').css ('visibility','visible');

		
	}
	catch (err) { }
	
	
	// document.getElementById("timbro-ao").style.display = "none";

	// -----------------------------------------------------------------------
	// [start] Style-hack för att få sökrutan på rätt plats i Safari 3 + Opera	
	// -----------------------------------------------------------------------
   if(window.devicePixelRatio ||										// Safari
   	/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent)) 	// Opera
   {
   	/*document.getElementById ("sokruta").style.marginTop = "-0px";*/
   }
   // ----------------------------------------------------------------------
   // [slut] Style-hack för att få sökrutan på rätt plats i Safari 3 + Opera
   // ----------------------------------------------------------------------
	
}




function setPageHeight ()
{
	var sida = document.getElementById("sida");
	var huvudinnehall = document.getElementById("huvudinnehall");
	
	if (parseInt(navigator.appVersion)>3) {
		
		// Netscape/Mozilla et al
		if (navigator.appName=="Netscape") {
			winW = window.innerWidth;
			winH = window.innerHeight;
		}
		
		// IE
		if (navigator.appName.indexOf("Microsoft")!=-1) {
			winW = document.documentElement.clientWidth;
			winH = document.documentElement.clientHeight;
		}
	}
	
	
	if (winH > sida.offsetHeight)
	{
		height = winH - 50;
		sida.style.height = height + "px";
		//height = winH - 373;
		//huvudinnehall.style.height = height + "px";
	}
}


function setTabContentHeight ()
{
	try 
	{
		var omradeHuvudcell = document.getElementById("omrade-huvudcell");
		
		if (omradeHuvudcell.offsetHeight < 575)
		{
			//omradeHuvudcell.style.height = "575px";
		}
	}
	catch (err)
	{
	}
	
}


function showSocialWindow ()
{
	document.getElementById ("dela-ruta").style.display = "block";
}


function hideSocialWindow ()
{
	document.getElementById ("dela-ruta").style.display = "none";
}


function initTabs () {
	$('#flik-innehall-1').pager('div', {
  		navId: 'flik-nyheter-nav',
  		prevText: '<<&nbsp;TILLBAKA&nbsp;',
  		nextText: '&nbsp;&nbsp;FLER&nbsp;NYHETER&nbsp;>>',
  		linkText: ['&nbsp;1&nbsp;', '&nbsp;2&nbsp;', '&nbsp;3&nbsp;', '&nbsp;4&nbsp;'],
		height: '490px'		
	});
	
	$('#flik-innehall-2').pager('div', {
  		navId: 'flik-evenemang-nav',
  		prevText: '<<&nbsp;TILLBAKA&nbsp;&nbsp;',
  		nextText: '&nbsp;&nbsp;FLER&nbsp;EVENEMANG&nbsp;>>',
  		linkText: ['&nbsp;1&nbsp;', '&nbsp;2&nbsp;', '&nbsp;3&nbsp;', '&nbsp;4&nbsp;'],
		height: '490px'		
	});
	
	$('#flik-innehall-3').pager('div', {
  		navId: 'flik-bocker-nav',
  		prevText: '<<&nbsp;TILLBAKA&nbsp;',
		nextText: '&nbsp;&nbsp;FLER&nbsp;B&Ouml;CKER&nbsp;>>',
		linkText: ['&nbsp;1&nbsp;', '&nbsp;2&nbsp;', '&nbsp;3&nbsp;', '&nbsp;4&nbsp;'],
		height: '490px'	
	});
	
	$('#flik-innehall-4').pager('div', {
  		navId: 'flik-rapporter-nav',
  		prevText: '<<&nbsp;TILLBAKA&nbsp;',
		nextText: '&nbsp;&nbsp;FLER&nbsp;RAPPORTER&nbsp;>>',
		linkText: ['&nbsp;1&nbsp;', '&nbsp;2&nbsp;', '&nbsp;3&nbsp;', '&nbsp;4&nbsp;'],
		height: '490px'	
	});
}


function initEnTabs () {
	$('#flik-innehall-1').pager('div', {
  		navId: 'flik-nyheter-nav',
  		prevText: '<<&nbsp;BACK&nbsp;',
  		nextText: '&nbsp;&nbsp;MORE&nbsp;NEWS&nbsp;>>',
  		linkText: ['&nbsp;1&nbsp;', '&nbsp;2&nbsp;', '&nbsp;3&nbsp;', '&nbsp;4&nbsp;'],
		height: '490px'	
	});
	
	$('#flik-innehall-2').pager('div', {
  		navId: 'flik-evenemang-nav',
  		prevText: '<<&nbsp;BACK&nbsp;&nbsp;',
  		nextText: '&nbsp;&nbsp;MORE&nbsp;EVENTS&nbsp;>>',
  		linkText: ['&nbsp;1&nbsp;', '&nbsp;2&nbsp;', '&nbsp;3&nbsp;', '&nbsp;4&nbsp;'],
		height: '490px'		
	});
	
	$('#flik-innehall-3').pager('div', {
  		navId: 'flik-bocker-nav',
  		prevText: '<<&nbsp;BACK&nbsp;&nbsp;',
		nextText: '&nbsp;&nbsp;MORE&nbsp;BOOKS&nbsp;>>',
		linkText: ['&nbsp;1&nbsp;', '&nbsp;2&nbsp;', '&nbsp;3&nbsp;', '&nbsp;4&nbsp;'],
		height: '490px'	
	});
	
	$('#flik-innehall-4').pager('div', {
  		navId: 'flik-rapporter-nav',
  		prevText: '<<&nbsp;BACK&nbsp;&nbsp;',
  		nextText: '&nbsp;&nbsp;MORE&nbsp;´REPORTS&nbsp;>>',
		linkText: ['&nbsp;1&nbsp;', '&nbsp;2&nbsp;', '&nbsp;3&nbsp;', '&nbsp;4&nbsp;'],
		height: '490px'
	});
}


// ---------------------------------------------------------
// FocusTextField - funktion för att ta bort beskrivnings-
//                  text i ett textfält. Används t ex för
//                  att ta bort ordet "SÖK" i sökrutor.
//
// theTextFieldObj: 	Textrutans DOM-objekt
//
// myText:				Texten som ska finnas i rutan för att
//							rutan ska tömmas. (För en sökruta är
//							denna text typiskt "SÖK".)
// 
// ---------------------------------------------------------
function FocusTextField (theTextFieldObj, myText)
{
	if (theTextFieldObj.value == myText)
	{
		theTextFieldObj.value="";
	}
}


// -------------------------------------------------------------
// UnFocusTextField - 	Funktion för att återinsätta standard-
//								text i en textruta om textrutan är
//								tom.
//                  
// theTextFieldObj: 	Textrutans DOM-objekt
//
// myText:				Texten som ska sättas in i ruta (För en 
//							sökruta är denna text typiskt "SÖK".)
// 
// -----------------------------------------------------------
function UnFocusTextField (theTextFieldObj, myText)
{
	if (theTextFieldObj.value == "")
	{
		theTextFieldObj.value = myText;
	}
}


function toggleTimbroAtillO ()
{
	// PÅ
	//if (document.getElementById("timbro-ao").style.display == "none") {
	//	document.getElementById("timbro-ao").style.display = "block";
	//	document.getElementById("menyklick-bkgr").style.display = "block";
	//	document.getElementById("timbro-ao-menyalt").style.color = "#555555";
	//}
	// AV
	//else
	//{
	//	document.getElementById("timbro-ao").style.display = "none";
	//	document.getElementById("menyklick-bkgr").style.display = "none";
	//	document.getElementById("timbro-ao-menyalt").style.color = "#aaaaaa";
	//}
}


function timbroAtillOOff ()
{
	try
	{
		document.getElementById("timbro-ao").style.display = "none";
		document.getElementById("timbro-ao-menyalt").style.color = "#aaaaaa";
		document.getElementById("menyklick-bkgr").style.display = "none";
		document.getElementById("timbro-ao-lank").style.color='#aaaaaa';
	}
	catch (err)
	{
	}
}


function AOBgColourOut ()
{	
	//alert ("AOBgColourOut");
	
	if (document.getElementById("timbro-ao").style.display == "block")
	{
		document.getElementById("timbro-ao-lank").style.color='#555555';
	}
	else {
		document.getElementById("timbro-ao-lank").style.color='#aaaaaa';
	}
}

		
function AOBgColourOver ()
{
	document.getElementById('timbro-ao-lank').style.color='#999999';
}


// -------------------------------------------------------------
// selectTab - 	Funktion för välja en flik och dess innehåll
//
// tabNo:		Flikens sekvensnummer (1 ... noOfTabs)
// noOfTabs:	Det totala antalet flikar
// -------------------------------------------------------------
function selectTab (tabNo, noOfTabs) {

	for (tabIndex = 1; tabIndex <=noOfTabs; tabIndex ++)
	{
		tabContentName = "flik-innehall-" + tabIndex;
		tabName			= "flik-" + tabIndex;
		tabLinkName		= "fliklank-" + tabIndex;
		
		if (tabIndex != tabNo)
		{
			document.getElementById(tabContentName).style.display = "none";
			document.getElementById(tabName).style.backgroundImage = "url('/bilder/sidgrafik/flik" + tabIndex + ".gif')"
			document.getElementById(tabLinkName).style.color = "#FFFFFF";
		}
		else
		{
			document.getElementById(tabContentName).style.display = "block";
			document.getElementById(tabName).style.backgroundImage = "url('/bilder/sidgrafik/flik" + tabIndex + "-vald.gif')"
			document.getElementById(tabLinkName).style.color = "#555555";
		}
	}
}

	
function showMenu(idToShow, count) {
	
	for (menuId = 0; menuId < count; menuId++) {
		
		menuName = "m-" + menuId + "-1";
		menuAlt = "m-alt-" + menuId;
		
		if (idToShow != menuId) {
			display = "none";
			//colour = "#aaaaaa";
			
		}
		else {
			display = "block";
			//colour = "#aaaaaa";
		}
		
		// alert (menuName);
		document.getElementById(menuName).style.display = display;
		//document.getElementById(menuAlt).style.color = colour;
	}
}


function initBookstore ()
{
		
	try {
		document.getElementById("timbro-ao").style.display = "none";
		
		var bokbild = document.getElementById("bokbild");
		var bokinfo = document.getElementById("bokinfo");
	
		if (bokinfo.offsetHeight < 350) {
			bokbild.style.height = "350px";
		}
		else {
			bokbild.style.height = bokinfo.offsetHeight + "px";
		}
		
	} 
	catch (err) {
		//alert("Det har uppstått ett fel: " + err.description);
	}
}


function toggleCheckout(checkState) {

	if (checkState == "on") {
		document.getElementById ("kundvagn-till-kassan").style.backgroundColor = "#dddddd";
		document.getElementById ("kundvagn-kassa-pil").style.backgroundColor = "#dddddd";
	}
	else {
		document.getElementById ("kundvagn-till-kassan").style.backgroundColor = "#ffffff";
		document.getElementById ("kundvagn-kassa-pil").style.backgroundColor = "#ffffff";
	}

}


function showBookstoreMenu(idToShow) {
	
	for (menuId = 0; menuId < 2; menuId++) {
		
		menuName = "m-" + menuId + "-1";
		menuAlt = "m-alt-" + menuId;
		
		if (idToShow != menuId) {
			display = "none";
			colour = "#aaaaaa";
			
		}
		else {
			display = "block";
			colour = "#555555";
			document.getElementById("timbro-ao-lank").style.title = "Klicka" +
			"för att stänga!";
		}
		
		document.getElementById(menuName).style.display = display;
		document.getElementById(menuAlt).style.color = colour;
	}
}


function AOBookstoreBgColourOut ()
{	
	//alert ("AOBgColourOut");
	
	if (document.getElementById("timbro-ao").style.display == "block")
	{
		document.getElementById("timbro-ao-lank").style.color='#555555';
	}
	else {
		document.getElementById("timbro-ao-lank").style.color='#aaaaaa';
	}
}
	
	
function OverBookstoreTopics ()
{
	showBookstoreMenu(99); 
	document.getElementById('timbro-ao-lank').style.color='#555555';
}


function toggleBookstoreTimbroAtillO ()
{
	// PÅ
	if (document.getElementById("timbro-ao").style.display == "none") {
		document.getElementById("timbro-ao").style.display = "block";
		document.getElementById("timbro-ao-menyalt").style.color = "#555555";
	}
	// AV
	else
	{
		document.getElementById("timbro-ao").style.display = "none";
		document.getElementById("timbro-ao-menyalt").style.color = "#aaaaaa";
	}
}

	
function isSafari3()
{
	isSafari3 = false;

	if(window.devicePixelRatio) 
	{
		isSafari3 = true;
	}
	
	return isSafari3;
}


function toggle_visibility(id) {

	var e = document.getElementById(id);

	if(e.style.display == 'block') {
		document.getElementById("indicator").src = "/bilder/sidgrafik/arr_up.gif";
		e.style.display = 'none';
	}
	else {
		document.getElementById("indicator").src = "/bilder/sidgrafik/arr_down.gif";
		setTimeout ("showEvent()", 500);
	}
	
	setTabContentHeight();
}

function showEvent() { 
	document.getElementById('event_form').style.display = 'block'; 
}


function hideEvent(e) { 
	e.style.display = 'none'; 
}


function toggleTellAFriend ()
{
	if (document.getElementById ("tipsa-van").style.display == "block")
		document.getElementById ("tipsa-van").style.display = "none";
	else
		document.getElementById ("tipsa-van").style.display = "block";
}

// +----------------------------+
// | Funktioner från Macromedia |
// +----------------------------+

function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_findObj(n, d) { //v4.01
	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
}
			
function MM_validateForm() { //v4.0
			
	var trans, i, p, q, nm, test, num, min, max, errors='', args = MM_validateForm.arguments;
	
	for (i = 0; i < (args.length-2); i += 3) { 
		
		test 	= args[i+2]; 
		val 	= MM_findObj(args[i]);
						
		if (val) { 
			
			nm=val.name;
	
			if (nm == "first_name")	trans = "'Förnamn'";
			if (nm == "last_name")	trans = "'Efternamn'";
			if (nm == "email")		trans = "'E-post'";
			
			if (nm == "recieverName")	trans = "mottagarens namn";
			if (nm == "recieverMail")	trans = "mottagarens e-postadress";
			if (nm == "senderName")	trans = "ditt namn";
			if (nm == "senderMail")	trans = "din e-postadress";

			if ((val=val.value)!="") {
				if (test.indexOf('isEmail')!= -1) { 
					p=val.indexOf('@');
				  	if (p < 1 || p == (val.length - 1)) 
				  		errors += 	"- " + trans + " måste innehålla en e-postadress " +
				  						"(e-postadressen ska ha utseendet namn@domän.org, " +
				  						"t ex stina@svensson.com)\n";
				} 
				else if (test != 'R') { 
					
					num = parseFloat(val);
	
					if (isNaN(val)) errors += 	"- " + trans + 
														" måste vara ett nummer " +
														"(fältet ska inte innehålla några mellanslag " +
														"eller andra tecken såsom bindestreck eller " +
														"bokstäver)\n";
	
					if (test.indexOf('inRange') != -1) { 
						
						p=test.indexOf(':');
						min=test.substring(8, p); 
						max=test.substring(p + 1);
						
						if (num < min || max < num) 
							errors += 	"- " + trans + " måste vara ett nummer mellan " + min + 
											" och " + max + "\n";
					}
				}
			} 
			else if (test.charAt(0) == 'R') errors += "- " + trans + " " +
																	"är ett obligatoriskt fält och måste " +
																	"således vara ifyllt.\n"; 
		}							
	}

	errorMessage = "Tyvärr inträffade nedanstående fel. \n\n" +
						"Vänligen, rätta till dessa och klicka sedan åter på knappen " +
						"\"Skicka\".\n\n" +
						errors
	
	if (errors) alert(errorMessage);
	
	document.MM_returnValue = (errors == '');
}


function renderPlayer (id_string, element_id)
{

	document.getElementById(element_id).innerHTML = "<object width=\"600\" height=\"486\"> " +
					"<param name=\"movie\" value=\"http://www.youtube.com/v/" + id_string + "&hl=sv&fs=1&\">" +
					"</param><param name=\"allowFullScreen\" value=\"true\"></param>" +
					"<param name=\"allowscriptaccess\" value=\"always\">" +
					"</param><embed src=\"http://www.youtube.com/v/" + id_string + "&hl=sv&fs=1&\" " +
					"type=\"application/x-shockwave-flash\" " +
					"allowscriptaccess=\"always\" " +
					"allowfullscreen=\"true\" " +
					"width=\"600\" " +
					"height=\"486\"></embed></object>";
}



function MM_validateFormENG() { //v4.0
			
	var trans, i, p, q, nm, test, num, min, max, errors='', args = MM_validateFormENG.arguments;
	
	for (i = 0; i < (args.length-2); i += 3) { 
		
		test 	= args[i+2]; 
		val 	= MM_findObj(args[i]);
						
		if (val) { 
			
			nm=val.name;
	
			if (nm == "first_name")	trans = "'First name'";
			if (nm == "last_name")	trans = "'Last naem'";
			if (nm == "email")		trans = "'Email'";
			
			if (nm == "recieverName")	trans = "'To (name)'";
			if (nm == "recieverMail")	trans = "'To (email address)'";
			if (nm == "senderName")		trans = "'From (name)'";
			if (nm == "senderMail")		trans = "'From (email address)'";

			if ((val=val.value)!="") {
				if (test.indexOf('isEmail')!= -1) { 
					p=val.indexOf('@');
				  	if (p < 1 || p == (val.length - 1)) 
				  		errors += 	"- " + trans + " must contain an email address " +
				  						"(the email address should be on the format of name@domain.org, " +
				  						"for instance stina@svensson.com)\n";
				} 
				else if (test != 'R') { 
					
					num = parseFloat(val);
	
					if (isNaN(val)) errors += 	"- " + trans + 
														" must contain a number " +
														"(the field must not contain any spaces, " +
														"or other characters such as dashes, commas or " +
														"letters)\n";
	
					if (test.indexOf('inRange') != -1) { 
						
						p=test.indexOf(':');
						min=test.substring(8, p); 
						max=test.substring(p + 1);
						
						if (num < min || max < num) 
							errors += 	"- " + trans + " must contain a number between " + min + 
											" and " + max + "\n";
					}
				}
			} 
			else if (test.charAt(0) == 'R') errors += "- " + trans + " " +
																	"is a required field and should " +
																	"hence be filled in.\n"; 
		}							
	}

	errorMessage = "We apologise but one or more errors were encountered. \n\n" +
						"Please correct these errors and press the " +
						"\"Send\" button again. \n\n The following " +
						"errors occurred:\n\n" + 
						errors
	
	if (errors) alert(errorMessage);
	
	document.MM_returnValue = (errors == '');
}


function PushTimbroPlayer (assetId)
{
	//alert ("Hello World");
	
	var flashvars = {
	};
	var params = {
		menu: "false",
		scale: "noScale",
		allowFullscreen: "true",
		allowScriptAccess: "always",
		bgcolor: "#ffffff",
		wmode: "transparent"
	};
	var attributes = {
		id:"flashspelare"
	};
	
	document.getElementById ("fade").style.display = "none";
	
	swfobject.embedSWF(	"http://www.world-television.se/projects/timbro/bin/TimbroChannelPlayer.swf?asset=" + assetId, 
								"bildspel", 
								"912", 
								"442", 
								"9.0.0", 
								"http://www.world-television.se/projects/timbro/bin/expressInstall.swf", 
								flashvars, 
								params, 
								attributes);

}


