/***************************************************************************
// INIT
***************************************************************************/
var domReady = false;

$(document).ready(function() {

		if( exists( "#slideshow" ) ) {
			initializeSlideshow();
		}
		// if 2 column
		if( exists( "#inner-right-full") ) {
			var h = $("#inner-right-full" ).height();
			var tab = $("#tabs" ).height();
			var sbHeight = $("#sidebar").height() + tab;
			if( h > sbHeight ){
				$("#sidebar" ).height( h - tab );
			}
		}
		// if 3 column
		if( exists( "#inner-right") ) {
			var centerHeight = $("#inner-center" ).height();
			var rightHeight = $("#inner-right" ).height();
			var tab = $("#tabs" ).height();
			var sbHeight = $("#sidebar").height() + tab;
			var gt = centerHeight > rightHeight ? centerHeight : rightHeight;
			if( gt > sbHeight ){
				$("#sidebar" ).height( gt - tab );
			}
		}

		if( exists( "#drop-downs")) {
			createDropDowns();
		}
		domReady = true;
});


/***************************************************************************
// DROP DOWNS
***************************************************************************/
var current;

function createDropDowns(){
	$('.dropdown').each(function( index ) {
		var dd = $(this);
		$(this).find( 'dt a' ).click(function(e) {
			console.log( "MAIN");
			e.preventDefault();
			current = dd;
			closeOthers( dd );
	        $(dd).find("dd ul").toggle();

	    });
	});

    $(".dropdown dd ul li a").click(function(e) {
		e.preventDefault();
        var text = $(this).html();
		$( this ).closest("ul").hide();
		var dd = $( this ).closest("dl");
		$( dd ).find( "dt a span").html(text);
    });

    $(document).bind('click', function(e) {
        var $clicked = $(e.target);
        if (! $clicked.parents().hasClass("dropdown"))
            $(".dropdown dd ul").hide();
    });
}

function closeOthers( clicked ){
	var attr = $(clicked).attr( 'id' );
	$('.dropdown').each(function( index ) {
		var dAtt = $(this).attr( 'id' );
		if( dAtt != attr ){
			var attr1 = $(this).attr( 'id' );
			$(this).find("dd ul").hide();
		}
	});
}


/***************************************************************************
// HOMEPAGE SLIDESHOW
***************************************************************************/


function initializeSlideshow(){

    $('#slideshow').cycle({
		fx: 'scrollLeft',
		timeout: 6000,
		delay:  -1000,
		after: onAfter,
		pager:  '#pager-items',
    	pagerAnchorBuilder: function(idx, slide) {
        	return '#pager-items li:eq(' + idx + ') a';
    	}
	});

	$('#slide-details').cycle({
		fx: 'scrollLeft',
		timeout: 6000,
		delay:  -1000,
		after: onAfter,
		pager:  '#pager-items',
    	pagerAnchorBuilder: function(idx, slide) {
        	return '#pager-items li:eq(' + idx + ') a';
    	}
	});


}

function onAfter(curr,next,opts) {}


/***************************************************************************
// HELPERS
***************************************************************************/

function isReady() {
    return domReady;
}

/* 	@param		string		#element */
function exists( e ){
return $(e).length > 0;
}


















