addDOMLoadEvent=(function(){var load_events=[],load_timer,script,done,exec,old_onload,init=function(){done=true;clearInterval(load_timer);while(exec=load_events.shift())exec();if(script)script.onreadystatechange='';};return function(id,func){if(document.getElementById(id)){if(done)return func();if(!load_events[0]){if(document.addEventListener)document.addEventListener("DOMContentLoaded",init,false);/*@cc_on @*//*@if (@_win32)document.write("<script id=__ie_onload defer src=//0><\/scr"+"ipt>");script=document.getElementById("__ie_onload");script.onreadystatechange=function() {if(this.readyState=="complete")init();};@end @*/if(/WebKit/i.test(navigator.userAgent)){load_timer=setInterval(function(){if(/loaded|complete/.test(document.readyState))init();}, 10);}old_onload=window.onload;window.onload=function(){init();if(old_onload)old_onload();};}load_events.push(func);}}})();
function GetXmlHttpObject(){var xmlHttp=null;try{xmlHttp=new XMLHttpRequest();}catch(e){try {xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}}return xmlHttp;}
function getUrlParameter(name){name=name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");var regexS="[\\?&]"+name+"=([^&#]*)";var regex=new RegExp(regexS);var results=regex.exec(window.location.href );if(results==null )return "";else return results[1];}
function isNumeric(value){if(value==null||!value.toString().match(/^[-]?\d*\.?\d*$/))return false;return true;}
function getRadioValue(name){var radios=document.getElementsByName(name);var length=radios.length;for(var i=0;i<length;i++)if(radios[i].checked)return radios[i].value;}

function setLinks() {
	$('#sidebar a,#topnav li a').click(function(){
		if(this.href!='http://bobdrew.wordpress.com/') {
			var page = getLinkId(this.href);
			$(this).attr('href','http://www.ecovieenvironmental.com/#'+page);
			$.historyLoad(page);
		}
    });
	$('#inner-content a[href*="ecovieenvironmental"]').livequery('click',function() {
		if($(this).parent().attr('id')=='formlink') {
			$(this).parent().before('<h2 class="contact">Contact Us:</h2><div class="info"><h2 class="contact2 fn org">Ecovie</h2><p>Phone: <span>(404) 824-0318</span><br />Email: <img src="images/layout/image-Email.gif" alt="" /></p><div></div></div>').remove();
			$('form#landscapers-buildersform').show('slow');
			return false;
		} else {
			var page = getLinkId(this.href);
			$(this).attr('href','http://www.ecovieenvironmental.com/#'+page);
			$.historyLoad(page);
		}
	});
}

function getPage(page) {
	$('#inner-content').css('minHeight',0);
    $('#inner-content').slideUp(1000,loadContent).prev().animate({opacity:0},500);
    function loadContent() {$('#inner-content').load(page+'/ajax',showNewContent())}
    function showNewContent() {
		$('#inner-content').parent().removeClass().addClass('content').addClass(page);
    	$('#inner-content').prev().removeClass().addClass(page+'-deco').addClass('deco').animate({opacity:1.0},1000).end().slideDown(1000,function() {
			if(page=='index') {
				$('#flowplayer').html('<a href="/media/Ecovie.flv" id="player"></a>');
				$('#player').flowplayer("/jscripts/flowplayer-3.1.3/flowplayer.swf",{
					clip:{ 
			        	autoPlay: false,  
			        	autoBuffering: true}  
			    });
			}
		});
		$('#sidenav li,#topnav li a').removeClass('active');
		$('#sidenav li ul li a[href$='+page+']').parent().addClass('active').parent().parent().addClass('active');
		$('#topnav li a[href$='+page+']').addClass('active');
		//$('#inner-content').css('minHeight','375px');
	}
}

function getLinkId(link) {
	var params = link.split('/').reverse();
	if(params[0]=='')params[0]='index';
	return params[0].replace('#','');
}

function pageload(hash) {
	if(hash) getPage(hash);
	else $("#load").empty();
}

function setupForm() {
	$('form').livequery('submit',function() {
		var type = this.id;
		$.ajax({
			type: "POST",
			url: "js_functions.php?type="+type,
			data: $(this).serialize(),
			success: function(data) {
				var array = data.split('|||');
				$('div#status_message').html(array[1]);
				if(array[0]=='success') {
					$('form :input').each(function() {
						var type = this.type;
						var tag = this.tagName.toLowerCase();
						if(type=='text'||type=='password'||tag=='textarea') this.value='';
						if(!google_conversion_loaded) {
							/*$.getScript("http://www.googleadservices.com/pagead/conversion.js", function(data,textStatus) {
								alert('status of request was: ' + textStatus);
							});*/
							$('body').append('<img height="1" width="1" style="border-style:none;" alt="" src="http://www.googleadservices.com/pagead/conversion/1061803870/?label=VW-DCPTejwEQ3q6n-gM&amp;guid=ON"/>');
							google_conversion_loaded = true;
						}
					});
				} else if(array[2]=='') $('input#email').val("");
				else if(array[3]=='') $('input#phone').val("");
			}
		});
  	return false;
	});
}

addDOMLoadEvent('sidenav',setLinks);
addDOMLoadEvent('sidenav',setupForm);

$(function(){
	$.historyInit(pageload);
	$("a[rel='history']").click(function(){
		var hash = this.href;
		hash = hash.replace(/^.*#/, '');
		$.historyLoad(hash);
		return false;
	});
	$('#flowplayer').html('<a href="/media/Ecovie.flv" id="player"></a>');
	$('#player').flowplayer("/jscripts/flowplayer-3.1.3/flowplayer.swf",{
		clip:{ 
        	autoPlay: false,  
        	autoBuffering: true} ,
		playlist: [ 
	        // video 
	        '/media/Ecovie.flv',
			{url:'/media/AudioClips.gif',autoPlay:true,duration:0},
			{url:'/media/RadioInterview.mp3',autoPlay:true}]
    });
	$('div#media a.media-controls').click(function() {
		var rel = this.rel;
		var rel = rel.split(',');
		seekVideo(parseInt(rel[0]),parseInt(rel[1]));
		return false;
	});
});

var currentPlaylistIndex = -1; 
function seekVideo(playlistIndex,seconds) {
  var p = $f(); 
  // If the video is done playing, or not yet loaded, we have to  
  // give it a kickstart. 
  if (p.getState() == 1 || !p.isLoaded() || playlistIndex != currentPlaylistIndex) { 
    _startAndSeek(p, playlistIndex, seconds); 
  } else { 
    p.seek(seconds); 
    if (!p.isPlaying()) p.play(); 
  } 
} 
 
function _startAndSeek(p, playlistIndex, seconds) { 
  var initialLoad = true; 
  // Add a callback to do the actual seeking after the player 
  // loads and we've got enough of the video buffered to know 
  // where the seekpoints are. 
  p.onStart(function () { 
    if (!initialLoad) return; // Make sure we don't do this for future onStarts. 
    initialLoad = false; 
    p.seek(seconds);  
    if (!p.isPlaying()) p.play(); 
  }); 
  if (p.isLoaded()) { 
    p.play(playlistIndex); 
    currentPlaylistIndex = playlistIndex; 
  } else { 
    p.load(function () { p.play(playlistIndex); currentPlaylistIndex = playlistIndex }); 
  } 
} 
