var emAutoplay = 'false';
var emColor = '0x7e858c';
var emCode;
var emFormat = 'std';
var emId;
var emHeight = 520;
var emWidth = 425;
var pId;
var pTitle;
var viewed = false;

$(document).ready(function() {
	pId = $('h1').attr('alt');
	pTitle = $('#urlcode').attr('class');
	var puser = $('#viewer-title p a').text();
	var user = $('a.header-uname').text();
	if((puser != 'guest') && (puser==user)){
		$('#content').prepend('<div id="playlist-user-opts"><strong>Playlist Owner Options:</strong> <a href="/edit/'+pTitle+'">edit this playlist</a> | <a href="#" onclick="deletePlaylist('+pId+')">delete this playlist</a></div>');	
	}
	else if(user=='sean' || user=='srbsausage'){
		$('#content').prepend('<div id="playlist-user-opts"><strong>Site Admin Options:</strong> <a href="/edit/'+pTitle+'">edit this playlist</a> | <a href="#" onclick="deletePlaylist('+pId+')">delete this playlist</a></div>');	
	}
	$('#opt-width').numeric();
	$('#colorSelector').ColorPicker({
		color: '#7e858c',
		onShow: function (colpkr) {
			if($(colpkr).css('display')=='none'){
				$(colpkr).slideDown();
			}
			else{
				$(colpkr).slideUp();
			}
			return false;
		},
		onHide: function (colpkr) {
			$(colpkr).slideUp();
			return false;
		},
		onChange: function (hsb, hex, rgb) {
			$('#colorSelector div').css('backgroundColor', '#' + hex);
			emColor = '0x'+hex;
			udColor('0x'+hex);
			udEmbed();
		}
	});	
	$('#customToggle').toggle(
		function () {
			$(this).html('[x] close');
			$('#customOpts').show();
		},
		function () {
			$(this).html('&raquo; advanced embed options');
			$('#customOpts').hide();
		}
	);
	$('input[name="aspect"]').click(
		function() {
			if ($("input[id='aspect-std']:checked").val()){
				emFormat = 'std';
				udEmbed();
				udRatio('std');
			}
			else{
				emFormat = 'wide';
				udEmbed();
				udRatio('wide');
			}
		}
	);
	$('input[name="opt-autoplay"]').click(
		function() {
			if ($("input[id='opt-autoplay']:checked").val()=='on'){
				emAutoplay = 'true';
				udEmbed();
			}
			else{
				emAutoplay = 'false';
				udEmbed();
			}
		}
	);
	$('input[id="opt-width"]').bind("change keyup", function() {
		emWidth = $(this).attr('value');
		udEmbed();
    }); 
	$('#color-opts div').click(
		function() {
			swColor = $(this).attr('id');
			emColor = '0x'+swColor;
			$('#colorSelector').ColorPickerSetColor(swColor);
			$('#colorSelector div').css('backgroundColor', '#' + swColor);
			udColor('0x'+swColor);
			udEmbed();
		}
	);
});
function deletePlaylist(id){
	var x=window.confirm("Are you sure you want to delete this playlist? You cannot undo this action.")
	if (x){
		$.ajax({
			type: 'POST',
			url: '/submit/delete',
			data: 'playlist='+id+'&title='+pTitle,
			beforeSend: function(){
				
			},
			error: function(){
				alert('Error deleting playlist. Try Again.');
			},
			success: function(msg){
				location.reload(true);
			},
			timeout: 10000
		});
	}
}
function udColor(color) {
	viewerSwf = document.getElementById("viewer-content");
	viewerSwf.changeBg(color);
}
function udEmbed() {
	if(emWidth > 240){
		if(emFormat=='wide'){
			emHeight = (Math.round((emWidth-20)*.5625))+217;
		}
		else{
			emHeight = (Math.round((emWidth-20)*.75))+217;
		}
		emId = $('#urlcode').attr('class');
		emCode = '<div style="width:'+emWidth+'px;height:'+emHeight+'px;"><object width="'+emWidth+'" height="'+emHeight+'"><param name="movie" value="http://embedr.com/swf/slider/'+emId+'/'+emWidth+'/'+emHeight+'/'+emColor+'/'+emAutoplay+'/'+emFormat+'"></param><param name="allowFullScreen" value="true"></param><param name="wmode" value="transparent"><embed src="http://embedr.com/swf/slider/'+emId+'/'+emWidth+'/'+emHeight+'/'+emColor+'/'+emAutoplay+'/'+emFormat+'" type="application/x-shockwave-flash" allowFullScreen="true" width="'+emWidth+'" height="'+emHeight+'" wmode="transparent"></embed></object><a href="http://embedr.com/playlist/'+emId+'" target="_blank" style="background:transparent url(http://embedr.com/img/embedr-custom-video-playlists.gif);float:right;margin:0;padding:0;outline:none;width:115px;height:35px;position:relative;top:-35px;"><span style="display:none;">Build your own custom video playlist at embedr.com</span></a></div>';
		var msStart = 'http://www.myspace.com/Modules/PostTo/Pages/?t='+$('h1').text()+'&c=';
		var msCode = '<div style="width:'+emWidth+'px;height:'+emHeight+'px;"><object width="'+emWidth+'" height="'+emHeight+'"><param name="movie" value="http://embedr.com/swf/slider/'+emId+'/'+emWidth+'/'+emHeight+'/'+emColor+'/'+emAutoplay+'/'+emFormat+'"></param><param name="wmode" value="transparent"></object><a href="http://embedr.com/playlist/'+emId+'" target="_blank" style="background:transparent url(http://embedr.com/img/embedr-custom-video-playlists.gif);float:right;margin:0;padding:0;outline:none;width:115px;height:35px;position:relative;top:-35px;"><span style="display:none;">Build your own custom video playlist at embedr.com</span></a></div>';
		var msEnd = '&u=http://embedr.com/playlist/'+emId;
		$('#embedcode').attr('value',emCode);
		$('#link-myspace a').attr('href',msStart+msCode+msEnd);
	}
}
function udInfo(udInsert) {
	$('#now-playing').html(udInsert);
}
function udRatio(newRatio) {
	viewerSwf = document.getElementById("viewer-content");
	viewerSwf.changeRatio(newRatio);
}
function udSwapInfo(divId) {
	$('#now-playing div').hide();
	$('#now-playing div#video-'+divId).show();
}