/*
 * MAIN FUNCTIONS
 * (c) 2011
 * core menu and page functions, mostly for index.html
 */

// ===========
// SYSTEM //

	function detect_current_page() {
		var match = RegExp('[?&]page=([^&]*)').exec(window.location.search);
		var urlPage = match && decodeURIComponent(match[1].replace(/\+/g, ' '));
		if (urlPage=="" || urlPage==null || urlPage=="home") { urlPage="home"; }
		return urlPage;
	}
	
	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 = "iphone";
		}
		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_html5() {
		 /*Modernizr.load({
			test: Modernizr.canvas,
			yep : 'geo.js',
			nope: 'geo-polyfill.js'
		 });*/
		 var isHTML5 = false;
		 if (Modernizr.canvas && Modernizr.video) { isHTML5 = true; }
		 //if (Modernizr.video) { isHTML5 = true; }
		 return isHTML5;
	}
	
// ===
// PAGE

	function load_page(page) {
		var useCache=false;
		if (useCache==false) { 
			var cache = "?cache="+ new Date().getTime(); 
		} else {
			var cache = "";	
		}
		
		if (page=="services" || page=="newmedia" || page=="agency" || page=="brands") { 
			$(".pageContent").load("about/"+currentPage+'.html' + cache);
		} else if (page=="work" || page=="recent" || page=="archive") {
			$(".pageContent").load("work/videos.html" + cache);
		} else if (page=="clients") {
			$(".pageContent").load("work/clients.html" + cache);
		} else if (page=="who") {
			$(".pageContent").load("who/aboutus.html" + cache);
		} else if (page=="contact") {
			$(".pageContent").load("contact/"+currentPage+'.html' + cache);
		} else { 
			if (isHTML5Compatible) { 
			$(".pageContent").load("home/home.html" + cache);
			} else { 
				//alert(navigator.userAgent)
				$(".pageContent").html('<img src="home/images/golden_knob-white.jpg" width="900" height="500" border="0" /><div class="pullQuote">DRIVER will help get you from point A to point B seamlessly.</div>');
			}
			/*$(".pageContent").html(' <div class="slideshow"><img src="home/images/golden_knob-white.jpg" width="900" height="500" border="0" /></div><div class="pullQuote"><div class="pullQuoteText">DRIVER will help get you from point A to point B seamlessly.</div></div><script type="text/javascript" src="js/driver-slideshow.js"></script> '); */
		}
		//$('.debug').append("<BR>>loading page content...");
	}
	
	
// ================
// MENU  

	function set_menu_to(which) { 
		// make submenus
		if (which == "services") { 
			$('.navSub').append('<div class="navSubItem" id="services"><a href="index.html?page=services">SERVICES</a></div>');
			$('.navSub').append('<div class="navSubItem" id="agency"><a href="index.html?page=agency">AGENCY PRODUCTION</a></div>');
			$('.navSub').append('<div class="navSubItem" id="newmedia"><a href="index.html?page=newmedia">NEW MEDIA DISTRIBUTION</a></div>');
			$('.navSub').append('<div class="navSubItem" id="brands"><a href="index.html?page=brands">BRAND CONSULTANCY</a></div>');
		}
		else if (which=="work") { // || which=="recent" || which=="archive" || which=="clients") { 
			$('.navSub').append('<div class="navSubItem" id="recent"><a href="index.html?page=recent">RECENT</a></div>');
			$('.navSub').append('<div class="navSubItem" id="archive"><a href="index.html?page=archive">ARCHIVE</a></div>');
			$('.navSub').append('<div class="navSubItem" id="clients"><a href="index.html?page=clients">CLIENTS</a></div>');
			$('.navSub').append('<div class="navSubItem" id="happenings"><a href="http://drivertv.tumblr.com/" target="_blank">HAPPENINGS</a></div>');
	}
		// hilite current top menu
		$('.navItem#'+which).addClass("navItemSelected");
		// hilite current submenu
		$('.navSubItem#'+currentPage).addClass("navSubItemSelected");
	}
	
	
// ================
// PAGE

	function extend_page_height_by(extendHeight) { // used by clients.html
		var headHeight = 336;
		var pageHeight = 900;
		var newHeight = extendHeight + pageHeight; // ie, 900+300=1200
		var columnsHeight = (newHeight - headHeight) - 64; 
		$('.pageBG').css({'height':newHeight+"px"});
		$('.columns').css({'height':columnsHeight+"px"});
	}

// ================
// UNUSED

/*

	function update_footer_position() { 
		var tall = ('innerHeight' in window? window.innerHeight :
			document.compatMode!=='BackCompat'? document.documentElement.clientHeight :
			document.body.clientHeight
		);
		$('.footer').css({ 'margin-top':(tall-0)+"px" }).show(); 
	}
	
	function update_footer_position_to(newy) { 
		$('.bottomStripeLine').css({ 'top':(newy)+"px" });
	}
*/
	
// ================
// MISC //

	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);
	}

	
// ===============
