var posterImgUrl = "/sites/all/themes/frameworkhawkeye/images/poster.png";
var profileXMLUrl = "/sites/all/themes/frameworkhawkeye/xml/homepage-profiles.xml";
var thmbInit = 0;

$().ready(function(){
	$('#flashAlt').hide();
	loadProfiles();
  
	//variables for the profiles
	var detailsState = 'ready';
	var titleTimer;
  
	//rollover effect
	$('.profileThm img').live('mouseover', function(){$(this).fadeTo('fast', 0.5);});
	$('.profileThm img').live('mouseout', function(){$(this).fadeTo('fast', 1);});
  
	//switch to details view
	$('.profileThm img').live('click', function(){
		if (detailsState != 'processing') {
			detailsState = 'processing';
			var parentId = $(this).parent().attr('id').split('_');
			var parentIdNum = parentId[1];
			var detailId = '#profileDetail_' + parentIdNum;
			var videoId = '#videoWrapper_'+ parentIdNum;
			var videoPlayerId = "html5Video";
			var flashID = 'vPlayer_'+ parentIdNum;
      
			$('#profilesPeople').fadeTo('slow', 0);
			$('#profilesPlusMinus').fadeTo('fast',0);
			$('#profilesTopBar').animate({
				top: '0'
			}, 1500, 'easeOutExpo', function(){
				$('#profilesDetailsBG').fadeTo('fast', 1);
				$('#profilesDetailsBG').animate({
					top: '12'
				}, 600, 'easeOutQuart', function(){
				
					$('#profilesDetails').fadeIn();
				
						titleTimer = setTimeout("$('"+detailId+"').fadeIn(500);",5100);
				  

					if (VideoJS.browserSupportsVideo()){
						$('#videoWrapper').show();
					} 
					else{
						setTimeout("$('#flashAlt').show();",200);
					}
				
				
					if (VideoJS.browserSupportsVideo()) {
						$('#videoWrapper').html('<video id="html5Video" class="video-js" width="300" height="180" preload autoplay controls><source id="src_mp4" src="'+profileVideos[parentIdNum]+'.mp4" type="video/mp4"><source id="src_ogg" src="'+profileVideos[parentIdNum]+'.theora.ogv" type="video/ogg"></video>');
						$("video").VideoJS();

						$("video")[0].player.play();
					}
					else{
						flvPlay(profileVideos[parentIdNum]);
					}
					$('#profilesPlusMinus').attr('class', 'plusMinus_minus');
					$('#profilesPlusMinus').fadeTo('fast',1);
					detailsState = 'showing';
				});
			}); 
		}
		else{
			return false;
		}
	});
  
	//plus minus action => turns off details view and refreshes profile list
	$('#profilesPlusMinus').click(function(){
		if(detailsState == 'showing'){
			$('#profilesPlusMinus').hide();
			$('#profilesTopBar').animate({top: '-188'}, 1000, 'easeInOutCubic', function() {
				$('#profilesPeople').fadeTo('slow', 1);
			});
			$('#profilesDetailsBG').fadeTo('fast', 0);
			$('#profilesDetailsBG').animate({top: '195'}, 1000,'easeOutCirc');
			clearTimeout(titleTimer);
			$('#profilesDetails').hide();
			$('.profileDetail').hide();
			if (VideoJS.browserSupportsVideo()){$('#videoWrapper').hide();} 
			else{$('#flashAlt').hide();}
			$('#profilesPlusMinus').attr('class', 'plusMinus_plus');
			$('#profilesPlusMinus').fadeIn(3500);
			detailsState = 'ready';
		}
		else if(detailsState == 'ready'){
			$('#profilesDetails').hide();
			$('.profileDetail').hide();
			if (VideoJS.browserSupportsVideo()){$('#videoWrapper').hide();} 
			else{$('#flashAlt').hide();}
			loadProfiles();
		}
		else{
			return false;
		}      
	});
  
  
	//text animations

	setTimeout("$('#greeting #greeting_hi').fadeIn(2500)",500);
	setTimeout("$('#greeting #greeting_txt').fadeIn(2500)",2000);
	setTimeout("$('#greeting p').fadeIn(1500)",3000);
	setTimeout("$('#profilesTopBar').fadeIn(2500)",4000);
	setTimeout("$('#profilesPlusMinus').fadeIn(2500)",4000);
  
});


var thisSelection;
var totalEntries;
var profileVideos = new Array();

function getRandomArray(){
	var drn = new Array();
	var rand;
	var x = 0;
	
	while(x < totalEntries){
	    rand = Math.floor(totalEntries*Math.random())
	    rand = rand+1;
		if (jQuery.inArray(rand, drn) == -1){
			drn[x] = rand;
			x=x+1;
		}
	}
	return drn;
}


function loadProfiles(){
	$('#profilesPeople').html("");
	$('#profilesDetails').html("");
	//setup ajax call for profiles from xml file
	$.ajax({
		type: "GET",
		url: profileXMLUrl,
		dataType: "xml",
		success: function(xml) {
			 var id = new Array();
			 var pName = new Array();
			 var pImage = new Array();
			 var pTitle = new Array();
			 var pVideo = new Array();
			 var pProfileUrl = new Array();
			 var x = 1;
			 var p = 1;
			$(xml).find('profile').each(function(){
				id[x] = $(this).attr('id');
				pName[x] = $(this).find('name').text();
				pImage[x] = $(this).find('image').text();
				pTitle[x] = $(this).find('title').text();
				pVideo[x] = $(this).find('video').text();
				pProfileUrl[x] = $(this).find('profile_url').text();
				x=x+1;
			});
			totalEntries = x-1;
			thisSelection = getRandomArray();
			$.each(thisSelection, function(){        
				$('<div class="profileThm" id="profile_'+p+'"></div>').html('<img src="'+pImage[this]+'" id="pImage_'+p+'" alt="'+pName[this]+'" title="'+pName[this]+'" />').appendTo('#profilesPeople');
				$('<div class="profileDetail" id="profileDetail_'+p+'"></div>').html('<span class="pName"><a href="'+pProfileUrl[this]+'">'+pName[this]+'</a></span> <span class="pTitle">'+pTitle[this]+'</span> <span class="pProfileUrl"><a href="'+pProfileUrl[this]+'">view profile</a></span>').appendTo('#profilesDetails');
				profileVideos[p] =  pVideo[this];
				p = p+1;
			});
			showThms();
		}
	});
}

function randOrd(){
	return (Math.round(Math.random())-0.5); 
} 

function showThms(){
	var timeOut = 3000;
	if (thmbInit) {	timeOut =0; }
	var thmHolder = [1,2,3,4,5,6,7,8,9,10,11];
	thmHolder = $.shuffle(thmHolder);
	$.each(thmHolder, function(){
		setTimeout('$(\'#profile_'+this+' img\').fadeIn(1000)',timeOut);
		timeOut = timeOut +250;
	});
	thmbInit = 1;
}

function getFlashMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}

function flvPlay(flvFile){
	getFlashMovie("vidwidget").vidSelect(flvFile+".flv");
	getFlashMovie("vidwidget").vidControl('rewind');
	getFlashMovie("vidwidget").vidControl('play');
}


(function($){
	$.fn.shuffle = function() {
		return this.each(function(){
			var items = $(this).children();
			return (items.length)
			? $(this).html($.shuffle(items))
			: this;
		});
	}
	
	$.shuffle = function(arr) {
		for(
			var j, x, i = arr.length; i;
			j = parseInt(Math.random() * i),
			x = arr[--i], arr[i] = arr[j], arr[j] = x
		);
		return arr;
	}
})(jQuery);



