$(document).ready(function(){
	setTimeout(initOverLabels, 50);
	$('.tabnav').tabz();
	$('#fontsize').fontz();
	$('a[rel*=facebox]').facebox(); 
	$('a.opengallery').facebox();
	
	initSubMenus();
	
	$('a.comment').click(function(){
		$('#stfForm').hide();
		$('#commentForm').slideToggle();
		return false;
	});
	$('a.sendtofriend').click(function(){
		$('#commentForm').hide();
		$('#stfForm').slideToggle();
		return false;
	});	
	
	$("#featuredSlider > ul").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 5000, true);

	$('.ui-tabs-panel').mouseover(function(){
		$('#featuredSlider > ul').tabs('rotate', null); 
	});
	
	
});

function closeBox(){
	$.facebox.close();
}

function initSubMenus(){
	
	$('.dropdown').mouseover(function(){
		clearTimeout(timer);
	});

	$('.dropdown').mouseout(function(){
		timer = setTimeout(function(){
			hideAllMenus();
		},2000)
	});
	
	$('li#news').mouseover(function(){
		hideAllMenus();
		$("#sub1").show();
	});
	$('li#news').mouseout(function(){
		timer = setTimeout(function(){
			$("#sub1").hide();
		},2000)
	});

	$('li#faith').mouseover(function(){
		hideAllMenus();
		$("#sub2").show();
	});
	$('li#faith').mouseout(function(){
		timer = setTimeout(function(){
			$("#sub2").hide();
		},2000)
	});

	$('li#living').mouseover(function(){
		hideAllMenus();
		$("#sub3").show();
	});
	$('li#living').mouseout(function(){
		timer = setTimeout(function(){
			$("#sub3").hide();
		},2000)
	});

	$('li#sports').mouseover(function(){
		hideAllMenus();
		$("#sub4").show();
	});
	$('li#sports').mouseout(function(){
		timer = setTimeout(function(){
			$("#sub4").hide();
		},2000)
	});

	$('li#memorials').mouseover(function(){
		hideAllMenus();
		$("#sub5").show();
	});
	$('li#memorials').mouseout(function(){
		timer = setTimeout(function(){
			$("#sub5").hide();
		},2000)
	});
	
	$('li#home').mouseover(function(){
		hideAllMenus();
	});
	$('li#submit').mouseover(function(){
		hideAllMenus();
	});						
}

function hideAllMenus(){
	clearTimeout(timer);
	$('#sub1').hide();
	$('#sub2').hide();
	$('#sub3').hide();
	$('#sub4').hide();
	$('#sub5').hide();
}

/* for the switching labels on the login form, should probably rewrite in Jquery... */

function initOverLabels () {
  if (!document.getElementById) return;  	

  var labels, id, field;
  labels = document.getElementsByTagName('label');
  for (var i = 0; i < labels.length; i++) {
	
    if (labels[i].className == 'overlabel') {

      id = labels[i].htmlFor || labels[i].getAttribute('for');
      if (!id || !(field = document.getElementById(id))) {
        continue;
      }

      labels[i].className = 'overlabel-apply';

      if (field.value !== '') {
        hideLabel(field.getAttribute('id'), true);
      }

      field.onfocus = function () {
        hideLabel(this.getAttribute('id'), true);
      };
      field.onblur = function () {
        if (this.value === '') {
          hideLabel(this.getAttribute('id'), false);
        }
      };

      labels[i].onclick = function () {
        var id, field;
        id = this.getAttribute('for');
        if (id && (field = document.getElementById(id))) {
          field.focus();
        }
      };

    }
  }
};

function hideLabel (field_id, hide) {
  var field_for;
  var labels = document.getElementsByTagName('label');
  for (var i = 0; i < labels.length; i++) {
    field_for = labels[i].htmlFor || labels[i].getAttribute('for');
    if (field_for == field_id) {
      labels[i].style.textIndent = (hide) ? '-10000px' : '0px';
      return true;
    }
  }
}


