//	VARS
var _data;
var _loadCount = 0;
var _contentH;
var _pageH;
var _videoH;
var _aboutUsH;
var _lastMarginTop;
var _currentPageData;

var _thumbTag = '<div class="%CLASS"><a href="%TITLE" style="background-image:url(%THUMB);">&nbsp;</a></div>';
var _scrollerTag = '<div class="scrollbar"><div class="track"><div class="thumb"><div class="end"></div></div></div></div><div class="viewport"><div class="overview"></div></div>';



//	Load XML
function loadXML(xml) {
	if (!xml) {
		$.ajax({
			url : 'content_en.xml',
			dataType : 'xml',
			error : function() { alert('load failed'); },
			success: loadXML
		});
	} else {
		parseXML(xml);
	}
}


//	Parse XML
function parseXML(xml) {
	_data = [];
	$(xml).find('trailer').each(function() {
		var o = {
			slides : getArrayTexts($(this).find('slide')),
			awards : getArrayTexts($(this).find('award')),
			videoID : $(this).find('videoID').text().length == 0 ? null : $(this).find('videoID').text(),
			title : $(this).find('title').text(),
			thumb : $(this).find('thumb').text(),
			info : $(this).find('info').text(),
			isTrailer : String($(this).find('isTrailer').text()) == '0' ? false : true
		}
		_data.push(o);	
		
	});
	init();
}

function getArrayTexts(arr) {
	if(arr == null) return null;
	for (var i = 0, l = arr.length; i < l; i += 1) {
		arr[i] = $(arr[i]).text();
	}
	return arr.length == 0 ? null : arr;
}



//	INIT

function init() {
	$('#content .items').css('width', '350px');
	$('#enable_js').html('');
	$('#enable_js').css( { 'display' : 'none', 'height' : '0px' } );
	
	_loadCount++;
	if (_loadCount == 2) {
		_contentH = $('#content').height();
		_pageH = $('#page').height();	
		_videoH = $('#video_player').height();
		_aboutUsH = $('#about_us').height();
		
		jQuery.easing.def = "easeInOutQuart";
		
		buildThumbs();
		

		//m events
		$('#page .button').click(mClick);
		$('#video_player .button.back').click(closeVideoPlayer);
		$('#about_us_button').click(openAboutUs);
		$('#about_us .button.back').click(closeAboutUs);

		// open page if in url after ?
		var searchStr = $(window.location).attr('href');
		searchStr = searchStr.substr(searchStr.indexOf('?')+1);
		searchStr = unescape(searchStr);
		
		if (searchStr == $(window.location).attr('href')) return;
		if (searchStr.replace(/ /g,'') == '') return;
		if (searchStr == 'about' || searchStr == 'contact') {
			openAboutUs(); return;
		}
		var title = titleBySearchStr(searchStr);
		if (!title) return;
		populatePageByTitle(title);
		openPage();
		
	}
	
	//$(window).scroll(function() {
	//	$(window).scrollTop(0);
	//});
	
	$('#overlay').click(function(){
		$(window).scrollTop(0);
	});
}


function buildThumbs() {
	$('#content .items').html('');
	for (var i = 0, l = _data.length; i < l; i += 1) {
		var tag = _thumbTag;
		tag = tag.replace(/%TITLE/g, escape(_data[i].title));
		tag = tag.replace(/%THUMB/g, _data[i].thumb);
		tag = tag.replace(/%CLASS/g, 'item' + ( i % 4 == 0 ? ' first' : '' ));
		$('#content .items').append( tag + '\n' );
	}
	$('#content .items').append( '<div style="clear:both;"></div>\n' );
	$('#content .items').append( '<div class="title_box"></div>\n' );
	$('#content .items .item a').unbind();
	$('#content .items .item a').mouseover( mOver );
	$('#content .items .item a').mouseout( mOut );
	$('#content .items .item a').click( mClick );
}


function titleBySearchStr(str) {
	var foundInText = [];
	for (var i = 0, l = _data.length; i < l; i += 1) {
		if (_data[i].title.indexOf(str) != -1) return _data[i].title;
		if (_data[i].info.indexOf(str) != -1) foundInText.push(i);
	}
	
	if (foundInText.length > 0) {
		return _data[foundInText[0]].title;
	}
	
	return false;
}



//	MOUSE EVENTS
function mOver(e) {
	var str = stripHref($(e.currentTarget).attr('href'));
	$('#content .items .title_box').html(str.replace(/_/g,' '));
	$('#content .items .title_box').stop(true, false).animate( { opacity : 1 }, { duration : 200 } );
}

function mOut(e) {
	$('#content .items .title_box').stop(true, false).animate( { opacity : 0 }, { duration : 200 } );
}

function mClick(e) {
	e.preventDefault();	
	if ($(e.currentTarget).hasClass('button')) {
		//	hide page and show frontpage
		if($(e.currentTarget).hasClass('back')) {
			closePage();
		}		
		//	play trailer
		if($(e.currentTarget).hasClass('play')) {
			openVideoPlayer(_currentPageData.videoID)
		}
		return;
	}
	populatePageByTitle(stripHref(unescape($(e.currentTarget).attr('href'))));
	// show page
	openPage();
}

function populatePageByTitle(t) {	
	for (var i = 0, l = _data.length; i < l; i += 1) {
		if (_data[i].title == t) {
			_currentPageData = _data[i];
			break;
		}
	}

	//	play trailer button
	$('#page .button.play').css('display','block');
	if (!_currentPageData.videoID)
		$('#page .button.play').css('display','none');
	
	$('#page .button.play').html('play movie');
	if(_currentPageData.isTrailer) 
		$('#page .button.play').html('play trailer');
		
	
	//	slideshow
	var slides = '';
	for (i = 0, l = _currentPageData.slides.length; i < l; i += 1) {
		slides += '<div class="slide" style="background-image:url(' + _currentPageData.slides[i] + ');"></div>\n';
	}
	$('#slideshow').cycle('destroy');
	$('#slideshow').html(slides);
	$('#slideshow').cycle({
		speed : 3000,
		timeout : 6000	
	});
	
	//	info
	$('#scroller').html(_scrollerTag);
	$('#scroller .overview').html('<h2>' + _currentPageData.title.replace(/_/g,'<br />') + '</h2>\n' + _currentPageData.info);
	$('#scroller').tinyscrollbar();

	//	awards
	$('#page .awards').html('');
	if (_currentPageData.awards) {
		for (i = 0, l = _currentPageData.awards.length; i < l; i += 1) {
			$('#page .awards').append('<div class="award" style="background-image:url(' + _currentPageData.awards[i] + ');"></div>');
			var award = $('#page .awards').children()[$('#page .awards').children().length - 1];
			$(award).css('margin-top', '60px');
			$(award).stop(true, false).delay(i*100 + 200).animate( {'margin-top' : '0px'}, {duration:400});
		}
	}
	
	
}



//	PAGE - TRAILER INFO PAGE
function openPage() {
	var bodyH = $('body').height();
	var contentLastMarginTop = $('#content').css('margin-top');
	_lastMarginTop = $('#page').css('margin-top');
	$('#content').stop(true, false).animate( { 'margin-top' : (-bodyH + (_contentH >> 1 | 0) )  + 'px'  }, { duration : 600, complete:function(){
		$('#content').css({ 'margin-top' : contentLastMarginTop, 'top' : '-' + _contentH + 'px' });
	} } );
	$('#page').stop(true, false).animate( { top : '50%', 'margin-top': '-' + (_pageH >> 1 | 0) + 'px' }, { duration : 600 } );
}

function closePage() {
	$('#content').stop(true, false).animate( { top : '50%', 'margin-top': '-' + (_contentH >> 1 | 0) + 'px' }, { duration : 600 } );
	$('#page').stop(true, false).animate( { top : '100%', 'margin-top': _lastMarginTop }, { duration : 600 } );
}

//	VIDEOPLAYER
function openVideoPlayer(vidID) {	
	
	var bodyH = $('body').height();
	var pageLastMarginTop = $('#page').css('margin-top');
	$('#page').stop(true, false).animate( { 'margin-top' : (-bodyH + (_pageH >> 1 | 0) ) + 'px' }, { duration : 600, complete:function(){
		$('#page').css({'top' : '-' + _pageH + 'px', 'margin-top' : pageLastMarginTop });
	} } );
	$('#video_player').stop(true, false).animate( { top : '50%', 'margin-top' : '-' + (_videoH >> 1 | 0) + 'px' }, { duration : 600, complete:function() {
		$('#video_player .inner').html('<iframe src="http://player.vimeo.com/video/' + vidID + '?autoplay=true" width="800" height="450" frameborder="0"></iframe><div class="close_button"></div>');	
	} } );
}

function closeVideoPlayer(e) {	
	$('#page').stop(true, false).animate( { top : '50%', 'margin-top': '-' + (_pageH >> 1 | 0) }, { duration : 600 } );
	$('#video_player').stop(true, false).animate( { top : '100%', 'margin-top':'0px' }, { duration : 600, complete:cleanVideoPlayer });
}

function cleanVideoPlayer() {
	$('#video_player .inner').html('');
}


//	ABOUT / CONTACT US - PAGE

function openAboutUs(e) {
	if (e) e.preventDefault();
	var bodyH = $('body').height();
	var contentLastMarginTop = $('#content').css('margin-top');
	_lastMarginTop = $('#about_us').css('margin-top');
	$('#content').stop(true, false).animate( { 'margin-top' : (-bodyH + (_contentH >> 1 | 0) )  + 'px'  }, { duration : 600, complete:function(){
		$('#content').css({ 'margin-top' : contentLastMarginTop, 'top' : '-' + _contentH + 'px' });
	} } );
	$('#about_us').stop(true, false).animate( { top : '50%', 'margin-top': '-' + (_aboutUsH >> 1 | 0) + 'px' }, { duration : 600 } );
}

function closeAboutUs() {
	$('#content').stop(true, false).animate( { top : '50%', 'margin-top': '-' + (_contentH >> 1 | 0) + 'px' }, { duration : 600 } );
	$('#about_us').stop(true, false).animate( { top : '100%', 'margin-top': _lastMarginTop }, { duration : 600 } );
}




//	UTILS

function stripHref(str) {
	str = str.split('/');
	str = str[str.length-1];
	return unescape(str);
}



//	START

loadXML();
$(document).ready(init);









