/*
IMPORTANT!!!
This is the javascript for the blip.tv plugin for wordpress.  it's shared between all purekast sites and should not be modified.  
all customizable js is in application.js

*** BlipTV CurrentItem Object Notation ***

$('blipPlayer').getCurrentItem();

show: Show Title (Modern Man Comedy)
link: URL to the Blip Site (http://blip.tv/file/541525)
author: Blip Login (OktaneMicroMedia )
id: Video Id, used for link (541525)
description: Description from BlipTV (Whether you're Catholic, Protestant, Orthodox, Sunni...)
category: Category from BlipTV (Default Category)
date: Date in Integer (1197658086) 
file: URL to video file on the BlipTV site (http://blip.tv/file/get/OktaneMicroMedia-ep05_notjew745.flv )
image: URL to thumbnail image file on the BlipTV site  (http://e.static.blip.tv/OktaneMicroMedia-ep05_notjew745-223.jpg)
duration: Don't work (undefined) 
type: File Type (flv)
postsId: Cross reference to the Post used on BlipTV (547020) 
title: Title of the individual Episode (Wait, You're Not A Jew?)


*** Blip.tv JSON Feed Object Notation ***

'title':'comedy central tonight',
'description':'8-10pm<br /> LIVE<br />',
'tags':[],
'datestamp':'04-13-08 06:06pm',
'datestampUnixtime':'1208124418',
'postsId':830661,
'postsGuid':'EFA4EC3C-09A5-11DD-803A-B9EA042700A9',
'itemType':'file',
'itemId':824273,
'url':'http://blip.tv/file/824273',
'mediaUrl':'http://blip.tv/file/get/Rblog-comedyCentralTonight463.mov?source=2',
'thumbnailUrl':'http://panther2.video.blip.tv/Rblog-comedyCentralTonight935.jpg',
'thumbnail120Url':'http://panther2.video.blip.tv/Rblog-comedyCentralTonight935-417.jpg',
'login':'rblog',
'userId':74359,
'showName':'r blog',
'blogUrl':'http://www.rosie.com',
'media':{
	'url':'http://blip.tv/file/get/Rblog-comedyCentralTonight463.mov?source=2',
	'mimeType':'video/quicktime',
	'duration':68,
	'width':320,
	'height':240
},
'advertising':''
*/

var currentPermalink;
var currentBlipDownloadableVideo;
var currentPostion;
var blipSort;
var playListData = new Array();

// Old school way to get the return player element.  Cause jQuery don't play nice.
function player(id) {
	return document.getElementById(id);
}

// Callback to pull the post associated with the video currently playing in the blip player.
function pullAssociatedPost(id) {
	getPost(id);
}

// returns the info for the current item in play in the player.
function bite() {
	return player('blipPlayer').getCurrentItem();
}

// sends the next event to the player to trigger it to move to the next video in the playlist.
function getNextShow() {
	player('blipPlayer').sendEvent("next");
}

// same as above, just in reverse.
function getPrevShow() {
	player('blipPlayer').sendEvent("prev");
}

// plays the video at the position initcated by pos.  useful for skipping to a particular video.  Note: playlist starts at 0 not 1.
function playShow(pos) {
	player('blipPlayer').sendEvent('playitem', pos);
}

// blip player callback that we use to run the whole mess.
/*
	types: time, item
	time is called every second of every video.  So theoretically you can have something happen every second.  we don't use it but i've commented out an example.
	item is called every time the video changes.
	state is the state of the player, stopped, playing, etc.
	arg1 and arg2 are different for each type.  
		with time arg1 is the seconds elapsed and arg2 is the seconds left in the video.
		with item the args are not used.
		with state arg1 is the state of the player
			0: stopped
			1: playing
*/
var playingFlag = false;
var first = true;
function getUpdate(type, arg1, arg2, pid) {
		switch(type) {
			case "time":
					// $('theTime').innerHTML = "Elapsed: " + arg1 + " - Left: " + arg2;
			break;
			case "item":
					// gets the blip.tv post_id from the player.  pulls the associated wordpress post automatically, then scrolls the playlist to the appropriate thumb.
					p = bite().posts_id;
					pullAssociatedPost(p);
					cv = playListData[p];
					currentPosition = cv.position;
					currentBlipDownloadableURL = cv.mediaUrl;  // also sets this variable which can be used in updateTitle to provide a way to download the particular episode from blip.tv
					caro.scroll(currentPosition + 1);
			break;
			case "state":
				// a specific kind of callback which should play a video based on which video is requested.  used when an incoming link has a specific post.  works except the playlist doesn't scroll.
				if (arg1 == 1 && first) {
					if (firstVideo == parseInt(firstVideo)) {
						currentPosition = playListData[parseInt(firstVideo)].position;
						playShow(currentPosition);
						caro.scroll(currentPosition + 1);
					}
					first = false;
				}
			break;
		}
}


// callback to load the playlist on initialization of the blip player.  optionally anything with the id #scrollLeft or #scrollRight will get the ability to scroll the playlist left and right.  Not very useful with jcarousel skinning.
var caro;
function playListLoad(carousel, state) {
	$("#scrollLeft").click(function() {carousel.prev()});
	$("#scrollRight").click(function() {carousel.next()});
	if (state == 'init') {
		caro = carousel;
		addToPlayList();
	}
}

// gets the JSON feed from blip.tv through a proxy php page included with the plugin.  variables are set on the page automatically.  blipPageLength and blipPage are not supported yet.  No pagination for the playlist.
function addToPlayList() {
	var url = "/wp-content/plugins/blip_tv/ajax-load-playlist.php?sort="+blipSort+"&page="+blipPage+"&pagelen="+blipPageLength+"&topic_name="+blipTopicName;
	$.ajaxSetup({beforeSend: function(xhr) {xhr.setRequestHeader("Cache-Control", "no-cache");}});
	j = $.getJSON(url, function(data) {makePlayList(caro, data)});
}

// uses the JSON data to build a playlist.  the makeThumb function is defined in application.js and can be customized.
function makePlayList(caro, data) {
	if (data) {
		buildJSArray(data);
		for(i = 0; i < data.length; i++) {
			caro.add(i, makeThumb(i, data[i].thumbnailUrl, data[i].title));
		}
	}
}

// to convert the category
function slugize(text) {
	text = text.replace(" ", "-").toLowerCase();
	return text;
}

// builds an array of the playlist data to be used internally by the blip plugin.
function buildJSArray(data) {
	for(i = 0; i < data.length; i++) {
		data[i].position = i;
		playListData[data[i].postsId] = data[i];
	}
}
