/***************************************
****************************************
    GENERAL SCRIPTS
    
    There is one page Default.aspx 
    which returns diferent content 
    depending on the query string. This
    script mainly changes the behaviour 
    of the links so the content is replace
    via ajax rather than a whole page 
    refresh.
    
    This means is is usable w/o JS
    and crawlable.
    
****************************************
***************************************/

/**************************************************************/
/******  preload images and set rotating backgrounds  *********/
/**************************************************************/
var swaptime = 10000 + (15*100); //frame times + a bit more (each frame plays once and has a black final frame
var images = [];
var numBackgrounds = 18;
var counter = 0;
var loadCount = 0;

function timer() { 
    setTimeout("timer();", swaptime);
    $("#content").css("background-image", "url(images/backgrounds/" + counter % numBackgrounds  + ".gif)");

    counter++;
}

function hideLoading() {
    $("#loading").fadeOut("slow"); 
    timer();
}

// create an image object
objImage = new Image();

// set what happens once the image has loaded
objImage.onLoad=hideLoading();

// preload the image file
objImage.src='images/backgrounds/0.gif';
objImage.src='images/backgrounds/1.gif';
objImage.src='images/backgrounds/2.gif';
objImage.src='images/backgrounds/3.gif';
objImage.src='images/backgrounds/4.gif';
objImage.src='images/backgrounds/5.gif';
objImage.src='images/backgrounds/6.gif';
objImage.src='images/backgrounds/7.gif';
objImage.src='images/backgrounds/8.gif';
objImage.src='images/backgrounds/9.gif';
objImage.src='images/backgrounds/10.gif';
objImage.src='images/backgrounds/11.gif';
objImage.src='images/backgrounds/12.gif';
objImage.src='images/backgrounds/13.gif';
objImage.src='images/backgrounds/14.gif';
objImage.src='images/backgrounds/15.gif';
objImage.src='images/backgrounds/16.gif';
objImage.src='images/backgrounds/17.gif';

//checks out the url of the link clicked to determine which items to highlight
$.highlightLinks = function ($theLinks, clickedLinkHref)
{
	$theLinks.removeClass("selected");
	$("a[@href='"+clickedLinkHref+"']").addClass("selected");
	
	if(clickedLinkHref.indexOf("page") > -1)
	{
		clickedLinkHref = clickedLinkHref.replace(/Default.aspx\?/,"");
		var pairs = clickedLinkHref.split("&");	
		var sectionPair = pairs[0].split("=");
		var section = sectionPair[1];


		$("#globalnav a[@href$='"+section+"']").addClass("selected");	
	}
	
};//$.highlightLinks 



/*****************************/
/******   ajax           *****/
/******   load pages via. ****/
/*****************************/
//alter behaviour of links
$.makeAjax = function ()
{
	$theLinks = $(".localnav a, #globalnav a[href*='Default.aspx'], #rhslinks a, #next, #prev");
	
	//undo all binds
	$theLinks.unbind("click");
	
	
    $theLinks.each(function()
    {	
		
		$(this).click(function(e)
		{	
			var $clickedLink = $(this);
			var clickedLinkHref = $clickedLink.attr("href");
			
			//make sure the user can't click on a link while a page is still loading
			$theLinks.unbind("click");
			$theLinks.click(function(e){e.preventDefault()});
			e.preventDefault();
			
			
			
			//if home page item clicked add the flash intro and empty local nav
			if(clickedLinkHref == "Default.aspx")
			{	
				$("#content").html("<div id='intro'></div>");	
				$(".localnav").empty();
				swfobject.embedSWF("Flash/intro.swf", "intro", "630", "470", "6");
				
				//highlight the menu items and reset all the links
				$.highlightLinks($theLinks, clickedLinkHref);	
				$.makeAjax();				
			}
			
			else //get html from db with the href of the clicked link
			{				
				$.ajax({
					url: $clickedLink.attr("href")+"&ajax=true", //get server to return xml instead of whole page
					dataType: "xml",
					success: function(xml)
					{
						$("page", xml).each(function()
						{
							$("#content").html($("content", this).text());		
							$("#localnav").html($("localnav", this).text());	
							
							document.title = $("title", this).text();
													
							$.highlightLinks($theLinks, clickedLinkHref);	
						
							
							$.makeAjax();
							colorBullets();
						});//each
					}//success					
				});	//ajax
				
			}
			
		});	//click
			    
    });//each anchor
    
};//makeAjax

// for the flash intro to call when its finished
function gotoHome()
{
    $('#globalnav a[href="Default.aspx?section=KinaFreedom"]').trigger('click');
}

function colorBullets(){
    $("div#content_inside ul li").wrapInner("<font style='color:#ccc;'></font>");
}

/***************************************
    ON DOCUMENT LOAD
***************************************/
$(document).ready(function(){
    // ajax menu
    $.makeAjax();

    colorBullets();

    /*****************************/
    /******   mp3 swf        *****/
    /*****************************/
    var options = {
        "filepath": "",								// foreground color (button
        "repeat": "yes",										// repeat mp3?
        "volume": "70",										// mp3 volume (0-100)
        "autoplay": "true",								// play immediately on page load?
        "showdownload": "true",								// show download button in player
        "showfilename": "false"								// show .mp3 filename after player
    };

    /*****************************/
    /******   swf            *****/
    /*****************************/
    var flashvars = {};
    var params = { swliveconnect:"true", wmode: "transparent" };
    
    swfobject.embedSWF("Flash/Header_Glowy.swf", "headerglowy", "600", "156", "7", "", flashvars, params);
    swfobject.embedSWF("Flash/freedom of expression.swf", "freedomofexpression", "400", "60", "7", "", flashvars, params);
    swfobject.embedSWF("Flash/RHS_menushow.swf", "rhsmenushow", "167", "270", "7", "", flashvars, params);
    swfobject.embedSWF("Flash/Small glowy Kinafreedom.swf", "smallglowy", "200", "120", "7", "", flashvars, params);
    
    var params = { swliveconnect:"true"}; //no transparency for intro
    swfobject.embedSWF("Flash/intro.swf", "intro", "630", "470", "7", "expressInstall.swf", flashvars, params);

    var flashvars = {};
    var params = { swliveconnect:"true", wmode: "transparent" };
    swfobject.embedSWF("Flash/Small glowy Kinafreedom.swf", "smallglowy", "200", "120", "7", "", flashvars, params);

    var flashthing = '<object type="application/x-shockwave-flash"';
    flashthing += 'data="flash/xspf_player_slim.swf"'
    flashthing += 'width="25" height="34">'
    flashthing += '<param name="movie" value="flash/xspf_player_slim.swf" />"'
    flashthing += '<img src="noflash.gif" width="25" height=34" alt="" />"'
    flashthing += '<img src="noflash.gif" width="25" height="34" alt="" />'
    flashthing += '</object>'

    $("#mp3").append(flashthing);
});