var videoIds = [];

function computeVideoData(data) {
	var videoDur = [];
	var parts = explode("<entry>", data);

	var videoDiv = document.getElementById("slideBar");	

	for(i=1;i<parts.length;i++) {

		// Get video-title
		var vidTitle = parts[i].between("<media:title type=\\'plain\\'>","<\/media:title>");

		// extract video-ids and create images
		videoIds[i] = substr(parts[i].between("<id>","<\/id>"),-11);
		var vImg = document.createElement("IMG");
		vImg.setAttribute("src", "http://i.ytimg.com/vi/"+videoIds[i]+"/"+rand(1,3)+".jpg");
		vImg.setAttribute("alt", "Loading...");
		vImg.setAttribute("title", vidTitle);
		vImg.setAttribute("id", videoIds[i]);
		vImg.className = "videoImage";
		vImg.style.left = ((30*i)+((i-1)*120))+"px";
		setOpacity(vImg, 50);

		// create onclickevent
		vImg.onclick = function() {
				var params = { allowScriptAccess: "always" };
				var atts = { id: "player" };
				swfobject.embedSWF("http://www.youtube.com/v/"+this.getAttribute("id")+"&enablejsapi=1&playerapiid=ytplayer&rel=0", "player", "1", "1", "8", null, null, params, atts);
		}

		// create fancy roll-overeffect
		vImg.onmouseover = function() {
			var opc = 55
			var obj = this
			var lightUp = window.setInterval(function() {
				if(opc===100) {
					window.clearInterval(lightUp);
				} else {
					setOpacity(obj, opc);
					opc +=5 ;
				}
			},1);
			this.onmouseout = function() {
				window.clearInterval(lightUp);
				while((opc%5)!=0){
					opc--;
				}
				var lightDim = window.setInterval(function() {
					if(opc==50) {
						window.clearInterval(lightDim);
					} else {
						setOpacity(obj, opc);
						opc -= 5;
					}
				},1);
			};
		};

		// get duration in seconds, convert to readable time and put in div
		var sec = parts[i].between("<yt:duration seconds=\\'","\\'\/>");
		var left = Math.floor(sec / 60);
		var right = (sec % 60);
		if(right===0){right="00";} 
		else if(right<10){right="0"+right.toString();}
		videoDur[i] = (left+":"+right);

		var time = document.createElement("DIV");
		time.className = "videoTime";
		time.style.left = (((30*i)+(i*120))-30)+"px";
		time.innerHTML = videoDur[i];


		// Append Elements to document
		videoDiv.appendChild(time);
		videoDiv.appendChild(vImg);

	}
}

function createSlider() {
		document.getElementById("right").onmouseover = function() {
			var scrollToRight = window.setInterval(function() {
				document.getElementById("slideBar").scrollLeft += 10;
			}, 1);
			document.getElementById("right").onmouseout = function() {
				window.clearInterval(scrollToRight);
			};
		};
		document.getElementById("left").onmouseover = function() {
			var scrollToLeft = window.setInterval(function() {
				document.getElementById("slideBar").scrollLeft -= 10;
			}, 1);
			document.getElementById("left").onmouseout = function() {
				window.clearInterval(scrollToLeft);
			};
		};
}


function showVideo() {
	var gid = function(id) {
		return document.getElementById(id);
	}
	gid("filter").style.visibility="visible";
	gid("slideBar").style.visibility="visible";
	gid("left").style.visibility="visible";
	gid("right").style.visibility="visible";
	gid("player").style.visibility="visible";

	var idPresent = window.setInterval(function() {
		if(videoIds[1]) {
			window.clearInterval(idPresent);
			var params = { allowScriptAccess: "always" };
			var atts = { id: "player" };
			swfobject.embedSWF("http://www.youtube.com/v/"+videoIds[1]+"&enablejsapi=1&playerapiid=ytplayer&rel=0", "player", "1", "1", "8", null, null, params, atts);
		}
	},100);
	document.getElementsByTagName("HTML")[0].style.overflow = "hidden";
	window.scrollTo(0,0);
}


function hideVideo() {
	var gid = function(id) {
		return document.getElementById(id);
	}
	gid("filter").style.visibility="hidden";
	gid("slideBar").style.visibility="hidden";
	gid("left").style.visibility="hidden";
	gid("right").style.visibility="hidden";
	gid("player").style.visibility="hidden";
	document.getElementsByTagName("HTML")[0].style.overflow = "visible";
}

var currentShowedId = null;
function show(id) {
	currentShowedId.style.display = "none";
	document.getElementById(id).style.display = "block";
	currentShowedId = document.getElementById(id);
}

function setOpacity(object0, value) {
	/*@cc_on
		@if (@_jscript_version < 9)
			object0.style.filter = 'alpha(opacity='+ value +')';
			return false;
		@end
	@*/
	object0.style.opacity = (value / 100);
	object0.style.MozOpacity = (value / 100);
	object0.style.KhtmlOpacity = (value / 100);
}

function mixTextElem(obj) {
	obj.innerHTML = obj.innerHTML.split("").shuffle().join("");

}

function setMenuEvents() {
	var items = document.getElementById("menu").getElementsByTagName("SPAN");
	window.setTimeout(function() {
		var r = rand(0,(items.length-1));
		var title = items[r].innerHTML
		mixTextElem(items[r]);
		window.setTimeout(function() {
			items[r].innerHTML = title;
			setMenuEvents();
		}, 120);
	}, rand(10000,12000));
}

Array.prototype.shuffle = function() {
	var s = [];
	while (this.length) {
		s.push(this.splice(Math.random() * this.length, 1));
	}
	while (s.length) {
		this.push(s.pop());
	}
	return this;
} 

String.prototype.between = function(prefix, suffix) {
	var s = this;
	var i = s.indexOf(prefix);
	if (i >= 0) {
		s = s.substring(i + prefix.length);
	} else {
		return '';
	}
	if (suffix) {
		i = s.indexOf(suffix);
		if (i >= 0) {
			s = s.substring(0, i);
		} else {
			return '';
		}
	}
	return s;
}

function explode (delimiter, string, limit) {

    // version: 1009.2513
    // discuss at: http://phpjs.org/functions/explode    // +     original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     improved by: kenneth
    // +     improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     improved by: d3x
    // +     bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)    // *     example 1: explode(' ', 'Kevin van Zonneveld');

     var emptyArray = { 0: '' };
    
    // third argument is not required
    if ( arguments.length < 2 ||
        typeof arguments[0] == 'undefined' ||        typeof arguments[1] == 'undefined' ) {
        return null;
    }
 
    if ( delimiter === '' ||        delimiter === false ||
        delimiter === null ) {
        return false;
    }
     if ( typeof delimiter == 'function' ||
        typeof delimiter == 'object' ||
        typeof string == 'function' ||
        typeof string == 'object' ) {
        return emptyArray;    }
 
    if ( delimiter === true ) {
        delimiter = '1';
    }    
    if (!limit) {
        return string.toString().split(delimiter.toString());
    } else {
        // support for limit argument        var splitted = string.toString().split(delimiter.toString());
        var partA = splitted.splice(0, limit - 1);
        var partB = splitted.join(delimiter.toString());
        partA.push(partB);
        return partA;    }
}

function substr (str, start, len) {

    // version: 909.322
    // discuss at: http://phpjs.org/functions/substr
    // +     original by: Martijn Wieringa
    // +     bugfixed by: T.Wild
    // +      tweaked by: Onno Marsman
    // +      revised by: Theriault
    // +      improved by: Brett Zamir (http://brett-zamir.me)

    var i = 0, allBMP = true, es = 0, el = 0, se = 0, ret = '';
    str += '';
    var end = str.length;

    // BEGIN REDUNDANT
    this.php_js = this.php_js || {};
    this.php_js.ini = this.php_js.ini || {};
    // END REDUNDANT
    switch(
        (this.php_js.ini['unicode.semantics'] && 
            this.php_js.ini['unicode.semantics'].local_value.toLowerCase())) {
        case 'on':
            // strlen()
            for (i=0; i < str.length; i++) {
                if (/[\uD800-\uDBFF]/.test(str.charAt(i)) && /[\uDC00-\uDFFF]/.test(str.charAt(i+1))) {
                    allBMP = false;
                    break;
                }
            }

            if (!allBMP) {
                if (start < 0) {
                    for (i = end - 1, es = (start += end); i >= es; i--) {
                        if (/[\uDC00-\uDFFF]/.test(str.charAt(i)) && /[\uD800-\uDBFF]/.test(str.charAt(i-1))) {
                            start--;
                            es--;
                        }
                    }
                }
                else {
                    var surrogatePairs = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
                    while ((surrogatePairs.exec(str)) != null) {
                        var li = surrogatePairs.lastIndex;
                        if (li - 2 < start) {
                            start++;
                        }
                        else {
                            break;
                        }
                    }
                }

                if (start >= end || start < 0) {
                    return false;
                }
                if (len < 0) {
                    for (i = end - 1, el = (end += len); i >= el; i--) {
                        if (/[\uDC00-\uDFFF]/.test(str.charAt(i)) && /[\uD800-\uDBFF]/.test(str.charAt(i-1))) {
                            end--;
                            el--;
                        }
                    }
                    if (start > end) {
                        return false;
                    }
                    return str.slice(start, end);
                }
                else {
                    se = start + len;
                    for (i = start; i < se; i++) {
                        ret += str.charAt(i);
                        if (/[\uD800-\uDBFF]/.test(str.charAt(i)) && /[\uDC00-\uDFFF]/.test(str.charAt(i+1))) {
                            se++;
                        }
                    }
                    return ret;
                }
                break;
            }
        case 'off':
        default:
            if (start < 0) {
                start += end;
            }
            end = typeof len === 'undefined' ? end : (len < 0 ? len + end : len + start);
            return start >= str.length || start < 0 || start > end ? !1 : str.slice(start, end);
    }
    return undefined;
}

function rand (min, max) {

    // version: 1009.2513
    // discuss at: http://phpjs.org/functions/rand
    // +   original by: Leslie Hoare
    // +   bugfixed by: Onno Marsman

    var argc = arguments.length;
    if (argc === 0) {
        min = 0;
        max = 2147483647;    } else if (argc === 1) {
        throw new Error('Warning: rand() expects exactly 2 parameters, 1 given');
    }
    return Math.floor(Math.random() * (max - min + 1)) + min;
}



// REDUNDANT JUNK

/*
function mixTextElem(obj, l) {
	var str = "";
	var charsStart = [];
	var charsEnd = [];

	charsStart[0] = 33, charsEnd[0] = 64, charsStart[1] = 65, charsEnd[1] = 90, charsStart[2] = 91, charsEnd[2] = 102;

	for(var i=0;i<l;i++) {
		str += String.fromCharCode(rand(charsStart[rand(0,2)],charsEnd[rand(0,2)]));
	}
	obj.innerHTML = str;
}

function setMenuEvents() {
	var items = document.getElementById("menu").getElementsByTagName("SPAN");
	for(var i in items) {
		items[i].onmouseover = function() {
			var obj = this;
			var title = obj.innerHTML;
			//var l = title.length
			var menuIntval = window.setInterval(function() {
				mixTextElem(obj);
			}, 100);
			obj.onmouseout = function() {
				window.clearInterval(menuIntval);
				this.innerHTML = title;
			};
		};
	}
}
*/
