var $playlist_loading = false;
var $playlist_image = null;
var $playlist_command = null;

var wwindow_created = false;
var wwindow_visible = false;
var wwindow_flagged = false;

function bookmark_us (url, title)
{
	if (window.sidebar) // firefox
	{
		window.sidebar.addPanel(title, url, "");
	}
	else if (window.opera && window.print) // opera
	{
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	}
	else if(document.all) // ie
	{
		window.external.AddFavorite(url, title);
	}
}

function wwindow_open (id, name)
{
	if (!wwindow_created)
	{
		$('<div id="wwindowSpacer">&nbsp;</div>').prependTo($('#pageContainer'));
		$('<div id="wwindowContainer"><div id="wwindowStatus"><span>You just watched... <a id="wwindowWatching" href="#"></a></span></div><div id="wwindowOptions"><a id="wwindowOptionsClose" href="#">Close</a><a id="wwindowOptionsFlag" href="#">Flag As Broken/Inappropriate</a><a id="wwindowOptionsRelated" href="#">Related Flicks</a><a id="wwindowOptionsBookmark" href="#">Bookmark</a></div></div>').prependTo($('body'));
		
		// Set the currently watching target
		$('#wwindowWatching').attr('target', '_blank');
		
		// Bind the close button
		$('#wwindowOptionsClose').click(function() {
			$('#wwindowContainer').hide();
			$('#wwindowSpacer').hide();
			wwindow_visible = false;
			return false;
		});
		
		wwindow_created = true;
	}

	// Set the values
	$('#wwindowWatching').attr('href', '/go/' + id).text(name);
	$('#wwindowOptionsRelated').attr('href', '/related/' + id);
	$('#wwindowOptionsBookmark').unbind('click').click(function () {
		bookmark_us('http://www.triplexflicks.com/go/' + id, name + ' | Triple XXX Flicks');
		
		return false;
	});
	$('#wwindowOptionsFlag').unbind('click').click(function () {
		if (!wwindow_flagged)
		{
			$.ajax({
				url: "/flag/" + id,
				type: "GET",
				dataType: "html"
			});
			
			wwindow_flagged = true;
		}
		
		return false;
	});
	
	
	// Show the windows
	$('#wwindowSpacer').show();
	$('#wwindowContainer').show();
	
	wwindow_flagged = false;
	wwindow_visible = true;
}

function wwindow_link ()
{
	wwindow_open($(this).attr('href').substring(4), $(this).children('span').text());
}

$(document).ready(function() {
	$('.flickContainer .link a').click(wwindow_link);
	$('#playlist .link a').click(wwindow_link);
	$('.playlist').click(function() {
		if (!$playlist_loading) {
			$playlist_image = $(this).children(':first');
			$playlist_loading = true;
			
			$playlist_command = ($playlist_image.attr('src').substring(13, 17) == 'full') ? 'remove' : 'add';
		
			// Load the page to add/remove this flick from the playlist
			$.ajax({
				url: '/playlist/' + $playlist_command + '/' + $(this).attr('id').substring(8),
				type: 'GET',
				dataType: 'html',
				success: function (result) {
					$('#playlist').html(result);
					$playlist_image.attr('src', '/images/star_' + (($playlist_command == 'add') ? 'full' : 'empty') + '.gif');
					
					// Ensure new playlist entries follow same rules as old playlist entries
					$('#playlist .link a').unbind('click').click(wwindow_link);
					$('#playlist .link a').attr('target', '_blank');
					
					$playlist_loading = false;
				}
			});
		}
		
		return false;
	});
	
	// Autohighlight search
	$('#searchField').focus();
});