/*reel.js
*
*
*/
var thumbsContainer = document.getElementById("reelThumbs");

function loadVids(){
	if (request){
		var url = "adminWorkhorse.php";
		request.open("POST",url,true);
		request.onreadystatechange = showVids;
		var params = "functionGuide=loadVids";
		request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		request.setRequestHeader("Content-Length",params.length);
		request.setRequestHeader("Connection","close");
		request.send(params);
	}
}

function showVids(){
	if ((request.readyState == 4) && (request.status == 200)){
		var vidList = json_parse(request.responseText);
		for(i=0;i<vidList.length;i++){
			var vidContainer = document.createElement("div");
			vidContainer.setAttribute((document.all ? "className" : "class"),"subVidContainer");
			if (((i%3)==0) || (i==0)) vidContainer.setAttribute("id","firstSubVid");
			
			var vidLink = document.createElement("a");
			var vidFile = "";
			if (vidList[i]["filename"].substr(0,4) != "http")
				vidFile = window.location.host + vidList[i]["filename"];
			else
				vidFile = vidList[i]["filename"];
			
			//var height = vidList[i]["height"];
			//var width = vidList[i]["width"];
			
			vidLink.setAttribute("href", 'video.php?slug='+vidList[i]['slug']);
			
			var vidThumb = document.createElement("img");
			var thumbCount = 1;
			if (vidList[i]["thumb2"] !== null){
				if (vidList[i]["thumb3"] !== null)
					thumbCount = 3;
				else
					thumbCount = 2;
			} else
				thumbCount = 1;
			var randomThumb = Math.floor(Math.random()*(thumbCount+1));
			if (randomThumb == 0) randomThumb = 1;
			vidThumb.src = "reel/thumbs/"+vidList[i]["thumb"+randomThumb];
			//determine margin by subtracting thumbnail width + 8 (4px border x 2) and dividing by 2
			/*
if(!document.all){
				//bug fix - needs some delay to correctly determine width in next line.
				settimeout((vidThumb.style.marginLeft = ((278-(vidThumb.width+8))/2)+"px"),1000);
			}
*/
			vidLink.appendChild(vidThumb);
			
			var vidTitle = document.createElement("p");
			vidTitle.setAttribute((document.all ? "className" : "class"), "subVidTitle");
			vidTitle.innerHTML = vidList[i]["title"];
			
			var vidNotes = document.createElement("p");
			vidNotes.setAttribute((document.all ? "className" : "class"), "subVidNotes");
			vidNotes.innerHTML = vidList[i]["thumbnotes"];
			
			vidContainer.appendChild(vidLink);
			vidContainer.appendChild(vidTitle);
			vidContainer.appendChild(vidNotes);
			thumbsContainer.appendChild(vidContainer);
		}
	}
}

function setVidMargins(width){
	var vidContainer = document.getElementById("fullVidContainer");
	vidContainer.style.width=width+"px";
}

