$(document).ready( function() {
  
  $("a.colorbox").colorbox({height:"520px", innerWidth:"600px", iframe:true});  // sets popup boxes
  $("a.welcome-colorbox").colorbox({height:"426px", innerWidth:"426px", iframe:true}); 

//FLYOUT FUNCTIONS
  $(".flyout").each(function(e){
		var $this = $(this);
		$this.append("<img class='fl-arrow' src='/files/images/site-resources/flyout-arrow.gif' width='17' height='33' />");
  });
  
   var timer = setTimeout(hideActiveFlyout,0);
   
   $("#pc-mm-box").find("li").bind('mouseenter touchstart',function(e){
	   clearTimeout(timer);
	   var $this = $(this),
		   id = $this.attr("id"),
		   idstripped = id.replace("mm-","");
		   pos = $this.position(),
		   eurl = $this.find("a").attr("href"),
		   flyout = $(".flyout[id=flyout-"+idstripped+"]"); 
   	   if(e.type === 'touchstart'){
			$this.unbind('mouseenter');
		}
	   $(".flyout").each(function(e){
			$(this).hide();
			$(this).unbind('mouseleave touchend');
	   });
	   if ( window.isIndex) {
		   flyout.find(".fl-arrow").css({top:pos.top + 180});
	   } else {
	       flyout.find(".fl-arrow").css({top:pos.top+1});
	   }
	   //flyout.find(".seeall").attr("href",eurl);
	   flyout.css({display:'block'}).show();  
	   flyout.bind({
		   mouseover:function(){ 
			  clearTimeout(timer);
		   },
		   touchstart:function(){
			  clearTimeout(timer);
		   }, 
		   mouseleave: function(){
			  timer = setTimeout(hideActiveFlyout,500);
		   }
	   }); 
	   
  }).bind('mouseleave touchend',function(e){
	  if(e.type !== 'touchend'){ 
	  	timer = setTimeout(hideActiveFlyout,500);
	  }
  });
  function hideActiveFlyout(){
    $(".flyout:visible").fadeOut();
	clearTimeout(timer);
  } 


//SEARCH FUNCTIONS

  $("#pc-search-text").hover(function() {                                       // searchbox mouseover handler  
    if ($("#pc-search-text").val() == "Search . . .") {
      $("#pc-search-text").val("");
    }
    $("#pc-search-text").addClass("hovered");
  }, 
  function() {                                                                  // searchbox mouseout handler  
    if ($("#pc-search-text").val() == "" && $("#pc-search-text").hasClass("focused") != true) {
      $("#pc-search-text").val("Search . . .");
    }
    $("#pc-search-text").removeClass("hovered");

  });

  $("#pc-search-text").click(function() {                                       // searchbox click handler  
    if ($("#pc-search-text").val() == "Search . . .") {
      $("#pc-search-text").val("");
    }
    $("#pc-search-text").addClass("focused");
  });

  $("#pc-search-text").blur(function() {                                        // searchbox blur handler
    if ($("#pc-search-text").val() == "") {
      $("#pc-search-text").val("Search . . .")
    }
    $("#pc-search-text").removeClass("focused");
  });


  $("#pc-search-submit").click(function() {                                     // searchbox submit
    $("#pc-search-form").submit();
	$("#pc-search").submit();
    return false;
  });


  
});
