// This is the carosell slider for liquidproductions.co.uk
// please do not reuse this code without credit
//
// alan@kalelmedia.com

// START EDIT HERE //

var element = "cats";
var videoElement = "thevideo";
var catsPerLine = 8;
var vidoesPerLine = 8;
var firstVideo = "<img src='./vidstill.jpg' />";
var secondDiv = "upperstrip";
var lastopen = "";
var currentvideo = "";
var nothing = "";

// DO NOT EDIT BELOW THIS LINE //

// check if we have prototype and scriptaculous
var hasPrototype = function(){ Prototype == "Undefined" ? 0 : 1; }
var hasScript = function(){ Scriptaculous == "Undefined" ? 0 : 1; }
var openCat = 0;
// set our reserved types, that is elements we dont want
// to hide on our loop of childElements
var reservedTypes = ["A","SPAN","EM","I","B","U","IMG"];

// modify the array prototype with in_array
// could have used prototypes indexOf but
// like the cleaner approch of in_array
Array.prototype.in_array = function(p_val) {
	for(var i = 0, l = this.length; i < l; i++) {
		if(this[i] == p_val) {
			return true;
		}
	}
	return false;
} 

// another array prototype modification to
// split the array into chunks of a set size
Array.prototype.chunk = function chunk(s){
    for(var x, i = 0, c = -1, l = this.length, n = []; i < l; i++)
        (x = i % s) ? n[c][x] = this[i] : n[++c] = [this[i]];
    return n;
}


// function to test tagType against
// reservedTypes array
function reservedTag(e){
	if(reservedTypes.in_array(e.tagName)){
		return 1;
	} else { return 0; }
}

// a wrapper function for getElementsByTagName
// for element level requests and returns as
// true Array for Enumerating
function getAll(ele, tag){
	return $A(ele.getElementsByTagName(tag));
}

// function to hide all but reserved tags
// this function only extends 2 nodes deep
// "children and childrens children"
function hideAll(ele){
	ele.childElements().each(function(s){
		s.childElements().each(function(d){
			if(!reservedTag(d)){
				d.hide();
			}
		});
	});
}

// this function will section a number
// of a given element type into div's
// and will add next and back options
// to each end of the container
function section(ele,number,type){
	var all = getAll(ele, type);
	if(all.size() > number){
		
		var chunks = all.size() / number;
		var peices = "";
		
		var current = ele.down(2).innerHTML;
		var previous = "<a href='#' onClick='previousSlide();'>Prev</a>";
		var next = "<a href='#' onClick='nextSlide();'>Next</a>";
		
		var newHTML = previous + peices + next;
		ele.down(2).update(newHTML);
		
		//alert('yup 2 many');
	}
}

// function to show all hidden elements
// this function is NOT the reverse of
// hideAll as it dose not check reservedTags
// and affects ALL descendants not just children
function showAll(ele){
	ele.descendants().each(function(s){
		if(!s.visible()){
			s.show();
		}
	});
}

// function to build the slider
function build(){
	
	var ele = $(element);
	
	if(hasPrototype){
		if(hasScript){
			if(ele){
			
				hideAll(ele);
				$(videoElement).innerHTML = firstvideo();
				
			} else { throw "Sorry, element not found"; }
		} else { throw "Sorry, Script.aculo.us must be loaded"; }
	} else { throw "Sorry, prototype must be loaded"; }

}

// function to show, 
function showCwat(idx){
	//first hide all
	if(openCat == idx){
		hideAll($("upperstrip"));
		openCat = 0;
	} else {
		hideAll($("upperstrip"));
		$('cat'+idx+'sub').show();
		openCat = idx;
	}
}

function showCat(idx){
	lastopen = idx;
	if($('upperstrip').visible()){
		Effect.BlindUp('upperstrip',{
			afterFinish: function(){
				if(!$("cat"+idx+"sub").visible()){
					slide(idx);
				}			
			}
		});
	} else {
		slide(idx);
	}
}

function slide(idx){

	toggle.each(function(s, index){
		if(Prototype.Browser.IE){
			if(typeof s=='undefined'){
				nothing = 0;
			} else { $(s).hide();}
		//$(s).hide();
		} else {
			$(s).hide();
		}
	});
	
	$("cat"+idx+"sub").show();
	Effect.BlindDown('upperstrip');

}

// a function to clone content
// to another place
function clone2(what, where){
	var what = $(what).innerHTML;
	var where = $(where).innerHTML = what;
}

function showVideo(idx){
	if(currentvideo == ""){
		$('description').appear();
	}
	currentvideo = idx;
	var ele = $(videoElement);
	ele.innerHTML = makevideo(idx);
}

function playvideo(idx){
	//ping(idx);
	Effect.BlindUp('cats',{
		afterFinish: function(){
			Effect.BlindUp('upperstrip', {
				afterFinish: function(){
					Effect.BlindDown('arrow');
					showVideo(idx);
				}
			});
		}
		
	});
	
	//alert('playing');
	
}

function showcatsagain(){

	Effect.BlindUp('arrow',{
		afterFinish: function(){
			Effect.BlindDown('upperstrip', {
				afterFinish: function(){
					Effect.BlindDown('cats');
				}
			});
		}
		
	});	
}

function browseServer(dir){
	var url = "browser.php?path="+dir+"";
	window.open(url,"","width=300,height=500")
}

function getDescription(){
	return $('videodescription'+currentvideo).innerHTML;
}

function getVideo(idx){
	return $('videofile'+idx).innerHTML;
}

function getDimentions(idx){
	var dimentions = { width: "", height: "" }
	dimentions.width = $('videowidth'+idx).innerHTML;
	dimentions.height = $('videoheight'+idx).innerHTML;
	return dimentions
}

function makevideo(idx){
	var d = getDimentions(idx);
	return '<object type="application/x-shockwave-flash" data="'+ getVideo(idx) +'" width="'+ d.width +'" height="'+d.height+'"><param name="allowScriptAccess" value="sameDomain" /><param name="allowFullScreen" value="true" /><param name="wmode" value="transparent" /><param name="movie" value="'+ getVideo(idx) +'" /></object><iframe id="myframe" src="./video-played_en_'+idx+'.html" style="display: none"></iframe>';
	
}

function viewdescription(){
	$('thetext').hide();
	$('description').morph('width: 585px; height: 70px', 
	{
	
	afterFinish: function(){ $('thetext').appear(); } 
	
	});
	$('viewlink').innerHTML = "<span style='margin-left: 5px; margin-bottom: 5px;'>&nbsp;&nbsp;&nbsp;<a href='javascript: void(0);' onclick=\"shrinkdescription();\">x</a></span>";
	$('thetext').innerHTML = getDescription(currentvideo);
}

function shrinkdescription(){
	$('description').morph('width: 63px; height: 15px');
	$('viewlink').innerHTML = "<a href='javascript: void(0);' onclick='viewdescription()'>Description</a>";
	$('thetext').fade();
}


function backFile(file){
		opener.document.getElementById('video').value = file;
		opener.document.getElementById('button').value = "Change..";
		window.close();
}

function ping(idx){
     
    var iframe = document.getElementById('myframe'); 
    iframe.src = './video-played_en_'+idx+'.html';  

}

function firstvideo(){
	return '<object type="application/x-shockwave-flash" data="./userfiles/port/videos/portfolio-landing-video.swf" width="615" height="346"><param name="allowScriptAccess" value="sameDomain" /><param name="allowFullScreen" value="true" /><param name="wmode" value="transparent" /><param name="movie" value="./userfiles/port/videos/portfolio-landing-video.swf" /></object>';
}

Event.observe(window, 'load', function() { build(); }); 