
$disabled = false;
$video = null;


function popup($url, $width, $height, $scrolling)
{
	if ($scrolling == null) $scrolling = 0;

	$x = (screen.width / 2) - ($width / 2);
	$y = (screen.height / 2) - ($height / 2);
	$properties = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=" + $scrolling + ",resizable=0,width=" + $width + ",height=" + $height + ",top=" + $y + ",left=" + $x;
	$popup = window.open($url, 'default', $properties);
	$popup.focus();
}


function detail()
{
	return;
	
	if ($('#detail').is(':visible'))
	{
		$('#detail').hide();
		return;
	}

	if ($('#detail').html().length == 0) return;

	$('#detail').show();
	detailSetup();	
}


function detailSetup()
{
	$disabled = false;
	
	$('#detail .items').css('overflow', '');
	$('#detail .items').css('height', '');

	$('#detail .videos a.image').hover(
		function() { $(this).find('span em').css('display', 'block'); },
		function() { $(this).find('span em').css('display', 'none'); }
	);

	$('#detail .videos .pagination button').click(function() {
		$parent = $('#detail');
		$direction = ($(this).attr('class') == 'next');
		$page = parseInt($parent.find('input[name=page]').val(), 10) + ($direction ? 1 : -1);
		$pages = parseInt($parent.find('input[name=pages]').val(), 10);

		if ($page < 1 || $page > $pages) return;

		$data = {
			id: $parent.find('input[name=id]').val(),
			page: $page
		};

		detailPage($data, $direction);					   
	});
	
	sIFR.replaceElement('#detail strong.header', named({ sFlashSrc: 'http://www.miamiadschool.com/videos/assets/swf/fonts/helvetica-neue-medium-condensed.swf', sColor: '#333333', sWmode: 'transparent' }));
}


function videoPlayer($data)
{
	if ($data.length == 0) return;
	
	if ($video == null)
	{
		$video = new SWFObject("http://www.miamiadschool.com/assets/swf/player.swf", 'video-player', 990, 369, '8', '#bebebe');
		$video.addParam('allowScriptAccess', 'always');
		$video.addParam('allowFullScreen', 'true');
		$video.addParam('wmode', 'transparent');
		$video.addVariable('data', $data);
		$video.write('video');
	}
	else
	{
		$('#video-player')[0].update(unescape($data));
	}
}


function videoPopup($url)
{
	popup($url, 643, 369);	
}


function rating($button, $class, $index)
{
	$parent = $($button).closest('div');
	$index = parseInt(($index === true ? $parent.find('input[name=score]').val() : $($button).attr('name')), 10);

	jQuery.each($parent.find('a'), function() {
		 $i = parseInt($(this).attr('name'), 10);
		 $(this).attr('class', ($i <= $index ? $class : ''));
	});	
}


function category($parent, $data, $direction)
{
	if ($disabled) return;
	
	$disabled = true;

	$parent.find('input').each(function() {
		$name = $(this).attr('name');
		if ($data[$name] == null) $data[$name] = $(this).val();
	});

	$url = 'http://www.miamiadschool.com/videos/category/?';

	jQuery.each($data, function($key, $value) { $url += $key + '=' + $value + '&'; });
	jQuery.getJSON($url, function($data) {
		$items = $parent.find('.items');
		$items.css('overflow', 'hidden');
		$parent.find('.items ul').animate({ marginLeft: ($direction ? -1 : 1) * 878 }, 1250, function() {
			$parent.find('.order').html($data.order);
			$parent.find('.pagination').html($data.pagination);
			$parent.find('form').html($data.form);
			$(this).html($data.items);
			$(this).animate({ marginLeft: ($direction ? 1 : -1) * 878 }, 0);
			$(this).animate({ marginLeft: 0 }, 1250, function() { setup(); });
		});
	});	
}


function setup()
{
	$disabled = false;
	
	$('#list .items').css('overflow', '');
	$('#list .items').css('height', '');

	$('#list a.image').hover(
		function() {
			if ($disabled) return;
			$(this).find('span em').css('display', 'block');
			$tooltip = $(this).closest('li').find('.tooltip');
			$x = $tooltip.parent().position().left;
			$tooltip.find('.content').css('backgroundPosition', 'top ' + ($x > 300 ? 'right' : 'left'));
			$tooltip.css('marginLeft', ($x > 300 ? -400 : 188));
			
			if ($.browser.msie) {
				$tooltip.show();
			}
			else {
				$tooltip.fadeIn('slow');
			}
		},
		function() {
			$(this).find('span em').css('display', 'none');
			$tooltip = $(this).closest('li').find('.tooltip');

			if ($.browser.msie) {
				$tooltip.hide();
			}
			else {
				$tooltip.fadeOut(0);
			}
		}
	);

	$('#list .order a').click(function() {
		$parent = $(this).closest('li.category');
		$data = { order: $(this).attr('name'), page: 1 };
		category($parent, $data);
	});

	$('#list .pagination button').click(function() {
		$parent = $(this).closest('li.category');
		$direction = ($(this).attr('class') == 'next');
		$page = parseInt($parent.find('input[name=page]').val(), 10) + ($direction ? 1 : -1);
		$pages = parseInt($parent.find('input[name=pages]').val(), 10);

		if ($page < 1 || $page > $pages) return;

		$data = { page: $page };
		category($parent, $data, $direction);
	});

	$('#list .rating a').hover(
		function() { rating(this, 'hover'); },
		function() { rating(this, 'active', true); }
	);

	$('#list .rating a').click(function() {
		$parent = $(this).closest('div');
		$parent.load('http://www.miamiadschool.com/videos/videos/rating/update', {
			id: $parent.find('input[name=id]').val(),
			score: parseInt($(this).attr('name'), 10)
		}, function() { setup(); });							   
	});

	sIFR.replaceElement('#list h2', named({ sFlashSrc: 'http://www.miamiadschool.com/videos/assets/swf/fonts/helvetica-neue-medium-condensed.swf', sColor: '#333333', sWmode: 'transparent' }));
}


$().ready(function() {

	$('#search input').focus(function() { if (this.value == 'Enter search') this.value = ''; });
	$('#search input').blur(function() { if (this.value == '') this.value = 'Enter search'; });
	$('#search button').click(function() {
		$value = $('#search input').val();

		if ($value.length == 0 || $value == 'Enter search')
		{
			$('#search input').val('Enter search');
			return;
		}

		$('#search form').submit();
	});

	$('#search form').submit(function() { $('#search button').trigger('click'); return false; });

	setup();
	
	$('#video textarea').each(function() {
		videoPlayer($(this).val());										  
	});
	
	$('#scroller').each(function() {
		$flash = new SWFObject("http://www.miamiadschool.com/assets/swf/scroller.swf", null, 920, 189, '8', '#f4f4f4');
		$flash.addParam('allowScriptAccess', 'always');
		$flash.addParam('allowFullScreen', 'true');
		$flash.addVariable('data', $(this).find('textarea').val());
		$flash.write('scroller-content');							  
	});
});