
$(document).ready(function(){

	$('#mainNav ul li ul').hide();
	
	if ( $('#mainNav ul li[class*="active"]').length < 0 ) $('#mainNav ul li ul:first').show();
	else $('#mainNav ul li[class*="active"]').parents('ul').show();
	
	$('#mainNav ul li a').click(function() {
		var checkElement = $(this).next();
		if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
			return false;
		}
		if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
			var prevTarget = $(this).parents('li.active').html();
			var target = checkElement.html();
			/*
			console.log(prevTarget);
			console.log('---------------------------------------');
			console.log(target);
			*/
			if (target != null && prevTarget == null) {
				$('#mainNav ul li ul:visible').slideUp();	
			}
			if (target != null && prevTarget != null) {
				$('#mainNav ul li ul li ul:visible').slideUp();	
			}
			checkElement.slideDown('normal');
			$('#mainNav ul li:has(ul)').removeClass('active');
			$(this).parents('li').addClass('active');
			return false;
		}
	});
	
 	/*------------------- Galeries ------------------------------------------------------------------------*/
	//----------- Main Slideshow
	
	$('#slideshow_projects').cycle({ 
		fx:     'fade', 
		timeout: 0, 
		speed:   10, 
		speedIn: 2500,
		speedOut: 2500,
		next:   '#next',
        prev:   '#prev',
		before:  onBefore, 
		after:   onAfter
	 });
	$('#slideshow').cycle({ 
		fx:     'fade', 
		timeout: 3500, 
		speed:   10, 
		speedIn: 2500,
		speedOut: 2500,
		next:   '#next',
        prev:   '#prev',
		before:  onBefore, 
		after:   onAfter
	 });
	
	function onBefore() { 
		$('#output').fadeOut(2000);		
	}
	
	function onAfter(curr,next,opts) { 
		if (this.alt != '') {
			$('#output').html("");
			$('#output').append('<p>' + this.alt + '</p>'); 
			$('#output').fadeIn(2000);
		}
		if (opts.slideCount==1) {
			$('#slideshow').cycle('stop');
			return false;
		}
	}
	
	$('#pause').click(function() { $('#slideshow').cycle('pause'); return false; });
    $('#play').click(function() { $('#slideshow').cycle('resume'); return false; });

	/*-----------------------------------------------------------------------------------------*/
	$.easing.custom = function (x, t, b, c, d) { 
		var s = 1.70158;  
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b; 
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b; 
	}
	$.easing.easeOutExpo = function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	};
	$.easing.easeOutBounce = function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	};
	$('#mainNav ul li.active ul li.active ul li.first a').addClass('selected');
				  

	$("#leftSidebar,#contentWrapper").equalHeights();
	
}); //Close document.ready

$(window).bind("load", function() {							
    //-----------  Preload images for 
	$('#loading').hide();
	$('#slideshowWrapper').fadeIn('slow');
	/*$('#mainNav ul li.active ul').slideDown();*/

});
jQuery.fn.equalHeights = function() {
	var maxHeight=0;
	this.each( function() {
		if (this.offsetHeight>maxHeight) {maxHeight=this.offsetHeight;}
	});
	this.each( function() {
		$(this).height(maxHeight + "px");
		if (this.offsetHeight>maxHeight) {
			$(this).height((maxHeight-(this.offsetHeight-maxHeight))+"px");
		}
	});
};
