﻿// ******************************************************************** //
// Page Load/Unload                                                     //
// ******************************************************************** //

	Event.observe ( window, "load", function(){ setImageLinks();setUpPlayer('http://media.dev/images_resize_sites'); } );

// ******************************************************************** //
// Observe Functions for links                                          //
// ******************************************************************** //


// Replaces the Wiki Links to include the HF value
function formatWiki(hf,js){

    var wikiLinks = document.getElementsByTagName("a");
    for (var i=0; i < wikiLinks.length; i++) {
        if (wikiLinks[i].className.match("wikiLink")) {
            wikiLinks[i].href = wikiLinks[i].href + "&hf="+hf+"&js="+js
        }
    }
}

/* Used to display Flash videos on the technology page*/
/*Startup - Define flash player properties, then create video links and attach event listeners to them (which will run the flash player)*/
function setUpPlayer(mediaServer){
// checks if the required version of flash is installed
	if(hasFlashVersion(5)) {
		var flashPlayerProperties = new FlashPlayer(512,314,512,288,"true",6,2,"sameDomain","true", "#666666", "#333333");
		currentflashPlayer = defineFlashPlayer(flashPlayerProperties, mediaServer);
		setVideoLinks(currentflashPlayer, flashPlayerProperties);
	}else{
		return false;
	}
}
	
function FlashPlayer(width, height, vidWidth, vidHeight, fullscreen, buffer, skipTime, allowscriptaccess, allowFullScreen, uiBarColour, uiBarBufferColor){
	this.width = "567";
	this.height = "425";
	this.vidWidth = "567";
	this.vidHeight = "400";
	
	this.fullscreen = fullscreen;
	this.buffer = buffer;
	this.skipTime = skipTime;
	this.allowscriptaccess = allowscriptaccess;
	this.allowFullScreen = allowFullScreen;
	this.uiBarColour = uiBarColour;
	this.uiBarBufferColor = uiBarBufferColor;
}


function defineFlashPlayer(flashPlayerProperties, mediaServer){
//http://media.dev/inside_dyson/loader.swf
// /swf/video/flvPlayer.swf

	var so = new SWFObject(mediaServer + "/swf/video/flvPlayer.swf", "flv", flashPlayerProperties.width, flashPlayerProperties.height, "8", "#000000");
	so.addVariable("viewWidth", "567");
	so.addVariable("viewHeight", "400");
	so.addVariable("fullscreen", flashPlayerProperties.fullscreen);
	so.addVariable("buffer", flashPlayerProperties.buffer); 
	so.addVariable("skipTime",flashPlayerProperties.skipTime);
	so.addVariable("uiBarTimeColor", "#FFFFFF");
	so.addVariable("uiBarBufferColor", "#ffcc00");
	so.addParam("allowscriptaccess",flashPlayerProperties.allowscriptaccess);
	so.addParam("allowFullScreen",flashPlayerProperties.allowFullScreen);
	so.addVariable("uiBgGradTop", "#8f9da0");
		so.addVariable("uiBgGradBottom", "#8f9da0");
	so.addVariable("uiHighCol", "#666666");
so.addVariable("uiDividerLineColor", "#8f9da0");
so.addVariable("uiIdleCol", "#FFFFFF");
	
	
	
	return so;
//flashPlayerProperties.uiBarColour
//flashPlayerProperties.uiBarBufferColor
}



/*Creates a video link for each occurrance of a product(css class) on the page*/

function setVideoLinks(currentFlashPlayer, flashPlayerProperties){

    var videoLinks = document.getElementsByTagName("a");
    for (var i=0; i < videoLinks.length; i++) {
        if (videoLinks[i].className.match("videoLink")) {
            videoLinks[i].onclick = function() {

                flvLink = "http://media.dev/images_resize_sites/inside_dyson/assets/uk/main/"+this.id
 	            loadFlashPlayerVideo(currentFlashPlayer, flashPlayerProperties, flvLink);	
            }
        }
    }
}


function setImageLinks(){
    
    // If IE6 then we will just have the page load rather than trying to change the image src
    // This is because the image doesn't swap correctly.
    if (browser != "ie6" && browser != "safari2" ){
        var a = 0
        var imagesLinks = document.getElementsByTagName("a");
        
        for (var i=0; i < imagesLinks.length; i++) {
            if (imagesLinks[i].className.match("assetLink")) {
            
                var currentID = imagesLinks[i].id
                imagesLinks[i].onclick = function() {
                
                var linkID = document.getElementById(this.id)
                var imgsrc = linkID.getElementsByTagName("img")
               
                    this.href = "javascript:void(0);"
                    var mainImage = imgsrc[0].src.replace(/thumbs/,"main")

                    // Preload image to overcome IE6 issue with non-preloaded images not displaying
                    var preloadImg = new Image();
                    preloadImg.src = mainImage;
                    
                    showImage(mainImage)
                }
                a++;
            }
        }   
    }
}

function showImage(imageURL){
    var newImg
    if ($('mainImg')){
        $('mainImg').src = imageURL;
    }else{
        $('articleImages').innerHTML = "<img src=\""+imageURL+"\" id=\"mainImg\" class=\"mainImg\" height=\"425\" width=\"567\">";
    }
}

/*Loads the flash player with the relevant video*/
function loadFlashPlayerVideo(currentFlashPlayer, flashPlayerProperties, videoName){
	currentFlashPlayer.addVariable("file", videoName);
	currentFlashPlayer.write("articleImages");
	
	if (browser == "ie6" || browser == "ie7"){
	    $('articleImages').className = 'videoFile';   
	}
}
// ******************************************************************** //
