var array;
var infoDisplayed = false;

function loadVideo(){
	video = array[currentObject].file;
	name = array[currentObject].name;
	vimeoId = array[currentObject].vimeoId;
	$("media-container").innerHTML = '<div id="video"><object width="848" height="484"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id='+vimeoId+'&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id='+vimeoId+'&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="848" height="484"></embed></object></div>';
}

function loadDataFromServer(){
	new Ajax.Request("index.php?getArray=1", 
			{method: 'post',
			 onSuccess: function(transport){
			 	array = transport.responseText.evalJSON(true);
			 	display();
			 }});
}

function fadeout(i){
	$(""+currentObject).fade({duration: 0.5, from: 1, to: .4});
	currentObject = i;
	imageName = array[currentObject].image;
	image = new Image();
	image.src = "images/browsing/"+imageName;
	
	$("media-container").fade({ duration: 0.5, from: 1, to: 0 });
	setTimeout("display()", 550);
}

function display(){
	$("media-container").style.opacity = 0.0;
	$("media-container").show();
	
	//alert("display");
	imageName = array[currentObject].image;
	if(array[currentObject].type == "video"){
		$("media-container").innerHTML = '<div id="image"><img class="selectable" onclick="loadVideo()"  src="images/browsing/'+imageName+'" /></div>';
	}
	else{
		$("media-container").innerHTML = '<div id="image"><img src="images/browsing/'+imageName+'" /></div>';
	}
	
	descriptionName = array[currentObject].description;
	$("description").innerHTML = '<img src="images/descriptions/'+descriptionName+'" />';
	
	$("info").innerHTML = array[currentObject].infoTxt;
	
	thumbs = '';
	for(i = 0; i < 4; i++){
		if(array[i]){
			thumbName = array[i].thumb;
			thumbs += '<td><img id="'+i+'" class="selectable" onclick="fadeout('+i+');" src="images/Thumbnails/'+thumbName+'" /></td>';
		}
		else{
			thumbs += '<td></td>';
		}
	}
	$("tray").innerHTML = thumbs;
	
	for(i = 0; i < 4; i++){
		if($(""+i)){
			$(""+i).style.opacity = .4;
		}
	}
	
	$("media-container").fade({ duration: 0.5, from: 0, to: 1 });
	$(""+currentObject).fade({duration: 0.5, from: .4, to: 1});
	
	pages = "";
	for(i = 0; i < nbPages; i++){
		if(i == currentPage){
			pages += '<img src="images/page_select.png" />';
		}
		else{
			pages += '<img src="images/page_deselect.png" />';
		}
		
	}
	
	$("thumbs-page").innerHTML = pages;
	
	if(currentPage == 0){
		$("previous").style.opacity = 0.4;
		$("previous").removeClassName("selectable");
	}
	else {
		$("previous").style.opacity = 1.0;
		$("previous").addClassName("selectable");
	}
	
	if(currentPage == nbPages - 1){
		$("next").style.opacity = 0.4;
		$("next").removeClassName("selectable");
	}
	else{
		$("next").style.opacity = 1.0;
		$("next").addClassName("selectable");
	}
}

function nextPage(){
	if(currentPage < nbPages - 1){
		new Ajax.Request("index.php?next=1", 
			{method: 'post',
			 onSuccess: function(transport){
			 	array = transport.responseText.evalJSON(true);
			 	currentObject = 0;
			 	currentPage ++;
			 	display();
			 }});
	}
}

function previousPage(){
	if(currentPage > 0){
		new Ajax.Request("index.php?previous=1", 
			{method: 'post',
			 onSuccess: function(transport){
			 	array = transport.responseText.evalJSON(true);
			 	currentObject = 0;
			 	currentPage --;
			 	display();
			 }});
	}
}

function toggleInfo(){
	if(infoDisplayed){
		Effect.BlindUp('info');
		infoDisplayed = false;
	}
	else{
		Effect.BlindDown('info');
		infoDisplayed = true;
	}
}

function download(){	
	$("downloadFrame").src = "download.php?index="+currentObject;
}
