/*  ================================
     Sitewide JavaScript
    ================================ */

	// Detected and set from HTML
	var isIE6;
	isIE6 = false;


	function getQuerystring(key, default_)
	{
		if (default_==null) default_="";
		key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
		var qs = regex.exec(window.location.href);
		if(qs == null)
			return default_;
		else
			return qs[1];
	}

	var editTemplate = getQuerystring('editTemplate');
	var editWorking = getQuerystring('working');
	var templateView = getQuerystring('templateview');

function getSwf()
{
	if(deconcept.SWFObjectUtil.getPlayerVersion()['major'] >= 9)
	{
		if(window.location.hash=='#html-only' || window.location=="http://thundertech.com/careersform.aspx" || window.location=="http://www.thundertech.com/careersform.aspx" || editTemplate == "1" || templateView == "1" || editWorking == "1")
		{
			//we do nothing
			document.getElementById("swf-page-main").style.display = "none";
			document.getElementById("no-swf").style.display = "block";
		}		
		else
		{
			document.getElementById("swf-page-main").style.display = "block";
			document.getElementById("no-swf").style.display = "none";
			var so = new SWFObject("/flash/thundertech-1.1.2.swf", "thunder-swf", "980", "100%", "9", "");
			so.addVariable("dconsole", "no");
			so.addVariable("useDynamic", "1");
			so.addVariable("beginAt", parseHash(window.location.hash));
			so.addParam("quality", "high");
			so.addParam("wmode", "transparent");
			so.write("swf-area");
			verticalSize(800);
		}
	}
	else {
		document.getElementById("swf-page-main").style.display = "none";
		document.getElementById("no-swf").style.display = "block";	
	}
}

			function getHeight(c) { var h = window.innerHeight != null? window.innerHeight: document.body != null? document.body.clientHeight:800; return Math.max(850, Math.max(h, c)); }
			function setHTMLHash(v)
			{
				window.location.hash = "#"+v;
				ignore = true;
			}
			var lastV = 800;
			function verticalSize(v)
			{
				lastV = v;
				executeSize();
			}
			function executeSize(e)
			{
				document.getElementById('swf-area').style.height = getHeight(lastV) + 'px';
			}
			window.onresize = executeSize;
			var lastHash = 'No hash';
			var ignore = false;
			setInterval(checkHash, 500)
			function checkHash()
			{
				if(lastHash=='No hash')
				{
					lastHash = window.location.hash;
				}
				else if(ignore==true)
				{
					ignore = false;
					lastHash = window.location.hash;
				}
				else if(lastHash!=window.location.hash)
				{
					lastHash = window.location.hash;
					document.getElementById('thunder-swf').setSWFHash(parseHash(lastHash));
				}
			}
			function parseHash(h)
			{
				var cLoc = window.location.href;
				
				if (!cLoc.contains("default.aspx")){
					var nHash = cLoc.replace("http://www.thundertech.com/", "");
					nHash = nHash.replace("http://thundertech.com/", "");									
					nHash = nHash.replace(".aspx","");					
					h = nHash;
				}
				
				if((h=='') || (h=='#'))
				{
					h = 'home';
				}
				else if(h)
				{
					if(h.indexOf('#')==0)
					{
						h = h.substr(1);
					}
				}
				else
				{
					h = 'home';
				}
				return h;
			}

	
	$(document).ready(function(){
			//$("#Map > area").mouseout(function () {navOff();});
			
			$("#sidenav > li").mouseenter(function () {$(this).addClass('nav-on');  });
			$("#sidenav > li").mouseleave(function () {$(this).removeClass('nav-on'); });

			$(".social-icons > a > img").mouseenter(function(){ btnOn(this);}).mouseleave(function(){ btnOff(this);});
	});
			
	function btnOn(btn){
		btn.src = btn.src.substring(0, btn.src.lastIndexOf(".")) + "_RO" + btn.src.substring(btn.src.lastIndexOf("."));
	}
	
	function btnOff(btn){
		btn.src = btn.src.replace("_RO","");
	}
	
	function navRO(top, left, temp) {	
		//safe function to show an element with a specified id
		$(".highlight").css("top", top + "px");
		$(".highlight").css("left", left + "px");	
	}
	
	//displays the first ul in the container when called in rollover script init()
	function navOff(temp) {
		//safe function to hide an element with a specified id
		$(".highlight").css("top", "-3000px");
	}
	
	function changeBanner(name){
		
		switch(name){
			case 'gmm':
				$('.mainBanner > img').attr('src', '/images/masterpage_templates/banner-homeGMM.jpg');
				$('.bannerLnk').attr('href', 'http://www.youtube.com/user/thundertechvideo#p/a/BD51949294B428BD/0/Kswth6R9epU');
				videoSrcTo('http://www.thundertech.com/media/videos/v-gmm306/v-gmm306.m3u8');
				break;
			case 'integrated':
				$('.mainBanner > img').attr('src', '/images/masterpage_templates/banner-ttPerspective.jpg');
				$('.bannerLnk').attr('href', 'http://www.youtube.com/user/thundertechvideo#p/c/C22E560B7E44C473/3/7ZN42rmLp-w');
				videoSrcTo('http://www.thundertech.com/media/videos/v-ttMobile/v-ttMobile.m3u8');
				break;
			case 'reel':
				$('.mainBanner > img').attr('src', '/images/masterpage_templates/banner-portfolioReel.jpg');
				$('.bannerLnk').attr('href', 'http://www.youtube.com/user/thundertechvideo#p/u/10/SMOWDqvwYAM');
				videoSrcTo('http://www.thundertech.com/media/videos/v-tt_portReel09/v-tt_portReel09.m3u8');
				break;
		}
	}
	
	function videoSrcTo(thisVideoSrc)
	{
		var videoQuery = $('.feature-html5video').children('video');
		if(videoQuery.length > 0)
		{
			videoQuery[0].src = thisVideoSrc;
			videoQuery[0].load();
			videoQuery[0].play();
		}
	}
	
	function changeDetails(x){
		if($(x).html() == "show details"){
			$(".portfolioText").css("display", "block");
			$(".portfolioType").css("display", "block");
			$(".case-study").css("display", "block");
			
			$(x).html("hide details");
		} else {
			$(".portfolioText").css("display", "none");
			$(".portfolioType").css("display", "none");
			$(".case-study").css("display", "none");
			
			$(x).html("show details");
		}
	}
	
	function closeGallery(){
	    $(".portfolioWindow").hide();
	}
	
	$(document).ready(function(){
	    try {
	        if ($("#overridepageTitle").length > 0){	    
	            var overridepageTitle = $("#overridepageTitle").val().toUpperCase();
	            var overridepageTitleLink = $("#overridepageTitleLink").val();
	            var fLink = '<a href="' + overridepageTitleLink + '">' + overridepageTitle + '</a>';
	            $("#titleH1").html(fLink);
	        }
	        else {
	            var pageTitle = $("#pageTitle").val().toUpperCase();	    
	            $("#titleH1").html(pageTitle);
	        }
	    } catch(E){}			    
	    
	    loadHeaderArea();
	});
	
	function loadHeaderArea(){
	    try {
	        var rotateImg = $(".rotateImg");	    
	        var pageAccent = $("#pageAccent");
    	    
	        if (pageAccent.val().contains('.jpg')){
	            var pageTref = $("#pageTref").val();
	       
	            rotateImg.attr('src', '/services/imagehandler.ashx?action=genHeaderImage&file=' + pageAccent.val());
	            rotateImg.click(function(){
	                if (pageTref != '')
	                    window.location = pageTref;
	            });
	        }
	    }
	    catch(E){}	    
    }
    
    String.prototype.contains = function(txt){
        if (this.indexOf(txt) >= 0) return true;
        return false;
    }
    
    String.prototype.left = function(n){
	    if (n <= 0)
	        return "";
	    else if (n > String(this).length)
	        return this;
	    else
	        return String(this).substring(0,n);
    }
    
    String.prototype.right = function(n){
        if (n <= 0)
           return "";
        else if (n > String(this).length)
           return this;
        else {
           var iLen = String(this).length;
           return String(this).substring(iLen, iLen - n);
        }
    }