// Site Functions

var curr_menu_id;
var rss_array;
var anum = 0;

// update page on #hash changes
$(window).bind('hashchange', function() {
	site_nav_load(window.location.hash,'');
});

function ga_track(pageview)
{
	// "_trackEvent" is the pageview event, 
	_gaq.push(['_trackPageview', pageview]);
}


function site_nav_load(site_hash,uri_query)
{

// Reset menu item to deselected state	
	$("#mi_" + curr_menu_id).attr("src", "images/menu_" + curr_menu_id + "_off.png");
	$("#ms_" + curr_menu_id).stop().animate({ opacity: 0.4 }, { duration:3000, easing:'easeOutExpo' });

// Store clicked on menu item
	curr_menu_id = site_hash.replace(/#/g, "");

// Set menu item to selected state
	nav_select(curr_menu_id);

	ga_track(uri_query + site_hash);
	
	switch (site_hash)
	{
		case "#featured":
			clear_div();
			reset_to_top();
			$("#main_content").load("page_featured.inc.php");
			flipbook_trigger("#featured");
	  break;
		case "#archive":
			clear_div();
			reset_to_top();
			$("#main_content").load("page_archive.inc.php");
			flipbook_trigger("#archive");
	  break;
		case "#contact":
			clear_div();
			reset_to_top();
			$("#main_content").load("page_contact.inc.php");
			flipbook_trigger("#contact");
	  break;
		case "#permalink":
			get_permalink_post(uri_query);
			flipbook_trigger("#blog");
	  break;
		case "#blog":
			get_blog_posts(uri_query);
			flipbook_trigger("#blog");
	  break;
		case "#techspecs":
			clear_div();
			reset_to_top();
			$("#main_content").load("page_techspecs.inc.php");
			flipbook_trigger("#techspecs");
	  break;
		case "#play":
			show_lightbox(uri_query);
			flipbook_trigger("#featured");
	  break;
		case "#playlist":
			show_lightbox_playlist(uri_query);
			flipbook_trigger("#featured");
	  break;
		default:
			nav_select('featured');
			curr_menu_id = 'featured';
			$("#main_content").load("page_featured.inc.php");
			flipbook_trigger("#featured");
	  break;
	}
}

function nav_select(menu_id)
{
	//$("#mi_" + menu_id).stop().attr("src", "images/menu_" + menu_id + "_on.png");
	$("#ms_" + menu_id).stop().css({ opacity: 1.0 });
}

function nav_deselect(menu_id)
{
// Set to deselected state if menu item is not currently clicked on
	if (menu_id !== curr_menu_id)
	{
		//$("#mi_" + menu_id).stop().attr("src", "images/menu_" + menu_id + "_off.png");
		$("#ms_" + menu_id).stop().animate({ opacity: 0.2 }, { duration: 2000, easing:'easeOutQuad' });		
	}
}

function get_permalink_post(wp_link)
{
	clear_div();
	reset_to_top();
	$("#main_content").html("<img src='images/ajax-loader.gif' style='margin: 200px 0 0 200px;'>");

	$.get("http://colr.com/blog/?" + wp_link + "&ajax=true#content",
	function(data){
			$("#main_content").html(data);
	});
}

function get_blog_posts(wp_link)
{
	clear_div();
	reset_to_top();
	$("#main_content").html("<img src='images/ajax-loader.gif' style='margin: 200px 0 0 200px;'>");
	// auto-refresh tweets $("#main_content").load("http://colr.com/blog/?" + wp_link + " #content", function () { update_ftweets() });

	$.get("http://colr.com/blog/?" + wp_link + "&ajax=true#content",
	function(data){
		// For long blog loads, make sure the page is still in #blog when the the content finishes loading 
		if (window.location.hash == "#blog")
		{
			$("#main_content").html(data, function () { get_ftweets() });
		}
	});
}

// Tweet Update

function update_ftweets()
{
	setTimeout('get_ftweets()', 15000);
}

function get_ftweets()
{
	$.get("tweets.ajax.php", { feed: "home", maxtweets: "10" },
	function(data){
		tweetsf_update(data);
		// uncomment below for tweet auto-refresh
		//tweetsf_fadeout(data);
		//tweetsf_fadein();
		//update_ftweets();
	});
}

function tweetsf_update(data)
{
	$("#tweets_following").html(data);
}

function tweetsf_fadeout(data)
{
	$("#tweets_following").animate({ opacity: 0.0 }, { duration:350, easing:"easeOutQuad", complete:function() { tweetsf_update(data) } });
}

function tweetsf_fadein()
{
	$("#tweets_following").animate({ opacity: 1.0 }, { duration:350, easing:"easeInQuad" });
}


// Ticker
function get_ticker_rss()
{
	$.get("rss.ajax.php",
	function(data){
		rss_array = jQuery.parseJSON( data );
		ticker_fadein();
	});
}

function ticker_fadein()
{
	if ( anum < rss_array.length )
	{
		$("#ticker_content").html(rss_array[anum] + "&nbsp;&nbsp;&#183;&nbsp;&nbsp;" + $("#ticker_content").html());
		anum++;
	} else {
		anum = 0;
		$("#ticker_content").html("");
		$("#ticker_content").html(rss_array[anum] + "&nbsp;&nbsp;&#183;&nbsp;&nbsp;");
		anum++;
	}
	setTimeout('$("#ticker_content").animate({ opacity: 1.0 }, { duration:750, easing:"easeInQuad", complete:function() { ticker_fadeout() }  })', 750);
}

function ticker_fadeout()
{
	setTimeout('$("#ticker_content").animate({ opacity: 0.0 }, { duration:750, easing:"easeOutQuad", complete:function() { ticker_fadein() }  })', 7000);
}
// End Ticker

function reset_to_top()
{
	window.scrollTo(0, 0);
	
	if(!!('ontouchstart' in window))
	{
		$('div#main_content').css('-webkit-transform', 'matrix(1,0,0,1,0,0)');
	}
}

function clear_div()
{
	$("#main_content").html("");
}

function flipbook_trigger(section)
{
	if (!jQuery.browser.msie) 
	{
		switch (section)
		{
			case "#featured":
				$('#flipbook[images]').flipbook({
						'start': 0,
				    'end': 24,
				    'loop': false,
				    'fps': 24,
				    'mobileStep': 1,
				    'images': 'images/flipbook/sprout_01_%2d.jpg'
				});		
		  break;
			case "#archive":
				$('#flipbook[images]').flipbook({
						'start': 0,
				    'end': 26,
				    'loop': false,
				    'fps': 24,
				    'mobileStep': 1,
				    'images': 'images/flipbook/sprout_02_%2d.jpg'
				});
		  break;
			case "#contact":
				$('#flipbook[images]').flipbook({
						'start': 0,
				    'end': 27,
				    'loop': false,
				    'fps': 24,
				    'mobileStep': 1,
				    'images': 'images/flipbook/sprout_03_%2d.jpg'
				});
		  break;
			case "#blog":
				$('#flipbook[images]').flipbook({
						'start': 0,
				    'end': 154,
				    'loop': false,
				    'fps': 24,
				    'mobileStep': 1,
				    'images': 'images/flipbook/globe_01_%3d.jpg'
				});
		  break;
			case "#techspecs":
				$('#flipbook[images]').flipbook({
						'start': 0,
				    'end': 28,
				    'loop': false,
				    'fps': 24,
				    'mobileStep': 1,
				    'images': 'images/flipbook/sprout_04_%2d.jpg'
				});
		  break;
			default:
				$('#flipbook[images]').flipbook({
						'start': 0,
				    'end': 24,
				    'loop': false,
				    'fps': 24,
				    'mobileStep': 1,
				    'images': 'images/flipbook/sprout_01_%2d.jpg'
				});	
		  break;
		}
	}
}


// Lightbox Control

function show_lightbox(clip_id)
{
	$("#lightbox").css({ width: "100%", height: "200%" });
	$("#lightbox").load("player.ajax.php?" + clip_id, function () { show_lightbox_pt2() });
}

function show_lightbox_playlist(pl_id)
{
	$("#lightbox").css({ width: "100%", height: "200%" });
	$("#lightbox").load("playlist.ajax.php?" + pl_id );
}

function show_lightbox_pt2()
{
	$("#vimeo_player").stop().animate({ opacity: 1.0 }, { duration:750, easing:'easeInExpo' });
}

function hide_lightbox(clip_id,ignore)
{
	$("#vimeo_player").stop().animate({ opacity: 0.0 }, { duration:300, easing:'easeOutQuad', complete:function() { hide_lightbox_pt2(clip_id) }  });
}

function hide_lightbox_pt2(clip_id)
{
	if (navigator.userAgent.match(/iPad/i) != null)
	{
		reset_to_top();
	}
	$("#lightbox").html('');
	$("#lightbox").css({ width: "0px", height: "0px" });
	
	// If clip_id, fresh load the site
	if (clip_id)
	{
		window.location.href = "http://colr.com/";
	}
}

function save_order(section,order)
{
	// pl_xml value only exists on playlist sites
	$.get("manage_clip_order.ajax.php", { section: section, order: order, pl_xml: $("#pl_xml").val() } );
}

function clip_add(section)
{
	var thumb = $('input:radio[name=thumb]:checked').val();

	$.get("manage_clip_add.php", { section: section, clip_id: $("#clip_id").val(), client: encodeURIComponent($("#client").val()), title: encodeURIComponent($("#title").val()), director: encodeURIComponent($("#director").val()), agency: encodeURIComponent($("#agency").val()), thumb: encodeURIComponent(thumb) },
	function(data){
		alert(data);
		window.location.href = "manage.php?section=" + section;
	});
}

function clip_delete(section,clip_id)
{
	$.get("manage_clip_delete.php", { section: section, clip_id: clip_id },
	function(data){
		alert(data);
		window.location.href = "manage.php?section=" + section;
	});
}

function clip_edit(section)
{
	var thumb = $('input:radio[name=thumb]:checked').val();

	$.get("manage_clip_edit.php", { section: section, clip_id: $("#clip_id").val(), client: encodeURIComponent($("#client").val()), title: encodeURIComponent($("#title").val()), director: encodeURIComponent($("#director").val()), agency: encodeURIComponent($("#agency").val()), thumb: encodeURIComponent(thumb) },
	function(data){
		alert(data);
		window.location.href = "manage.php?section=" + section;
	});
}

function clip_relocate(source_section,clip_id)
{
	$.get("manage_clip_relocate.php", { section: source_section, clip_id: clip_id },
	function(data){
		alert(data);
		window.location.href = "manage.php?section=" + source_section;
	});
}

function playlist_add()
{
	$.get("manage_playlist_add.php", { title: encodeURIComponent($("#pl_title").val()) },
	function(data){
		alert(data);
		window.location.href = "manage.php?section=playlists";
	});
}

function save_playlist_title(pl_xml)
{
	$.get("manage_playlist_title.ajax.php", { pl_xml: pl_xml, title: encodeURIComponent($("#pl_title").val()) },
	function(data){
		alert(data);
	});
}

function playlist_clip_add(pl_xml)
{
	var clip = $('input:radio[name=clip]:checked').val();

	$.get("manage_playlist_clip_add.php", { pl_xml: pl_xml, title: encodeURIComponent($("#title").val()), duration: encodeURIComponent($("#duration").val()), clip: encodeURIComponent(clip) },
	function(data){
		alert(data);
		window.location.href = "manage.php?section=playlist&pl_xml=" + pl_xml;
	});
}

function playlist_clip_edit(pl_xml,clip_id)
{
	var clip = $('input:radio[name=clip]:checked').val();

	$.get("manage_playlist_clip_edit.php", { pl_xml: pl_xml, clip_id: clip_id, title: encodeURIComponent($("#title").val()), duration: encodeURIComponent($("#duration").val()), clip: encodeURIComponent(clip) },
	function(data){
		alert(data);
		window.location.href = "manage.php?section=" + section;
	});
}

function playlist_clip_delete(pl_xml,clip_id)
{
	var r = confirm("Delete this clip?");
	if (r !== true)
	{
		return;
	}
	
	$.get("manage_playlist_clip_delete.php", { pl_xml: pl_xml, clip_id: clip_id },
	function(data){
		alert(data);
		window.location.href = "manage.php?section=playlist&pl_xml=" + pl_xml;
	});
}

function playlist_delete(pl_xml)
{
	var r = confirm("Delete this playlist?");
	if (r !== true)
	{
		return;
	}

	$.get("manage_playlist_delete.ajax.php", { pl_xml: pl_xml },
	function(data){
		window.location.href = "manage.php?section=playlists";
	});
}

function updateOrientation()
{  
	var contentType = "show_";  
	switch(window.orientation)
	{  
	case 0:  
		contentType += "normal";  
	break;  
	
	case -90:  
		contentType += "right";  
	break;  
	
	case 90:  
		contentType += "left";  
	break;  
	
	case 180:  
		contentType += "flipped";  
	break;  
	}  
	document.getElementById("page_wrapper").setAttribute("class", contentType);
}
