$(document).ready(function() {	
	//
	//All info must be stored before being hidden!
	//
	var h = $("#header").height();
	var m = $("#main").height();
	var f = $("#footer").height();
	var header = $("#header");
	var footer = $("#footer");

	$("body").hide().find("ul#mainMenu-index").children("li").hide().end().end().find("#video-list").children("div").hide().end().end();
	/*
	*
	*these are the original(more complicated transitio effects)
	*preserved here to improve them later
	*
	$("#header a").each(function()
	{
		$(this).click(function(e)
		{ 
			e.preventDefault();
			var anchorClicked = $(this);
			var currentMenuState = $("#header #mainMenu-index").size() ? 'expanded' : 'contracted';
			var selectedMenuState = ( $(this).attr('href') == '/' ) ? 'expanded' : 'contracted';
			var transition = ( currentMenuState != selectedMenuState ) ? 1 : 0;
			if( transition )
			{
				//beause of "bubbling" the LI is also fired and the effect can take off from there
				//
				//unless the element clicked is the main image because there is not LI there
				if( selectedMenuState == 'expanded' )
				{
					if( selectedMenuState == 'contracted' ){ contractMenu($("#header li a"), anchorClicked); }
					else{ expandMenu( $("#header li a") ); }
				}
			}
		});
    });
    //
    //same as above but for the LI's should combine into one function....maybe???
    //
    $("#header li").each(function()
	{
		$(this).click(function(e)
		{ 
			e.preventDefault();
			var anchorClicked = $(this).children('a');;
			var currentMenuState = $("#header #mainMenu-index").size() ? 'expanded' : 'contracted';
			var selectedMenuState = ( $(this).attr('href') == '/' ) ? 'expanded' : 'contracted';
			var transition = ( currentMenuState != selectedMenuState ) ? 1 : 0;
			if( transition )
			{
				if( selectedMenuState == 'contracted' ){ contractMenu($("#header li a"), anchorClicked); } else{ expandMenu( $("#header li a") ); }
			}
		});
    });
	*/
	$("#header a").each(function()
	{
		$(this).click(function(e)
		{ 
			e.preventDefault();
		});
	});
	
	$("#header li").add('#header div a').each(function()
	{
		$(this).click(function(e)
		{
			$("#main").queue(function()
			{
				$(this).fadeOut(1500);
				$(this).dequeue();
			});
			var anchorClicked = $(this).is('a') ? $(this) :  $(this).children('a');
			var liCurrent = $("#current");
			var currentMenuState = $("#header #mainMenu-index").size() ? 'expanded' : 'contracted';
			var selectedMenuState = ( $(this).attr('href') == '/' ) ? 'expanded' : 'contracted';
			var transition = ( currentMenuState != selectedMenuState ) ? 1 : 0;
			if( transition )
			{
				if( selectedMenuState == 'contracted' )
				{
					$("#main").queue(function()
					{
						contractMenu($("#header li a"), anchorClicked);
					});
				}
				else
				{
					expandMenu(liCurrent, $("#header li a") );
				}
			}else if( selectedMenuState == 'contracted' )
			{
				liCurrent.removeAttr("style").removeAttr("id");
				anchorClicked.parent('li').attr('id', 'current');
				$('.menuText').hide().remove();
				//$("#header li").not("#current").each(function()
				$("#header li").each(function()
				{
					//menuHover($(this).children('a'));
					menuHover($(this));
				});
			}
			$("#main").queue(function()
			{
				/**/
				if( anchorClicked.hasClass('blog') ){
					window.location = anchorClicked.attr('href');
				}else{
				
					$("#main").load( $(anchorClicked).attr('href'), '', function()
					{
						//
						//grab new measurements; then, call the resize function
						//
						h = $("#header").height();
						m = $("#main").height();
						f = $("#footer").height();
						
						$(window).trigger('resize');
						$(this).fadeIn(1500);
						$(this).dequeue();
						Shadowbox.setup();
					});
				}
			});
		});
	});
	$('#main').click(function (e)
	{
		if( $(e.target).parent('div').attr("id") == 'video-controls' || $(e.target).parent('div').attr("id") == 'docs-controls' )
		{
			var margin = $('#video-list').size() ? $('#video-list').css('marginLeft') : $('#docs-list').css('marginLeft');
			margin = parseInt(margin, 10);
			//
			//at first the 'left' is turned off since we cannot move any further left
			//
			if( margin || $(e.target).attr("id") == 'right' )
			{
				slideVideos($(e.target));
			}
		}
	});
	$('#main').mouseover(function (e)
	{
		if( $(e.target).parent('div').attr("id") == 'video-controls' || $(e.target).parent('div').attr("id") == 'docs-controls' )
		{
			var margin = $('#video-list').size() ? $('#video-list').css('marginLeft') : $('#docs-list').css('marginLeft');
			margin = parseInt(margin, 10);
			//
			//at first the 'left' is turned off since we cannot move any further left
			//
			//if( margin || $(e.target).attr("id") == 'right' )
			//{
				arrowOn($(e.target));
			//}
		}
		if(( 
			$(e.target).parent('div').attr("class") == 'video-list-item' || $(e.target).parent('a').parent('div').attr("class") == 'video-list-item'
			)||(
			$(e.target).parent('div').attr("class") == 'docs-list-item' || $(e.target).parent('a').parent('div').attr("class") == 'docs-list-item'
			))
		{
			if($(e.target).is('img'))
			{
				$(e.target).parent('a').parent('div').children("a").children("img").css({'border' : '1px solid #D5D5D5'});
				$(e.target).parent('a').parent('div').children("span").css({'color' : '#FF9933'});
				$(e.target).css({ cursor: 'pointer' });
			}
			else
			{
				$(e.target).parent('div').children("a").children("img").css({'border' : '1px solid #D5D5D5'});
				$(e.target).parent('div').children("span").css({'color' : '#FF9933'});
				$(e.target).css({ cursor: 'pointer' });
			}
		}
	});
	$('#main').mouseout(function (e)
	{
		
		if( $(e.target).parent('div').attr("id") == 'video-controls' || $(e.target).parent('div').attr("id") == 'docs-controls' )
		{
			//
			//include a test to get either the #video-list or #docs-list
			//
			var margin = $('#video-list').size() ? $('#video-list').css('marginLeft') : $('#docs-list').css('marginLeft');
			margin = parseInt(margin, 10);
			//
			//at first the 'left' is turned off since we cannot move any further left
			//
			//if( margin || $(e.target).attr("id") == 'right' )
			//{
				arrowOff($(e.target));
			//}
		}
		if(( 
			$(e.target).parent('div').attr("class") == 'video-list-item' || $(e.target).parent('a').parent('div').attr("class") == 'video-list-item'
			)||(
			$(e.target).parent('div').attr("class") == 'docs-list-item' || $(e.target).parent('a').parent('div').attr("class") == 'docs-list-item'
			))
		{
			if($(e.target).is('img'))
			{
				$(e.target).parent('a').parent('div').children("a").children("img").css({'border' : '1px solid #7A7878'});
				$(e.target).parent('a').parent('div').children("span").css({'color' : 'inherit'});
				$(e.target).css({ cursor: 'auto' });
			}
			else
			{
				$(e.target).parent('div').children("a").children("img").css({'border' : '1px solid #7A7878'});
				$(e.target).parent('div').children("span").css({'color' : 'inherit'});
				$(e.target).css({ cursor: 'auto' });
			}
		}
	});
    var margin = calculateMarginOffset(h, m, f);
    $(header).css('margin-top', margin['header'] + 'px');
	$(footer).css('margin-top', margin['footer'] + 'px');
	$(window).resize(function(){
		var margin = calculateMarginOffset(h, m, f);
		$(header).animate( { marginTop: margin['header'] + 'px' }, 500 );
		$(footer).animate( { marginTop: margin['footer'] + 'px' }, 500 );
	});
	
	$("body").show(.001, function()
	{
		fadeInChildrenIndividually( "#mainMenu-index", "li", "slow" );
		fadeInChildrenIndividually( "#video-list", "div", "slow" );
	});
	/**
	* Fades-in the children of the containder one at a time
	* 
	* @param string containerid The name of parent that holds all elements to be faded in
	* @param string childTag The html tag to be faded in
	* @param string speed how fast to fade in each child element
	*/
	function fadeInChildrenIndividually(containerid, childTag, speed) {
		$(containerid).children(childTag + ":first").fadeIn(speed, function()
		{
			$(this).css("display", "inline-block");
			if ($(this).next().is(childTag))
			{
				inFader($(this).next(), childTag, speed);
				$(this).next().css("display", "inline-block");
			}
		});
	}
	/**
	 * Companion function used by fadeInChildrenIndividually()
	 * 
	 * @param string el The name element to be faded in
	 * @param string tag The html tag to be faded in
	 * @param string speed how fast to fade in each child element
	 */
	function inFader(el, tag, speed) {
		el.fadeIn(speed, function() {
			if (el.next().is(tag)) {
				inFader(el.next(), tag, speed);
				el.next().css("display", "inline-block");
			}
		});
	}
	var id;
	
	$("li").each(function(e) {
		menuHover($(this));
   	});
   	$("div.gallery-list-item").each(function(e)
   	{
   		$(this).hover(function () {
			$(this).children("a").children("img").css({'border' : '1px solid #D5D5D5'});
			$(this).children("span").css({'color' : '#FF9933'});
		}, 
		function () {
			$(this).children("a").children("img").css({'border' : '1px solid #7A7878'});
			$(this).children("span").css({'color' : 'inherit'});
		});
   	});
   	$("div.video-list-item").each(function(e)
   	{
   		$(this).hover(function () {
			$(this).children("a").children("img").css({'border' : '1px solid #D5D5D5'});
			$(this).children("span").css({'color' : '#FF9933'});
		}, 
		function () {
			$(this).children("a").children("img").css({'border' : '1px solid #7A7878'});
			$(this).children("span").css({'color' : 'inherit'});
		});
   	});
   	$(".video-list-item").each(function()
   	{
   		if( $(this).children("a").attr("rel") == "lbox-test" )
   		{
   			$(this).click(function (e) { e.preventDefault(); jQuery.slimbox( "http://www.sudiorb.com/images/coming-soon.jpg", "Amsterdam Lights" ); });
   		}
   	});
});

function say(text){ $("#myConsole").append(text); }

function calculateMarginOffset(h, m, f)
{
	var w = $(window).height();
	var unaccountedOffset = 21;
	var minimumHeaderMarginTop = 15;//integers ONLY....px will be appended at the end
	var hOffset = null;
	var fOffset = null;
	
	var spaceNeeded = (h + m + f); // - unaccountedOffset should the unaccountedOffset be included in the calculation?
	
	if( spaceNeeded >= w  )
	{
		hOffset = minimumHeaderMarginTop;
		fOffset = 0;
	}
	else
	{
		var spaceAvailable = w - spaceNeeded;
		//
		//calculate default measurements
		//
		hOffset = Math.round( spaceAvailable *.5);
		fOffset = (spaceAvailable - hOffset) - unaccountedOffset;
		//
		//make sure values are not negative or less than the minimum
		//
		if( hOffset < minimumHeaderMarginTop ){ hOffset = minimumHeaderMarginTop; }
		if( fOffset < 0 ){ fOffset = 0; }
	}
	return { 'header' : hOffset, 'footer' : fOffset };
}
function contractMenu(anchorList, anchorClicked)
{
	anchorList.each(function()
	{
		$(this).fadeOut(1500, function()
		{
			$(this).parent('li').animate({ opacity: 0 }, 1000, 'linear', function()
			{
				anchorClicked.parent('li').parent('ul').attr('id', 'mainMenu');
				$(anchorClicked).parent('li').css({ backgroundColor:'#FF9933' }).attr("id", "current");
				menuHover($(this));	
				
				$(this).animate({ opacity: 1 },1000, 'linear', function()
				{
					$(this).removeAttr('style');
					if( $(this).children('a').text() == $(anchorList).filter(':last').text() )
					{
						$("#main").dequeue();
					}
				});
			});
		});
	});
}
function expandMenu(liCurrent, anchorList)
{
	anchorList.each(function()
	{
		$(this).parent('li').fadeOut(1500, function()
		{
			liCurrent.removeAttr("id").parent('ul').attr('id', 'mainMenu-index');
			$(this).fadeIn(1500, function()
			{
				menuHover($(this));
				$(this).children('a').fadeIn(1500, function()
				{
					$(this).parent('li').removeAttr('style');
					$(this).removeAttr('style');
				});
			});
		});
	});
}
function arrowOn(thisObj)
{
	$(thisObj).css({ marginTop:'-44px', cursor: 'pointer' });
}
function arrowOff(thisObj)
{
	$(thisObj).css({ marginTop:'-3px', cursor: 'auto' });
}
function slideVideos(thisObj)
{
	var jump = $(thisObj).attr("id") == 'left' ? 189 : -189;
				
	if( $(thisObj).attr("id") == 'left' )
	{
		$(thisObj).siblings().filter("#right")
			.unbind('mouseover')
			.unbind('mouseout')
			.unbind('click')
			.bind('mouseover', function(){ arrowOn($(thisObj).siblings().filter("#right")); })
			.bind('mouseout', function(){ arrowOff($(thisObj).siblings().filter("#right")); })
			.bind('click', function(){ slideVideos($(thisObj).siblings().filter("#right")); });
	}
	if( $(thisObj).attr("id") == 'right' )
	{
		$(thisObj).siblings().filter("#left")
			.unbind('mouseover')
			.unbind('mouseout')
			.unbind('click')
			.bind('mouseover', function(){ arrowOn($(thisObj).siblings().filter("#left")); })
			.bind('mouseout', function(){ arrowOff($(thisObj).siblings().filter("#left")); })
			.bind('click', function(){ slideVideos($(thisObj).siblings().filter("#left")); });
	}
	
	var oldMargin = $('#video-list').css('marginLeft');
	oldMargin = parseInt(oldMargin, 10);
	var newMargin = jump + oldMargin;
	
	//
	//this part below whould be outside of the 'click' call but since info is not available because of initial load hide effect
	//
	var videoListWidth = 0;
	//say( $('#video-list li').size() );
	/**/
	//re adjust real size of maxoffset, div has width
	/**/
	$('#video-list li').each(function()
	{
		constantVideoListWidth += $(this).outerWidth(true);
	});
	//say( parseInt($('#video-list').css("margin-left")) );
	videoListWidth = $('#video-list').outerWidth(true);
	videoListWidth += Math.abs(parseInt($('#video-list').css("margin-left")));
	
	var maxOffset = $('#video-area').width() - videoListWidth;
	
	if( newMargin >= 0 )
	{
		$(thisObj).unbind('mouseover').unbind('click');
		newMargin = 0;
	}
	if( Math.abs(newMargin) >= Math.abs(maxOffset) )
	{
		$(thisObj).unbind('mouseover').unbind('click');
		newMargin = maxOffset;
	}
	$('#video-list').animate({ marginLeft : newMargin + 'px' }, 500, 'linear');
}
function menuHover(thisObj)
{
	if( $(thisObj).parent().attr("id") == "mainMenu-index" )
	{
		$(thisObj).unbind('mouseenter').unbind('mouseleave').hover(function ()
		{
			$(thisObj).css({'background-color' : '#FF9933', 'cursor' : 'pointer'});
			$(thisObj).children().css({'background-color' : '#FF9933', 'color' : '#000000'});
			window.status = "Go to \"http://www.sudiorb.com/" + $(thisObj).children("a").attr("href") + "\"";
		}, 
		function ()
		{
			$(thisObj).removeAttr('style');
			$(thisObj).children('a').removeAttr('style');
			window.status = "";
		});
	}else if( ($(thisObj).parent().attr("id") == "mainMenu") && ($(thisObj).attr("id") != "current") )
	{
		$(thisObj).unbind('mouseenter').unbind('mouseleave').hover(function ()
		{
			$(thisObj).css({'background-color' : '#FF9933', 'cursor' : 'pointer'});
			window.status = "Go to \"http://www.sudiorb.com/" + $(thisObj).children("a").attr("href") + "\"";
			$(thisObj).append( '<span class="menuText">' + $(thisObj).children('a').text() + '</span>' );
		}, 
		function ()
		{
			$(thisObj).removeAttr('style');
			window.status = "";
			$('.menuText').hide().remove();
		});
	}else if( thisObj.attr("id") == "current" )
	{
		$(thisObj).unbind('mouseenter').unbind('mouseleave');
	}
}