$(document).ready(function() {
	setLevel2();
	/* Highslide */
	//hs.registerOverlay({ overlayId:'closebutton', position:'top right', fade:2 });
	
	//$('.accordion').accordion();
	//$(".accordion #open").trigger('click');
	
	// #nav
	sfHover();
	
	$(".video-item").each(function(){
		
		var thisVideoItem = this;
		var videoItem = $(thisVideoItem);
		
		this.originalHeight = videoItem.height();
		this.contracted = true;
		
		videoItem.attr("class", "video-item-contracted");
		
		this.contractedHeight = videoItem.height();
		
		var readMoreDiv = document.createElement("div");
		var readMoreA = document.createElement("a");
		readMoreA.href = "javascript:";
		readMoreA.appendChild(document.createTextNode("Read More"));
		readMoreDiv.appendChild(readMoreA);
		readMoreDiv.style.textAlign = "right";
		
		this.parentNode.insertBefore(readMoreDiv, this.nextSibling);
		
		readMoreA.onclick = function(){
			if(thisVideoItem.contracted){
				videoItem.animate({"height": thisVideoItem.originalHeight + "px"}, 1000);
				readMoreA.textContent = "Hide";
			}
			else{
				videoItem.animate({"height": thisVideoItem.contractedHeight + "px"}, 1000);
				readMoreA.textContent = "Read More";
			}
			
			thisVideoItem.contracted = !thisVideoItem.contracted;
		}
	});

});

/* ----------------------
   Scripts
---------------------- */

function sfHover() {
	if (document.all&&document.getElementById) {
		var sfEls = document.getElementById("nav").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" over";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" over\\b"), "");
			}
		}
	}
}

/* Popup:
popup(URL, Width, Height, Left, Top, AutoCenter[0=False, 1=True], FullScreen[0=False, 1=True])
<a href="javascript:popUp('domain', 640, 480, 50, 50, 1, 0)">Popup!</a>
*/
function popup(URL, popWidth, popHeight, popLeft, popTop, autoCenter, fullScreen) {
	day = new Date();
	id = day.getTime();
	if ( autoCenter == 1 ) {
		var popLeft = (screen.width - popWidth) / 2;
		var popTop = (screen.height - popHeight) / 2;
	}
	if ( fullScreen == 1 ) {
		// Open in Full Screen window!
		eval("page"+id+" = window.open(URL, '"+id+"', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width="+(screen.width-10)+",height="+(screen.height-26)+",left=0,top=0');");
	} else {
		// Open in normal window!
		eval("page"+id+" = window.open(URL, '"+id+"', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width="+popWidth+",height="+popHeight+",left="+popLeft+",top="+popTop+"');");
	}
}


/* Auto Highlight Main Menu */
function extractLevelName(hrefString) {
	var arr = hrefString.split('/');
	arr = arr[arr.length-2]+'/'+arr[arr.length-1];
	return arr.toLowerCase();	
}

function setActiveLevel2Menu(arr, crtPage) {
	for( var i=0; i<arr.length; i++ ) {
		if(extractLevelName(arr[i].href) == crtPage) {
			if ( arr[i].className == "" ) {
				arr[i].className = "active";
			}
		}
	}
}

function setLevel2() {
	if( document.location.href ) {
		hrefString = document.location.href;
	} else {
		hrefString = document.location;
	}
	if ( document.getElementById("subnav") != null )  {
		setActiveLevel2Menu(document.getElementById("subnav").getElementsByTagName("a"), extractLevelName(hrefString));
	}
}

function initWordInputLimit(input, wordLimit, statusOutput){
	function trim(text){
		text = text.replace(/^[ ,;.!\r\n\t:?]+/, "");
		text = text.replace(/[ ,;.!\r\n\t:?]+$/, "");
		return text;
	}
	function update(){
		var text = trim(input.value);
		var words = (text.length ? text.split(/[ ,;.!\r\n\t:?]+/) : []);
		var span = document.createElement("span");
		if(words.length <= wordLimit)
			$(span).addClass("positive").text((wordLimit - words.length) + " words left");
		else
			$(span).addClass("negative").text((words.length - wordLimit) + " words over");
		$(statusOutput).empty().append(span);
	}
	update();
	$(input).keyup(update).change(update).bind("paste", update);
}

