$(init_page);
  
var slide_timeout = 9000;
var slide_speed = 1000;
var slide_number = 0;
var slide_timer = undefined;

function init_page(){	

	$("a.lightbox").lightBox();

	// setup_fieldset();
	init_toggle_items();
	if($('#slide').get(0)!=undefined){
    autoplay_slide();
  }

	init_job_postings();
		
	if($("#content").height() > $(".sidebar").height())
		fix_sidebar();
		
	if ($.browser.msie && $.browser.version.substr(0, 1) <= 7) {
		$("hr").replaceWith('<div class="hr"></div>');
	}
	
	if($.browser.msie && $.browser.version.substr(0, 1) <= 8 && $.browser.version.substr(0, 1) >= 7) {
		$("#content").height(($("#content").height() + 125) + "px");  
	}
	
  $('#form_search input[type="text"]').focus(function(){
    $(this).css('background-position','0 1000px');  
  }).blur(function(){
    if($(this).val()==''){
      $(this).css('background-position','2px 1px');  
    }
  });
	
	if($.browser.msie && $.browser.version.substr(0, 1) <= 6) {									
		$("#main").height(parseInt($("#main").height()) - 120 + "px");
		$('body[class!="home"] #curve').css('top', '-95px');
	}
	else if($.browser.msie && $.browser.version.substr(0, 1) >= 9) {			
		$('#curve').css('bottom', '-100px');
	}
	else if($.browser.msie && $.browser.version.substr(0, 1) >= 8) {			
	//	$('#curve').css('bottom', '-300px');
	}
     
  if(!$('body').hasClass('home')){
    
    $('#main_menu').menu_makeover();

    
    
	} else {
		$(this).find('#main_menu >li.page-item').css('z-index', '4000').hover(function () {
			
			$('#main_menu >li.page-item ul[menuopen="true"]').not(this).attr("menuopen", "false").show().animate({top:(top_start / 2) + 'px',opacity:0, 'height':'0px'},{duration:200, queue:false, complete:function() {	}});
			
			if($(this).has("ul")) {		
				
				$(this).find('ul').not('[oheight$="px"]').attr('oheight', $(this).find('ul').css('height').toLowerCase());
				$(this).find('ul').not('[otop$="px"]').attr('otop', ('-' + ($(this).height() + $(this).find('ul').height() + 36) + 'px'));
				
				var tmp_top = $(this).find("ul").attr("otop");	
				var top_start = tmp_top.replace('px', '');
				var tmp_height = $(this).find("ul").attr('oheight');
			
				$('#main_menu >li.page-item ul[menuopen="false"]').css("z-index", "2500");
				
				$(this).find("ul").not('[menuopen="true"]').attr("menuopen", "true").css({'z-index':'3000', opacity:0, top:'-' + 60 + 'px', 'height':'0px'}).show().animate({top:tmp_top,opacity:1,'height':tmp_height},{ duration:300, queue:false,easing:'cubicEaseOut' });
			}
		}, function() {
			
			$("#main_menu >li.page-item ul:visible").attr("menuopen", "true");
			$("#main_menu >li.page-item ul:hidden").attr("menuopen", "false");
			
			$('#main_menu >li.page-item ul[menuopen="false"]').css("z-index", "2500");
			
			var tmp_top = $(this).find("ul").attr("otop");	
			var top_start = tmp_top.replace('px', '');
						
			$('#main_menu >li.page-item ul[menuopen="true"]').attr("menuopen", "false").css({'z-index':'2500'}).hide().animate({top:(top_start / 2) + 'px',opacity:0, 'height':'0px'},{duration:200, queue:false, complete:function() {	}}); 
		});
	} 
	
	$("#emergency_popup").hover(function() {
		$(this).stop(false, false).animate({
			'width':'250px'
		}, 300);
	}, function() {
		$(this).stop(false, false).animate({
			'width':'35px'
		}, 300);		
	});

  if($('#emergency_popup').get(0)!=undefined){	
  	if($(window).width() > 1440)
  		$("#emergency_popup").css('left', ($("#slider_mask").position().left - 720) + 'px');
  	else
  		$("#emergency_popup").css('left', '0px');
  	
  	$(window).resize(function() {
  		if($(window).width() > 1440)
  			$("#emergency_popup").css('left', ($("#slider_mask").position().left - 720) + 'px');
  		else
  			$("#emergency_popup").css('left', '0px');
  	});
  }
  $('.fancybox').fancybox({titlePosition:'inside'});   
	
}

function fix_sidebar() {	
	$(".sidebar").height($("#content").height() + 50);
}

/* Toggle Items */
function init_toggle_items(){
 
  $('.toggle_body').each(function() {
		$(this).attr("oheight", $(this).height());
		
		$(this).hide();
	}); 
	
	//$('.toggle_body').hide();
	
  $('.toggle_arrow a').bind('click', function(e) {
    e.preventDefault();
    
    var o_next = $(this).parent().next();
    if(o_next.css('display')=='none'){
	    if($.browser.msie && $.browser.version.substr(0, 1) <= 6) {} else	
  			$("body").css('overflow', 'hidden');
    	$('.sidebar').height((parseInt($("#content").height()) + parseInt(o_next.attr('oheight'))) + 'px');
      o_next.slideDown(400, function() {
      	if($.browser.msie && $.browser.version.substr(0, 1) <= 6) {} else
					$("body").css('overflow', 'visible');
      }); 
      
      $('.toggle_arrow.selected a').trigger('click');
      $(this).parent().addClass('selected'); 
      
			
			if($(this).html() == "Can't find what you're looking for?") {		
				if($.browser.msie && $.browser.version.substr(0, 1) <= 8) {		
					$("#content").animate({
						'height':((parseInt($("#content").height()) + (parseInt(o_next.attr("oheight"))) + 25) + "px")
					}, {'duration':'500', 'queue':'false'});
					
					if($.browser.msie && $.browser.version.substr(0, 1) <= 6) {		
					/*	$("#curve").animate({
							'top':((parseInt(o_next.attr("oheight"))) - 250) + "px"
						}, {'duration':'500', 'queue':'false'});*/
						
						$("#main").animate({
							'height':((parseInt($("#main").height()) + (parseInt(o_next.attr("oheight"))) + 25) + "px")
						});		
					}	
				}	 				
				$(".sidebar").animate({
					'height':((parseInt($(".sidebar").height()) + (parseInt(o_next.attr("oheight"))) + 25) + "px")
				}, {'duration':'500', 'queue':'false'});
			}
    }else{
      o_next.slideUp(400, function() {
      	fix_sidebar();
      });  
			
			if($(this).html() == "Can't find what you're looking for?") {	
				if($.browser.msie && $.browser.version.substr(0, 1) <= 8) {	
					$("#content").animate({
						'height':((parseInt($("#content").height()) - (parseInt(o_next.attr("oheight"))) - 25) + "px")
					}, {'duration':'500', 'queue':'false'});  

					
					if($.browser.msie && $.browser.version.substr(0, 1) <= 6) {		
			/*			$("#curve").animate({
							'top':"0px"
						}, {'duration':'500', 'queue':'false'});	*/	
						
						$("#main").animate({
							'height':((parseInt($("#main").height()) - (parseInt(o_next.attr("oheight"))) - 25) + "px")
						});		
					}			
				}					
				
				$(".sidebar").animate({
					'height':((parseInt($(".sidebar").height()) - (parseInt(o_next.attr("oheight"))) - 25) + "px")
				}, {'duration':'500', 'queue':'false'});
			}	
			
      $(this).parent().removeClass('selected'); 
    }
    return false;  
  });  
  
  var anchor = window.location.hash;

 // if(anchor !=''){
    $('.toggle_arrow.selected').each(function() {
    	var tmp_obj = $(this);
    	setTimeout(function() { tmp_obj.find("a:first").trigger('click') }, 200);
    	$(this).removeClass('selected');
    });
    
    $('a').each(function(i){
      if($(this).attr('href')== anchor){
        $(this).trigger('click');
      }
    });
//  }else{
//  	$('.toggle_arrow.selected').next().show();
 // }
  //$('.toggle_arrow.selected').removeClass('selected');
  //$('.toggle_arrow.selected a').trigger('click');
  
}

function init_job_postings() {
	$("a.job_posting").click(function(event) {
		event.preventDefault();

		$.fancybox({
			href: this.href + '&layout=blank',
      autoDimensions:false,
      autoScale:false,
      width:600,
      height:616,
      scrolling:'no',
			onComplete: function() {
				$('#submit_resume_form').ajaxForm({
					url:'/view_job_posting',
					type:'POST',
					dataType:'json',
					data: {'return':'json'},
					iframe:true,
					success:submit_resume_callback
				});
				showRecaptcha($("#recaptcha_div").get(0));
				$("#fancybox-inner").css({ 'overflow': 'hidden' });
			}
		});
		
		
	});
}
function showRecaptcha(element) { 
      Recaptcha.create("6LfRR8YSAAAAANm95iQOHIrQb76fI-y-fnBrbaNz" , element, { 
      theme: "red" 
      }); 
} 
function submit_resume() {
	$('.error_field').removeClass('error_field'); 
	$('#submit_resume_form').submit();
}
function submit_resume_callback(data) {
	if(typeof(data.error)=='undefined'){
    // success
    $.fancybox.close();
    setTimeout(function(){
      alert("Thank you for your submission");
    },1000);
  
  }else{
    var n =0;
    for(i in data.error){
      $('#entry_' + i).addClass('error_field');
      if(n==0){
        $('#entry_' + i).focus();  
      }
      n++;
    }
      alert("Error with your submission, Please make correction on red fields");    

    //alert(data.message);  
    
  }
}

function setup_fieldset() {
	$("#content fieldset.collapse").each(function(index) {
		$(this).html('<div class="fieldset_content">' + $(this).html());
		
		$(this).find("h3:first").css('cursor', 'pointer').click(function() { if($(this).find("img:first").length < 1) { show_fieldset($(this).find("canvas:first"));  } else { show_fieldset($(this).find("img:first")); } }).prepend('<img width="15px" height="15px" src="' + WWW_TEMPLATE + '/images/arrow.gif" style="position: relative; top: 0px; left: -1px; padding-right: 9px;" />');

	/*	if(!jQuery.browser.msie)
			$(this).find("h3:first img:first").rotate(0);
		
		if(jQuery.browser.mozilla) {
			$(this).find("h3:first canvas:first").animate({'top':'7px', 'left':'-4px'}, {
				duration: 250,
				easing: 'easeOutQuad'
			});
		}
		*/
		$(this).find(".fieldset_content").attr('origheight', $(this).find(".fieldset_content").height() + 'px');
		
		if(index > 0) {
		//	if($(this).find("img:first").length >= 1)
				hide_fieldset($(this).find("h3:first img:first"));
			//else
				//hide_fieldset($(this).find("h3:first canvas:first"));
		} else
			$(this).find(".fieldset_content").css('overflow', 'visible');
	});
}

function show_fieldset(obj) {
	
	if(obj.target)
		obj = $(this);
		
	if(obj.parents(".fieldset_content").css("overflow") == 'visible')
		return;
		
	obj.parents(".fieldset_content").animate({height: obj.parents(".fieldset_content").attr("origheight")}, {
  	duration: 400,
  	complete: function(){
			$(this).css({'overflow':'visible'});
  	}
  });
		
/*	if (!jQuery.browser.msie) {
		obj.rotateAnimation({
			animateAngle: 0,
			easing: $.easing['easeOutQuad'],
			duration: 300,
			preservePositionBoolean: false
		});
		if(jQuery.browser.mozilla) {
			obj.animate({'top':'7px', 'left':'-4px'}, {
				duration: 250,
				easing: 'easeOutQuad'
			});
		} else {
			obj.animate({'top':'-1px', 'left':'0px'}, {
				duration: 250,
				easing: 'easeOutQuad'
			});
		}
	} else {*/
		obj.attr('src', WWW_TEMPLATE + '/images/arrow.gif');//.css({'top':'2px'});
	//}
	
	$("#content .fieldset_content").not(obj.parents(".fieldset_content")).each(function(index) {
		if($(this).find("img:first").length < 1) {
			hide_fieldset($(this).find("canvas:first"));
		} else {
			hide_fieldset($(this).find("img:first"));
		}
	});
}

function hide_fieldset(obj) {
	if(obj.target) 
		obj = $(this);
		
		obj.parents(".fieldset_content").animate({height: "50px"}, {
	  	duration: 400,
	  	complete: function(){
				$(this).css({'overflow-y':'hidden'});
	  	}
	  });
		
	/*if (!jQuery.browser.msie) {
		obj.rotateAnimation({
			animateAngle: -90,
			easing: $.easing['easeOutQuad'],
			duration: 300,
			preservePositionBoolean: false,
			bind:[
				{'click': show_fieldset }
			]
		});
		if(jQuery.browser.mozilla) {
			obj.animate({'top':'5px', 'left':'-4px'}, {
				duration: 250,
				easing: 'easeOutQuad'
			});			
		} else {
			obj.animate({'top':'-7px', 'left':'-8px'}, {
				duration: 250,
				easing: 'easeOutQuad'
			});
		}
	} else {*/
		obj.attr('src', WWW_TEMPLATE + '/images/arrow2.gif');//.css({'top':'1px'});
	//}
}

function autoplay_slide(){
  if(slide_timer!=undefined) clearTimeout(slide_timer);
  slide_timer = setTimeout(function(){next_slide(); autoplay_slide(); },   slide_timeout);
  
}
function next_slide() {
  if($('#slider').get(0).animating)  return false;
 
	slide_number++;

	if (slide_number > $("#slider").find("li").length) { // restart
		slide_number = 1;
	}
  $('#slider').get(0).animating = true;
 
	$('#slider').animate({marginLeft: '-' + $('#slider_slide_' + slide_number).width() + 'px'}, slide_speed, "cubicEaseOut", function() {
		//var tmp = $('#slider_slide_' + slide_number).clone();
		var first = $('#slider li:first-child');
		var tmp = first.clone();
		
		first.remove();
		$('#slider').append(tmp);
		$('#slider').css({marginLeft: '0px'});
		$('#slider').get(0).animating = false;
    autoplay_slide();
		
	});
	
}

function prev_slide(){
  if($('#slider').get(0).animating)  return false;
  if(slide_timer!=undefined) clearTimeout(slide_timer);
    
	slide_number--;

	if (slide_number < 1 ) { // restart
		slide_number = $("#slider").find("li").length;
	}
  $('#slider').get(0).animating = true;
	
	var last = $($('#slider li').get($('#slider li').length-1)) ;
  $(last).css({'position':'absolute', left:'-1440px'  }).show();
   
	
	$('#slider').animate({marginLeft:   $('#slider_slide_' + slide_number).width() + 'px'}, slide_speed, "cubicEaseOut", function() {
	  
	  var tmp = $(last).clone();
	  $(last).remove();
	  tmp.insertBefore($('#slider li').get(0));
	  tmp.css({'position':'relative','left':'auto'});
		$('#slider').css({marginLeft: '0px'});
		$('#slider').get(0).animating = false;
 
	});
	    
}


/* Main Menu */
(function($) {
	$.fn.menu_makeover = function(settings) {
    
 
    var root_menu =  this;
    var current_submenu = undefined; 
    var current_menu = $(this).find('>li.current-page-parent').get(0);
 
    $(this).find('ul').each( function(){
      
      var _display = 'none';
      
      if($(this).parent().hasClass('current-page-parent') || $(this).parent().hasClass('current-page-item')){
        _display = 'block'
      }
      var wrapper = $('<div class="submenu"></div>');
    
    
      this.parent_menu = $(this).parent().find('a').get(0);
      
      wrapper.css({position:'absolute', display:_display,top:54,left:$(this).css('left'), width:$(this).width(), height:$(this).height() , overflow:'hidden'});

      
      $(this).wrap(wrapper);
      
      if(_display !='block') {// current item doesn't need to show/hide sub menu
        $(this).mouseout(check_parent_menu);    
        $(this).mouseover(function(){
          debug('clear: ' + this.timeout);
          clearTimeout(this.timeout);
          this.timeout = undefined;
        });
      }
    });

    $(this).find('>li>a').mouseover(show_submenu);
    $(this).find('>li>a').mouseout(check_submenu);
   
   
    function debug(txt){
      return;
      if($('#debug').get(0)==undefined){
        $('body').append('<div id="debug"></div>');  
      }
      $('#debug').append('<p>' + txt  + '</p>');
      
    }
    function show_submenu(){
      if($(this).parent().hasClass('current-page-parent')|| $(this).parent().hasClass('current-page-item')) return;

      $(this).parent().find('>div').show().css({'z-index':100}); 
      if(current_submenu !=undefined){
        $(current_submenu).parent().find('>div').css({'z-index':95});  
      }
      current_submenu = this;
      var ul = $(this).parent().find('div>ul');
      if(typeof(ul.get(0))!='undefined'){
        if(ul.get(0).timeout!=undefined){
          debug('clear: ' + ul.get(0).timeout);
          clearTimeout(ul.get(0).timeout);
          ul.get(0).timeout = undefined;
        }
      }
      ul.css({top:-30,opacity:0}).show().animate({top:0,opacity:1},{ duration:300, queue:false,easing:'cubicEaseOut' });
      
      //ul.fadeIn('fast');;
    }
    
    function check_submenu(e){
      if($(this).parent().hasClass('current-page-parent')|| $(this).parent().hasClass('current-page-item')) return;
      var obj = this;
      if($(this).parent().find('>div>ul').get(0)== undefined) return;
      $(this).parent().find('>div>ul').get(0).timeout = setTimeout(function(){hide_submenu(obj, e);}, 300);
    }
    function check_parent_menu(e){
      var obj = this.parent_menu;
 
      this.timeout = setTimeout(function(){hide_submenu(obj, e);}, 300);
      
    }
    
    function hide_submenu(obj,e){
     
      var wrapper = $(obj).parent().find('>div');
      if(wrapper.get(0)==undefined) return;
      
      wrapper.css({'z-index':95});
    
      
      var offset = wrapper.offset();
       
      if(e.pageX > offset.left && e.pageX < offset.left + wrapper.width() && e.pageY >=  offset.top && e.pageY <  offset.top + wrapper.height()){
        // still mouseover the submenu. do nothing
      }else{
        // outside the submenu, hide submenu
      
        var ul = $(obj).parent().find('div>ul');
        ul.show().animate({top:-40,opacity:0},{duration:200, queue:false, complete:function(){
          
          wrapper.hide();
          this.timeout = undefined;  
        }});
        //ul.get(0).timemout = setTimeout(function(){wrapper.hide()}, 300);
      
      }
    }
   
	};
})(jQuery);  


 



