var videoDir = domain + '/media/video/flv/';
var currentVideoIndex;

jQuery.each($('.gallery-item'),function(index, video) {
	var id = this.id;
	var file = this.file;
	$(video).click(function(){ getVideo(index); });
});

function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
    } else {
        return document[movieName];
    }
}
function startNextVideo() {
	if((currentVideoIndex + 1) < thisMovie('player').getPlaylist().length) {
		currentVideoIndex++;
	} else {
		document.location.href = 'index.php';
	}
}
function setCurrentVideoIndex(value) {
	currentVideoIndex = value;
}
function getVideo(index) {
	setCurrentVideoIndex(index);
	thisMovie('player').sendEvent('ITEM',index);
	
}
function setVideoListener() {
  	thisMovie('player').addModelListener("STATE","handleVideoComplete");
  	thisMovie('player').addViewListener("NEXT","handleNextButtonClick");
  	thisMovie('player').addViewListener("PREV","handlePreviousButtonClick");
}

function handleVideoComplete(e) {
	if(e.newstate == "COMPLETED") {
		startNextVideo();
	}
};

function handleNextButtonClick(e) {
	if((currentVideoIndex + 1) < thisMovie('player').getPlaylist().length)
	{
		currentVideoIndex++;
	}
	else
	{
		setCurrentVideoIndex(0);
	}
}

function handlePreviousButtonClick(e) {
	if(currentVideoIndex > 0) {
		currentVideoIndex--;
	}
	else
	{
		setCurrentVideoIndex(thisMovie('player').getPlaylist().length - 1);
	}
}

$(document).ready(function() { 
  if($('.gallery-item').length > 7) {
	$('#gallery-previous-btn').css('display','block');
	$('#gallery-next-btn').css('display','block');
  }
  setCurrentVideoIndex(0);
  setTimeout('setVideoListener()',10000);
});       
