var next = "&nbsp;&nbsp;<a href='javascript:nextPage()'>next</a>&nbsp;<img src='images/pinkArrow.gif' alt='' width='5' height='6' border='0' class='arrow'>";
var nextDisabled = "&nbsp;&nbsp;<span class='liteGrey'>next</span>&nbsp;<img src='images/greyArrow.gif' alt='' width='5' height='6' border='0' class='arrow'>";
var prev = "<img src='images/pinkArrowPrev.gif' alt='' width='5' height='6' border='0' class='arrow'>&nbsp;<a href='javascript:prevPage()'>prev</a>";
var prevDisabled = "<img src='images/greyArrowPrev.gif' alt='' width='5' height='6' border='0' class='arrow'>&nbsp;<span class='liteGrey'>prev</span>";

function showEmbed(id, location, embedCode) {
	var bodyText = "<div id='embedplayer'>" + embedCode + "</div>\r\n";
	bodyText += "<div id='embedCodeDiv' style='padding-top: 10px;'>Embed Code: <input style='width: 360px;' type=text name=code onClick='this.select();' id=code value=\"" + embedCode + "\"></div>";
	$('#embedDialog').html(bodyText).dialog("open");
};

// Plays the current reel
function playit() {
	if(!iphone) {
		// Set reel info  
		setplayerinfo();
		var autoString = "f";
		if(autostart) {
			autoString = "t";
		}
		var flashvars = {
			id: reel[current].id,
			dom: domain,
			thumb: owner.thumbnail,
			prod: owner.name,
			comp: owner.company,
			reel: reel[current].title,
			desc: reel[current].description,
			cat: reel[current].category,
			auto: autoString
		};
		var params = {'wmode': 'transparent', 'allowFullScreen': 'true'};
		var attributes = {};
		flashembed('videoplayer', { src:'/js/mm_player.swf', width:640, height:360, version:[9,115], express:'/js/expressInstall.swf', wmode:'transparent'}, flashvars);
	
	} else {
		$('#videoplayer').html("<a href='/video=" + reel[current].id + "'><img src='/videothumb=" + reel[current].id + "' style='width:640px;height:360px;'></a>");
	}	
}

function nextPage() {
	page++;
	checkcontrols();
	loadplaylist();
}

function prevPage() {
	page--;
	checkcontrols();
	loadplaylist();
}

function checkcontrols() {
	var html = "";
	if(page > 0) html += prev;
	else html += prevDisabled;
	if((reelcnt - ((page + 1) * maxShow)) > 0) html += next;
	else html += nextDisabled;
	$('#playlistcontrol').html(html);
}  

// Sets up the information for the current reel
function setplayerinfo() {
	var html = "<div class='floatAlignRite165' style='padding-bottom:7px'>";
	html += reel[current].category + "<br><span class='fieldName'>Budget:</span> "+ reel[current].budget + "</div>";
	html += "<div style='width:415px;'><h6><a href='" + owner.url + "_videoid=" + reel[current].id + "'>" + reel[current].title + "</a></h6>";
	html += reel[current].description + "<span class='grey10pt'> - posted: " + reel[current].date + "</span></div>";
		
	$('#playerinfo').html(html);
}

function selectReel(reel) {
	current = reel;
	loadplaylist();
	playit(true);
}


function initPlayer()  {
	if(reelcnt > 0) {
		checkcontrols();	
		loadplaylist();
		playit();		
	}
}

function loadplaylist() {
	var startingVideo = page * maxShow;
	var endingVideo = startingVideo + maxShow;
	if(endingVideo > reelcnt) endingVideo = reelcnt;
	$('#startvideo').text(startingVideo + 1);
	$('#endvideo').text(endingVideo);	
	var html = "";
	for(var i=startingVideo; i < endingVideo; i++) {
		if(i == current) html += "<div class='nowPlaying'>";
		else html += "<div class='vidListing'>";
		html += "<div class='vidThumbBox'><a href='" + owner.url + "_videoid=" + reel[i].id + "'>";
		html += "<img src='/videothumbsmall=" + reel[i].id + "' width='64' height='36' class='greyBorder'></a></div><div class='vidMeta'>";
	    	html += "<a href='" + owner.url + "_videoid=" + reel[i].id + "'>" + reel[i].title + "</a><br>";
	    	html += reel[i].category + "</div><br class='clearAll'></div>"; 	
	}
	$('#playlist').html(html);
}	

$(document).ready(function() {
	initPlayer();
	
	$( "#embedDialog" ).dialog({
		autoOpen: false,
		width: 500,
		height: 350,
		modal: true
	});

});

