/**
 * RCBI -- Main Javascript file
 * Prepared by CDG Interactive
 */
var lastIdx=1;
var popUpTimer;

$(document).ready(function(){
						   
	$('#logoutLnk').click(function(){
	  //var result = $.ajax({url: '/user-applications/21stpublic/actions/_logout.cfm?UID='+getUID(), async: false}).responseText;	
	  var result = $.ajax({url: '/user-applications/21stpublic/actions/_logout.cfm?UID='+getUID(), async: false}).responseText;	
	  window.location=location.href+'&UID='+getUID();								   
    });	 

	$('.printlink').click(function(){print_page();});


// Horizontal Scroller
	var sliderWidth = $(".slider").css("width");
	$('#scroll-right').click(function(){
		var leftMargin = $(".slider").css("margin-left");
		pushOver = parseInt(leftMargin) - 110 + "px";
		var myNumb = 0 - parseInt(leftMargin);
		if (myNumb < parseInt(sliderWidth) - 550){
			$(".slider").animate({
				marginLeft: pushOver
			});
		}
	});
	$('#scroll-left').click(function(){
		var leftMargin = $(".slider").css("margin-left");
		pushOver = parseInt(leftMargin) + 110 + "px";
		var myNumb = 0 - parseInt(leftMargin);
		if (myNumb > 0){
			$(".slider").animate({
				marginLeft: pushOver
			});
		}
	});

// Hover Images
	$(".sub-sites img, li.pulldown, div.primary>ul>li img, div.utility>ul>li:not(.pull-down) a img, div.utility>ul>li#search-down span img").hover(	

		function() {
			offImg3 = $(this).attr('src');					
			overImg3 = offImg3.replace('off/','on/');														
			$(this).attr('src', overImg3);	
		},		
		function() {		
			$(this).attr('src', offImg3);
		}		
	);
	
	$("div.slider img").hover(	
		function() {
			offImg2 = $(this).attr('src');					
			overImg2 = offImg2.replace('off/', 'on/');									
			$(this).attr('src', overImg2);	
			//alert(offImg + "   " + overImg);
		
		},
		function() {		
			$(this).attr('src', offImg2);
			
		}
	);
	
	//$("div.slider img").click(function(){
	//								   alert("test");
	//	$('div.featured>div.screen, div.featuredInfo>div.screen').hide();
	//	$('div.featured>div#'+$(this).attr("id")+', div.featuredInfo>div#'+$(this).attr("id")).show();
    //});

// Show Callouts

	$('div#interior div.col-span div.featured ul li a.pointer').hover(function(){
		clearTimeout(popUpTimer);	
		closeBlurb();
		$(this).css('background-image','url(frame-images/rcbi_com/buttons/on/callout.gif)');
		$(this).siblings('div.callouts').css('display','block');				
	},
	function(){		
		popUpTimer = setTimeout("closeBlurb()",1000);		
		}
	);
	
	$('div.callouts').hover(function(){
		clearTimeout(popUpTimer);		
	},
	function(){		
		popUpTimer = setTimeout("closeBlurb()",1000);		
		}
	);
	
	
	
	

// Show Pull Down Menu

	$("li.pull-down").hover(function(){
		offImg = $(this).children("a").children("img").attr('src');					
		overImg = offImg.replace('off/','on/');	
		$(this).children("a").css('background', '#ffffff');
		$(this).children("a").children("img").attr("src", overImg);
		$(this).children("div").css('display', 'block');
		},
	function(){
		$(this).children("a").css('background', 'transparent none');
		$(this).children("a").children("img").attr("src", offImg);
		$(this).children("div").css('display', 'none');
		}
	);

    $("li#search-down>span").click(function(){ 
		$("li#search-down>div.frame").fadeIn(500);
    });
	$("li#search-down div.frame span").click(function(){
		$("li#search-down>div.frame").fadeOut(500);
	});
	

//Contact Us Functions

	$("li#form form fieldset").hide();
	$("li#form form input.submit").click(function(){
	  if ($("li#form form fieldset").css("display") == "none")
	  {
		$("li#form form fieldset").show("fast");
		return false;
	  }
	  else { return validateContact(); }
	});	
	if($("li#form div#contactThanks").is('#contactThanks')) {
		var t = this.title || this.name || null;
		var currentUrl = document.URL + "#TB_inline?height=200&width=500&inlineId=contactThanks";
		TB_show(t,currentUrl,false);
	};
	
	if($("div#regThanksBox").is('#regThanksBox')) {
		var t = this.title || this.name || null;
		var currentUrl = document.URL + "#TB_inline?height=200&width=500&inlineId=regThanksBox";
		TB_show(t,currentUrl,false);
	};
	
});



function validateContact()
{
	if (formHasErrors()) clearErrors('contactErrorBox','contactError');

	if (isBlank(document.getElementById('contactName')) || document.getElementById('contactName').value == 'Name') {
		setError('contactName','Name is a required field');
	}
	if (isBlank(document.getElementById('contactEmail'))) {
		setError('contactEmail','Email is a required field')
	}
	else if (!isValidEmail(document.getElementById('contactEmail'))) {
		setError('contactEmail','Email must be in the correct format, ie <i>name@company.com</i>');
	}
	if (isBlank(document.getElementById('contactMessage')) || document.getElementById('contactMessage').value == 'Message') {
		setError('contactMessage','Message is a required field');
	}
	
	if (formHasErrors()) { displayErrorMessages('contactErrorBox','contactError'); return false; }
	return true;
}


function validateReg()
{	
 var hasRegErrors=false;
 document.getElementById('regFirstName').style.border='1px solid #999999';
 document.getElementById('regLastName').style.border='1px solid #999999';
 document.getElementById('regEmail').style.border='1px solid #999999';
 
 if(!document.getElementById('regFirstName').value.length == 0) {
  document.getElementById('regFirstName').style.border='1px solid #b9181f';  
  hasRegErrors=true;
 }
 if(!document.getElementById('regLastName').value.length == 0) {
  document.getElementById('regLastName').style.border='1px solid #b9181f';  
  hasRegErrors=true;
 }

 if(!document.getElementById('regEmail').value.length == 0 ) {
  document.getElementById('regEmail').style.border='1px solid #b9181f';  
  hasRegErrors=true;
 }
 
 if(hasRegErrors) {
  return false; 
 }
 else {	
  return true; 
 }	

}

function getKeyCode(e) {
 if (window.event) return window.event.keyCode;
  else if (e) return e.which;
   else return null;
}

function keyRestrict(e, validchars) {
 var key='', keychar='';
 key = getKeyCode(e);
 if (key == null) return true;
 keychar = String.fromCharCode(key);
 keychar = keychar.toLowerCase();
 validchars = validchars.toLowerCase();
 if (validchars.indexOf(keychar) != -1) return true;
 if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 ) return true;
 return false;
}

function loadFlashVideo(flashFile,imgFile) {
 $('#screen1').flash({ 

                        src: '../user-assets/Images/homepage/vidloader.swf',

                        width: 483,

                        height: 262,
						
						flashvars: {vidnum:flashFile,picnum:imgFile},
						
						wmode: 'transparent'
                        },

                        {version: 8.0}
						
						
            );				
}

function getVideo(id,idx) { 
  if(lastIdx) {
	document.getElementById('videoControl'+lastIdx).src=document.getElementById('videoControl'+lastIdx).src.replace('on/', 'off/');  
  }  
  document.getElementById('videoControl'+idx).src=document.getElementById('videoControl'+idx).src.replace('off/', 'on/');  
  lastIdx = idx;
  
   AjaxRequest.get(
  {
   'url':'/includes/_getVideo.cfm?pageID='+id   
   ,'onSuccess':function(req){ loadAsset(req.responseText); }
   ,'onError':function(req){ }  
  }
 ); 
 
}

function loadAsset(asset) {	
	var assetObj = asset.split("^");		
	document.getElementById('screen1').innerHTML='';
	document.getElementById('screenInfo').innerHTML='';
	var imageFile = '';
 if(assetObj[0].indexOf(".flv") != -1) {
  imageFile=assetObj[0].replace('.flv','.jpg');	 
  loadFlashVideo(assetObj[0],imageFile);
 } 
 else {
	
	 var oImg = document.createElement("img");
	 oImg.src = assetObj[0];
	
	 
	 document.getElementById('screen1').appendChild(oImg);
	 } 	
	 document.getElementById('screenInfo').innerHTML=assetObj[1];
	
}


function displayErrorsMsgs() {	
 $('#formErrors').show();
 window.scroll(0,0); 
}

function addError(o,msg) {	
 if(msg != "") { $('#formErrors ul').append('<li>'+msg+'</li>'); }
 $('#'+o+'Container').addClass('errorContainer');
}

function clearErrors() {
 $(".errorContainer").removeClass('errorContainer');
 $('#formErrors ul').html(''); 
 $('#formErrors').hide();	 
}

function addOverlay() { 
 var de = document.documentElement;
 var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
 var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;		
 $("body").append("<div id='overlayContainer'></div>");	
 h = h + getVerticleScrollOffset();
 $("#overlayContainer").css('width', w);
 $("#overlayContainer").css('height', h); 
 $('select').css("visibility","hidden");
}

function removeOverlay() { 
 $('#overlayContainer').remove();
 $('select').css("visibility","visible");
}

function displayStatusWindow(title,msg) {   
 removeStatusWindow();
 addOverlay();
 window.scroll(0,0);
 var statusWindow = getStatusWindow(title,msg);  
 document.getElementsByTagName('body').item(0).appendChild(statusWindow); 
}

function removeStatusWindow() {	
 if(document.getElementById('statusWindow')) {
  document.getElementsByTagName('body').item(0).removeChild(document.getElementById('statusWindow'),true);	  
 }
 removeOverlay(); 
}

function updateStatusMsg(title,msg) {
 if(document.getElementById('statusWindow')) { 
  document.getElementById('statusTitle').innerHTML = title;
  document.getElementById('statusMsg').innerHTML = msg;
 }	
}

function getStatusWindow(title,msg) {	
 var de = document.documentElement;
 var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
 var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;	
 
 var oDivContainer = document.createElement("div"); 
 var oDivTitle = document.createElement("h2");
 var oDivIcon = document.createElement("div");
 var oDivMessage = document.createElement("div"); 
 var oImg = document.createElement("img"); 
 var oButton = document.createElement("input");
 oButton.setAttribute("type","button");
 oButton.setAttribute("value","Ok");
 oButton.setAttribute("id","statusBtn");
 oButton.onclick=resetScreen; 
 oDivContainer.setAttribute("id","statusWindow");
 oDivContainer.style.top = getVerticalCenterScreen(200,h)+'px';
 oDivContainer.style.left = getHorizontalCenterScreen(400,w)+'px';
 oDivTitle.setAttribute("id","statusTitle");
 oDivTitle.innerHTML=title; 
 oDivMessage.setAttribute("id","statusMsg");
 oDivMessage.innerHTML = msg;
 oImg.setAttribute('src','/frame-images/sub_21st/icons/progress_icon.gif'); 
 oDivIcon.appendChild(oImg);
 oDivContainer.appendChild(oDivTitle);
 oDivContainer.appendChild(oDivIcon);
 oDivContainer.appendChild(oDivMessage); 
 oDivContainer.appendChild(oButton); 
 return oDivContainer;
}

function resetScreen() {
 //removeStatusWindow();
 if(document.referrer) {
  window.location=document.referrer;
 }
 else {
  window.location=location.href; 
 } 
}

function getVerticalCenterScreen(heightOffset,winHeight) {
 var x = heightOffset/2;
 var y = winHeight/2;  
 var z = getVerticleScrollOffset();
 return Math.floor(y-x+z);
}
 
function getHorizontalCenterScreen(widthOffset,winWidth) {
 var x = widthOffset/2;
 var y = winWidth/2;  
 return Math.floor(y-x);	 
} 

function getVerticleScrollOffset() {
 if(window.pageYOffset) {
  return window.pageYOffset;
 }
 else {
  return document.documentElement.scrollTop; 
 }  
}

function getUID() { 	
 var date = new Date(); 
 var UID = ''+date.getSeconds()+date.getMinutes()+date.getHours()+date.getDate()+date.getMonth()+date.getFullYear()+(Math.floor(Math.random()*10000)+1000);
 return UID;
}

function closeBlurb() {
 $('div#interior div.col-span div.featured ul li a.pointer').css('background-image','url(frame-images/rcbi_com/buttons/on/callout.gif)');
 $('div.callouts').css('display','none'); 
}

function openFlashPlayer(flashFile,imgFile) { 
 flashvid_win = window.open("/flashplayer.cfm?flashFile="+flashFile+"&imgFile="+imgFile,"Flashvideowin","toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,top="+((screen.availHeight/2)-300)+",left="+((screen.availWidth/2)-400)+",width=483,height=262");
 flashvid_win.focus();  
}

