//////Live/////
jQuery.fn.fadeToggle = function(speed, easing, callback) {
   return this.animate({opacity: 'toggle'}, speed, easing, callback);

};
$.include('/js/jScrollPane.js', [
	$.include('/js/jquery.em.js'),
	//$.include('/js/swfobject.js'),
	$.include('/js/jquery.mousewheel.js')
]);

var index = 0;
var playlistARRAY = [];
var typeARRAY = [];
var titleARRAY = [];
var authorARRAY = [];
var srcARRAY = [];
var thumbARRAY = [];
var descriptionARRAY = [];
var sectionARRAY = [];

var linkgeneral1ARRAY = [];
var linkgeneraltext1ARRAY = [];
var linkgeneral2ARRAY = [];
var linkgeneraltext2ARRAY = [];
var linkgeneral3ARRAY = [];
var linkgeneraltext3ARRAY = [];
var linkgeneral4ARRAY = [];
var linkgeneraltext4ARRAY = [];

var linkdownload1ARRAY = [];
var linkdownloadtext1ARRAY = [];
var linkdownload2ARRAY = [];
var linkdownloadtext2ARRAY = [];
var linkdownload3ARRAY = [];
var linkdownloadtext3ARRAY = [];
var linkdownload4ARRAY = [];
var linkdownloadtext4ARRAY = [];

var linkstreaming1ARRAY = [];
var linkstreamingtext1ARRAY = [];
var linkstreaming2ARRAY = [];
var linkstreamingtext2ARRAY = [];
var linkstreaming3ARRAY = [];
var linkstreamingtext3ARRAY = [];
var linkstreaming4ARRAY = [];
var linkstreamingtext4ARRAY = [];

function clearARRAY(){
	index = 0;
	typeARRAY = [];
	titleARRAY = [];
	authorARRAY = [];
	srcARRAY = [];
	thumbARRAY = [];
	descriptionARRAY = [];
	sectionARRAY = [];
	
	linkgeneral1ARRAY = [];
	linkgeneraltext1ARRAY = [];
	linkgeneral2ARRAY = [];
	linkgeneraltext2ARRAY = [];
	linkgeneral3ARRAY = [];
	linkgeneraltext3ARRAY = [];
	linkgeneral4ARRAY = [];
	linkgeneraltext4ARRAY = [];
	
	linkdownload1ARRAY = [];
	linkdownloadtext1ARRAY = [];
	linkdownload2ARRAY = [];
	linkdownloadtext2ARRAY = [];
	linkdownload3ARRAY = [];
	linkdownloadtext3ARRAY = [];
	linkdownload4ARRAY = [];
	linkdownloadtext4ARRAY = [];
	
	linkstreaming1ARRAY = [];
	linkstreamingtext1ARRAY = [];
	linkstreaming2ARRAY = [];
	linkstreamingtext2ARRAY = [];
	linkstreaming3ARRAY = [];
	linkstreamingtext3ARRAY = [];
	linkstreaming4ARRAY = [];
	linkstreamingtext4ARRAY = [];
}

function activate(number){    	//carry over the index from flash
	//alert(number); //USED TO CHECK INDEX PASSED
	index = number - 1;
	refreshInfo();            	//update the info on click
	return index;	
}

function refreshInfo(){      	//Update the Info
	title = titleARRAY[index];
	author = 'by '+authorARRAY[index];
	description = descriptionARRAY[index];
	src = srcARRAY[index];
	var file = src.replace("http://files.kore.uk.com/", "");
	thumb = thumbARRAY[index];
	
	linkgeneral1 = '<a href="'+linkgeneral1ARRAY[index]+'">'+linkgeneraltext1ARRAY[index]+'</a>';
	linkgeneral2 = '<a href="'+linkgeneral2ARRAY[index]+'">'+linkgeneraltext2ARRAY[index]+'</a>';
	linkgeneral3 = '<a href="'+linkgeneral3ARRAY[index]+'">'+linkgeneraltext3ARRAY[index]+'</a>';
	linkgeneral4 = '<a href="'+linkgeneral4ARRAY[index]+'">'+linkgeneraltext4ARRAY[index]+'</a>';
	
	linkdownload1 = '<a href="'+linkdownload1ARRAY[index]+'">'+linkdownloadtext1ARRAY[index]+'</a>';
	linkdownload2 = '<a href="'+linkdownload2ARRAY[index]+'">'+linkdownloadtext2ARRAY[index]+'</a>';
	linkdownload3 = '<a href="'+linkdownload3ARRAY[index]+'">'+linkdownloadtext3ARRAY[index]+'</a>';
	linkdownload4 = '<a href="'+linkdownload4ARRAY[index]+'">'+linkdownloadtext4ARRAY[index]+'</a>';
	
	linkstreaming1 = '<a href="'+linkstreaming1ARRAY[index]+'">'+linkstreamingtext1ARRAY[index]+'</a>';
	linkstreaming2 = '<a href="'+linkstreaming2ARRAY[index]+'">'+linkstreamingtext2ARRAY[index]+'</a>';
	linkstreaming3 = '<a href="'+linkstreaming3ARRAY[index]+'">'+linkstreamingtext3ARRAY[index]+'</a>';
	linkstreaming4 = '<a href="'+linkstreaming4ARRAY[index]+'">'+linkstreamingtext4ARRAY[index]+'</a>';
	
	$('span.title').empty();
	$('span.title').append(title);
	$('div.info').empty();
	$('input#video').attr('value', title);
	$('div.info').append('<p class="small">'+author+'</p>' + description + '<div class="link"></div>');
	//$('p.small').empty();
	//$('p.small').append(author);
	$('div.link').empty(); 
	$('span.facebook a').attr('href', 'http://www.facebook.com/sharer.php?u=http://kore.uk.com/flicks&t='+title+', Great video at www.kore.uk.com/flicks');
	$('span.twitter a').attr('href', 'http://twitter.com/home?status='+title+', Great video at www.kore.uk.com/flicks from @KOREuk');
	$('span.delicious a').attr('href', 'http://del.icio.us/post?url=http://kore.uk.com/flicks&title='+title); 
	$('span.myspace input.title').attr('value', title+", Great video at www.kore.uk.com/flicks");
	$('div.link').append(linkgeneral1).append(linkgeneral2).append(linkgeneral3).append(linkgeneral4).append('<div style="clear:both;"></div>');
	$('div.link').append(linkdownload1).append(linkdownload2).append(linkdownload3).append(linkdownload4).append('<div style="clear:both;"></div>');
	$('div.link').append(linkstreaming1).append(linkstreaming2).append(linkstreaming3).append(linkstreaming4).append('<div style="clear:both;"></div>');
	
	$('div.fav a').attr('href', "http://www.kore.uk.com/_media/video.html?video=" + file + "&title=" + title);
	//Click here to bookmark this video!
	
	$('div#mediaspace').empty();
	
	var flashvars = {
	  'file': '' + src + '',
	  'image': '' + thumb + '',
	  'skin': 'http://www.kore.uk.com/flash/flicks/kore.xml',
	  'dock': 'false',
	  'controlbar.position': 'over',
	  'quality': 'false'
	};
	swfobject.embedSWF("/flash/flicks/jwPLAYER-licensed.swf", "mediaspace", "377", "300", "9.0.0", "/flash/expressInstall.swf", flashvars, {allowFullScreen:"true", wMode:"opaque", allowScriptAccess:"always"});
	
	$('div.info').jScrollPane({
		scrollbarWidth: 9,
		dragMaxHeight: 7,
		dragMinHeight: 7,
		topCapHeight:5,
		bottomCapHeight: 10,
		maintainPosition: false
	});
	
} 

$(document).ready(function(){
	//$('#mediainfo').hide();
	
	$('span.containi').click(function(){
		$('img.info').attr('src', 'images/blue_down.png').attr('alt', '\/');
		$('img.share').attr('src', 'images/white_side.png').attr('alt', '>');
		$('div.share').css('display', 'none');
		$('div.jScrollPaneContainer').css('display', 'block');
	});
	
	/*$('div.fav a').click(function(){
		if(BrowserDetect.browser == "Safari"){
			jAlert("Sorry you're using Safari you'll have to bookmark it yourself.", 'Oops! Safari Error.');
		}else if(BrowserDetect.browser == "Firefox"){
			jAlert("You're using Firefox...nice!", 'Hooray!');
		}
		return false;
	});*/
    
    $('input.you').focus(function(){
		$(this).select();
	});
	
	$('input.them').focus(function(){
		$(this).select();
	});
	
	$('input.email').focus(function(){
		$(this).select();
	});

    //show/hide info/sharing
	$('div.share').css('display', 'none');
		
	$('span.contains').click(function(){
		$('img.share').attr('src', 'images/blue_down.png').attr('alt', '\/');
		$('img.info').attr('src', 'images/white_side.png').attr('alt', '>');
		$('div.top div.jScrollPaneContainer').css('display', 'none');
		$('div.share').css('display', 'block');
	})
	
	//sliding methods of sharing
	$('div.head').click(function() {
		$(this).next().slideToggle('fast');
		return false;
	}).next().hide();

	//about panel
	$('div.holder').jScrollPane({
		scrollbarWidth: 9,
		dragMaxHeight: 7,
		dragMinHeight: 7,
		topCapHeight:5,
		bottomCapHeight: 5
	});

	$('div.bottom').find('div.about').slideUp('fast');
	
	$('div.close').click(function(){
		$('div.about').slideToggle('slow');
		$('div.scroll').fadeIn('fast');
	});
	$('span.about').click(function(){
		$('div.bottom').find('div.about').slideToggle('slow');
		$('div.scroll').fadeToggle('fast');
		$('div.about').css('opacity','1');	
	});
	
	
	var playlistsUrl = $('div.koreFlicksPlayer input#flicksplaylists').val();
	//alert(playlistsUrl); //USED TO CHECK THE LOADED PLAYLIST URL
	
	var playLists = 0;
	var currentList = 0;
	var oldList = 0;
	
	$.ajax({
		type: "GET",
		url: playlistsUrl,
		dataType: "xml",
		success: function(xml) {
	 		$(xml).find('playlist').each(function(){
 				var path = $(this).attr('path');
				playlistARRAY.push(path);
				$('span.page ul').append('<li id="'+playLists+'" class="_'+(playLists)+'">'+ (playLists+1) +'</li>');
				$('span.page ul li').click(function(){
					var newList = $(this).attr("id");
					getList(newList);
				});
				
				playLists++;
				
				//var title = $(this).find('title').text();
				//var url = $(this).find('url').text();
				//$('<div class="items" id="link_'+id+'"></div>').html('<a href="'+url+'">'+title+'</a>').appendTo('#page-wrap');
			});
			getList(currentList);
			//alert(playlistARRAY); //USED TO CHECK THE LOADED PLATLIST ARRAY
		}
	});
	
	function getList(reqList){
		$('span.page ul li._'+currentList).removeClass('current'); 
		currentList = reqList;
		currentListPath = playlistARRAY[reqList];
		
		$('div.scroll').html();
		
		$('div.scroll').html('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="615" height="144" id="preview'+reqList+'" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="allowFullScreen" value="false" /><param name="movie" value="/flash/flicks/flicksSCROLLER.swf?path='+currentListPath+'" /><param name="quality" value="high" /><param name="bgcolor" value="#2e2e2e" /><embed src="/flash/flicks/flicksSCROLLER.swf?path='+currentListPath+'" quality="high" bgcolor="#2e2e2e" width="615" height="144" name="preview'+reqList+'" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>');
		
		//var flashvars = { };
		//swfobject.embedSWF("/flash/flicks/flicksSCROLLER.swf?path=" + currentListPath, "mediascroll", "615", "144", "9.0.0", "/flash/expressInstall.swf", flashvars, {bgcolor:"#2e2e2e", allowFullScreen:"false", allowScriptAccess:"sameDomain"});
		
		clearARRAY();
		
		$.ajax({
			type: "GET",
			url: currentListPath,
			dataType: "xml",
			success: function(xml) {
				
				$(xml).find('picture').each(function(){
					var $content = $(this);
					typeARRAY.push($content.attr('type'));
					titleARRAY.push($content.attr('title'));
					authorARRAY.push($content.attr('author'));
					srcARRAY.push($content.attr('src'));
					thumbARRAY.push($content.attr('thumb'));
					descriptionARRAY.push($content.find('description').text());
					
					linkgeneral1ARRAY.push($content.find('linkgeneral1').text());
					linkgeneraltext1ARRAY.push($content.find('linkgeneraltext1').text());
					linkgeneral2ARRAY.push($content.find('linkgeneral2').text());
					linkgeneraltext2ARRAY.push($content.find('linkgeneraltext2').text());
					linkgeneral3ARRAY.push($content.find('linkgeneral3').text());
					linkgeneraltext3ARRAY.push($content.find('linkgeneraltext3').text());
					linkgeneral4ARRAY.push($content.find('linkgeneral4').text());
					linkgeneraltext4ARRAY.push($content.find('linkgeneraltext4').text());
					
					linkdownload1ARRAY.push($content.find('linkdownload1').text());
					linkdownloadtext1ARRAY.push($content.find('linkdownloadtext1').text());
					linkdownload2ARRAY.push($content.find('linkdownload2').text());
					linkdownloadtext2ARRAY.push($content.find('linkdownloadtext2').text());
					linkdownload3ARRAY.push($content.find('linkdownload3').text());
					linkdownloadtext3ARRAY.push($content.find('linkdownloadtext3').text());
					linkdownload4ARRAY.push($content.find('linkdownload4').text());
					linkdownloadtext4ARRAY.push($content.find('linkdownloadtext4').text());
					
					linkstreaming1ARRAY.push($content.find('linkstreaming1').text());
					linkstreamingtext1ARRAY.push($content.find('linkstreamingtext1').text());
					linkstreaming2ARRAY.push($content.find('linkstreaming2').text());
					linkstreamingtext2ARRAY.push($content.find('linkstreamingtext2').text());
					linkstreaming3ARRAY.push($content.find('linkstreaming3').text());
					linkstreamingtext3ARRAY.push($content.find('linkstreamingtext3').text());
					linkstreaming4ARRAY.push($content.find('linkstreaming4').text());
					linkstreamingtext4ARRAY.push($content.find('linkstreamingtext4').text());
					
					//var title = $(this).find('title').text();
					//var url = $(this).find('url').text();
					//$('<div class="items" id="link_'+id+'"></div>').html('<a href="'+url+'">'+title+'</a>').appendTo('#page-wrap');
				});
				
				$('span.page ul li._'+currentList).addClass('current'); 				 	//makes the current one blue
				refreshInfo();   															//Initial Info Populate
			}
		});
	};
	
	$('div.right').fadeIn('fast');
	$('div.video').fadeIn('fast');
	$('div.scroll').fadeIn('fast');

});


