// JavaScript Document
//<![CDATA[ 

var panorama ;
var map;
var timertime = 12000;
var panSpeed = 150;
var streetView = true;
var currentstreetViewNum = 0;

var currentZoom = 0;
var currentYaw = 0;
var currentPitch = 0;
var timerPan;
var timerSwitchBk;
var panOn = true;
var artBk = false;
var artBkInt;
var panDirRight = true;
var mute=false;
var youtubeMute=false;

function initialize() 
{	
	if(FlashDetect.installed)
	{
		var homeBk = new google.maps.LatLng (40.759025447444884, -73.98446559906006); 
	
		var mapOptions = 
		{ 
			center: homeBk, 
			zoom: 14, 
			mapTypeId: google.maps.MapTypeId.ROADMAP, 
			streetViewControl: true
		}; 
		
		map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);  
		var panoramaOptions = { 
			position: homeBk, 
			pov: { heading: 0, pitch: 2, zoom: 1 },
			linksControl:false,
			navigationControl: false,
			addressControl:false  
		};
		
		if (login==true)
		{
			
		}
		else
		{
			if (readCookie('PosLat') != null & readCookie('PosLng')!=null & readCookie('PovHead')!=null & readCookie('PovPitch')!=null & readCookie('PovZoom')!=null)
			{
				homeBk = new google.maps.LatLng(readCookie('PosLat'),readCookie('PosLng'));  
				var panoramaOptionsCookie = {enableCloseButton:false,linksControl:false, navigationControl:false, addressControl:false, position:homeBk, pov: { 
				heading: parseFloat(readCookie('PovHead')), 
				pitch:parseFloat(readCookie('PovPitch')) , 
				zoom:parseFloat(readCookie('PovZoom'))		
				}
				}
				
				panorama = new google.maps.StreetViewPanorama(document.getElementById("map_canvas"), panoramaOptionsCookie); 
				map.setStreetView(panorama);	
		
				currentYaw = parseFloat(readCookie('PovHead'));
				currentPitch = parseFloat(readCookie('PovPitch'));
				currentZoom = parseFloat(readCookie('PovZoom'));
		
				document.getElementById("clearView").style.display = "";
				
			}
			else
			{
				panorama = new google.maps.StreetViewPanorama(document.getElementById("map_canvas"), panoramaOptions); 
				map.setStreetView(panorama);	
				currentYaw = 0;	
			}
		}
	
		//artBk
	
			//autoPan
		if (readCookie('autoPan') != null)
		{
			if (readCookie('autoPan') == 'false')
				panOn = false;
			else
				panOn = true;
		}		
			
		
		if (panOn==false)
		{
			document.getElementById("autoPan").value = "PAN ON";		
		}
		else
		{
			document.getElementById("autoPan").value = "PAN OFF";
			timerPan = window.setInterval(mapPan, panSpeed);
		}
		
		if (readCookie('artBk') != null)
		{
			if (readCookie('artBk') == 'false')
				artBk = false;
			else
				artBk = true;
		}
		
		if (artBk==false)
		{
			timerSwitchBk = window.setInterval(randomImage, timertime);	
			document.getElementById("artModeBttn").value = "ART ON";					
		}
		else
		{
			clearInterval(timerPan);
			clearInterval(timerSwitchBk);
			clearInterval(artBkInt);	
			artBkInt = window.setInterval(artWorkBkChange, timertime);			
			document.getElementById("artModeBttn").value = "ART OFF";			
		}
		
		
		var agent=navigator.userAgent.toLowerCase();
		var is_iphone = ((agent.indexOf('iPhone')!=-1));
		
		if (is_iphone) 
		{  
		}
		else
		{
			if (readCookie('firstRun') == null )
			{	
				OpeniFrameFirstLoadPostCode();
			}
		}
		
		getMuteValue();
		
	}
	else
	{
		lookForFlash();	
	}
	
	//
} 

function getMuteValue()
{
		if (readCookie('muteSounds') != null)
		{
			if (readCookie('muteSounds') == 'false')
			{
				mute = false;
				document.getElementById("soundsBttn").value = "MUTE"
			}
			else
			{
				mute = true;
				document.getElementById("soundsBttn").value = "UNMUTE"
			}
		}
	
	return mute.toString();
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

//Blog Search
var blogSearch;
google.load('search', '1');

function searchComplete() 
{
  // Check that we got results
  document.getElementById('blogSearchResults').innerHTML = '';
  if (blogSearch.results && blogSearch.results.length > 0) {
    var ul = document.createElement('ul');
	for (var i = 0; i < blogSearch.results.length; i++) {

      // Create HTML elements for search results
      var li = document.createElement('li');
      var a = document.createElement('a');
      a.href = blogSearch.results[i].postUrl;
      a.innerHTML = blogSearch.results[i].title;

      // Append search results to the HTML nodes
      li.appendChild(a);
		ul.appendChild(li);
	}
	var results = document.getElementById('blogSearchResults');
	results.appendChild(ul);
  }
}

//Search blogs
function onLoad() {

  // Create a BlogSearch instance.
  blogSearch = new google.search.BlogSearch();
  
  // Return up to eight results
  
  var sPath = window.location.pathname;
  var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
  
  if (sPage=='buzzArchiveList.php')
	  blogSearch.setResultSetSize(google.search.Search.LARGE_RESULTSET);  	
  else
	  blogSearch.setResultSetSize(google.search.Search.SMALL_RESULTSET);


  // Order search results in reverse chronological order by publication date
  blogSearch.setResultOrder(google.search.Search.ORDER_BY_DATE);
  
  // Set searchComplete as the callback function when a search is complete.  The
  // blogSearch object will have results in it.
  blogSearch.setSearchCompleteCallback(this, searchComplete, null);

  // Execute search query
  blogSearch.execute('"Kid Adrift" -"free download" -mp3 -Cyantific');

}

//You Tube Video Bar

function OpeniFrameYouTubePlayer(videoId)
{
	clearInterval(timerSwitchBk);
	clearInterval(timerPan);		
	clearInterval(artBkInt);	
	
	if (mute==false)
	{
		MuteFlashSounds()
		youtubeMute = false;
	}
	else
		youtubeMute = true;
	
	$.fancybox(
		'youTubePlayer.html?id='+videoId,
		{
			'width'         		: 590,
			'height'        		: 440,
			'autoDimensions' : false,
			'autoScale'		: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'type'			: 'iframe',
			'onClosed'		: function() 
			{
				resetCams();
				if (youtubeMute == false)
					MuteFlashSounds();
			}
		})
}

//First load No postCode set
function OpeniFrameFirstLoadPostCode()
{
	clearInterval(timerPan);
	clearInterval(timerSwitchBk);
	clearInterval(artBkInt);			

	$.fancybox(
		'setPostcode.html',
		{
			'width'			: 320,
			'height'		: 75,
			'autoScale'		: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'type'			: 'iframe',
			'onClosed'		: function() 
			{
				resetCams();
				createCookie('firstRun', 'false', 360);
				
			}
		})
}

//Fancy Box open function	
function OpeniFrameURL()
{
	var cityValue = document.getElementById("queryInput").value;
	
	if (cityValue=='City/Postcode')
		cityValue = '';
		
	clearInterval(timerSwitchBk);
	clearInterval(timerPan);		
	clearInterval(artBkInt);
		
	$.fancybox(
		'location.php?postcode='+cityValue,
		{
			'width'			: '65%',
			'height'		: '65%',
			'autoScale'		: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'type'			: 'iframe',
			'onClosed'		: function() 
			{
				document.getElementById("queryInput").value = 'City/Postcode';
				resetCams();
			}
		})
}

function resetCams()
{
	clearInterval(timerSwitchBk);
	clearInterval(timerPan);
	clearInterval(artBkInt);
				
	if (artBk==false)
	{			
		if (readCookie('autoPan') != null)
		{
			if (readCookie('autoPan') == 'false')
				panOn = false;
			else
				panOn = true;
		}
						
		if (panOn==false)
		{
			
		}
		else
		{
			var type = parseFloat(randomXToY(1,2,0));
			if (type==1)
				panDirRight=true;
			else
				panDirRight=false;
			timerPan = window.setInterval(mapPan, panSpeed);
		}

		timerSwitchBk = window.setInterval(randomImage, timertime);
		clearInterval(artBkInt);
	}
	else
	{
		clearInterval(timerPan);
		clearInterval(timerSwitchBk);
		clearInterval(artBkInt);	
		artBkInt = window.setInterval(artWorkBkChange, timertime);		
	}
				
}

//Clear view CCTV if set
function clearCurrentView()
{
	eraseCookie('PovHead');
	eraseCookie('PovPitch');	
	eraseCookie('PovZoom');	
	document.getElementById("clearView").style.display = "none";		
	resetCams();

}
	
function eraseCookie(name) {
	createCookie(name,"",-1);
}
	

//Homepage text box
function clickclear(thisfield, defaulttext) 
{
	if (thisfield.value == defaulttext) 
	{
		thisfield.value = "";
	}
}
//Homepage search box put default text back
function clickrecall(thisfield, defaulttext) 
{
	if (thisfield.value == "") 
	{
		thisfield.value = defaulttext;
	}
}

//Sets a new location for the background
function setNewLocation()
{
	var type = parseFloat(randomXToY(1,2,0));
	if (type==1)
		panDirRight=true;
	else
		panDirRight=false;
	
	if (readCookie('PosLat') != null & readCookie('PosLng')!=null & readCookie('PovHead')!=null & readCookie('PovPitch')!=null & readCookie('PovZoom')!=null)
	{
		homeBk = new google.maps.LatLng(readCookie('PosLat'),readCookie('PosLng'));  
		panorama.setPosition(homeBk);

		panorama.setPov({ 
	    heading: parseFloat(readCookie('PovHead')), 
    	pitch:parseFloat(readCookie('PovPitch')) , 
		zoom:parseFloat(readCookie('PovZoom'))		
		});
		
		map.setStreetView(panorama);

		currentYaw = parseFloat(readCookie('PovHead'));
		currentPitch = parseFloat(readCookie('PovPitch'));
		currentZoom = parseFloat(readCookie('PovZoom'));	
		document.getElementById("clearView").style.display = "";			
		
		if (artBk==false)
		{			
			streetView = true;
			var hideImCall = getFlashMovie('flashOverlay');
			if (hideImCall!=null)
				getFlashMovie('flashOverlay').HideImage();	
		}
	}
}

function stopStartPan()
{
	if (panOn==true)
	{
		panOn=false;
		document.getElementById("autoPan").value = "PAN ON";
		clearInterval(timerPan);		
		createCookie('autoPan', 'false', 360);
	}
	else
	{
		panOn=true;		
		document.getElementById("autoPan").value = "PAN OFF";	
		clearInterval(timerPan);
		timerPan = window.setInterval(mapPan, panSpeed);		
		createCookie('autoPan', 'true', 360);
	}
}

//Form Button Rollover
function hov(loc,cls)
{
  if(loc.className)
    loc.className=cls;
}

//Flash calls
function getFlashMovie(movieName) 
{
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}
//flashOverlay
function callToActionscript(flash) 
{
	if (currentstreetViewNum > 2)
	{
		currentstreetViewNum=0;
		streetView = false;
		var temp1 = getFlashMovie(flash)
		if (temp1!=null)
			getFlashMovie(flash).ChangeImage();
	}
	else
	{	
		randomStreetView();
		streetView = true;
		var temp2 = getFlashMovie(flash)
		if (temp2!=null)		
			getFlashMovie(flash).HideImage();			
	}				
}

//Sets background
function randomImage()
{	
	if (readCookie('PosLat') != null & readCookie('PosLng')!=null & readCookie('PovHead')!=null & readCookie('PovPitch')!=null & readCookie('PovZoom')!=null)
		{
			if (streetView==true)
			{
				streetView = false;
				var temp1 = getFlashMovie('flashOverlay')
				if (temp1!=null)
					getFlashMovie('flashOverlay').ChangeImage();
			}	
			else
			{
				streetView = true;
				var temp2 = getFlashMovie('flashOverlay')
				if (temp2!=null)				
					getFlashMovie('flashOverlay').HideImage();	
			}
		}
	else
	{
		callToActionscript('flashOverlay');			
	}
}

function randomXToY(minVal,maxVal,floatVal)
{  
	var randVal = minVal+(Math.random()*(maxVal-minVal));  
	return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
}

//Pans the google map
function mapPan()
{
	if (panDirRight==true)
		currentYaw += 2;
	else
		currentYaw -= 2;
	
	panorama.setPov({heading: currentYaw, pitch: currentPitch, zoom: currentZoom})
}

function randomStreetView()
{
	var panoramaOptions;
	var homeBk = new google.maps.LatLng (40.759534379284226, -73.98470565676689); 
	switch(currentstreetViewNum)
	{
		case 0:	
			homeBk = new google.maps.LatLng (55.85791251602797, -4.272007942199707); 					
		break;			
		case 1:		
			homeBk = new google.maps.LatLng (55.880498721862196, -4.190855026245117); 						
		break;
		case 2:
			homeBk = new google.maps.LatLng (40.759025447444884, -73.98446559906006); 					
	  	break;
		}

		currentstreetViewNum = currentstreetViewNum+1;
		panorama.setPosition(homeBk);
		panorama.setPov({ 
	    heading: 0, 
    	pitch:0 , 
		zoom:0	
		});
		
		var type = parseFloat(randomXToY(1,2,0));

		if (type==1)
			panDirRight=true;
		else
			panDirRight=false;
			
		map.setStreetView(panorama);	
		currentYaw = 0;	
}

function createCookie(name,value,days) 
{
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function artWorkBkChange()
{
	var temp1 = getFlashMovie('flashOverlay')
	if (temp1!=null)
		getFlashMovie('flashOverlay').ChangeImage();	
}

function artBkChangeMode()
{
	if (artBk==false)
	{
		artWorkBkChange();
		artBk=true;
		createCookie('artBk', artBk, 360);
		document.getElementById("artModeBttn").value = "ART OFF";
	}
	else
	{
		var temp1 = getFlashMovie('flashOverlay')
		if (temp1!=null)
			getFlashMovie('flashOverlay').HideImage();	
		artBk=false;
		createCookie('artBk', artBk, 360);	
		document.getElementById("artModeBttn").value = "ART ON";			
	}
	resetCams();
}

function MuteFlashSounds()
{
	if (mute==false)
	{
		document.getElementById("soundsBttn").value = "UNMUTE"
		mute = true;	
		createCookie('muteSounds', 'true', 7);			
	}
	else if (mute==true)
	{
		mute = false;
		document.getElementById("soundsBttn").value = "MUTE"
		createCookie('muteSounds', 'false', 7);			
	}
	var temp = 	getFlashMovie('cctvMan');
	if (temp !=null)
		getFlashMovie('cctvMan').muteUnmuteSound();	
}

function showMuteButton()
{		
	if (document.getElementById("soundsBttn")!=null)
		document.getElementById("soundsBttn").style.display = "";		
}

function showArtMode()
{
	if (document.getElementById("artModeBttn")!=null)
		document.getElementById("artModeBttn").style.display = "";
}

function lookForFlash()
{
	if(!FlashDetect.installed)
	{
		if (document.getElementById("locationBar")!=null)
			document.getElementById("locationBar").style.display = "none";
		
		if (document.getElementById("videoChannelMainDiv")!=null)
			document.getElementById("videoChannelMainDiv").style.display = "none";			
	}
}
//]]> 