var size;
var page = ['#demoreel', '#produtora', '#portifolio', '#blog'];
var currentView = 0;
var nowPlaying = 0;

var getPageIndex = function(hash) {
	for(var i = 0; i < 4; i++)
		if(page[i] == hash) return i;
	return 0;
};

var leaveTo = function(newView) {
	$('.menublocker').css('zIndex', '10');
	setMenuClicked(newView);
	if(currentView == 0) $('#demoreel .content').animate( { 'opacity': '0' }, 700, function() { 
		$('#thumb').css({'opacity': '1', 'display': 'block'});
		$($('#demoreel iframe').get(0)).attr('src', $($('#demoreel iframe').get(0)).attr('src'));
		enter(newView); 
	});
	else if(currentView == 2) $('.videoList, .info').animate( { 'opacity': '0' }, 700, function() {
		$('#portifolio .content').animate( { 'opacity': '0' }, 700, function() { 
			$('#thumbp').css({'opacity': '1', 'display': 'block'});
			changeVideo(0);
			enter(newView); 
		});
	});
	else if(currentView == 3) {
		$($('#blog iframe').get(0)).attr('src', 'blog/index.php');
		$(page[currentView]+' .content').animate( { 'opacity': '0' }, 700, function() { enter(newView); });
	}
	else $(page[currentView]+' .content').animate( { 'opacity': '0' }, 700, function() { enter(newView); });
};

var enter = function(newView) {
	$(page[currentView]).css('display', 'none');
	$(page[newView]).css('display', 'block');
	if(newView == 0)	$('#demoreel .content').animate( { 'opacity': '1' }, 700, function() {
		setTimeout("$('#thumb').animate( { 'opacity': '0' }, 700, function() { $(this).css('display', 'none'); })", 500);
	});
	else if(newView == 2) $(".videoList, .info").animate({ 'opacity': '1' }, 700, function() {
		$('#portifolio .content').animate( { 'opacity': '1' }, 700, function() {
			setTimeout("$('#thumbp').animate( { 'opacity': '0' }, 700, function() { $(this).css('display', 'none'); })", 500);
		});
	});
	else {
		if(newView == 3) sendMessage($('#blog iframe').get(0));
		$(page[newView]+' .content').animate( { 'opacity': '1' }, 700);
	}
	$('.menublocker').css('zIndex', '-1');
	currentView = newView;
};

var setMenuClicked = function(newView) {
	$('.menu > a img').removeClass('clicked');
	$($('.menu a').get(newView)).find('img:first').addClass('clicked');
};

var changeVideo = function(index) {
	if(nowPlaying == index) return;
	fillInfo(index);
	$($('#portifolio iframe').get(index)).css('visibility', 'visible');
	$($('.corner').get(index)).css('borderColor', '#f6921e');
	$($('#portifolio iframe').get(nowPlaying)).css('visibility', 'hidden');
	$($('.corner').get(nowPlaying)).css('borderColor', '#bbbbbb');					
	$($('#portifolio iframe').get(nowPlaying)).attr('src', $($('#portifolio iframe').get(nowPlaying)).attr('src'));
	nowPlaying = index;
};

var fillInfo = function(id) {
	$('.info span').each( function(index, span) {
		if(videos[id]._get(index)[0].length != 0) {
			if(index == 0) $(span).html(videos[id]._get(index)[1]);
			else $(span).html(videos[id]._get(index)[0] + ': ' +  videos[id]._get(index)[1]);
		}
		else $(span).html('');
	});
};

var receiveMessage = function(event) {
	if (event.origin !== "http://hemisferiocriativo.com.br") return;
	resizeIframe(parseFloat(event.data));
};

var sendMessage = function(iframe) {
	resizeIframe(100);
	var newSize = size < 1 ? size : 1;
	iframe.contentWindow.postMessage(newSize + "px", "http://hemisferiocriativo.com.br");
};

var resizeIframe = function(frameHeight) {
	var blogIframe = $('#blog iframe').get(0);
	if(size >= 1) {
		if(frameHeight > 358) $(blogIframe).height(frameHeight+20);
		else $(blogIframe).height(358);
	} else {
		if(frameHeight > 358*size) $(blogIframe).height(frameHeight+20);
		else $(blogIframe).height(358*size);
	}
	$('.blogcenter').height($($('#blog iframe').get(0)).height() + 115);
};

var crossBrowser = function() {
	if(navigator.userAgent.search('Chrome') >= 0)
		$('dl').css('marginTop', '10%');
	if((navigator.appName.search('Explorer') >= 0)||(navigator.appName.search('Opera') >= 0)) {
		$('.content').css({'marginTop': '0', 'marginLeft': '0', 'borderWidth': '0'});
		$('#blog iframe').css({'marginTop': '0', 'borderWidth': '0'});
	}
};

var setBackground = function() {
	var bgs = ['bg800.gif', 'bg1280.gif', 'bgfullhd.gif'], bg;
	if(screen.width <= 800) bg = bgs[0];
	else if(screen.width <= 1280) bg = bgs[1];
	else bg = bgs[2];
	$('body').css('backgroundImage', 'url(img/' + bg + ')');
};

var positioningStuff = function() { // colocar no css
	$('.bottombar div').each( function(index, div) {
		$(div).css('left', index*23 + '%');
	});
};

var setSize = function() {
	if(screen.width/screen.height <= 4/3)	size = screen.width/1280;
	else size = screen.height*0.8/640;
	size = size < 1 ? size : 1;
};

var resizePage = function() {	
	if(size < 1) {
		$('.floater').css('marginTop', -320*size + 'px');
		$('.center').height(640*size).width(1280*size);
		$('body').css('fontSize', 16*size + 'px');
		$('.menu > a div').height('5.6%').css({'marginTop': 6*size + 'px', 'top': 20*size + 'px'});
		$('.menu > a img').height('100%');		
		$('.bottombar > a img').width('100%');
		$('.contact img').height('5.6%');
		$('#blog iframe').width(640*size).css('top', 109*size + 'px');
	}
	var bar_width = (screen.width-1280*size)/2;
	$('.leftbar').width(bar_width).css('marginLeft', -bar_width+'px');
	$('.rightbar').width(bar_width).css('marginRight', -bar_width+'px');

	$('.blogcenter').width(screen.width);
};

var addEvents = function() {
	$('.menuover a img').hover( 
		function() { $(this).animate( { opacity: 1 }, 300);	}, 
		function() { $(this).animate( { opacity: 0 }, 300);
	});

	$(".corner").hover( function() { 
		var index = $(".corner").index(this);
		$(".title span").text($(this).children()[0].alt); 
		$(".title").css("left", index*11.4 + "%");
		if(nowPlaying == index) return;
		$(this).css('borderColor', '#8f8f8f');
	}, function() {
		$(".title span").text("");
		if(nowPlaying == $(".corner").index(this)) return;
		$(this).css('borderColor', '#bbbbbb');
	});
	
	$('.corner').click( function() {
		changeVideo($('.corner').index(this));
	});
	
	$('#blog iframe').bind('load', function() {
		sendMessage(this);
  });
	
	window.addEventListener("message", receiveMessage, false);
};

var getDinamically = function() {
	$(videos).each( function(index, video) {
		$('#portifolio .content').append("<iframe src=\"http://player.vimeo.com/video/" + video._id + "\" frameborder=\"0\"></iframe>");
		$.getJSON('http://www.vimeo.com/api/v2/video/' + video._id + '.json?callback=?', {format: "json"}, function(data) {
			$("#thumb" + index).attr('src', data[0].thumbnail_small);
			$("#thumb" + index).attr('alt', data[0].title);
		});
	});
	$.getJSON('http://www.vimeo.com/api/v2/video/' + videos[0]._id + '.json?callback=?', {format: "json"}, function(data) {
		$('#portifolio #thumbp').attr('src', data[0].thumbnail_large);
	});
	$.getJSON('http://www.vimeo.com/api/v2/video/' + demoreelId + '.json?callback=?', {format: "json"}, function(data) {
		$('#demoreel #thumb').attr('src', data[0].thumbnail_large);
	});

	$('#demoreel iframe').attr('src', 'http://player.vimeo.com/video/' + demoreelId);

	$('#demoreel #thumb').load( function() {
		$('#demoreel iframe').load( function() {
			start();
		});
	});
};

var start = function() {
	$("#portifolio iframe:first").css("visibility", "visible");
	$(".corner:first").css('borderColor', '#f6921e');
	fillInfo(0);

	$('.cover').css('display', 'none');
};

var navigate = function() {  
  $(window).bind('hashchange', function () {
    var hash = window.location.hash || '#demoreel';
		var newView = getPageIndex(hash);
    leaveTo(newView);
  });
  $(window).trigger( "hashchange" );
};

$(document).ready(function() {
	crossBrowser();
	setBackground();
	positioningStuff();
	setSize();
	resizePage();
	addEvents();
	getDinamically();
	navigate();
});

