﻿/* ===================================================
	Global Javascripts - Dyson
	Version: v1.0
/* =================================================*/


/* Global Flags */
var w3 = !!(document.getElementById && document.createElement);
var browser = identifyBrowser();
var countryScrollPos = "closed";

/* 
	returns browser & version
	E.G. "ie7" or "ie5mac" or "mozilla" or "opera7" or "safari1.2"
*/

function identifyBrowser() {
	var agent = navigator.userAgent.toLowerCase();
	if (typeof navigator.vendor != "undefined" && navigator.vendor == "KDE" && typeof window.sidebar != "undefined")  {
		return "kde";
	}else if (typeof window.opera != "undefined"){
		var version = parseFloat(agent.replace(/.*opera[\/ ]([^ $]+).*/, "$1"));
	    if (version >= 7){
			return "opera7";
		}else if (version >= 5){
			return "opera5";
		}
	    return false;
	}else if (typeof document.all != "undefined"){
		if (typeof document.getElementById != "undefined"){
			var browser = agent.replace(/.*ms(ie[\/ ][^ $]+).*/, "$1").replace(/ /, "");
			if (typeof document.uniqueID != "undefined"){
				if (browser.indexOf("5.5") != -1){
					return browser.replace(/(.*5\.5).*/, "$1");
				}else{
					return browser.replace(/(.*)\..*/, "$1");
				}
      		}else{
				return "ie5mac";
			}
		}
		return false;
	}else if (typeof document.getElementById != "undefined")  {
		if( window.devicePixelRatio && window.getMatchedCSSRules && !window.Opera){
			return "safari3";
		}else if (navigator.vendor.indexOf("Apple Computer, Inc.") != -1){
			if (typeof window.XMLHttpRequest != "undefined"){
				return "safari1.2";
			}
			return "safari1";
		}else if (agent.indexOf("gecko") != -1){
			return "mozilla";
		}
	}
	return false;
}

function osType(){
	var os = navigator.platform;

	os = os.toLowerCase();
	if(os.indexOf("mac") != -1){
		os = "mac";
	}else if(os.indexOf("linux") != -1){
		os = "linux";	
	}else{
		os = "win";	
	}
	return os;
}


// checks if the required version of flash is installed
function hasFlashVersion(iVersion) {
	var flashinstalled = 0;
	var flashversion = 0;
	MSDetect = "false";
	if (navigator.plugins && navigator.plugins.length){
		x = navigator.plugins["Shockwave Flash"];
		if (x) {
			flashinstalled = 2;
			if (x.description) {
			    y = x.description;
			    //Hack for version 10 and above! Will need to be fixed at version 100!
			    if (y.charAt(y.indexOf('.')-2) != " ") {
			        flashversion = y.charAt(y.indexOf('.')-2)
			    }
			    flashversion = flashversion+y.charAt(y.indexOf('.')-1);
			}
		}else flashinstalled = 1;
		if (navigator.plugins["Shockwave Flash 2.0"]) { flashinstalled = 2; flashversion = 2; }
	} else if (navigator.mimeTypes && navigator.mimeTypes.length) {
		x = navigator.mimeTypes['application/x-shockwave-flash'];
		if (x && x.enabledPlugin) flashinstalled = 2;
		else flashinstalled = 1;
	} else MSDetect = "true";
	
	if(flashinstalled==0) {
		try{ var flash = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + iVersion); flashinstalled = 2; flashversion = iVersion; }
		catch(e){}
	}
	if(flashinstalled!=2) { return false; }
	else {
		if(flashversion < iVersion) { return false; } 
		else { return true; }
	}
}

// AJAX call to update the values of ASP Session vars.
// Used on the 'Recycle machine' checkbox for example.
function updateSession(sName,sVal,cbo){
    if (cbo){
        sVal = sVal;
    }else{
        sVal = "";
    }
    var url = "/store/ajax_updateSession.asp?sName="+sName+"&sVal="+sVal
    new Ajax.Request(url);
}


function globalInit() { 
   
    /* Set tracking for download links */
    downloadLinks();
    trackingLinks();
    
    /* Set external links */
    externalLinks();

    /* Set Support  */
	supportBuyPartsLink();
	
/* ----------------------------------------- */
/* Country Select Drop Down                  */
/* ----------------------------------------- */

    var countryScrollHeight
   /* Country Select */
    if ($('currentCountry')){


    if($('currentCountryDiv')){$('currentCountryDiv').removeClassName("hidden")} // Shows the country select for JS versions
   
        Event.observe(document.body, 'click', function(event) {
            var element = Event.element(event);
            // If the element clicked isn't the country drop down AND the drop down is open, then close it
            if (element.id != 'currentCountry' && element.id !='handle1' && element.id !='ddArrow'){
                if (countryScrollPos != "closed"){
                    countryScrollHeight = 0;
                    countryScrollPos = "closed";
                    countrySelect(countryScrollHeight); 
                }
            }
        });

    // vertical slider control on country select
    var slider1 = new Control.Slider('handle1', 'track1', {
	    axis: 'vertical',
	    onSlide: function(v) { scrollVertical(v, $('countrySelect'), slider1);  },
	    onChange: function(v) { scrollVertical(v, $('countrySelect'), slider1); }
    });


        $('currentCountry').onclick = function(){
            
            if (countryScrollPos == "closed"){
               countryScrollHeight = 110;
               countryScrollPos = "open";
               $('ddArrow').addClassName("ddOpen")
            }else{
                countryScrollHeight = 0;
                countryScrollPos = "closed";
                $('ddArrow').removeClassName("ddOpen")

            }
            countrySelect(countryScrollHeight);   // Open or close the country select drop down     
            return(false);
        }
    
    
        // Set the country links
        var countryLinks = $('countrySelect').getElementsByTagName("a");   
        for (var i=0; i<countryLinks.length; i++) {
            countryLinks[i].onclick = function() {
                document.location.href= this.href;
				document.location.href = "http://" + document.location.hostname + "/jumpto.asp?siteUrl=" + this.href;
                return(false);
            }
        }
    
    }
/* ----------------------------------------- */
/* ----------------------------------------- */

	
	/* Product Finder search */
	if ($('model_Product')){
	    $('model_Product').value = ""; // Reset the drop down
	    $('model_Product').onchange = function(){
	        if ($('model_Product').value != ""){
	            $('fmFinder').submit();
	        }
	    }
	}
	
	/* Text Search */
	if ($('txtSearch')){
	    $('txtSearch').onfocus = function(){
	        if($('txtSearch').value == "Search"){
	           $('txtSearch').value = ""; 
	        }
	    }
	}

	/* Image swap */
/*	
	var imageSwap = document.getElementById('imageSwapLinks').getElementsByTagName("a"); 
	
    for (var i=0; i<imageSwap.length; i++) { 
	    if (imageSwap[i].rel.match("imageSwap")) {
		    imageSwap[i].onclick = function(){
		        
		        
		        var imageSrc = document.getElementById(this.id).getElementsByTagName("img");
		        var imageName = imageSrc[0].src;
		        var imageRef = imageName.split("_")
		        $('productLarge').src = "http://media.stage/images_resize_sites/images/products/m_"+imageRef[imageRef.length-1]
		        return(false); 
		    }
		    
		}
	}
*/

}

/* ----------------------------------------- */
/* COUNTRY SELECT FUNCTION                   */
/* ----------------------------------------- */

    function scrollVertical(value, element, slider) {
	    element.scrollTop = Math.round(value/slider.maximum*(element.scrollHeight-element.offsetHeight));
    }


    function countrySelect(selHeight){
        var marHeight = "-111"
        if (selHeight == 0){
            marHeight = "0"
        }
        new Effect.Morph('countrySelect', {
          style: 'height:'+selHeight+'px;margin-top:'+marHeight+'px;', // CSS Properties
          duration: 0.2
        });
        new Effect.Morph('countryScroll', {
          style: 'height:'+selHeight+'px;margin-top:'+marHeight+'px;', // CSS Properties
          duration: 0.2
        });   
    }

/* -------------------------------------------------------------------- */
/* Checks for any download links and sets the Google Analytics tracking */
/* -------------------------------------------------------------------- */
/* 
    Requires download links to be set up with rel="trackDownload" - it then takes the HREF and passes this
    to Google Analytics and sets the target to blank (like the externalLinks() function).
*/
function downloadLinks(){

 	if (!document.getElementsByTagName) return; 
	var anchorDownloads = document.getElementsByTagName("a"); 
	
	var hrefLength = anchorDownloads.length;
	
	for (var i=0; i<hrefLength; i++){ 
		var downloadLink = anchorDownloads[i]; 
		//alert(downloadLink)
		
		if (downloadLink.getAttribute("href") && downloadLink.getAttribute("rel") == "trackDownload"){
		
		    downloadLink.target = "_blank";
		    downloadLink.onclick = function(){
		    
		  
		       pageTracker._trackPageview(this.href);
		       pageTrackerExternal._trackPageview(this.href);
		       
               pageTracker._trackEvent('Link', 'Download', this.href)
               pageTrackerExternal._trackEvent('Link', 'Download', this.href)
		       
		    }
        }
	}
}


/* EXTERNAL LINKS */
function externalLinks(){
    
	if (!document.getElementsByTagName) return; 
	var anchors = document.getElementsByTagName("a"); 
	var anchorLength = anchors.length;
	
	for (var i=0; i<anchorLength; i++) { 
		var anchoritem = anchors[i]; 
		var anchorRel = anchoritem.getAttribute("rel");
		
		if(anchoritem.getAttribute("rel")){
			if (anchoritem.getAttribute("href") && anchoritem.getAttribute("rel") == "printPage") {
				anchoritem.onclick = function() {
					anchoritem.print();
				}
			}		
			if (anchoritem.getAttribute("href") && anchoritem.getAttribute("rel") == "external"){
				anchoritem.target = "_blank";
				anchoritem.onclick = function(){
				   pageTracker._trackEvent('Link', 'External', this.href);
				   pageTrackerExternal._trackEvent('Link', 'External', this.href);
				}
			}
			if (anchoritem.getAttribute("href") && anchoritem.getAttribute("rel") == "supportBuyParts"){
				anchoritem.onclick = function(){
				   pageTracker._trackEvent('Link', 'PartsPurchase', this.href);
				   pageTrackerExternal._trackEvent('Link', 'PartsPurchase', this.href);
				}
			}
			
			if (anchoritem.getAttribute("href") && anchoritem.getAttribute("rel") == "brochureDownload"){
				anchoritem.target = "_blank";
				anchoritem.onclick = function(){
				   brochureTracking();
				}
			}
									
			if (anchoritem.getAttribute("href") && anchorRel.indexOf("videoInPage|")>-1) {
				var relArray
				var vWidth = "";
				var vHeight = "";
				var location = "";
				var menuAvailable = "true";
				
	            relArray = anchorRel.split("|");
				vWidth = relArray[1];
				vHeight = relArray[2];
				if(relArray[3]){
					location = relArray[3];
				}
				if(relArray[4]){
					menuAvailable = relArray[4];
				}
				var vReplay = relArray[5];
				anchoritem.onclick = function() {
					playInPageVideo(this.id, vWidth, vHeight, location, menuAvailable, vReplay);
					return (false);
				}
	
			}
		}	
	}

}

/*Used by lightbox so please don't rename - hides any instances of Flash when the lightbox or another item needs it hidden on the page e.g. overlapping your content*/
function hideFlashReplacement(){
	if(document.getElementsByTagName("embed")){
		var flashFiles = document.getElementsByTagName("embed");
			for(var i = 0; i < flashFiles.length; i++){
				if(flashFiles[i].getAttribute("type") == "application/x-shockwave-flash"){
					flashFiles[i].style.visibility = "hidden";
				}
			}
	}
	if ($("flashcontent")){
		$("flashcontent").style.visibility = "hidden";
	}
	if ($("insideContent")){
		$("insideContent").style.visibility = "hidden";
	}
	
	var hideText = document.getElementsByClassName("flashContent");
	
	for(var i = 0; i < hideText.length; i++){
		hideText[i].style.visibility = "hidden";
	}
/*
	if(typeof sIFR == "function"){
		if($$(".sIFR-flash")){
			var flashTitles = $$(".sIFR-flash");
			for(var i = 0; i < flashTitles.length; i++){
				flashTitles[i].style.visibility = "hidden";
			}
		}		
	}	
	if($$(".flashContent")){
		var flashContent = $$(".flashContent");
		for(i = 0; i < flashContent.length; i++){
			flashContent[i].style.visibility = "hidden";
		}
	}
	if($("spin")){
		$("spin").style.visibility = "hidden";
	}
	if($("assetViewer")){
		$("assetViewer").style.visibility = "hidden";
	}
	*/
}

/*Used by lightbox so please don't rename - undoes hideFlashReplacement function*/
function swithOnFlash(){
	//Switches on the Flash replacement text (if applicable) for the page
	if(document.getElementsByTagName("embed")){
		var flashFiles = document.getElementsByTagName("embed");
			for(var i = 0; i < flashFiles.length; i++){
				if(flashFiles[i].getAttribute("type") == "application/x-shockwave-flash"){
					flashFiles[i].style.visibility = "visible";
				}
			}
	}
	if ($("flashcontent")){
		$("flashcontent").style.visibility = "visible";
	}
	if ($("insideContent")){
		$("insideContent").style.visibility = "visible";
	}
	var hideText = document.getElementsByClassName("flashContent");
	
	for(var i = 0; i < hideText.length; i++){
		hideText[i].style.visibility = "visible";
	}


	/*
	if(typeof sIFR == "function"){
		if($$(".sIFR-flash")){
			var flashTitles = $$(".sIFR-flash");
			for(i = 0; i < flashTitles.length; i++){
				flashTitles[i].style.visibility = "visible";
			}
		}		
	}
	if($$(".flashContent")){
		var flashContent = $$(".flashContent");
		for(i = 0; i < flashContent.length; i++){
			flashContent[i].style.visibility = "visible";
		}
	}	
	if($("spin")){
		$("spin").style.visibility = "visible";
	}
	if($("assetViewer")){
		$("assetViewer").style.visibility = "visible";
	}
	*/
}


/*
	rel="videoInPage|100|150|jhjhjkhjh"
/*

/*Allows a flash player to be embedded within */
function playInPageVideo(flvFileName, vidWidth, vidHeight, location, menuAvailable, replayVideo) {
	if(menuAvailable == "true"){
		vidHeight_Menu = parseInt(vidHeight) + 24;
	}else{
		vidHeight_Menu = parseInt(vidHeight);
	}
	
	if(replayVideo!=""){
		var vPlayer = "flvPlayer_replay";
	}
	else{
		var vPlayer = "flvPlayer";
	}
	
    var so = new SWFObject(app_media + "/swf/video/" + vPlayer + ".swf", "advertPlayer", parseInt(vidWidth), parseInt(vidHeight_Menu), "8", "#FFFFFF");
    so.addVariable("file", app_media + "/swf/" + location + flvFileName + ".flv");
//    so.addVariable("viewHeight", 288);
    so.addVariable("viewHeight", vidHeight);
//    so.addVariable("viewWidth", 512);
    so.addVariable("viewWidth", vidWidth);
    so.addVariable("stretchToFill", false);
    so.addParam("menu", "false"); //Disables the menu on the flash video when you right click it
    so.addParam("allowscriptaccess", "sameDomain");
    so.addParam("allowfullscreen", "true");
    //so.addParam("class", "clear margTop1em");
    so.write($("embedFlashVideo"));
}


/* Order New Part Through Support */
function supportBuyPartsLink(){


if (!document.getElementsByTagName) return; 
	var anchors = document.getElementsByTagName("a"); 
	var anchorLength = anchors.length;
	
	for (var i=0; i<anchorLength; i++) { 
		var anchor = anchors[i]; 
		var anchorRel = anchor.getAttribute("rel")
		
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "supportBuyParts"){
		    anchor.onclick = function() {
		    eventTracking('Page View', 'Parts Purchase', this.href);
               //pageTracker._trackEvent('Link', 'PartsPurchase', this.href)
               //pageTrackerExternal._trackEvent('Link', 'PartsPurchase', this.href)
		    }
		} else if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "supportBuyPartsEx") {
		    anchor.target = "_blank";
		    anchor.onclick = function() {
		        eventTracking('Page View', 'Parts Purchase', this.href);
		        //pageTracker._trackEvent('Link', 'PartsPurchase', this.href)
		        //pageTrackerExternal._trackEvent('Link', 'PartsPurchase', this.href)
		    }
		}
	}

}


// Used to track Events in GA
// Links need to be set with rel = "TrackingLink|TRACK TYPE|EXTERNAL|EVENTYPE|SUBTYPE|ACTIONTYPE"
// i.e. rel="TrackingLink|event|true|Accessory Link|DC25-Animal|ACC-DC08T1115FLEXICREVICE"
function trackingLinks(){

    if (!document.getElementsByTagName) return; 
    var pageLinks = document.getElementsByTagName("a"); 
    var pageLinksLength = pageLinks.length;
    
    
    // Loop through all links in array
    for (var i=0; i<pageLinksLength; i++) { 
    
 		var pageLinkID = pageLinks[i]; 
		var pageLinksRel = pageLinkID.getAttribute("rel")
		if (pageLinksRel != null){ 
            
            if(pageLinkID.getAttribute("href") && pageLinksRel.indexOf("TrackingLink|")>-1){
	            relArray = pageLinksRel.split("|");
	            // If set as External - open in new window
	            if (relArray[2] == "true"){pageLinkID.target = "_blank";}

	            pageLinkID.onmouseup = function() {
	            pageLinksRel = this.rel
	            relArray = pageLinksRel.split("|");
	
	                if (relArray[1] == "event") {
	                    eventTracking(relArray[3], relArray[4], relArray[5]);
	                } else if (relArray[1] == "page") {
	                    pageTracking(relArray[3]);
	                }
	            }
            }
        }
    }
}

function eventTracking(eventType,subType,actionType){
    // Build up all GA accounts
    
    pageTracker._trackEvent(eventType, subType, actionType)
    pageTrackerExternal._trackEvent(eventType, subType, actionType)
    try {
        pageTrackerOLD._trackEvent(eventType, subType, actionType)
    } catch (err) {}    
}
function pageTracking(pageURL){

    // Build up all GA accounts
    pageTracker._trackPageview(""+pageURL+"")
    pageTrackerExternal._trackPageview(pageURL)
    try{
        pageTrackerOLD._trackPageview(pageURL)
    }catch(err) {}
}
document.observe("dom:loaded", function() { globalInit(); });
//Event.observe(window, 'load', function() { globalInit(); });