myVar = self.location.toString();
if(myVar.indexOf('#')==-1){
	document.location='/announcement/';
}


//general config
animTime=400;



function strstr (haystack, needle, bool) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: strstr('Kevin van Zonneveld', 'van');
    // *     returns 1: 'van Zonneveld'
    // *     example 2: strstr('Kevin van Zonneveld', 'van', true);
    // *     returns 2: 'Kevin '
    // *     example 3: strstr('name@example.com', '@');
    // *     returns 3: '@example.com'
    // *     example 4: strstr('name@example.com', '@', true);
    // *     returns 4: 'name'
    var pos = 0;

    haystack += '';
    pos = haystack.indexOf(needle);
    if (pos == -1) {
        return false;
    } else {
        if (bool) {
            return haystack.substr(0, pos);
        } else {
            return haystack.slice(pos);
        }
    }
}




var slug='';
var starting=true;
var lookforproject=false;
var loadTalentDefault=true;
//load everything
$(function(){
	
	//------------ JQADDRESS ------------
	
	$.address.init(function(event) {
	    $('#menu a:not(.outside)').address(function() {
	    	return $(this).attr('href').replace(location.pathname, '');
	    });
	}).change(function(event) {
		
		//redirect to announcement
		if(event.value=='/'){document.location='/announcement/';};
		
		if(event.pathNames[0]=='talent' && event.pathNames[1]!=undefined){
			//talent switching
			loadTalentDefault=false;
			loadTalent(event.pathNames[1]);
			return false;
		}
	
	    url = (event.value == "/" || event.value == "") ? "home" : event.value;

	   	//change page or project
		slug=strstr(url,'/home/');
		if(slug){
			slug=slug.split('/');
			slug=slug[2];
		
		   	if(slug!=''){
		   		lookforproject=true;
	   			url='home';
	   			if(starting){
	   				changePage(url.replace('/',''));	
	   			}
	   		}
	   	}else{
	   		changePage(url.replace('/',''));
	   	}
	});

	


	init();
});


function init(){
	
		
	
	
	$('#linkdown').bind('click', animateDown);
	$('#linkup').bind('click', animateUp);
	
	
		$("#talent-videos a").mouseover(function() {
		  //$('#log').append('<div>Handler for .mouseover() called.</div>');
		 $("#credit-image").css('display','block');
		 $("#credit-image").attr('src','');
		 $("#credit-image").attr('src',$(this).attr('rel'));
		 $("#credit-image").css('top',$(this).position().top);
	});
	
	$("#talent-videos a").mouseout(function() {
		 $("#credit-image").css('display','none');
	});
		
	
	/*
	var curcolor='';
	
	$('a').mouseover(function(){
		curcolor=$(this).css('color');
		$(this).stop().animate({color: '#000000'},'fast');

	});
	
	$('a').mouseout(function(){
		$(this).stop().animate({color: curcolor},'fast');
	});
	*/
	
	//-----------VIMEO-----------------
	
	$(".video-trigger").bind('click',function(event){
		event.preventDefault();
		var video=$(this).attr('rel');
		var width=$(this).attr('width');
		var html = '<iframe id="vimeoplayer" src="http://player.vimeo.com/video/'+video+'?title=0&autoplay=1&amp;byline=0&amp;portrait=0&amp;color=ffffff" width="'+width+'" height="402" frameborder="0"></iframe>';
		
		$(this).parent().parent().children('.video').html(html);
		$(this).parent().parent().children('.video').css('display','block');
		$(this).parent().parent().children('.image').css('display','none');		
	});
	
	
	$(".video-trigger").mouseover(function(event){
		
		$(this).parent().children('.playbutton').stop(true, true).fadeIn(200);

	});
	
	$(".video-trigger").mouseout(function(event){
		
		$(this).parent().children('.playbutton').stop(true, true).fadeOut(200);
	
	});
	
		

}

function deleteVideos(){
	 $('.image').css('display','block');
  	 $('.video').html('');
  	 $('.video').css('display','none');
}


function changePage(url,callback){
	loadTalentDefault=true;
	//alert(url);
	$("#menu a").removeClass('selected');
	$("#menu_"+url).addClass('selected');

	//fade out of the current content
	$('#content-fader').fadeOut(animTime, function() {
			deleteVideos();
			//load new talent via ajax
			$.ajax({
		 		url: url+'.php?cms_status='+cms_status,
			  	success: function(data){
	    			$('#content-fader').html(data);
	    			//animate talent-slide down	
					$('#content-fader').fadeIn();
					
					init();
					
					if(callback!=undefined && callback==true){
						if(url=='talent'){
							loadTalent(currentTalentId);
						}
					}
					
		  		}
			});
	});
	
}


function closeTalentContainer(callback){
	$('#talent-container').fadeOut(callback);
}


function openTalentContainer(callback){
	$('#talent-container').fadeIn(callback);
}



var currentTalentId=0;

function loadTalent(id){

	currentTalentId=id;

	if($("#talent-names").length == 0){
		changePage('talent',true);
		return false;
	}
	
	
	
	$("#talent-names a").removeClass('selected');
	$("#name_"+id).addClass('selected');
	$("#talent-bottom-links").fadeOut();
	$("#talent-bottom-video-links").fadeOut();
	$("#talent-bottom-bio-links").fadeOut();
	
	//close current talent-slide up
	closeTalentContainer(function() {	     
			//load new talent via ajax
			$.ajax({
		 		url: "loadtalent.php?cms_status="+cms_status+"&id="+id,
			  	success: function(data){
	    			$('#talent-container').html(data);
	    			
	    			init();
					
					$("#talent-bottom-links").fadeIn();
					
	    			openTalentContainer(function(){
	    			
	    			});  	
		  		}
			});
	});
	
}


var talentVideoList='';

function loadTalentVideo(id){	
	//hides the credit-image, just in case
	$("#credit-image").css('display','none');

	talentVideoList=$('#talent-content').html();
	$('#talent-bottom-links').fadeOut(animTime);
	$('#talent-content').fadeOut(animTime, function() {	  
		
		//load new talent via ajax
		$.ajax({
	 		url: 'load_talent_video.php?cms_status='+cms_status+'&id='+id+'&talentid='+currentTalentId,
		  	success: function(data){
    			$('#talent-content').html(data);
    			//animate talent-slide down	
				$('#talent-content').fadeIn();
				$('#talent-bottom-video-links').fadeIn();
				init();
				//$('html, body').animate({scrollTop:180}, 500);
	  		}
		});
	});
}


function closeTalentVideo(){	
	//$('html, body').animate({scrollTop:0}, 500);
	$('#talent-bottom-video-links').fadeOut();
	$('#talent-content').fadeOut(animTime, function() {
	
			$('#talent-content').html(talentVideoList);
			
			$('#talent-content').fadeIn();
			$('#talent-bottom-links').fadeIn();
			
	  		init();
	});
}


function openBio(){
	talentVideoList=$('#talent-container').html();

	$('#talent-bottom-links').fadeOut(animTime);
	closeTalentContainer(function() {	     
	 		
			//load new talent via ajax
			$.ajax({
				
		 		url: "load_bio.php?cms_status="+cms_status+"&id="+currentTalentId,
			  	success: function(data){
	    			$('#talent-container').html(data);
	    			
	    			init();
					//$('html, body').animate({scrollTop:180}, 500);
					$("#talent-bottom-bio-links").fadeIn();
					
	    			openTalentContainer(function(){
	    			
	    			});  	
		  		}
			});
	});
}





function closeBio(){	
	
	//$('html, body').animate({scrollTop:0}, 500);
	
	$('#talent-bottom-bio-links').fadeOut();
	
	$('#talent-container').fadeOut(animTime, function() {
	
			$('#talent-container').html(talentVideoList);
			
			$('#talent-container').fadeIn();
			$('#talent-bottom-links').fadeIn();
			
	  		init();
	});
}


function downloadCredits(){
	window.open('download_credits.php?id='+currentTalentId);
}


function changeTalentKind(pos){
	
	$("#talent-bottom-links").fadeOut();
	$("#talent-bottom-video-links").fadeOut();
	$("#talent-bottom-bio-links").fadeOut();
	
	//close current talent-slide up
	closeTalentContainer(function() {	     
	 		
			//load new talent via ajax
			$.ajax({
		 		url: "loadtalent.php?cms_status="+cms_status+"&id="+ currentTalentId +"&pos="+pos,
			  	success: function(data){
	    			$('#talent-container').html(data);
	    			
	    			init();
					
					$("#talent-bottom-links").fadeIn();
					
	    			openTalentContainer(function(){
	    			
	    			});  	
		  		}
			});
	});

}




