$(document).ready(function() {

	// Variables
	$submenu_visible = false;
	
	
	var url = $.url(); // parse the current page URL
	$segment_1 = url.segment(1);	


	// Statuses
	//$('#client-bar').css('height','0');
	
	// Functions
	submenu(); 

  
});



// SUBMENU ////////////////////////////////////////////////////////////////////////////////////////////////////////////

function showSubmenu() {
    //$('#submenu').show().animate({top: '+=50'},{duration:380, easing: 'easeOutBack'});
    $('#submenu').show().animate({top: '95'},{duration:380, easing: 'easeOutBack'});
    $submenu_visible = true;
}
function hideSubmenu() {
    $('#submenu').animate({top: '50'},{duration:400, easing: 'easeOutBack'});
    $submenu_visible = false;
}	




var closedByUser = false;

function submenu() {

    // INIT ON LOAD
    if      ($segment_1 == "productions") {$subShowing = "productions";}
    else if ($segment_1 == "galleries") {$subShowing = "galleries";}
    else    {$subShowing = "";}
    
    if ($subShowing) {
        $(this).delay(400).queue( function() {
			     $('#submenu ul').hide();			
			     $('#submenu ul.' + $subShowing).show();
			     showSubmenu();
            $(this).dequeue();
		});
    }

   
    // CLICK BEHAVIOUR
	$(".submenu_toggle").click( function() {
	
		
	   if($subShowing) {// Basically, on pages where that contain a submenu
	        // 1. If menu item clicked is the same as the submenu currently showing, lock up
            if(this.id == $subShowing) {
                if(closedByUser) {
                    showSubmenu();
                    closedByUser = false;
                } else {
                    $('#submenu').stop().animate({top: '+=5'},{duration:150, easing: 'easeOutBack'}).animate({top: '-=5'},{duration:450, easing: 'easeOutQuint'});
                }
                $subShowing = this.id;
            }
            // 2. If item clicked is not the same as the submenu that is currently showing, reload bar the bar to show related submenu
            else  {
                if(!closedByUser) { // just a conditional to not have hideSubmenu when not needed, this would add a slight delay, no need for that
                    hideSubmenu();
                } else {
                    closedByUser = false; // be sure to set that, nice and clean
                }
                $(this).delay(100).queue( function() {
                        $('#submenu ul').hide();
                        $('#submenu ul.' + this.id).show();
                        $subShowing = this.id;
                        showSubmenu();
                    $(this).dequeue();
                });
            }
        }
        else { // Basically, all pages that do not autoShow the submenu
            $(this).delay(10).queue( function() {
                    $('#submenu ul').hide();			
                    $('#submenu ul.' + this.id).show();
                    $subShowing = this.id;
                    showSubmenu();
                $(this).dequeue();
            });
            // hide after a while. Be sure to declare var t; outside of the .submenu_toggle function, it needs to stay in scope so can't be local, that would remove it with clearTimeout(t);
            // t = setTimeout(function() { hideSubmenu(); $subShowing = "";},5000);
        }              
                             
	});
	
	
	$(".submenu_close").click( function() {
	   hideSubmenu();
	   closedByUser = true;
	});
	
	$("#menu li:not(.submenu_toggle),#submenu li").click( function() {
	   hideSubmenu();
	});
	
	
	
}
        
            



// MEMBER BAR ////////////////////////////////////////////////////////////////////////////////////////////////////////////

function memberBar() {
	$('#member_bar_toggle').click( function() {
		if(! $member_bar_visible) {
			randomQuote();
			$('#member_bar')
				.animate({height: '125'},{duration:400, easing: 'easeOutQuint'})
			;
			$member_bar_visible = true;
			$('#member_login')
				//.css("top","125")
				.animate({top: '-=125'},{duration:400, easing: 'easeOutQuint'})
			;
			$('#username')
				.focus();	
		} 
		else {
			$('#member_bar')
				.animate({height: '0'},{duration:400, easing: 'easeOutQuart'})
			;
			$member_bar_visible = false;
			$('#member_login')
				.animate({top: '+=125'},{duration:400, easing: 'easeOutQuart'})
			;
		}	
	});
}


function randomQuote() {
	var length = $('#login_headings li').length;
	var ran = Math.floor(Math.random()*length) + 1;
	$('#login_headings li').each(function(){
		$(this).hide();
	});

	$('#login_headings li:nth-child(' + ran + ')').show();
};



// BREADCRUMBS ////////////////////////////////////////////////////////////////////////////////////////////////////////////

function breadcrumbs() {	
	if ($.cookie("showBreadcrumbs") == "yes") { 
		$("#breadcrumbs")
			.show();
			$("#breadcrumbs_toggle img.off").hide();
		}	
		else { 
			$("#breadcrumbs")
				.hide();
				$("#breadcrumbs_toggle img.on").hide();
	}
	$("#breadcrumbs_toggle").click( function() {
					
		if ($.cookie("showBreadcrumbs") == "no") {
			$.cookie("showBreadcrumbs", "yes");
			$("#breadcrumbs")
				.show();
				$("#breadcrumbs_toggle img.on").show();
				$("#breadcrumbs_toggle img.off").hide();
		}	
		else{
			$.cookie("showBreadcrumbs", "no");
			$("#breadcrumbs")
				.hide();
				$("#breadcrumbs_toggle img.on").hide();
				$("#breadcrumbs_toggle img.off").show();
		}
	});
}

	
	
// OTHER ////////////////////////////////////////////////////////////////////////////////////////////////////////////


function keywordsValue(){
	$("#keywords")
		.css("color", "#AAAAAA")
		.val("Search");
	$("#keywords").focus(function() {
		if ($(this).val() == 'Search') {
			$(this)
				.val('')
				.css("color", "#000000");
		}
	});
	$("#keywords").blur(function() {
		if ($(this).val() == '') {
				$(this)
					.val('Search')
					.css("color", "#AAAAAA");
		}
	});
};	
	
	
function connectButtons() {
	$("#connect a img").each(function(i) {
		$(this).hover( 		function()	{$(this).animate({ marginTop: -20 }, 1);}, function(){$(this).animate({marginTop: 0}, 1);} );
		$(this).mousedown(	function()	{$(this).animate({ marginTop: -20 }, 1);});
		$(this).mouseup(	function()	{$(this).animate({ marginTop: -20 }, 1);});
	});
}


function externalLinks() {
	//http://jquery-howto.blogspot.com/2009/06/find-select-all-external-links-with.html
	$.expr[':'].external = function(obj){ return !obj.href.match(/^mailto\:/) && (obj.hostname != location.hostname); };
	$('a:external').click( function(){window.open(this.href);return false;});
}



