//var HTTP_SERVER = 'http://www.ecovieenvironmental.com/';
var HTTP_SERVER = 'http://development.solutionbuilt.com/ecovie2/';
var videoHTML = new Array();

/*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_SERVER+'#'+page);
			$.historyLoad(page);
		}
    });
	$('#inner-content a[href*="'+HTTP_SERVER+'"]').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);
			if(getLinkClass(page) != 'download') {
				$(this).attr('href',HTTP_SERVER+'#'+page);
				$.historyLoad(page);
			} else if($(this).attr('target') != '_blank') {
				Shadowbox.open({
			        content:    $(this).attr('href'),
			        player:     "flv",
			        height:     236,
			        width:      420,
			        options: {
		                flashVars: {
			                image: HTTP_SERVER+'download/podcasts/AudioClips.gif',
			                autostart: true
		                }
	                } 
			    });
				return false;
			}
		}
	});
}

function getPage(page) {
	$('#inner-content').css('minHeight',0);
    $('#inner-content').slideUp(1000,loadContent).prev().animate({opacity:0},500);
    function loadContent() {$('div#media').css('display','none');$('#inner-content').load(page+'/ajax',showNewContent())}
    function showNewContent() {
    	var parentPage = getLinkClass(page);
		$('#inner-content').parent().removeClass().addClass('content').addClass(parentPage);
    	$('#inner-content').prev().removeClass().addClass(parentPage+'-deco').addClass('deco').animate({opacity:1.0},1000).end().slideDown(1000,function() {
			if(page=='media') {setVideos();$('ul#mycarousel').jcarousel();}
			else if(parentPage!='media') {
    			$.ajax({
    				type: "POST",
    				url: "js_functions.php?type=media&page="+page,
    				//data: $(this).serialize(),
    				success: function(data) {
    					$('div#media').html(data);
    					setMedia();
    				}
    			});
    		}
		});
		$('#sidenav li,#topnav li a').removeClass('active');
		$('#sidenav li ul li a[href$='+parentPage+']').parent().addClass('active').parent().parent().addClass('active');
		$('#topnav li a[href$='+parentPage+']').addClass('active');
	}
}

function getLinkId(link) {
	link = link.replace(HTTP_SERVER,'');
	if(link == '') link = 'index';
	return link.replace('#','');
}

function getLinkClass(link) {
	var page = link.split('/');
	return page[0];
}

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='';
					});
				} else if(array[2]=='') $('input#email').val("");
				else if(array[3]=='') $('input#phone').val("");
			}
		});
  	return false;
	});
}*/

function setVideos() {
	var i = 0;
	$('div#videos ul li').each(function() {
		var x = 0;
		var li = $(this);
		$('a', li).each(function() {
			var videoURL = getVideoURL($(this).attr('rel'));
			videoHTML[i] = $.flash.create({
				swf:videoURL,
				params:{allowScriptAccess:"always"},
				flashvars:{id:"myytplayer"},
				height:143,
				width:210
			});
			$(this).attr('rel', i)
				.click(function() {
					$('span.video', li).html(videoHTML[$(this).attr('rel')]);
					return false;
			});
			if(x == 0) {$('span.video', li).html(videoHTML[i]);}
			x++;
			i++;
		});
	});
}

function setPodcasts() {
	$('ul#podcasts li a').click(function() {
		var url = $(this).attr('href');
		if(url.indexOf('download.php') == -1) {
			Shadowbox.open({
		        content:    url,
		        player:     "flv",
		        height:     236,
		        width:      420,
		        options: {
	                flashVars: {
		                image: HTTP_SERVER+'uploads/Ecovie/AudioClips.gif',
		                autostart: true
	                }
                } 
		    });
			return false;
		}
	});
}

function getVideoURL(rel) {
	var params = rel.split('||');
	switch(params[0]) {
		case 'youtube':
			return 'http://www.youtube.com/v/'+params[1]+'?enablejsapi=1&amp;playerapiid=youtubeplayer';
			break;
		case 'local.divx':
		case 'local.flv':
		case 'local.mov':
		case 'local.mp3':
		case 'local.ogv':
		case 'local.silverlight':
		case 'local.wmv':
			return HTTP_SERVER+params[1].replace('ecovie2/','');
		default:
			return false;
	}
}

function setMedia() {
	var i= 0;
	$('div#media ul li a').each(function() {
		var videoURL = getVideoURL($(this).attr('rel'));
		if($(this).hasClass('video')) {
			if(videoURL) {
				videoHTML[i] = $.flash.create({
					swf:videoURL,
					params:{allowScriptAccess:"always"},
					flashvars:{id:"myytplayer"},
					height:150,
					width:220
				});
			} else {alert('Please contact your site administrator for help with this type of video');}
			$(this).attr('rel', i)
				.click(function() {
					$('div#media p').html(videoHTML[$(this).attr('rel')]);
					return false;
			});
			if(i == 0) {$('div#media p').html(videoHTML[i]);}
		} else if($(this).attr('target') != '_blank') {
			$(this)
				.attr('href',videoURL)
				.click(function() {
					Shadowbox.open({
				        content:    $(this).attr('href'),
				        player:     "flv",
				        height:     236,
				        width:      420,
				        options: {
			                flashVars: {
				                image: HTTP_SERVER+'uploads/Ecovie/AudioClips.gif',
				                autostart: true
			                }
		                } 
				    });
					return false;
				});

			if(i == 0) {
				var link2 = $(document.createElement("a"))
					.attr('href',videoURL)
					.html('<img src="'+HTTP_SERVER+'uploads/Ecovie/image-LogoSmall.png" alt="" />')
					.click(function() {
						Shadowbox.open({
					        content:    $(this).attr('href'),
					        player:     "flv",
					        height:     236,
					        width:      420,
					        options: {
				                flashVars: {
					                image: HTTP_SERVER+'uploads/Ecovie/AudioClips.gif',
					                autostart: true
				                }
			                } 
					    });
						return false;
					});
				$('div#media p').html(link2);
			}
		}
		i++;
	});
	$('div#media').css('display','block');
}

$(function(){
	$(document.body).addClass('jQuery');

	/*if($('div#sidebar').length > 0) {setLinks();setupForm();}
	
	$.historyInit(pageload);
	$("a[rel='history']").click(function(){
		var hash = this.href;
		hash = hash.replace(/^.*#/, '');
		$.historyLoad(hash);
		return false;
	});*/
	
	if($('div#videos').length > 0) {setVideos();}
	if($('ul#podcasts').length > 0) {setPodcasts();}
	else {setMedia();}
	
	$('ul#mycarousel').jcarousel();
});
/*
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 }); 
  } 
} 
*/
