/*
 * homepage.js - homepage specific scripts
 */
// configuration variables
// is it dev or prod? this list is "prod" urls
var lox = ['localhost','aspx','wtfsitecore','wolftrap.org','66.93','wolftrap.com','wolf-trap','wolftrap.net','thebarns','wolftrap.cc', 'wolftrap61'];
var url_slides = (isProd()) ? "/slideshowcontent/slideshow.aspx" : (encodeURI('slideshow.html?d=')+encodeURI(new Date()));
var txt_pause = 'Pause';
var txt_play = 'Play';

var bool_switchslideshow = false;
var transition_speed = 750;			// time in milliseconds to do slide transitions
var slide_time = 4000;				// time in milliseconds to display a slide

var slideshows = new Array();
var slideshow_slides = new Array();
var slideshow_current = null;

var loaded = 0;
var allImg = null;

var bool_debug = false;

function debugIt(string){
	if(bool_debug){
		console.log(string);
	}
};
// prod urls
function isProd(){
	var loc = location.href.toLowerCase();
	for (var i=0; i < lox.length; i++) {
		if (loc.indexOf(lox[i]) != -1) return true;
	};
	return false;
};
function doLoader(){
	debugIt('f:doLoader');
	loaded = 0;
	allImg = $('.slideshow.'+slideshows[slideshow_current]+ ' img').size();
	debugIt(' :: doLoader: vars: loaded='+loaded+', allImg'+allImg);
	var imgs = $('.slideshow.'+slideshows[slideshow_current]+ ' img').each(function(i){
		if ( $(this).attr('loaded') == 1 ) {
			loaded++;
		} else {
			$(this).bind('load',function(){
				$(this).attr('loaded',1);
				loaded++;
			});
			$(this).trigger('dblclick');
		}
	});
	checkLoad();
};
function checkLoad(){
	debugIt('f:checkLoad');
	var handle = window.setInterval(function(){
		debugIt(' :: checkLoad interval triggered');
		debugIt(' :: checkLoad: vars: loaded='+loaded+', allImg='+allImg);
		if (loaded >= allImg) { // no reason this should check for >= instead of ==, but FF keeps puking on occasion.
			window.clearInterval(handle);
			buildslideshow(slideshows[slideshow_current]);
			$('.slideshow.'+slideshows[slideshow_current]).fadeIn(transition_speed);
			$('#c'+slideshows[slideshow_current]+' a').addClass('on');
		}
	}, 500);
};
// reuseable functions
function switchslideshow(){
	debugIt('f:switchslideshow');
	debugIt(' :: switching to slideshow: '+slideshows[slideshow_current+1]);
	bool_switchslideshow = false;
	$('.slideshow.'+slideshows[slideshow_current]+' .slides').cycle('stop');
	$('.slideshow.'+slideshows[slideshow_current]).fadeOut(transition_speed);
	$('.slideshow.'+slideshows[slideshow_current]+' .nav a').remove();

	$('.slideshow .slide').attr('style','');
	if(slideshow_current == slideshows.length - 1){
		slideshow_current=0;
	} else {
		slideshow_current++;
	}
	doLoader();
};
function manualslideshow(y){
	debugIt('f:manualslideshow param:'+y);
	bool_switchslideshow = false;
	$('#slideControl a.on').removeClass('on');
	$('.slideshow.'+slideshows[slideshow_current]+' .slides').cycle('stop');
	$('.slideshow.'+slideshows[slideshow_current]).fadeOut(transition_speed);
	$('.slideshow.'+slideshows[slideshow_current]+' .nav a').remove();
	$('.slideshow .slide').attr('style',''); // this messes up the loader
	for(var ii=0; ii < slideshows.length; ii++){
		if(slideshows[ii] == y){
			slideshow_current=ii;
		}	
	}
	doLoader();
	$('#slideControl #c'+y+' a').addClass('on');
	$('.slideshow.'+y).fadeIn(transition_speed); // this messes up the loader also
};
function buildslideshow(x){
	debugIt('buildslideshow param:'+x);
	$('.slideshow.'+slideshows[slideshow_current]+' .nav a').remove();
	// if there's one single slide, 'cycle' isn't used
	if (slideshow_slides[slideshow_current] == 1) {
		debugIt(' :: single slide');
		window.setTimeout(function(){
			switchslideshow(x);
		}, slide_time)
	}
	debugIt(' :: triggering cycle plugin');
	$('.slideshow.'+x+' .slides').cycle({	// http://www.malsup.com/jquery/cycle/ for configuration options
		fx:'fade',							// transition to use
		speed:transition_speed,				// time for transitions
		timeout:slide_time,					// time to display slide
		pause:0,							// pause slideshow when mousing over a slide
		pager:'.slideshow.'+x+' .nav .p10',	// where the pager links go
		startingSlide:0,					// which slide to start with
		after:function(){
			debugIt('    :: slide transition complete');
			var str_thisclass = $(this).attr('class');
			var str_limitclass = 'slide index'+(slideshow_slides[slideshow_current]-1);
			if(str_thisclass==str_limitclass){
				bool_switchslideshow = true;
			}
		},
		before:function(){
			debugIt('    :: slide transition beginning');
			if(bool_switchslideshow){
				$('.slideshow.'+x).fadeOut(
					transition_speed,
					function(){
						$('#slideControl a.on').removeClass('on');
						switchslideshow(x);
					}
				);
			}
		}
	});
	$('.'+x+' .nav .p10').append('<a href="#" class="pause"><span class="pause">'+txt_pause+'<\/span><\/a>');
	$('.'+x+' .nav a.pause').toggle(
		function(){
			debugIt('    :: pause button clicked');
			$('.'+x+' .slides').cycle('pause');
			$('span',this).removeClass('pause').addClass('play').text(txt_play);
		},
		function(){
			debugIt('    :: play button clicked');
			$('.'+x+' .slides').cycle('resume');
			$('span',this).removeClass('play').addClass('pause').text(txt_pause);
		}
	);
	$('.'+x+' .nav .p10 a').focus(function(){
		this.blur();
		return false;
	});
	$('#slideControl #c'+x+' a').focus(function(){ this.blur(); return false; })
};
function setupSlides(){
	debugIt('f:setupSlides');
	$(".slideshow img").lazyload({
		placeholder : "_res/images/px.gif",
		event : "dblclick"
	});
	debugIt(' :: adding to controller arrays');
	$('.slideshow').each(function(j){
		slideshows.push($(this).attr('class').split(' ')[1]);
		slideshow_slides.push($('.slide',this).length);
		$('.slide',this).each(function(i){
			$(this).addClass('index'+i);
		});
	});
	for(var k=0;k<slideshows.length;k++){
		if(slideshows[k] == def_slideshow){
			slideshow_current=k;
		}
	}
	$('#c'+def_slideshow+' a').addClass('on');
	doLoader();
};
// apply JS to page
$(document).ready(function(){
	// if single slide, we're just not doing anything.
	if ($('div.slideshow.global').size() > 0) {
		$('#slideControl').hide();
		return true;
	} else {
		$('#slideControl').fadeIn();
		def_slideshow = $('.slideshow-default').attr('class').split(' ')[1];
		$('.slideshow-default').remove();
		$.ajax({
			datatype: "html",
			type : "GET",
			url : url_slides,
			timeout : 20000,
			global : false,
			error : function(){
				debugIt(' :: ajax error');
				return true;
			},
			success: function(data){
				// http://developer.apple.com/internet/safari/uamatrix.html (version 2 crashes!)
				if ($.browser.safari == true && $.browser.version < 523) {
					$('.slideshow-placeholder').after(data);
				} else {
					var zzz = $(data).each(function(){
						if ( $(this)[0].nodeName == 'DIV' ){
							$(this)[0].style.display = 'none'
						}
					});
					$('.slideshow-placeholder').after(zzz);
				}

				window.setTimeout(function(){
					setupSlides();
				}, 500);
				$('.slideshow.'+def_slideshow).fadeIn(transition_speed);
				$('#cfilene a').click(function(){ manualslideshow('filene'); this.blur(); return false; });
				$('#cbarns a').click(function(){ manualslideshow('barns'); this.blur(); return false; });
				$('#ceducation a').click(function(){ manualslideshow('education'); this.blur(); return false; });
				$('#copera a').click(function(){ manualslideshow('opera'); this.blur(); return false; });

				$('#cBack a').click(function(){
					this.blur();
					slideshow_current = (slideshow_current>0) ? slideshow_current-1 : slideshows.length-1;
					manualslideshow(slideshows[slideshow_current]);
					return false;
				});
				$('#cNext a').click(function(){
					this.blur();
					slideshow_current = (slideshow_current==slideshows.length-1) ? 0 : slideshow_current+1;
					manualslideshow(slideshows[slideshow_current]);
					return false;
				});
			}
		});
	}
});