/**
* Tyco FIS
*
* Javascript developed by Bloom Media Ltd. | www.bloommedia.co.uk
* Contributors: Dominic Kelly
*/

// Attach events - MooTools syntax (check objects exist first)
document.addEvent( 'domready' , function() {
		// dom ready
		// 'extra' links
		if($('doEmail')){$('doEmail').addEvent( 'click' , doEmail);} //email link
		if($('doPrint')){$('doPrint').addEvent( 'click' , doPrint);} // print link
		if($('doBookmark')){$('doBookmark').addEvent( 'click' , doBookmark);} // bookmark link
		// quick search
		if($('doQuickSearch')){$('doQuickSearch').addEvent( 'click' , doQuickSearch);} //quick search button
		if($('txtQuickSearch')){$('txtQuickSearch').addEvent( 'focus' , focusQuickSearch);} //quick search textbox
		if($('txtQuickSearch')){$('txtQuickSearch').addEvent( 'blur' , blurQuickSearch);} // quick search textbox
		// ask the expert
		if($('doAskTheExpert')){$('doAskTheExpert').addEvent( 'click' , doAskTheExpert);} //quick search button
		if($('txtAskTheExpert')){$('txtAskTheExpert').addEvent( 'focus' , focusAskTheExpert);} //quick search textbox
		if($('txtAskTheExpert')){$('txtAskTheExpert').addEvent( 'blur' , blurAskTheExpert);} // quick search textbox
		if($('ddl-ate-markets')){$('ddl-ate-markets').addEvent( 'change' , changeAskTheExpert);} // navigate to expert pages, drop down
		

		
		// attach new fValidfator class' to specific fields based on the contact form required
		if($('Sitekit_Form_219')){			
			document.getElementById('SKFCE_1_UserInput').className="fValidate['required']";
			document.getElementById('SKFCE_2_UserInput').className="fValidate['required','blank']";
			document.getElementById('SKFCE_3_UserInput').className="fValidate['required','email']";
			
			document.getElementById('SKFCE_4_UserInput').className="fValidate['required','blank']";	
			
			document.getElementById('SKFCE_5_UserInput').className="fValidate['required','blank']";	
			document.getElementById('SKFCE_7_UserInput').className="fValidate['required','blank']";	
			document.getElementById('SKFCE_9_UserInput').className="fValidate['required','blank']";
			document.getElementById('SKFCE_12_UserInput').className="fValidate['required','blank']";
			document.getElementById('SKFCE_13_UserInput').className="fValidate['required','blank']";
			document.getElementById('SKFCE_14_UserInput').className="fValidate['required','blank']";
		
			var exValidatorB = new fValidator("Sitekit_Form_219"); // careers form	
		}
		


function $get(key,url){   
    if(arguments.length < 2) url =location.href;   
    if(arguments.length > 0 && key != ""){   
        if(key == "#"){   
            var regex = new RegExp("[#]([^$]*)");   
        } else if(key == "?"){   
            var regex = new RegExp("[?]([^#$]*)");   
        } else {   
            var regex = new RegExp("[?&]"+key+"=([^&#]*)");   
        }   
        var results = regex.exec(url);   
        return (results == null )? "" : results[1];   
    } else {   
        url = url.split("?");   
        var results = {};   
            if(url.length > 1){   
                url = url[1].split("#");   
                if(url.length > 1) results["hash"] = url[1];   
                url[0].split("&").each(function(item,index){   
                    item = item.split("=");   
                    results[item[0]] = item[1];   
                });   
            }   
        return results;   
    }   
}  

				
	// flv videos - product pages
	if($('videoName')){

		// get video name
		var e = document.getElementById('videoName').innerHTML;
		
		// hide from user. This will appear in SiteKit as javascript is not parsed.
		document.getElementById('videoName').style.display = "none";
		
		// find value between hashes, i.e. #value#
		var firstDelimeter = e.indexOf("#") + 1;
		var lastDelimeter = e.lastIndexOf("#");
		
		// slice string inside delimeters
		e = e.slice(firstDelimeter,lastDelimeter)
		
		// only place the video if the video name is not blank
		if(e!=""){
			displayVideoPlayer(e.toLowerCase());
		}else{
			document.getElementById('video').style.display = "none";
		}			
}

	
	}
)

// Print 
function doEmail(){
	//email_str = "mailto:?subject=TycoFis website: " + document.title + &body= " + document.title + ": ";

var pagebit=String(location.href);

var qparts = pagebit.split("#");

pagebit=qparts[0];


var subjbit="Tyco Fire and Integrated Solutions - Page of interest...";
var bodbit="A friend has sent you a link to some valuable information on Tyco's fire and security solutions website.%0A%0A"+pagebit+"%0A%0ATake a look to find out what they thought you would find interesting.%0A%0AWe would advise you that the senders' details have not been verified. If you have any doubts about the authenticity of the sender, please ignore this mail.";

	email_str = "mailto:?subject="+subjbit+"&body=" + bodbit;
	//email_str += "&body= " + document.title + ": ";
	//email_str +=  location.href;
	location.href = email_str; 
}

// Email 
function doPrint(){
	window.print();
}

// Bookmark 
function doBookmark(){

	var title = "Tyco Fire & Integrated Solutions";
	var url = "http://www.tycofis.com";
	
	if (document.all){ // ie
		window.external.AddFavorite(url, title);
	}else if (window.sidebar){ // firefox
		window.sidebar.addPanel(title, url, "");
	}else{ // opera and safari and all other browsers not supported
		alert("Press CTRL + D to bookmark this page");
	}
}

// Quick search
function doQuickSearch(){		
	if(($('txtQuickSearch').value == "Enter keyword...")){
	}else{
		$('frmQuickSearch').submit();
	}
}

function focusQuickSearch(){
	this.value = "";	

}
function blurQuickSearch(){
	// user left the text box blank, so reset it
	if(this.value.length < 1){
		this.value = "Enter keyword...";
	}
}

// ask the expert search
function doAskTheExpert(){		
	if(($('txtAskTheExpert').value == "Ask a question...")){
		alert("Ask a question...");
	}else{
		$('frmFaqSearch').submit();
	}
}

function focusAskTheExpert(){
	this.value = "";	
}

function blurAskTheExpert(){
	// user left the text box blank, so reset it
	if(this.value.length < 1){
		this.value = "Ask a question...";
	}
}

function changeAskTheExpert(){
	if(this.selectedIndex !=0 && this.value != "default"){
		window.location = this.value;
	}else{
		window.location = "http://tycofis1.sitekit.net/ask-the-expert";
	}
}

// returns the html name of the page
function getPageName(){
	var sPath = window.location.pathname;
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	return sPage;
}

// display video on product pages
function displayVideoPlayer(e){
			var so = new SWFObject("flash/flv-player.swf", "FLV Player", "480", "307", "8", "#fff");
			so.addVariable("flvLocation", e); // value set in FCK editor. represents video file location, and is passed into flash
			so.useExpressInstall('../flash/expressinstall.swf'); // upgeade user
			so.write("flashcontent");
}


// add flash - nb mootools sometimes doesn't work in ie when flash is on a page


var MM_contentVersion = 4;
var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
if ( plugin ) {
		var words = navigator.plugins["Shockwave Flash"].description.split(" ");
	    for (var i = 0; i < words.length; ++i)
	    {
		if (isNaN(parseInt(words[i])))
		continue;
		var MM_PluginVersion = words[i]; 
	    }
		
	
	var MM_FlashCanPlay = MM_PluginVersion >= MM_contentVersion;
}
else if (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0 
   && (navigator.appVersion.indexOf("Win") != -1)) {
	document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n'); //FS hide this from IE4.5 Mac by splitting the tag
	document.write('on error resume next \n');
	document.write('MM_FlashCanPlay = ( IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & MM_contentVersion)))\n');
	document.write('</SCR' + 'IPT\> \n');
}



function placeflash(moviename,widd,hidd) {

document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="'+widd+'" height="'+hidd+'">');
document.write('<param name="movie" value="'+moviename+'">');
document.write('<param name="quality" value="high">');
document.write('<embed src="'+moviename+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+widd+'" height="'+hidd+'"></embed>');
document.write('</object>');

}



