// Vertical scrolling lists stuff
// Height of the scroll list (in pixels)
var scrollAreaHeight;
// How much you want to scroll (in pixels)
var scrollUnit;
// Scrolling Speed (in milliseconds)
var scrollSpeed = 400;
// Elements per scroll unit
var elementsPerUnit = 1;
// Height of the scrolling area
var scrollAreaHeight = 652;
// 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();
	
	// Scrolling functions
	$(".scrollable").each(function() {
	
		if ($(this).css("display") == "block") {
			
			// How much you want to scroll (in pixels)
			scrollUnit = 132;
		}
		
		// Disable the left arrow if the left margin is 0
		if ($(this).find("ul").css("margin-top") == "0px") {
			$(this).find(".prev").addClass("disabled");	
		}	
		
		// Disable the right arrow if the left margin is ul width - scroll area
		if ($(this).find("ul").css("height").replace("px","") > 0 && $(this).find("ul").css("margin-top").replace("px","") >= ($(this).find("ul").css("height").replace("px","") - scrollAreaHeight)) {
			$(this).find(".next").addClass("disabled");	
		}
		
		// Scroll to next items	
		$(this).find(".next").click(function() {
			
			scrollAhead($(this).parents(".scrollable"));
					
		});
		
		// Scroll to prev items
		$(this).find(".prev").click(function() {
						
			scrollBack($(this).parents(".scrollable"));
					
		});
		
	});
	
	// 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;
		
	});
	
	// Add the player view event listener

	
	
});

// Scroll ahead function (pass .scrollable to it)
function scrollAhead(e) {
	
	if ($(e).find(".next").hasClass("disabled") || (Math.abs($(e).find("ul").css("margin-top").replace("px","")) + scrollAreaHeight) > (($(e).find("ul").children().length - 1) * scrollUnit)) return false;
				
	$(e).find("ul").animate({
		marginTop: "-=" + scrollUnit + "px"
	}, scrollSpeed, "swing", function() {
		$(e).find(".prev").removeClass("disabled");
		if ((Math.abs($(this).css("margin-top").replace("px","")) + scrollAreaHeight) > ((Math.floor(($(this).children().length - 1) / elementsPerUnit)) * scrollUnit)) {
			$(e).find(".next").addClass("disabled");	
			$(this).queue("fx", []);
			$(this).stop();
		}
	});
	
}

// Scroll back function (pass .scrollable to it)
function scrollBack(e) {
	
	if ($(e).find(".prev").hasClass("disabled") || $(e).find("ul").css("margin-top").replace("px","") >= 0) return false;
					
	$(e).find("ul").animate({
		marginTop: "+=" + scrollUnit + "px"
	}, scrollSpeed, "swing", function() {
		if ($(this).children().length > elementsPerUnit) $(e).find(".next").removeClass("disabled");
		if ($(this).css("margin-top").replace("px","") >= 0) {
			$(e).find(".prev").addClass("disabled");	
			$(this).queue("fx", []);
			$(this).stop();
		}	
	});
	
}

// 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");

}
