$(document).ready(function() {

	//TOGGLE SCRIPT
	$(".flowToggle").flowToggle(
		{
			//	DEFAULTS
			// showHideSelector : ".flowShowHide",
			// toggleText : "",
			// toggleTextAfter : "",
			// hideToggle : false

			showHideSelector : ".flowShowHide",
			toggleText : "Continue reading",
			toggleTextAfter : "Close this",
			hideToggle : false
		}
	);

	//GLOBAL NOTIFICATIONS
	$(".notification").removeClass('noScript').addClass('hasScript').slideDown();

	$(".notification div").append("<a href='#' class='closethis'>&times;</a>");

	$(".notification").click(function(){
		$(".notification").fadeOut();
	});

	if(!$(".notification").hasClass('sticky'))
	{
	}
	
	$("#home-tabs, .tabs-box, .tabs-side").tabs();
	
	$(".tabs-bottom .ui-tabs-nav, .tabs-bottom .ui-tabs-nav > *") 
		.removeClass("ui-corner-all ui-corner-top") 
		.addClass("ui-corner-bottom");
	
	
	//BOXIFY
	$('.equalBox').boxify({
		slackParent : '.tabs-box',
		slack : '.slack',
		debug : true
	});
	
	var h = $('#bottom-tabs #tab-news').height();
	$('#bottom-tabs #tab-resources').height(h);
	
	
	//LIGHTBOX
	$('a.lightbox').lightBox({
		fixedNavigation : true
	}); // Select all links with lightbox class

	//SUPERFISH
	//$('ul.sf-menu').superfish(); 

	//SLIDESHOWS
	var promoSlide = $('#promoSlide');
	
	var h = promoSlide.find('img').height();
	var w = promoSlide.find('img').width();
	promoSlide.height(h);
	promoSlide.width(w);
	
	promoSlideCSS =	{
						'overflow': 'hidden',
						'background' : '#000 url(/theme/images/loaders/loader_1_black48.gif) center center no-repeat'
					};

	promoSlide.css(promoSlideCSS);	
	
	$('#promoSlide .slideItem').height(h).width(w);
	
	SlideCaptionCSS =	{
							'bottom': -1*$(this).height(),
							'opacity' : '0.7',
							'width' : '100%',
							'position' : 'absolute'
						};
						
	$('#promoSlide .slideCaption').each(function(){
		$(this).css(SlideCaptionCSS);
	});
	
	$('#promoSlide .slideItem').css({'background-color': '#ffe', 'border-left': '5px solid #ccc'});

	function onBefore() {
	}

	function onAfter() {
		sCaption = $(this).find('.slideCaption');
		sCaptionH = -1*sCaption.height();
		sCaption.show().animate({opacity: 0.7}, 500).animate({bottom: "0px"}, { queue:false, duration: 500 }).animate({opacity: 0.7}, 2000).animate({opacity: 0.0, bottom: sCaptionH}, 500);
	}

	function initSlideshow(slideShow)
	{
		slideShow.find('img').show();
		promoSlide.css({'background' : 'none'});
		
		slideShow.cycle({
		    timeout: 5000, 
			fx: 'fade',
			speed: 1000,
			before: onBefore,
			after: onAfter,
			pause:   1
		});
	}
	
	function is_image_loaded(image) {
	    if(!image.complete) {
	        return false;
	    }
	    if(typeof image.naturalWidth != "undefined" && image.naturalWidth == 0) {
	        return false;
	    }
	    return true;
	};

	var first_slide = $('#promoSlide img:first');
	
	if(first_slide.length > 0){
		if(is_image_loaded(first_slide.get(0))){
			initSlideshow($('#promoSlide'));
		} else {
		    first_slide.load(function(e) {
		        $('#loading').hide();
				initSlideshow($('#promoSlide'));
		    });
		}
	}
	

});


