// Default text in search box
var searchBoxText = "Search...";
// A placeholder variable for the flvplayer
var flvplayer;

$(document).ready(function(){
	
	// Search box display	
	$("#search_query").click(function() {
	
		if ($(this).val() == searchBoxText) $(this).val("");
		
	});
	$("#search_query").blur(function() {
	
		if ($(this).val() == "") $("#search_query").val(searchBoxText);
		
	});
	$("#search_query").blur();
	
	// Archive init
	$("#archives span").click(function() {
	
		$("#archives > ul").slideToggle();
		
	});
	
	// Archive sliding
	$("#archives .inactive h4").click(function() {
		
		if ($(this).parent("li").hasClass("active")) {
			
			$(this).parent("li").find("ul").slideUp("slow", function() {
				$(this).parent().removeClass("active").addClass("inactive");	
			});
			
		}
		else {
		
			$(this).parent("li").siblings(".active").find("ul").slideUp("slow", function() {
				$(this).parent().removeClass("active").addClass("inactive");	
			});
		
			$(this).parent("li").find("ul").slideDown("slow", function() {
				$(this).parent().removeClass("inactive").addClass("active");	
			});
			
		}
		
		return false;
		
	});
	
	// Social networking tools
	
	// Facebook
	$(".sntools .facebook").click(function() {
	
		// we'll need this
		var postmeta = $(this).parents(".postmeta");
		var addURL = "";

		addURL = "http://facebook.com/sharer.php?";
		if ($(".post h2").length) {
			// viewing a single post
			addURL = addURL + "u=" + encodeURIComponent(window.location.toString());
			addURL = addURL + "&t=" + encodeURIComponent($("h2 a").text());
		} else {
			// blog list page
			addURL = addURL + "u=" + encodeURIComponent('http://' + window.location.hostname + postmeta.find(".options a.readmore").attr("href"));
			addURL = addURL + "&t=" + encodeURIComponent(postmeta.find(".options a.readmore").attr("title"));
		}
				
		if (typeof analyticsAccount != 'undefined') {
			_gaq.push(['_trackPageview', '/social/facebook/' + postmeta.find(".token").text() + '/']);
		}
	
		window.open(addURL);
		
	});
	
	// Twitter
	$(".sntools .twitter").click(function() {
	
		// we'll need this
		var postmeta = $(this).parents(".postmeta");
		var shareURL = ""; 
		var shareTitle = "";
		var addURL = "";

		if ($(".post h2").length) {
			// viewing a single post
			shareURL = window.location.toString();
			shareTitle = $("h2 a").text();
		} else {
			// blog list page
			shareURL = 'http://' + window.location.hostname + postmeta.find(".options a.readmore").attr("href");
			shareTitle = postmeta.find(".options a.readmore").attr("title");
		}

		$.get("/shorten/?url=" + encodeURI(shareURL), function(result) {

			// Random insert
			randomWords = "Check the shred:";
	
			addURL = "http://twitter.com/?status=" + randomWords + " ";
			addURL = addURL + encodeURIComponent(shareTitle + " " + result);
				
			if (typeof analyticsAccount != 'undefined') {
				_gaq.push(['_trackPageview', '/social/twitter/' + postmeta.find(".token").text() + '/']);
			}
	
			window.open(addURL);
		
		});

	});
	
	// Facebook like button
	window.fbAsyncInit = function() {
		FB.init({appId: "166635303351075", status: true, cookie: true, xfbml: true});
	};
	(function() {
		var e = document.createElement("script");
		e.async = true;
		e.src = document.location.protocol + "//connect.facebook.net/en_US/all.js";
		if ($("#fb-root").length) document.getElementById("fb-root").appendChild(e);
	}());
	
	$("#fb-root").after("<fb:like href=\"http://" + window.location.hostname + location.pathname + "\" layout=\"button_count\" show_faces=\"false\" width=\"50\" action=\"like\" font=\"verdana\" colorscheme=\"light\"></fb:like>");
	

});

// Increment the view count when the video starts playing
function incrementView(obj) {

	if (obj['oldstate'] == "BUFFERING" && obj['newstate'] == "PLAYING") {

		// AJAX to register the hit
		requrl = "/api/contentfilehit/" + obj['id'] + "/";
		$.ajax({
			type: "GET",
			url: requrl,
			success: function() {
				//alert('good job player id '+obj['id']+', you got a view from url ' + requrl);
				return true;
			},
			error: function() {
				//alert("Couldn't register hit for content id " + obj['id']);
				return false;
			}
		});
	}

}

function playerReady(obj) {

	// register the listener to the hit function
	flvplayer = document.getElementById(obj['id']);
	flvplayer.addModelListener("STATE","incrementView");

}

