var currentState = "NONE"; 
var previousState = "NONE"; 
var curent = rand(0,filme.length-1);
var previous = 0;
var cur_menu = '';

function rand(l,u){
     return Math.floor((Math.random() * (u-l+1))+l);
}

var player = null;
function playerReady(thePlayer) {
	player = document.getElementById(thePlayer.id);
	addListeners();
}

function addListeners() {
	if (player) { 
		player.addModelListener("STATE", "stateListener");
	} else {
		setTimeout("addListeners()",100);
	}
}

function stateListener(obj) { //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
	currentState = obj.newstate; 
	previousState = obj.oldstate; 
	if((currentState == "COMPLETED") && (previousState == "PLAYING")) {
		$('#container').css('display','none');
		if(cur_menu!="") {
			$('.section').removeClass("current");
			$('#'+cur_menu).addClass("current");
			setTimeout("slider()",500);
		}else{
			cur_menu="company";
			setTimeout("slider()",500);
		}
	}
	if((currentState == "PAUSED") && (previousState == "PLAYING")){
		player.sendEvent('PLAY');
	}
}

function slider(){
	$('#'+cur_menu+' div:eq(2)').slideDown('slow');
}

function setBg(){
	$('body').css('background',"#000 url('"+url_base+"uploads/images/video/"+images[curent]+"') no-repeat top center fixed");	
}

function createPlayer(movie) {
	$('#movie2_'+previous).replaceWith("<div id='movie"+previous+"' align='center'></div>");
	$('#container').show();

	var flashvars = {
		file: url_base+"uploads/movies/"+movie, 
		autostart: "true",
		stretching: "fill",
		controlbar: "none"
	}
	var params = {
		allowfullscreen: "true", 
		allowscriptaccess: "always",
		wmode: "opaque"
	}
	var attributes = {
		id: "movie2_"+curent,  
		name: "movie2_"+curent
	}
	
	height=$(window).height();
	width=$(window).width();
	
	swfobject.embedSWF("player.swf", "movie"+curent, width, height, "9", false, flashvars, params, attributes);
}

$(document).ready(function(){
	height=$(window).height();
	$('div.jScrollPaneContainer, .content').css('height',(height*0.4)+'px');
	$('#clients .content').css('height',(height*0.2)+'px');
	$('#contact .content').css('height',(height*0.2)+'px');
	$('#company .content').css('height',(height*0.15)+'px');
	
	$('div#company div.content').jScrollPane();
	$('div#film-projects div.content').jScrollPane();
	$('div#commercials div.content').jScrollPane();
	$('div#video4business div.content').jScrollPane();
	$('div#clients div.content').jScrollPane();
	$('div#contact div.content').jScrollPane();
	
	$('div.jScrollPaneContainer:not(:first)').hide();
	$('div.jScrollPaneContainer').css('width','100%');
								   
	$('#wrapper').css('height',$(window).height()+'px');
	if($.browser.msie){
		$('#wrapper').css('margin-bottom','-16px');
	}
	
	setTimeout("setBg()",1000);
	createPlayer(filme[curent]);	
	
	
	$('.header, .header2').bind('mouseover', function(){
		$(this).parent().addClass("hover");
	}).bind('mouseout', function(){
		$(this).parent().removeClass("hover");
	}).bind('click',function(){	
		if($(this).next('div').css('display')!="block"){
			previous = curent;
			curent = rand(0,filme.length-1);
			$('div.jScrollPaneContainer').slideUp('slow');
			setTimeout("createPlayer(filme[curent])",500);

			if(cur_menu!="") {
				$('.section').removeClass("current");
				$('#'+cur_menu).addClass("current");
				setTimeout("slider()",500);
			}else{
				cur_menu="company";
				setTimeout("slider()",500);
			}
			setTimeout("setBg()",1000);
			cur_menu = $(this).parent().attr('id');
		}
	});
	
	$("div.scrollable").scrollable();
	
	$("a.group").fancybox({
		'overlayOpacity': 0.6,
		'hideOnContentClick': false,
		'frameWidth': 480,
		'frameHeight': 380,
		'callbackOnClose': function(){
			$('#fancy_content').html('');
		}
	});

	$('#film-projects .txt a.title, #film-projects .txt a.read_more').click(function(){
		$('#film-projects .scroller').each(function(){
			if($(this).css('display')=="block"){
				$(this).slideUp('slow');
				$(this).parent().find('.shortdesc').slideDown('slow');
				$(this).parent().find('.read_more').slideDown('slow');
			}
		});
		proj_id=$(this).attr('rel');
		if($('#'+proj_id+' .scroller').css('display')=="none"){
			$('#'+proj_id+' .scroller').slideDown('slow');
			$('#'+proj_id+' .shortdesc').hide();
			$('#'+proj_id+' .read_more').hide();
		}
		setTimeout("$('div#film-projects div.content').jScrollPane()",500);
	});
});
