/* Author: 
	Mathieu Davy
	ekynoxe.com
	2011 - 08
*/
$(document).ready(function() {

	initMenu();
	    
    /* Define a new temporary element */
    
    openElement = null;
    
    /* Add a mouse leave listener to close the list element
     * when the user moves to a different item 
    */
	$("#navigation").find("li").mouseleave(function () {
		
		$(this).children("ul").clearQueue();	
		
		openElement = $(this);
		
    	if(idle()<50) {
			mouseOut($(this));
		}
			
    });
    
    function mouseOut (element) {
        if(element.find("a").hasClass("selected")==false&&element.find("a").hasClass("active")==false) {
 	    	element.find("ul").delay(600).slideUp("fast");
    	}	
    }

    /* Add a mouse enter listener to open the list element
     * when the user selects it
    */
    $("#navigation").find("li").mouseenter(
		function () { 
			mouseOver($(this));	
	    }
	);
    function mouseOver (element) {
		if(idle() < 250) {
			if(openElement!=null) {
				//openElement.find("ul").slideUp("fast");		
				mouseOut(openElement);
    		}
			element.children("ul").delay(300).slideDown("fast");
		}
    }
    
    /* Determine when the user is using the mouse. This means 
     * that when the list slides up and the mouse passes over 
     * a number of elements these lists don't open
    */
    
    time = getTime();
	
	$("body").mousemove(function () {
		time = getTime();
	});
	
	function getTime () {
		date = new Date();
		return date.getTime();
	}
	
	function idle () {
		return getTime() - time;
	}
	
	
});

$(document).ready(function() {
	$("#filmbox-long-link").click(function () {
		//$(this).text("Show long video");
		$("#long-video").show();
		$("#short-video").hide();
		$(this).hide();
		$("#filmbox-short-link").show();
	});
	$("#filmbox-short-link").click(function () {
		$("#long-video").hide();
		$("#short-video").show();
		$(this).hide();
		$("#filmbox-long-link").show();
	});
});

$(document).ready(function() {
	initSlideShow();
});

$(document).ready(function() {
	initNav();
});

function initSlideShow() {
	$('.slideshow').cycle({
		fx: 'fade', speed: 'slow', timeout: 8000  // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});
}

function initMenu() {
	$("#navigation").treeview({
		animated: "fast",
		collapsed: true,
		unique: true,
		persist: "location",
		toggle: function() {
			window.console && console.log("%o was toggled", this);
		}
	});
}



function initNav() {
	$('header.main nav ul').droppy();
}


/*
$(document).ready(function() {
	//alert("hello");
	setPage();
});

function extractPageName(hrefString)
{
	var arr = hrefString.split('/');
	return arr[arr.length-1];
}

function setActiveMenu(arr, crtPage)
{
	for (var i=0; i < arr.length; i++)
	{
		if(extractPageName(arr[i].href) == crtPage)
		{
			if (arr[i].parentNode.tagName != "DIV")
			{
				
				$(arr[i]).addClass("current");
			}
		}
	}
}
 
function setPage()
{
	hrefString = document.location.href ? document.location.href : document.location;
 
	if ($('#navigation') !=null ) {
		console.log($('#navigation').find('a'));
		setActiveMenu($('#navigation').find('a'), extractPageName(hrefString));
	}
}*/

/*
$(document).ready(function() {		
	alert("alert");
	
	$("#collapsible-panels").
	
	$(".collabsible-tab").click(function(){
		
		collapse(1);
	})
});

function collapse(id) {
	$(".collabsible-panel").slideToggle("slow");
	$(this).toggleClass("active");
}
*/
$(document).ready(function(){

	$(".accordion h3").click(function(){
		$(this).siblings("p:visible").slideUp("fast");
		
		if($(this).next("p").is("p:visible")==false) {
			$(this).next("p").slideToggle("fast");
		}
		
	});
	$(".panels h3").click(function(){
		$(this).next("p").slideToggle("fast");
	});
	
});

