window.log = function(){
	log.history = log.history || [];   // store logs to an array for reference
	log.history.push(arguments);
	if(this.console){
		console.log( Array.prototype.slice.call(arguments) );
	}
};

// catch all document.write() calls
(function(doc){
	var write = doc.write;
	doc.write = function(q){ 
		log('document.write(): ',arguments); 
		if (/docwriteregexwhitelist/.test(q)) write.apply(doc,arguments);  
	};
})(document);


$(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'))
	{
	}
		

	//LIGHTBOX
	$('a.lightbox').lightBox({
		fixedNavigation : true
	}); // Select all links with lightbox class

	//SUPERFISH
	$('ul.sf-menu').superfish({
		//pathClass:     'current' // the class you have applied to list items that lead to the current page
	}); 
	
	$('.tabs-box').tabs();
	

	//SLIDESHOWS
	$('.promoSlide').each(function(e){
		makeSlideShow($(this), {pager : ".promoSlidePager"+e});
	});
	
	// makeSlideShow($('.flickr-feed'));
	
	function makeSlideShow(promoSlide, options){
		
		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);
		});

		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').fadeIn();
			slideShow.find(".hidden").removeClass('hidden');

			if(options.pager) slideShow.after("<div class='"+options.pager.substr(1)+"'></div>");
			
			defaultOptions = {
			    timeout: 5000, 
				fx: 'fade',
				speed: 1000,
				before: onBefore,
				after: onAfter,
				pause:   1
			}
			
			if(typeof(options) != 'undefined'){
				var cycleOptions = $.extend(defaultOptions, options);
			}
			
			log(cycleOptions);
			
			slideShow.cycle(cycleOptions);
		}
		
		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).find('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));
				});
			}
		}
		
	}
	

});





