// JavaScript Document

function extractIMG(p_src){
	var tmpDataList = p_src.split("&");
	// get the root
	if (tmpDataList.length > 0){
		tmpROOT = tmpDataList[0].split("swf/");
		tmpROOTStr = tmpROOT[0] + "/img/slides/";
	}
	var tmpIMG = ""
  	for(nn=0;nn < tmpDataList.length;nn++){
	  if(tmpDataList[nn].indexOf("rootPages=") > -1) {
		var tmpIMG = tmpDataList[nn].substring(10);
		if(tmpIMG.length > 2){
			tmpIMG = tmpIMG.substring(0,3);
		}
	  }
	}
	if(tmpIMG == ''){
		tmpIMG = tmpROOTStr + "LMR-162.jpg"
	}else{
		tmpIMG = tmpROOTStr + "LMR-" + tmpIMG + ".jpg"
	}
	return tmpIMG;
}




//****************************************************
// utilities

var gHiliteTabcolor;
var gHiliteLinkColor;
var gHiliteTabImage;
function rsHiliter(p_mode,p_obj,p_ref){

	switch (p_mode){
		case "ON":
			//gHiliteTabcolor = p_obj.style.backgroundColor;
			gHiliteTabImage = p_obj.style.backgroundImage;
			gHiliteLinkcolor = p_obj.style.backgroundColor;
			//p_obj.style.backgroundColor = '#447788';
			//getSeason();
			p_obj.style.backgroundImage = "url('/img/" + getSeason() + "/globalTab_ON.jpg')";
			var ee = document.getElementById('pagelink_' + p_ref);
			ee.style.color = '#FFFFFF';
			break;
		case "OFF":
			//p_obj.style.backgroundColor = gHiliteTabcolor;
			p_obj.style.backgroundImage = gHiliteTabImage;
			var ee = document.getElementById('pagelink_' + p_ref);
			ee.style.color = gHiliteLinkcolor;

			break;
		default:
			//nothing
	}
};

function goNav(p_href){
	document.location = p_href;
};

function getSeason(){
	var tmpStr = new String(document.location);
	var tmpArr = tmpStr.split("/");
	for (nn = 0;nn < tmpArr.length;nn++){
		switch (tmpArr[nn].toLowerCase()) {
			case "fall":
				return "fall";
				break;
			case "summer":
				return "summer";
				break;
			case "winter":
				return "winter";
				break;
			case "theranch":
				return "theranch";
				break;
			case "heritage":
				return "heritage";
				break;
			case "conferences":
				return "conferences";
				break;
			default:
				break;
		}
	}
	return "theranch";
				
}


function getImageSrc(p_src,p_delta){  //looks for the "_" back from the "."and works backward to create name
	var tmpString = new String(p_src);
	var tmpData = tmpString.substring(0,tmpString.lastIndexOf("_",tmpString.length));  //get everything before the last "_"
	var tmpType =  tmpString.substring(tmpString.lastIndexOf(".",tmpString.length));  //get everything after the "."
	//alert('rollover=' + tmpData + '_' + p_delta + tmpType)
	return(tmpData + '_' + p_delta + tmpType);
}

function hiliteMe(p_name,p_mode){  //mode="ovr" = rollover,"out" = mouseleave; p_obj = this
	var tmpObj = document.images[p_name]
	//alert(tmpObj);
	switch (p_mode) {
		case "ovr":
			tmpObj.src=getImageSrc(tmpObj.src,"OVR")
			break;
		case "out":
			tmpObj.src=getImageSrc(tmpObj.src,"OFF")
			break;
		default:
			alert("didn't get that:" + p_name)
	}
}


// -->

// JavaScript Document

//a global variable for all database queries.

// first lets make a slide show viewer for the front page...

var autoTO; // placeholder for TimeOut

function autoShow () {
	// clear out the to is if still exists
	clearTimeout(autoTO);
	if(slideShow.auto_pace > 0) {
		autoTO = setTimeout('slideShow.showNextSlide()', slideShow.auto_pace * 1000 );
	}
}


						   
var slideShow = {
	
	data:null
	,
	
	curSlide:0
	,
	
	rootURL:"/img/slides/"
	,
	
	screen_img_top:null
	,
	screen_img_bottom:null
	,
	screen_caption_top:null
	,
	screen_caption_bottom:null
	,
	
	gallery_scroll: $("#gallery_scroll")
	,
	
	gallery_thumbs: $("#gallery_thumbs")
	,
	
	gallery_tab: $("#gallery_tab")
	,
	
	topToggle:true // begins in a visible state.
	,
	
	toggleTop: function( p_Bool_state){
		this.topToggle = p_Bool_state;
	}
	,
	
	auto_pace : -1   // -1 means NO AUTO SHOW... otherwise numbers = seconds per slide
	,
	
	loadSlide: function(){
		this.hideGallery();
		// sniff for video...
		this.killVideo();
		if(this.topToggle){  // the top is covering so load the bottom...
			this.screen_img_bottom.attr("src",this.rootURL + this.data[this.curSlide].src);		
			this.screen_caption_bottom.html(this.data[this.curSlide].caption);
			this.screen_img_bottom.load(function() {
				slideShow.newSlide();
				autoShow();
			});
		}else {
			this.screen_img_top.attr("src",this.rootURL + this.data[this.curSlide].src);
			this.screen_caption_top.html(this.data[this.curSlide].caption);
			this.screen_img_top.load(function() {
				slideShow.newSlide();
				autoShow();
			});
		}
		
	}
	,

	newSlide: function(){
		// if the top is fading out to reveal, then fade out.
		if(this.topToggle) {
			this.screen_caption_top.fadeOut(600);
			this.screen_caption_bottom.fadeIn(600);
			this.screen_img_top.fadeOut(600, function(){
				slideShow.toggleTop( false );
				
			});
		}else{
			//this.screen_caption_top.html(this.data[this.curSlide].caption);
			this.screen_caption_top.fadeIn(600);
			this.screen_caption_bottom.fadeOut(600);
			this.screen_img_top.fadeIn(600, function(){
				slideShow.toggleTop( true );
				
			});
		}
	}
	
	, 
	
	showSlide: function(p_id) {
		var tmpIndex = this.getIndexByID( p_id);
		this.curSlide = tmpIndex;
		this.loadSlide();

	}
	,
	
	stopAutoShow: function(){
		clearTimeout(autoTO);
		this.auto_pace = -1;
	}
	,
	
	navSlideShow:function (p_dir ){
		this.stopAutoShow();
		if(p_dir == 0){
			this.gallery_tab = $("#gallery_tab");
			this.hideGallery();
			this.loadSlide();
			return;
		}
		if(p_dir < 0){
			this.showPrevSlide();
		}else{
			this.showNextSlide();
		}
	}
	,
	showNextSlide: function () {
		this.curSlide = ((this.curSlide + 1) < this.data.length) ? this.curSlide + 1 : 0;
		this.loadSlide();
	}
	,
	showPrevSlide: function () {
		this.curSlide = (this.curSlide <= 0) ? this.data.length - 1 : this.curSlide - 1;
		this.loadSlide();
	}
	,
	getIndexByID : function( p_id ){
		for(var nn=0; nn < this.data.length; nn++){
			if(this.data[nn].src == p_id){
				return nn;
			}
		}
		return nn;
	}
	,
	
	showGallery : function (){
		//this.thumbs_list.fadeOut(600);
		//this.thumbs_list.addClass("thumbs_open");
		this.killVideo();
		this.gallery_scroll = $("#gallery_scroll");
		this.gallery_thumbs = $("#gallery_thumbs");
		this.gallery_scroll.css("left","0px");
		this.gallery_scroll.css("z-index","130");
		this.gallery_scroll.css("width","790px");
		// postion in the vertical center...
		var tmpVCenter = 150  - (Math.ceil(Math.min(this.data.length, 50) / 10) * 28);
		var tmpHCenter = (750 * .5)  - (Math.min(this.data.length, 10) * (35));
		this.gallery_thumbs.css("top",tmpVCenter + "px");
		this.gallery_thumbs.css("left",tmpHCenter + "px");
		this.stopAutoShow();
		this.gallery_tab.css("height","24px");
		this.gallery_tab.html('<a id="gallery_btn" onclick="slideShow.hideGallery();">Close Gallery</a>');
		this.thumb_alpha = 0.7;
		 $("#gallery_thumbs img").fadeTo("fast", this.thumb_alpha);
		//this.thumbs_list.fadeIn(600, function(){
			//alert("showing gallery");
		//});
	}
	,
	
	hideGallery : function (){
		//this.thumbs_list.fadeOut(600);
		this.gallery_scroll.css("left","720px");
		this.gallery_scroll.css("width","70px");
		this.gallery_thumbs.css("top","0px");
		this.gallery_thumbs.css("left","0px");
		this.gallery_tab.css("height","42px");
		this.gallery_tab.html('<a id="gallery_btn" onclick="slideShow.showGallery();">Full<br/>Gallery</a>');
		this.thumb_alpha = 0.33;
		 $("#gallery_thumbs img").fadeTo("fast", this.thumb_alpha);
		//this.thumbs_list.fadeIn(600, function(){
		//alert("hiding gallery");
		//});
	}
	,
	
	thumb_alpha:0.33
	,
	
	showVideo : function() {
		$("#video_wrapper").css("visibility","visible");
		//LMRVideoLoader.videoURL = this.data[this.curSlide].originalURL ;
		//alert(LMRVideoLoader.ping);
		LMRVideoLoader.loadVideoInit(this.data[this.curSlide].originalURL);
	}
	,
	
	killVideo : function() {
		LMRVideoLoader.killVideo();
		$("#video_wrapper").css("visibility","hidden");
		
	}
	,
	showMedia : function(){
		
		if(this.data[this.curSlide].originalURL == ""){
			return false;
		}
		this.stopAutoShow();
		var tmpMedia = this.data[this.curSlide].originalURL.substr(this.data[this.curSlide].originalURL.length - 3, 3);
		//alert("media=" + this.data[this.curSlide].originalURL + ":" + tmpMedia);
		switch (tmpMedia ) {
			case "flv":
				this.showVideo();
				break;
		}
	}
			
	
}


var LMRVideoLoader = {
	ping : "here!"
	,
	videoURL:null
	,
	videoID:null
	,
	LMRplayer : null
	,
	checkTO:null
	,
	rechecker:0
	,
	
	loadVideoInit : function(p_url){
		this.videoURL = p_url;
		//alert(this.videoURL);
		this.loadVideo();
	}
	,
	killVideo : function() {
		if(this.LMRplayer != null){
			this.LMRplayer.sendEvent('STOP');
		}
	}
	,
	loadVideo : function (){
		if(this.LMRplayer != null){
			this.LMRplayer.sendEvent('LOAD', this.videoURL);
			this.rechecker = 0;
			clearTimeout(this.checkTO);
		} else {
			//alert("attempting to load " + this.videoURL);
			this.checkTO = setTimeout('LMRVideoLoader.loadVideo', 500);
			this.rechecker ++;
			if(this.rechecker > 4){
				alert("Problem with Flash!");
				this.rechecker = 0;
				clearTimeout(this.checkTO);
			}
		}
	}
	,
	createPlayer: function() {
    	var flashvars = {
         	file:"", 
			autostart:"true",
			skin:"/swf/stylish_slim.swf"
		}
		var params = {
			allowfullscreen:"true", 
			allowscriptaccess:"always"
		}
		var attributes = {
			id:"video_player",  
			name:"video_player"
		}
		//alert(typeof(swfobject));
		if(typeof(swfobject) != 'undefined'){
    		swfobject.embedSWF("/swf/player.swf", "video_screen", "720", "300", "9.0.115", false, flashvars, params, attributes);
		}

	}
}

$(document).ready(function(){
	sideBarObj.dimmer = $("#sidebar #dimmer");
	sideBarObj.sidebar = $("#sidebar");
	sideBarObj.init();
	LMRVideoLoader.createPlayer();
	
	
	
});


var sideBarObj = {
	dimmed:false
	,
	sidebar:null
	,
	
	dimmer: null
	,
	
	init : function (){
		this.sidebar.bind('mouseout',function(e){ 
				var offset = sideBarObj.sidebar.offset();
				var tmpXReturn = false;  // assume we dont leave... true = candidate for leaving
				
				if ( e.pageX < (166 + offset.left) && (e.pageX > offset.left) )  {
					tmpXReturn = true;
					
				}
				var tmpYReturn = false;
				
				if ( (e.pageY >  offset.top ) && ( e.pageY < (sideBarObj.dimmer.height() + offset.top) ) ){
					//alert("!");
					tmpYReturn = true;
				};
					
				if( tmpXReturn && tmpYReturn) {
					return;
				}
				//alert('x=' + e.pageX + ":" + offset.left);
				sideBarObj.dimmer.css("display","block");
      			sideBarObj.dimmer.fadeTo("fast", .5); 
    		}); 
	
	 	this.dimmer.mouseover(function () { 
      			$(this).fadeTo("fast", 0, function(){
						$(this).css("display","none");
				 	});
		});
		this.dimmer.height($("#wrapper").height() - this.dimmer.offset().top - 12);
	 	this.dimmer.fadeTo("fast", .5);
	}
}

function playerReady(obj) {
	if(LMRVideoLoader.LMRplayer == null) {
		LMRVideoLoader.LMRplayer = document.getElementById(obj['id']);
	}
};


/*
/* add this to any page you want a slide show... 
/* set the screenIMG to the image to change
/* set the screenCaption to the text div to show thecaption
/* set the data in the format below and assign the index reference to your buttons
/*
/*
$(document).ready(function(){
	// set the slideShow
	slideShow.screenIMG = $("#slideIMG");
	slideShow.screenCaption = $("#slideCaption");
	slideShow.data = [ {src:"slide_01.jpg",caption:"Playing games together increases the learning opportunity.", uiBtn: $("#uiBtn_01")},
		   {src:"slide_02.jpg",caption:"Make decisions around buying cars and using credit cards.", uiBtn: $("#uiBtn_02")},
		   {src:"slide_03.jpg",caption:"The choice is yours to start a business or go to school.", uiBtn: $("#uiBtn_03")},
		   {src:"slide_04.jpg",caption:"\"Money is a life skill - and as parents, grandparents, interested adults - it's up to us to make sure our children are prepared for the financial world they are going to face.\" <br />Sharon Lechter<br /> - Author<br /> - Founder of Pay Your Family First, LLC<br /> - Member of the President's Advisory Council on Financial Literacy", uiBtn: $("#uiBtn_04")}
		   ];
});

*/