/**
 * CLVS
 */

jQuery(function($) {
	
	$('.videos .arrow').click(function() {
		
		var $t = $(this);
		
		// Do correct actions for correct direction
		if ($t.hasClass('arrow-up')) {
			
			$('.videos .viewport ul li:first').appendTo('.videos .viewport ul');
			
		} else {
			
			$('.videos .viewport ul li:last').prependTo('.videos .viewport ul');
		}
		
		return false;
	});
	
	$('.videolink').click(function() {
		
		var url = $(this).attr('rel');
		window.open(url, '_blank');
		return false;
	});
	
	function setCountdown(day, hour, min) {

		if (!day) day = 0;
		if (!hour) hour = 0;
		if (!min) min = 0;

		// Set values to prepend with zero if below 10
		if (day < 10) day = '0'+day;
		//if (day < 100 && day > 10) day = '0'+day;
		if (hour < 10) hour = '0'+hour;
		if (min < 10) min = '0'+min;

		// Update the values
		$('.cd_days').text(day);
		$('.cd_hours').text(hour);
		$('.cd_min').text(min);
	}
	
	// Run looping function for countdown
	(function() {

		// Set dates
		var currentDate = new Date(),
		    targetDate  = new Date(2011, 11, 3, 0, 0),
		    diff = (targetDate.getTime()) - currentDate.getTime();

		// Stop if passed
		if (diff <= 0) {

			setCountdown();
			return;
		}

		// Set day, hours and minutes
		var min  = 1000*60,
		    hour = min*60,
		    day  = hour*24;

		// Set new dates
		var newDay, newHour, newMin;

		// Math the differences
		// DAYS
		if (diff < day) {
			newDay = 0;
		} else {
			newDay = Math.floor(diff / day);
		}	

		// Get remaining hours
		var remainingHours = diff % day;

		// HOURS
		if (remainingHours < hour) {
			newHour = 0;
		} else {
			newHour = Math.floor(remainingHours / hour);
		}
		
		// Set remaining minutes
		var remainingMin = remainingHours % hour;

		// HOURS
		if (remainingMin < min) {
			newMin = 0;
		} else {
			newMin = Math.floor(remainingMin / min);
		}

		// Set the new date
		setCountdown(newDay, newHour, newMin);

		// Recall current function
		setTimeout(arguments.callee, 1000);
	})();
	
	// Star blinking loop
	(function(id) {
		
		// Set variables
		var thisFunc = arguments.callee,
			nextId = (id == 'stars') ? 'stars2' : 'stars',
		    stars, stars2;
		
		// Get the current context
		stars  = $('#'+id);
		stars2 = $('#'+nextId);
		
		// Check if not animating already
		if (jQuery.queue(stars) != '') return;
		
		// Fade in or out
		if (stars.hasClass('off')) {
			
			stars2.fadeOut(1500);
			stars.fadeIn(1000, function() {
				
				// Set class and repeat
				$(this).removeClass('on off').addClass('on');
				thisFunc(nextId);
			});
			
		} else {
			
			stars2.fadeIn(1000);
			stars.fadeOut(1500, function() {
				
				// Set class and repeat
				$(this).removeClass('on off').addClass('off');
				thisFunc(nextId);
			});
		}
		
	})('stars');
	
	// Set placeholder for top position of banner bg image
	var _rotatingBannerTop;
	
	// Start banner rotating loop
	(function(undef) {
		
		// Set following top offset
		if (_rotatingBannerTop === undef || _rotatingBannerTop == -290) {
			_rotatingBannerTop = 0;
		} else {
			_rotatingBannerTop -= 58;
		}
		
		// Set background position and update after a short interval
		$('#announcement .rotating_banner').css({
			backgroundPosition: 'left '+_rotatingBannerTop+'px'
		});
		
		setTimeout(arguments.callee, 2500);
	})();
});
