// JavaScript Document


/* Begin contact modal popup */
(function( $ )
{
	///////// begin SEARCH
		var oops = 'Oops! You forgot a query.';
		
		
	$().ready( function ()
	{
		
		
	  if( window.location.href.match( /\/search\/query\// ))
	  {
		var query = unescape( window.location.href.split( '/' ).pop() );
		$( '#search_box_wrap input' ).val( query );
	  }
	  var og_search_value = $( '#search_box_wrap input' ).val();
	  
	  $( '#search_box_wrap input' ).click( function( ev )
	  {
		ev.stopPropagation();
		//alert( 'oooooooohhhhhhhh Yeah' );
	  });
	  
	 
	  $( '#search_box_btn' ).click( function()
	  {
		  
	
		 sendToSearch();
	  });
	  $( '#search_box_wrap' ).click( function()
	  {
		 sendToSearch();
	  });
	  $( '#search_box_wrap input' ).focus( function( ev )
	  {
		//if( $.trim( this.value ) == og_search_value  )
		//{
		  //this.value = '';
		//}
		
		
		if( og_search_value =='I am looking for...' )
		{
		  this.value = '';
		}
		
		
		$( this ).keyup( function( ev )
		{
		 if( ev.keyCode == 13 )
		 {
		   sendToSearch();
		 }
		});
	  });
	  $( '#search_box_wrap input' ).blur( function( ev )
	  {
		$( this ).unbind( 'keyup' );
		if( ! $.trim( this.value ))
		{
		  this.value = og_search_value;
		}
	  });
	}); // end dom ready
	var sendToSearch = function ()
	{
	  //alert( encodeURI( $( '#search_box_wrap input' ).val() ));
	  var search_term =  $( '#search_box_wrap input' ).val();
	  //  var search_term = encodeURI( $( '#search_box_wrap input' ).val() );

	   if( search_term=='' || search_term=='I am looking for...' ||  search_term==oops){
			 $( '#search_box_wrap input' ).val( oops );
			 return false;
		}
		
		
	  //alert( search_term );
	  window.location.href = site_root + '/search/' + search_term;
	}
	////////////// end SEARCH
	






$(document).ready( function (){	
							 
		

				
	$('body').click( function(){
						//  alert('ve');
		//e.preventDefault();
		removeBubble();		
	});
				 
		
		


	/* PNG FIX */
	//NEED RELIABLE PNG FIX FOR IE 6 OR NEED TO SWITCH OUT BG IMAGES ON BUBBLE
 	//THIS PNG FIX DOES NOT SUPPORT BG-POSITION
	$(document).pngFix(); 
	


	/* BEGIN ADD POP UP BUBBLES */
	var T=0;
	var R=0;
	var B=0;
	var L=0;
	var link_width=0;	
	var link_offset = 4 // space from top of the a tag to the actual a text
	var link_padding = 10 // padding to the right of the trigger link to keep the bubble showing
	var left_margin= 0;
	var top_margin = 0;
	var curr_id='null';
	var bubble_content='';		

	var bubble_html='<div id="info_pop"> \
      					<div id="info_pop_arrow"><!-- --></div> \
      					<div id="info_pop_top"> \
      						<div id="info_content"></div> \
     					</div> \
     					<div id="info_pop_btm"><!-- --></div> \
     				</div>'; 
     		
	
	function removeBubble(){
		curr_id='null';
		$('#info_content').html('');
		$('#info_pop').remove();
		$(document).unbind('mousemove');
	}
	
	function trackMouse(){
		$(document).bind('mousemove', function(e){
			//console.log('mouse: ' + e.pageX +', '+ e.pageY);
			if(e.pageX < L || e.pageX > R){
				removeBubble();
			}
			if(e.pageY < T || e.pageY > B){
				removeBubble();
			}
			if(e.pageX > (L + left_margin) + link_width && e.pageY < T + top_margin){
				removeBubble();
			}
			if(e.pageX < L + left_margin && e.pageY < T + top_margin){
				removeBubble();
			}
		});
	}
	if($('.info_bubbles').length){ //if the element exists
	
 	$('.info_bubbles a').each(function(i){
 		$(this).attr('id','bubble_trigger' + i);
 			if($(this).parent().find('.info_holder').length){ // if there is bubble content
 				$(this).mouseover(function(){
 					if (curr_id != $(this).attr('id') || curr_id == 'null'){
 						curr_id = $(this).attr('id');
 						$(this).parent().prepend(bubble_html);
 						bubble_content = $(this).parent().find('.info_holder').html();
 						$('#info_content').append(bubble_content);
 						
 						//NEED RELIABLE PNG FIX FOR IE 6 OR NEED TO SWITCH OUT BG IMAGES ON BUBBLE
 						//THIS PNG FIX DOES NOT SUPPORT BG-POSITION
 						$('#info_pop').pngFix(); //thanks so much ie
 						//$('#info_pop').fadeIn('fast');
 						$('#info_pop').show();
 						
 						
 						//get the link width
 						link_width=($(this).width())+link_padding;	
 						// get the coordinates of the bubble
						var bubble = $('#info_pop');
						var bubble_position = bubble.position();
						bubble_height=bubble.height(); bubble_width=bubble.width();
						left_margin = Math.abs($('#info_pop').css('margin-left').replace("px", "")); //get the left offset margin
						top_margin = Math.abs($('#info_pop').css('margin-top').replace("px", "")); //get the top offset margin
						//T = bubble_position.top - link_offset; //leave some room for the link we're on
						T = bubble_position.top + link_offset;
						L = (bubble_position.left) - left_margin;
						R = L + bubble_width;
						//B = T + bubble_height + (link_offset-10);
						B = T + bubble_height + top_margin;
						//console.log('T:' + T +'R:' + R + 'B:' + B + 'L:' + L);
			
						$(this).mouseout(function(){
 		 					trackMouse()// start tracking the mouse to see if we are out of bounds 			
						})
					} //end if curr_id
				}) //end mouseover
			} //end if there is bubble content	
 		}); // end each funtion
 	}
	/* END ADD POP UP BUBBLES */
	

	/* begin EAP/CEO EXPAND/COLLAPSE */
	if($('.exp_col_container').length){//if there are expandable / collapsible containers
		$('.exp_col_container').hide(); 
		$('.exp_col_trigger').each(function() {
			$(this).click(function(ev){
			ev.preventDefault();
				if ($(this).hasClass('collapsed')){
					$(this).removeClass('collapsed').addClass('expanded');
					$(this).parent().find('.exp_col_container').slideDown('fast')
						}else{
					$(this).parent().find('.exp_col_container').slideUp('fast');
					$(this).removeClass('expanded').addClass('collapsed');
				}
			})
		});
	}
	/* END EXPAND/COLLAPSE */



	/* Begin Attach click events for change text size */
	
	$('.styleswitch').click(function() {
         switchStylestyle(this.getAttribute("rel"));
         return false;
      });
      var c = readCookie('style');
      if (c) switchStylestyle(c);
  

   function switchStylestyle(styleName)
   {
      $('link[@rel*=style][title]').each(function(i) 
      {
         this.disabled = true;
         if (this.getAttribute('title') == styleName) this.disabled = false;
      });
      createCookie('style', styleName, 365);
   }


})	
/* END document.ready */


/* cookie functions */
function createCookie(name,value,days){
 if (days){
	var date = new Date();
	date.setTime(date.getTime()+(days*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	}else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name){
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++){
	 var c = ca[i];
	while (c.charAt(0)==' ') c = c.substring(1,c.length);
	if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
 return null;
}

function eraseCookie(name){
	 createCookie(name,"",-1);
}

// /cookie functions







/* BEGIN SF DROPDOWNS */
var sf; // this will be a reference to the sucker fish functions - object literal
sfHover = function() {
	if(document.getElementById("nav")){ 
		var sfEls = document.getElementById("nav").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);
/* END SF DROPDOWNS */

/* begin EAP/CEO EXPAND/COLLAPSE 
if($('.exp_col_container').length){//if there are expandable / collapsible containers
alert();
	$('.exp_col_container').hide(); 
	$('.exp_col_trigger').each(function() {
										
		$(this).click(function(ev){
		ev.preventDefault();
			if ($(this).hasClass('collapsed')){
				$(this).removeClass('collapsed').addClass('expanded');
				$(this).parent().find('.exp_col_container').slideDown('fast')
					}else{
				$(this).parent().find('.exp_col_container').slideUp('fast');
				$(this).removeClass('expanded').addClass('collapsed');
			}
		})
	});
}*/
/* END EXPAND/COLLAPSE */




})( jQuery )






