/*
 * Ajax Video Lister 0.5 - jQuery plugin for calling rss video lists and displaying them on the page
 * by Vaughan Risher
 *
 * To use, create one of 2 attributes in the div where you want your playlist to be displayed.
 * the "ajax" attribute creates a list with thumbnails, titles, and descriptions
 * the "ajaxl" attribute creates a list of pure links
 * for now, you'll have to figure out the css + the javascript for playing the videos on your own
 *
 */
 
$(document).ready(function() {
	var ajaxnum = 0;
	var maketitle = true;
	var titletext = "";
	
	var ajaxlnum = 0;
	var makeltitle = true;
	var ltitletext = "";
	
	$("*").each(function(i) {
		if ($(this).attr("ajax") != null) {
			curtag = $(this);
			var active = "";
			ajaxnum++;
			if ($(this).find("h1").length < 1) {
				titletag = $("<h1></h1>");
			} else {
					maketitle = false;
					titletag = $(this).find("h1").eq(0);
					if (titletag.find("img").length > 0) {
						titletext = "imghere";
					} else {
						titletext = titletag.text();
					}
				}
			curstrip = $(this).parent().parent().attr("id");
			ajaxurl = $(this).attr("ajax");
			tlen = 24;
			dlen = 26;
			$.ajax({
				url: ajaxurl,
				async: false,
				cache: false,
				type: 'GET',
				dataType: "xml",
				timeout: 1000,
				error: function(){
					alert('Error loading XML document');
				},
				success: function(xml){
					if ((ajaxurl == curplaylist) || (curplaylist == "video/Default/whatshot.xml")) { 
						$(this).attr("active", true);
					} else { 
							$(this).attr("active", false);
							active = "hide";
						}
					var pl_title = $(xml).find("title").eq(0).text();
					var plength = $(xml).find('item').length;
					if (maketitle == true) {
						curtag.append(titletag.text(pl_title));
					} else {
							if (titletext == "") { titletag.text(pl_title); }
						}
					$(xml).find('item').each(function(itemindex){
						var item_title = $(this).find("author").text();
						var itlong = item_title;
						if (item_title.length > tlen) {
							item_title=item_title.substring(0, (tlen-4))+"...";
						}
						var item_descriptionlong = $(this).find("title").text();
						if (item_descriptionlong.length > dlen) {
							item_descriptionshort=item_descriptionlong.substring(0, (dlen-3))+"...";
						} else { 
								item_descriptionshort=item_descriptionlong;
							}
						var thumbtemp = $(this).find("title").next().next().attr("url").split("/");
						var item_thumb = thumbtemp[1]+"/"+thumbtemp[2]+"/thumbs/"+thumbtemp[3];
						var alink = "javascript:playItem('mediaplayer','"+ajaxurl+"',"+itemindex+","+plength+");";
						if (curstrip == "moviebox") {
							$("<a></a>").attr("class", "vid").addClass(active).attr("href", alink).html("<img src='"+item_thumb+"'><span class='itext'>"+itlong+"<br><em>"+item_descriptionlong+"</em></span>").appendTo(curtag);
						} else {
							$("<a></a>").attr("class", "vid").addClass(active).attr("href", alink).html("<img src='"+item_thumb+"'><span class='itext'>"+item_title+"<br><em>"+item_descriptionshort+"</em></span>").appendTo(curtag);
						}
					});
					if ($(this).attr("active")) {
						$("<p></p>").html("open").attr("class", "togvids").css("display", "none").appendTo(curtag);
					} else { $("<p></p>").html("open").attr("class", "togvids").appendTo(curtag); }
				}
			});
		}
		
		if ($(this).attr("ajaxl") != null) {
			curtagl = $(this);
			var activel = "";
			ajaxlnum++;
			if ($(this).find("h1").length < 1) {
				ltitletag = $("<h1></h1>");
			} else {
					makeltitle = false;
					ltitletag = $(this).find("h1").eq(0);
					if (ltitletag.find("img").length > 0) {
						ltitletext = "imghere";
					} else {
						ltitletext = ltitletag.text();
					}
				}
			lcurstrip = $(this).parent().parent().attr("id");
			ajaxlurl = $(this).attr("ajaxl");
			$.ajax({
				url: ajaxlurl,
				async: false,
				cache: false,
				type: 'GET',
				dataType: "xml",
				timeout: 1000,
				error: function(){
					alert('Error loading XML document');
				},
				success: function(xml){
					if (ajaxlurl == curplaylist) { 
						$(this).attr("activel", true);
					} else { 
							$(this).attr("activel", false);
							activel = "hide";
						}
					var pl_title = $(xml).find("title").eq(0).text();
					var plength = $(xml).find('item').length;
					if (makeltitle == true) {
						curtagl.append(ltitletag.text(pl_title));
					} else {
							if (ltitletext == "") { ltitletag.text(pl_title); }
						}
					$(xml).find('item').each(function(itemindex){
						var item_title = $(this).find("author").text();
						var itlong = item_title;
						if (item_title.length > 24) {
							item_title=item_title.substring(0, 22)+"...";
						}
						var item_descriptionlong = $(this).find("title").text();
						if (item_descriptionlong.length > 26) {
							item_descriptionshort=item_descriptionlong.substring(0, 23)+"...";
						} else { 
								item_descriptionshort=item_descriptionlong;
							}
						var item_thumb = $(this).find("title").next().next().attr("url");
						var item_video = $(this).find("title").next().attr("url");
						var alink = "javascript:playItem('mediaplayer','"+ajaxlurl+"',"+itemindex+","+plength+");";
						$("<a></a>").attr("class", "vidlink").attr("href", alink).html("<strong>"+item_title+"</strong> - "+item_descriptionlong).appendTo(curtagl);
					});
				}
			});
		}
	});
	
	if (ajaxnum < 2) { $("p.togvids").hide(); }
	
	$("a.vidlink").click(function() {
		$.scrollTo("div#bannerbox");
	});
	
	$("a.vid").click(function() {
		$.scrollTo("div#bannerbox");
	});
	
	$("p.togvids").hover(function() {
		$(this).addClass("togvidsover");						  
	}, function() {
		$(this).removeClass("togvidsover");
	});
	
	$("p.togvids").click(function() {
		$("p.togvids").show();
		$("a.vid").hide();
		adjustLeftcolumn();
		$.scrollTo("div#bannerbox");
		togpar = $(this).parent();
		if (curstate < 2) {
			curplaylist = togpar.attr("ajax");
			loadFile("mediaplayer", {file:curplaylist});
		}
		$(this).hide();
		
		togpar.find("a.vid").slideDown("slow", function() {
			adjustLeftcolumn();												
		});
		
	});
});
						  
