esfUI.extend({

	initPage:function(){
		// Preload Glance BG Images
	var pre = Array('/images/glance/glancebgtop.gif', '/images/glance/glancebgtile.gif','/images/glance/glancebgfooter.gif',
	        '/images/glance/glancebgbluetop.gif',  '/images/glance/glancebgbluetile.gif',  '/images/glance/glancebgbluefooter.gif', 
	        '/images/glance/glancebggreentop.gif', '/images/glance/glancebggreentile.gif', '/images/glance/glancebggreenfooter.gif', 
	        '/images/glance/glancebgtantop.gif', '/images/glance/glancebgtantile.gif', '/images/glance/glancebgtanfooter.gif');
		$(pre).each(function(i){
			$('<img>').attr('src',pre[i]);
		});
		
		var flashvars = {};
		var params = {wmode:'opaque'};
		params.base = "/flash/";
		var attributes = {};
		attributes.id = "flashcontent";
		swfobject.embedSWF("/flash/ESFCamps.swf", "homeflash", "946", "479", "9.0.124", "/flash/expressInstall.swf", flashvars, params, attributes);
		
		this.scrollList('#quotes', 'li');
		this.scrollList('#homebanners', 'li');
 
		
		$('#glance-nav a:not(\'.link\')').click(function(){
			var atag = $(this);
			$('#glance-nav a').each(function(){
				$(this).removeClass('active');
			});
			atag.addClass('active');
			esfUI.showCampInfo(atag.attr('href').substr(1).split(':').pop());
			return false;
		});
		
		// Set default content
		var s = window.location.hash;
		if(s!=''){
			$('#glance-nav a').each(function(){
				if($(this).attr('href') == s){
					$(this).trigger('click');
				}
			});
		}
      
	},
	snippet:function(text, words){
		var words = words || 25;
		var content = text.replace(/^\s+?|\s+$/,''); // Strip leading and trailing spaces
		var clean = content.replace(/(\<([^>]+)\>)/ig,'');	// Strip HTML as this won't affect display	
		
		
		// Truncate to word count
		var re = new RegExp("\\s*(?:\\S*\\s*){"+words+"}");
		var words = clean.match(re);
		
		if(words[0].length == clean.length){
			return content
			item.html(content);
		}else{
			return words[0] + '&#8230;'
		}				
	},
	scrollList:function(pnode, cnode){	
		
		var list = $(pnode+' '+cnode);
		var len = list.length;
		var inst = this;
		var cid = $(pnode).attr('id');
		inst.currentItems = inst.currentItems || [];
		var currentItem = inst.currentItems[cid] || 0;
		
		if(currentItem > 0){
			var prev = $(list[currentItem-1]);
			prev.hide();
		}

		currentItem = currentItem > (len-1) ? 0 : currentItem;
		
		var item = $(list[currentItem]);
		
		/*
		 * Truncate our content if necessary
		*/
		
		item.html(inst.snippet(item.html(), 25));

		item.css({opacity:0,display:'block'}).animate({opacity:1},2000).animate({top:0},2000,function(){			
			item.animate({opacity:0},2000,function(){
				inst.currentItems[cid] = currentItem+1;
				setTimeout(function(){
					inst.scrollList(pnode,cnode);
				});
			});
		});		
		
	},
	showCampInfo:function(camp){
		var camp = camp;
		$('.glance-content').each(function(){
			if($(this).is(':visible')){
				$(this).slideUp(function(){
				    $(this).removeClass('active');
					$('#glance').removeClass().addClass('display-'+camp);
					$('#glancefooter').removeClass().addClass('displayfooter-'+camp);
					$('#glance-'+camp).slideDown(function(){
                      $('#glance-'+camp).css("display", "inline-block")
                    });
				});
			}			
		});       
	}      
});
