/* create a timeOut function in jQuery */
jQuery.fn.idle = function(time){
    return this.each(function(){
        var i = $(this);
        i.queue(function(){
            setTimeout(function(){
            i.dequeue();
            }, time);
        });
    });
};
$(document).ready(function(){
    // search specialities 
    if(document.getElementById("businessSpec")){
        $("#businessSpec").click(function () {
		  $("#businessCats").slideToggle("fast");
		});

    }
    if(document.getElementById("residentialSpec")){
        $("#residentialSpec").click(function () {
		  $("#residentialCats").slideToggle("fast");
		});

    }
    // roll overs
		$('.topNavItem').hover(
			function() { $('.subNavItem', this).css('display', 'block'); },
			function() { $('.subNavItem', this).css('display', 'none'); });
	
	// description count
	if(document.getElementById("cimy_uef_29")){
		// get current number of characters
	   $('#counter').html($('#cimy_uef_29').val().length + ' characters');
		
	   $('#cimy_uef_29').keyup(function()
	   {
	   // get new length of characters
	   $('#counter').html($(this).val().length + ' characters');
	   });
	}	
	// rotating ad banners
	
	function onBefore() { 
		$(".wp125ad").css('display', 'block');
	} 
	//function onAfter() { 
	//	$(".wp125ad").fadeOut('fast');
	//}
	
	if (document.getElementById("ad1")){
		// home slider rotation
		$('#wp125adwrap_1c').cycle({ 
			fx:    'fade', 
			timeout:  5000,
			slideExpr: 'img', 
    		before:  onBefore
		
		});
		
	}
	// checkbox count on register page
	if(document.getElementById("registerCodes")){
		$(".regCodes").click(function () {
		  var count = $(".regCodes:checked").length;
			//alert (count);
			if (count >= 10){
				$('.regCodes').each( 
				  function(){
					var isChecked = $(this).attr('checked');
						if(!isChecked){
							$(this).attr("disabled", true);
							
						}
				  }
				);
				alert("You are only allowed to select 10 categories.");
			}
			else{
				//alert (count);
				$('.regCodes').each( 
				  function(){
					var isChecked = $(this).attr('checked');
						if(!isChecked){
							$(this).attr("disabled", false);
						}
				  }
				);
			}
		});
		// enhanced listing more codes
		$(".regCodesE").click(function () {
			var eCodeList = "";
			var count = $(".regCodesE:checked").length;
			//alert (count);
			if (count >= 10){
				$('.regCodesE').each( 
				  function(){
					var isChecked = $(this).attr('checked');
						if(!isChecked){
							$(this).attr("disabled", true);
							
						}
						else{
							eCodeList += $(this).val() + ",";
							
						}
				  }
				);
				alert("You are only allowed to select 10 categories.");
			}
			else{
				//alert (count);
				$('.regCodesE').each( 
				  function(){
					var isChecked = $(this).attr('checked');
						if(!isChecked){
							$(this).attr("disabled", false);
						}
						else{
							eCodeList += $(this).val() + ",";
						}
				  }
				);
			}
			// select the value of the dropdown
			var selOne = count - 1;
			document.PayPalForm.os0.options[selOne].selected = true;
			// build the list to be sent through with the payment
			eCodeList	 = eCodeList.substr(0,(eCodeList.length-1));
			document.PayPalForm.newCodes.value = eCodeList;
		});
		
	}
});
