Cufon.replace('h1, h2');

(function($){
	$.urlVars = function(prefix) {
		if (typeof prefix == 'undefined') prefix = '$';
		var qString = top.location.search.substring(1);
		var pairs = qString.split(/\&/);
		for (var i in pairs) {
			var nameVal = pairs[i].split(/\=/);
			window[prefix + unescape(nameVal[0])] = unescape(nameVal[1]);
		}
	}
	
	var $vidview = 4;
	var $vidcount = 0;
	var $vidwidth = 0;
	var $vidpos = 0;
	var $ready = false;
	var $next = $('.gallery-next');
	var $prev = $('.gallery-prev');

	/* DOM-ready  */
	$(document).ready(function(){
	
		$.urlVars;
		$('#preview #client_playlist').nikelodeon('#client_vid_viewer', {
			playerSrc: "./media/flash/rmvPlayer.swf",
			skinSrc: "./media/flash/playerSkin.swf",
			playerWidth: 440,
			playerHeight: 330,
			wmode: 'transparent'
		});
		
		$('.gallery-videos').wrap('<div class="gallery-mask"></div>');
		$('.gallery-videos .video').css({
			opacity: 0,
			position: 'relative',
			top: '1em'
		});
		
		$('#gallery .gallery-list').nikelodeon('#gallery-viewer', {
			playerSrc: "./media/flash/rmvPlayer.swf",
			skinSrc: "./media/flash/playerSkin.swf",
			playerWidth: 440,
			playerHeight: 330,
			wmode: 'transparent'
		});
	});
	
	/* Window-load  */
	$(window).load(function(){
		
		$('#splash #preview_login_form').css({
			opacity: 0,
			left: '-20px',
			display: 'none'
		});
		
		$('#splash #preview_login_link').click(function(e) {
			e.preventDefault();
			$($(this).attr('href')).css('display', 'block').animate({
				opacity: 1,
				left: '10px'
			}, 1000, 'easeOutElastic');
		});
		
		$('#splash #preview_login_cancel').click(function(e) {
			$(this).parents('#preview_login_form').animate({
				opacity: 0,
				left: '-20px'
			}, 'slow', function(){
				$(this).css('display', 'none');
			});
		});
		
		/*// SPLASH INTRO & NEWS */
		
		if($('#splash #news').size() > 0) { 
			var $introDone = false;
			
			setTimeout(function(){
				showNews();
				$introDone = true;
			}, 3000);
			
			$('#intro_news').toggle(function(){
				if($introDone) showIntro();
			}, function(){
				if($introDone) showNews();
			});
			
			function showNews() {
				$('#splash #intro').stop().animate({
					opacity: 0
				}, 'slow');
				$('#splash #news').stop().animate({
					opacity: 1
				}, 'slow');
			}
			
			function showIntro() {
				$('#splash #news').stop().animate({
					opacity: 0
				}, 'slow');
				$('#splash #intro').stop().animate({
					opacity: 1
				}, 'slow');
			}
		}
		
		/*///////////////////////*/
		
		$vidcount = $('.video').size();
		$vidwidth = parseInt($('.video').outerWidth());
		
		$('.gallery-list').prepend('<div class="gallery-control" id="gallery-prev">Previous</div><div class="gallery-control" id="gallery-next">Next</div>');
		
		$('.gallery-mask').css('width', $vidview * $vidwidth);
		$('.gallery-list').css({
			width: $vidview * $vidwidth,
			paddingLeft: $('.gallery-control').outerWidth(),
			paddingRight: $('.gallery-control').outerWidth()
		});
		
		if($vidcount <= $vidview) $('.gallery-control').hide();
		
/*
		$('.video img').reflect({
			height: .15,
			opacity: .25
		});
*/
		
		$('.video .video-description').css({
			display: 'none'
		});
		
		$('.gallery-videos').css('width', $vidcount * $vidwidth);
/* 		.animate({ opacity: 1 }, 1000); */
		
		$i = 1;
		$('.video').each(function(){
			$(this).pause($i * 100).animate({ 
				opacity: 1,
				top: 0
			}, 500, 'easeOutElastic', function(){
				/* Blast JS's single-threadedness! */
				if($i > $vidcount) $ready = true;
			});
			$i++;
		});
		
		$('.video').mouseover(function(){
			if($ready){
				$(this).siblings().not($(this)).stop().animate({ opacity: .5 });
				$(this).stop().animate({ opacity: 1 });
			}
		});
		
		$('.video a').mouseover(function(){
			$desc = $('.video-description', $(this).parents('.video')).html();
			$('.blurb').html($desc).show().css('opacity', 0).animate({
				opacity: 1
			});
		});
		
		$('.gallery-videos').mouseout(function(){
			if($ready) $('.video', this).stop().animate({ opacity: 1 });
		});
		
		$('#gallery-next').click(function(){
			if(Math.abs($vidpos) + ($vidview * $vidwidth) < ($vidcount * $vidwidth)) $vidpos = $vidpos - ($vidview * $vidwidth);
			else $vidpos = 0;
			$('.gallery-videos').animate({
				marginLeft: $vidpos
			}, 'slow', 'easeOutQuad');
		});
		
		$('#gallery-prev').click(function(){
			if(Math.abs($vidpos) > 0) $vidpos = $vidpos + ($vidview * $vidwidth);
			else $vidpos = -(($vidwidth * $vidcount) - ($vidwidth * $vidview));
			$('.gallery-videos').animate({
				marginLeft: $vidpos
			}, 'slow', 'easeOutQuad');
		});
		
		if($('#packages #gallery-viewer a').size() > 0) {
			$v = $('#packages #gallery-viewer a').attr('href');
			$('#packages #gallery-viewer').empty().flash({
				src: "./media/flash/rmvPlayer.swf",
				width: 440,
				height: 330,
				flashvars: {
					player_source: $v,
					player_skin: "./media/flash/playerSkin.swf"
				},
				wmode: "transparent"
			}, {
				version: 7,
				update: false
			});
		}
	});
})(jQuery);