/*
 * MAIN FUNCTIONS
 * (c) 2011
 * global menu and page functions
 *
 * contains:
 *  site-wide global vars
 *  audio controls
 *  cookies
 *
 * ---
 * 
 * information flow: 
 *    www.bp.com/index.html?page=something
 * index.html is the header for all pages.
 * /somedir/index.php redirects to /index.html?page=somedir
 * the value of ?page= is checked and loads corresponding content.html and menu options
 *
 * 
*/


// ================================
// PUBLIC VARS 

	// system
	var canCanvas = Modernizr.canvas; // boolean
	var browser = detect_browser(); // string	
	var isMP4Compatible = detect_video(); // bool
	// global
	var currentPage="";
	var currentSubPage="";
	var masterVolume = true; // on
	var videoIsPlaying=false;
	var bgMusic;
	//bgMusic.volume=1;

// ================================
// FUNCTIONS

	// called after redirect
	function page_init() {
		//if (netscape.security.PrivilegeManager.enablePrivilege) netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
		//
		adjust_for_screen_height();
		//
		read_all_cookies();	
	}
	
	function adjust_for_screen_height() {
		var screenWidth = window.innerWidth;
		var screenHeight = window.innerHeight;
		//trace(screenHeight);
		if (screenHeight<700) { 
			$('.page').css({"margin-top":"-10px"}); 
			window.scrollTo(0,50);
		}
	}

// ================================
// SYSTEM 
	
	function detect_browser() {
		var detected="";		
		if ( navigator.userAgent.match(/firefox/i) ) { 
			detected= "firefox";
		} 
		else if ( navigator.userAgent.match(/safari/i) ) { 
			detected= "safari";
		} 
		else if ( navigator.userAgent.match(/IE/i) ) { 
			detected= "IE";
		} 
		else { 
			detected= "unknown";
		}
		// determine if iphone
		if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || navigator.userAgent.match(/iPad/i)) {
			detected = "iOS";
		}
		return detected;
	}
	
	function detect_video() {
		var mp4Compatible=false; // init
		var v = document.createElement('video');
		mp4Compatible = !!(v.canPlayType && v.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"').replace(/no/, ''));	
		return mp4Compatible;
	}
	
	function detect_audio() {
		//var canAudio=false; // init
		//var a = document.createElement('audio');
		//canAudio = !!(a.canPlayType.replace(/no/, ''));	
		//return canAudio;
		return !!document.createElement('audio').canPlayType;
	}
	
	function detect_canvas() {
		return !!document.createElement('canvas').getContext;
	}
	
	function detect_compatibility() {
		var IEVer = get_IE_version();
		if (IEVer>1 && IEVer<9) { isCompat=false; }
		//var isCompat = true;
		//isCompat = Modernizr.canvas;
		//isCompat = Modernizr.audio;
		if (!isCompat || isCompat==false || isCompat=='') { window.location="unsupported/index.html"; } 
	}
	
	function get_IE_version() { // Returns the version of Internet Explorer or a -1 if not IE
	  var rv = -1; 
	 // var app = navigator.userAgent.match(/IE/i);
	 // app = navigator.appName == 'Microsoft Internet Explorer'
	  if (navigator.appName == 'Microsoft Internet Explorer') {
		 var ua = navigator.userAgent;
		 var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		 if (re.exec(ua) != null)
			rv = parseFloat( RegExp.$1 );
	  }
	  return rv;
	}
		

// SOUND 
	
	function adjust_current_volume(destVol) {
		if (destVol==false && videoIsPlaying==true) { mp4Player.volume=0; }
		else if (destVol==true && videoIsPlaying==true) { mp4Player.volume=1; } // (videoObject.volume/10)
		adjust_bg_music(destVol);
	}
	
	function adjust_bg_music(toggleVol) { 
		if (canAudio) { //(Modernizr.audio) {
			if(!bgMusic) bgMusic = new Audio(); //start_music();
			if (toggleVol==true && videoIsPlaying==false) { 
				bgMusic.volume=1;
				bgMusic.play();
			} else { 
				bgMusic.volume=0;
				bgMusic.pause();
			}
		}
	}
	
	function start_music() {
	  	if (canAudio) { //(Modernizr.audio) {
			bgMusic = new Audio();
			$(bgMusic).attr({"id":"bgmusic", "autoplay":"true"})
			$('body').append(bgMusic);
			//bgMusic = document.getElementById("bgmusic");
			//bgMusic.volume=1;
			bgMusic.volume=0.7;
			//
			if (currentSubPage=="home") {
				var introMusicPath = "sound/intro_music_long";
				var amp3 = document.createElement('source');
				$(amp3).attr({"src":introMusicPath+".mp3", "type":"audio/mpeg"});
				var awav = document.createElement('source');
				$(awav).attr({"src":introMusicPath+".ogg", "type":"audio/ogg"});
				$("#bgmusic").append(amp3).append(awav);
			}
			//
			else if (currentPage=="contact") {
				var introMusicPath = "sound/contact_music";
				var amp3 = document.createElement('source');
				$(amp3).attr({"src":introMusicPath+".mp3", "type":"audio/mpeg"});
				var aogg = document.createElement('source');
				$(aogg).attr({"src":introMusicPath+".ogg", "type":"audio/ogg"});
				bgMusic.volume=0.5;
				$("#bgmusic").append(amp3).append(aogg);
			}
		}
	}

// ==================
// MISC
	
	function set_master_volume(toVal) { 
		if (toVal==null) { toVal = !masterVolume; }  // if not param then toggle
		masterVolume = toVal; 
		$('#volume').attr({ "src":"images/icon-volume-"+masterVolume+".png" });
		//
		adjust_bg_music(masterVolume); //// if (masterVolume==true) { bgMusic.volume=1; } else { bgMusic.volume=0; }
		if (masterVolume) { var setVolume="on"; } else { setVolume="off"; }
		// bake
		set_cookie("masterVolume", setVolume, 365); 
		//
		//try {
		//mp4Player.muted = masterVolume;
		//} catch(error) { } //trace("note: can't find video to mute"); }
		// flash video fscommand?
		//
		trace("- Volume: cookie:"+ get_cookie("masterVolume") + " / mV:"+masterVolume)
	}

// ===================
// BAKE COOKIES

	function read_all_cookies() {
		// volume
		var masterVolCookie=get_cookie("masterVolume"); 
		if (masterVolCookie=="off") { masterVolume=false; } else { masterVolume=true; }
		trace("- setting vol to "+masterVolCookie);
		// update display and write cookie
		set_master_volume(masterVolume); 
	}
	
	
	function set_cookie(c_name, value, expdays) {
		var exdate=new Date();
		exdate.setDate(exdate.getDate() + expdays);
		var c_value=escape(value) + ((expdays==null) ? "" : "; expires="+exdate.toUTCString());
		document.cookie=c_name + "=" + c_value;
	}
	
	function get_cookie(c_name) {
		var i,x,y,ARRcookies=document.cookie.split(";");
		for (i=0;i<ARRcookies.length;i++) {
			x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
			y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
			x=x.replace(/^\s+|\s+$/g,"");
			if (x==c_name) {
		 		return unescape(y);
		 	}
		}
	}
	

// ================================
// MISC FUNCTIONS 

	function trace(what) {
		$('.debug').append("<BR>"+what);	
	}
	
	function max_decode(str) { 
		var rstr = decodeURIComponent(str);
		rstr = rstr.replace(/&amp;/g, '&').replace(/\+/g,' ');
		return rstr;
	}
	
	String.prototype.capitalize = function() {
    	return this.charAt(0).toUpperCase() + this.slice(1);
	}
	
	// =====

	function included(arr, obj) {
   	for(var i=0; i<arr.length; i++) {
			if (arr[i] == obj) return true;
    	}
	}
	

// ===
