//Globals
var nextlink='';
var prevlink='';

window.addEvent('domready', function() {
	$$('.thumbnail').each(function(dhtmlThumbs){
		dhtmlThumbs.addEvent('click', function(changeLinkToJS){
			var size=dhtmlThumbs.rel.split("x");
			insertAsset('assetPanel', dhtmlThumbs.href, size[0], size[1]);
			new Event(changeLinkToJS).stop();
		});
	});

	$('downloadThis').addEvent('click', function(changeLinkToJS){
		var movie=$('downloadThis').getProperty('href');
		window.location='index.php?filename='+movie+"&ac=dlFile";
		new Event(changeLinkToJS).stop();
	});
	//IE Hacks
	if(Browser.Engine.trident){

		$('menuPanel').setStyle('margin-bottom', '0px')
		if(Browser.Engine.version==4){//IE6
			$('thumbnailPanel').setStyle('width', '100%')
			$('downloadLinks').setStyle('font-size', '.9em')
			
		}
	}
});


function getNextBack(currentMovie){
	$$('#thumbnailMenu a').each(function(imageLinks){
		if(currentMovie == (imageLinks.getProperty('href'))){
			var tr=imageLinks.getParent();
			var prevtr=tr.getPrevious();
			var nexttr=tr.getNext();
			if(nexttr!=null){
				nextlink = nexttr.getChildren('a');
			}
			if(prevtr!=null){
			    prevlink = prevtr.getChildren('a');
			}
		}
	});	
}


function back(id, height, width){
	insertAsset(id, String(prevlink.getProperty('href')), height, width, 'true')
}

function next(id, height, width){
	insertAsset(id, String(nextlink.getProperty('href')), height, width, 'true')
}


function getName(movie){
	//add the dl this link probably not the besst place but what the hell
	$('downloadThis').set('href', movie);
	
	while(movie.search('/')!=-1){
		movie=movie.substring(movie.search('/')+1,movie.length);
	}
	movie=movie.substring(0, movie.length-4);
	order=movie.substring(0, 3);
//	if(isNaN(order)==false){
//		movie=movie.substring(4, movie.length)
//	}
	movie=movie.replace(/_/g,' ');
	$('name').set('html', movie);
}

var downoading='false';
function downloadZip(id){
	if(downoading=='false'){
		downoading='true';
		var checkForFile = new Request.HTML({url:'index.php', onSuccess:
			function(responseTree, responseElements, responseHTML, responseJavaScript){
				if(responseHTML=='true'){
					makeZipFile(id);
				}
				else{
					alert("Your file is being prepared, this can take up to 10 minutes. When the file is ready, your download will start automatically. Please do not navigate away from this page until download starts.\n\n Watching other videos in this section is OK")
					makeZipFile(id);
				}
			}
		}).get({'id': id, 'ac':'checkZip'});
	}else{
		alert("Your file is being prepared please be patient")
	}
}

function makeZipFile(id){
	window.location='index.php?id='+id+"&ac=makeZip";
	downoading=='false';
}

//Generic asset injection code
function insertAsset(id, movie, height, width, autostart, thumbimage){
	getNextBack(movie);
	getName(movie);
	var type=movie.substring((movie.length-3),(movie.length)).toLowerCase();
	if(autostart==undefined) var autostart='true';
	if(type=='mov'){
		height=parseInt(height)+20;
		insertQuicktime(id, movie, height, width, autostart)
	}
	else if((type=='mp4')||(type=='flv')){
		insertFLV(id, ''+movie, height, width, autostart, thumbimage)
	}
}

function insertQuicktime(id, movie, height, width, autostart){
	var assetPanel=document.getElementById(id);
	var QTCode=QT_GenerateOBJECTText_XHTML(movie, width, height, '', 'autoplay', autostart, 'emb#bgcolor', 'white', 'align', 'middle');
//	QTCode=QTCode+'<br><a href="'+movie+'">Download Movie</a> (right/control click and \'save as\')';
	assetPanel.innerHTML=QTCode;
}

function insertFLV(id, movie, height, width, autostart, thumbimage){
	var s1 = new SWFObject("includes/player.swf","ply",width,height,"9","#ffffff");
	if(thumbimage!=undefined)
		var thumbcode="image="+thumbimage+"&";
	else
		var thumbcode='';
		
	s1.addParam("allowfullscreen","true");
	s1.addParam("allowscriptaccess","always");
	s1.addParam("flashvars", thumbcode+"file="+movie+"&skin=includes/bekle.swf&autostart="+autostart+"&controlbar=over");
	s1.write(id);
}

