/**
* Laurynas Karvelis
* 2011
*/

// this event fires as soon as DOM is loaded successfully, so it's fired faster than $(window).ready()
jQuery(function() {});

// this event is triggered only when all DOM elements are downloaded and loaded
$(window).ready(function (){
	if($('#hello-anim').length > 0) {
		// intro animation
		$('#hello-anim').height($(window).height());

		$('#strip1').fadeIn(800, function() {
			$('#strip2').fadeIn(700, function() {
				$('#strip3').fadeIn(400, function() {
					$('#hello-word').fadeIn(300, function() {
						$('#hello-anim #intro-logo').fadeIn(2000);
					});
				});
			});
		});

		$(window).click(function() {
			$('#main-page').removeClass('hidden');
			$('#hello-anim').hide();

			initializeWindow();
		});
	} else {
		initializeWindow();
	}

	$('a:not(a[target=_blank])').not('.jp-audio-container a').not('a.watch-now').click(function() {
		var link = this;

		if($('#content').length > 0) {
			$('#content').animate({ opacity: 0.4 }, 300, function() {
				if($(link).attr('target') == '_blank') {
					window.open(link.href);
				} else {
					window.location = link.href;
				}
			});

			return false;
		}
	});
});

function initializeWindow() {
	$("#slideshow").smoothDivScroll({ scrollStep: 8, scrollInterval: 7 }).smoothDivScroll('showHotSpotBackgrounds');

	// resize content blocks to be the same height
	var heights = [], maxHeight = 0;

	heights.push($('#bbox').height());
	heights.push($('#wbox1').height());
	heights.push($('#wbox2').height());

	for(var i = 0; i < heights.length; i++) {
		if(heights[i] > maxHeight) {
			maxHeight = heights[i];
		}
	}

	$('#content').height(maxHeight);
	$('#content>div').height(maxHeight);

	$('a.watch-now').click(function() {
		$.fancybox({
			'padding'		: 0,
			'autoScale'		: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'title'			: this.title,
			'width'		    : 680,
			'height'		: 495,
			'href'			: 'http://www.youtube.com/v/' + this.rel,
			'type'			: 'swf',
			'swf'			: {
				'wmode'		: 'transparent',
				'allowfullscreen'	: 'true'
			}
		});

		return false;
	});
}

function getId(url) {
	return /https?:\/\/(?:[a-zA_Z]{2,3}.)?(?:youtube\.com\/watch\?)((?:[\w\d\-\_\=]+&amp;(?:amp;)?)*v(?:&lt;[A-Z]+&gt;)?=([0-9a-zA-Z\-\_]+))/i.exec(url)[2];
}
