$(document).ready(function(){
    $(document).pngFix();
	setupdropdowns();
	scrollservicesinit();
	nextclient();
//	$('.fontreplace').each(function(i){var strHTML=this.innerHTML;this.innerHTML="";this.innerHTML="<img src='"+strRelativePath+"maketextred.php?text="+strHTML+"' />";$(this).removeClass("fontreplace");});
	$('.fontreplace').each(function(i){var strHTML=this.innerHTML;this.innerHTML="";this.innerHTML="<img src='"+strRelativePath+"_images/_titles/"+fnString2File(strHTML)+".gif' />";$(this).removeClass("fontreplace");});

	$('.cmslogin').validate();
	
	$(".email").blur(function() {
  	$(".telnum").valid();
	});
	$(".telnum").blur(function() {
  	$(".email").valid();
	});
	
	$("a.newwindow").each(function(i){
		$(this).click(function(){
			window.open($(this).attr("href"));
			return false;
		});
	});

	$('.usereditform').validate({
	rules: {
    	Data: "required",
    	dataconf: {
      		equalTo: "#Data"
    	},
		Email: {
      		required: true,
      		email: true
    	}
  	}
	});
	servertabs();
});

setupdropdowns = function() {
	//$('.secondarynav').each(function(i){$(this).css({'width' : $(this).parent().width()});});
	//$('.secondarynav').css({'opacity' : '0.95'});
	// Need to get secondary nav width and compare to hassecondary width.
	$('.hassecondary').each(function(i){ 
		if ($(this).children('ul').width() < $(this).width()) {
			$(this).children('ul').css({'width' : $(this).width() - 2 + 'px'});
		}
	});
	$('.secondarynav > li').each(function(i){
		$(this).css({'width' : $(this).parent().width() + 'px'});
	});
	$('.secondarynav > li > a').each(function(i){
		$(this).css({'width' : $(this).parent().width() - 50 + 'px'});
	});
	$('.hassecondary').hover(function(i){$(this).find('.secondarynav').css({'left': 'auto'});},
	function(i){$(this).find('.secondarynav').css({'left': '-9999px'});});
	$('.secondarynav').hover(function(i){
		// $(this).prev().css({'background-image' : 'url('+strRelativePath +'_images/background-menu-hover.jpg)'});
		$(this).prev().css({'background-color' : '#DDDDDD'});
		$(this).prev().css({'color' : '#000000'});
		$(this).prev().css({'background-repeat' : 'repeat-x'});
		},
		function(i){
		//$(this).prev().css({'background-image' : ''});
		$(this).prev().css({'background-color' : ''});
		$(this).prev().css({'color' : ''});
	});
	//$('.secondarynav > li').hover(function(i){
	//	$((this)+' > a').css({'background-color':'red'});
	//	},
	//	function(i){
	//	$((this)+' > a').css({'background-color':'none'});
	//	}
	//);
};

var clientnum = 1;
nextclient = function(){
	$('.clientbox').each(function(i){
		$(this).css({'display' : 'none', 'visibility' : 'hidden'});
		$(this).click(function(){
			$('.clientbox').each(function(){$(this).css({'display' : 'none', 'visibility' : 'hidden'});});
			clientnum += 1;
			if ($('.client'+clientnum).length === 0 ) {clientnum = 1;}
			$('.client'+clientnum).css({'display' : 'block', 'visibility' : 'visible'});
		});
	});
	$('.client1').css({'display' : 'block', 'visibility' : 'visible'});
};






var scrollservicestime; 
var intCurActive = 2;
var scrollservicestime;
scrollservicesinit = function() {
	$(".serviceslinks > a").each(function(i){
		$(this).click(function() {
      	$(".serviceslinks > a").each(function(i){$(this).removeClass();});
			$(this).addClass("active");
			intCurActive = parseInt($(this).attr("id").substring($(this).attr("id").length-1),10);
			clearTimeout(scrollservicestime);
			scrollservicesswitch(intCurActive);
		});
	});
	scrollservicestime = setTimeout("scrollservicesswitch("+intCurActive+")",15000);
}

scrollservicesswitch = function(intCurActive){
			//alert(intPrevActive+"-"+intCurActive);
			$(".servicesad").each(function(i){$(this).fadeOut(1200);});
			numberofads = $(".serviceslinks > a").length;
			(intCurActive == numberofads+1? intCurActive = 1: intCurActive=intCurActive);
			$(".services"+intCurActive).fadeIn(1200);
      	$(".serviceslinks > a").each(function(i){$(this).removeClass();});
			$(".serviceslinks > a#servicelink"+intCurActive).addClass("active");
			intCurActive = intCurActive + 1;
			scrollservicestime = setTimeout("scrollservicesswitch("+intCurActive+")",15000);
			
}


 
 
 
 

//Craig's Fader Stuff
//var opa;
//var fadetime;
//var previousactive=0;
// 
//function scrollservicesswitch(intDir)
// {
//  clearTimeout(scrollservicestime);
//  var intHomeActive = 1;
//  if (typeof($("div.serviceslinks a.active").val())!="undefined") intHomeActive = parseInt(fnRight($("div.serviceslinks a.active").attr("id"),$("div.serviceslinks a.active").attr("id").length-11),10);
//  intHomeActive=intHomeActive+intDir;
//  if (typeof($("input#HomeDelay"+intHomeActive).val())=="undefined") intHomeActive=1;
//  $('.servicesad').each(function(i){$(this).css({'display' : 'none', 'visibility' : 'hidden'});});
//  $('.serviceslinks > a').each(function(i){$(this).removeClass();});
//  $('a#servicelink'+intHomeActive).addClass('active');
//  // craigs dodgy fader effect
//  $('.services'+(previousactive)).css({'display' : 'block', 'visibility' : 'visible', 'z-index' : '0'});
//  $('.services'+intHomeActive).css({'display' : 'block', 'visibility' : 'visible', 'opacity' : '0', 'z-index' : '1'});
//  if (previousactive != 0) fadein(intHomeActive,0); else fadein(intHomeActive,0.97);
//  previousactive = intHomeActive;
//  scrollservicestime = setTimeout("scrollservicesswitch(1)",$("input#HomeDelay"+intHomeActive).val()*1000);
// }
//
//function fadein(intHomeActive,opa) {
//
//  clearTimeout(fadetime);
//
//  if (opa <= 1) {
//    opa = opa + 0.03;
//    $('.services'+intHomeActive).css({'opacity' : opa});
//    fadetime = setTimeout("fadein("+intHomeActive+","+opa+")",20);
//  };
//};










function fnString2File(strInput)
 {
  while (strInput.indexOf(" ")>-1)strInput=strInput.replace(" ","-");
  strInput = escape(strInput);
  while (strInput.indexOf("%")>-1)strInput=strInput.replace("%","+");
  return strInput.toLowerCase();
 }
function fnRight(str,count){return (count<=0?"":(count>String(str).length?str:String(str).substring(String(str).length,String(str).length-count)))}

function servertabs() {
	$(".servertypetabs > a").click(function(){
		$(this).parents().children(".servercontent").hide();
		$(this).parent().children("a").each(function(){$(this).removeClass("active");});
		$(this).addClass("active");
		var content = $(this).attr("href");
		$(content).show();
		return false;
	});
	$(".openserver").click(function(){
		if ($(this).siblings(".serverbox").css("display") == "none") {
			$(this).siblings(".serverbox").show();
			$(this).attr({"src" : "../_images/button-hide-info.jpg"});
		} else {
			$(this).siblings(".serverbox").hide();
			$(this).attr({"src" : "../_images/button-more-info.jpg"});
		}
		return false;
	});
}
