$(document).ready(function() {
	
	$("#slides").slides({
	    preload: true,
	    preloadImage: '/images/loading.gif',
	    play: 5000,
	    pause: 2500,
	    hoverPause: true,
		effect: 'fade',
		crossfade: true,
		randomize: false,
		bigTarget: true	
	  });
	
	if($("ul.tabs_inner").length > 0)
	{
		$("ul.tabs_inner").tabs("div.panes > div");
	}
	
	$('.tabs a').click(function(){
		switch_tabs($(this));
		return false;
	});
	switch_tabs($('.defaulttab'));
	
	//Set custom configurations
	var config = {
	     sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
	     interval: 100, // number = milliseconds for onMouseOver polling interval
	     over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
	     timeout: 500, // number = milliseconds delay before onMouseOut
	     out: megaHoverOut, // function = onMouseOut callback (REQUIRED)
	};

	$("#navigation ul li .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
	$("#navigation ul li").hoverIntent(config); //Trigger Hover intent with custom configurations
	$('.multicolumn_menu').append('<img src="http://www.inform-consult.com/wp-content/themes/inform/images/menu-services-datagem01.png" class="multi_col_image service1">');
	$('.multicolumn_menu').append('<img src="http://www.inform-consult.com/wp-content/themes/inform/images/menu-services-datagem02.png" class="multi_col_image service2">');
	
	$('.menu_xxEvents').append('<img src="http://www.inform-consult.com/wp-content/themes/inform/images/menu-events-datagem01.png" class="multi_col_image_event event1">');
	$('.service1').live('click',function(){
		location.href='http://www.inform-consult.com/2011/02/designing-an-im-solution-using-sharepoint-2010/';    
	});
	$('.service2').live('click',function(){
		location.href='/contact-us/';
	});
	$('.event1').live('click',function(){
		location.href='http://www.inform-consult.com/2011/02/designing-an-im-solution-using-sharepoint-2010/';
	});
	//On Hover Over
	function megaHoverOver(){
	    $(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
	    (function($) {
	        //Function to calculate total width of all ul's
	        jQuery.fn.calcSubWidth = function() {
	            rowWidth = 0;
	            //Calculate row
	            $(this).find("ul").each(function() { //for each ul...
	                rowWidth += $(this).width(); //Add each ul's width together
	            });
	        };
	    })(jQuery); 

	    if ( $(this).find(".row").length > 0 ) { //If row exists...

	        var biggestRow = 0;	

	        $(this).find(".row").each(function() {	//for each row...
	            $(this).calcSubWidth(); //Call function to calculate width of all ul's
	            //Find biggest row
	            if(rowWidth > biggestRow) {
	                biggestRow = rowWidth;
	            }
	        });

	        $(this).find(".sub").css({'width' :biggestRow}); //Set width
	        $(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin

	    } else { //If row does not exist...

	        $(this).calcSubWidth();  //Call function to calculate width of all ul's
	        $(this).find(".sub").css({'width' : rowWidth}); //Set Width

	    }
	}
	//On Hover Out
	function megaHoverOut(){
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
	     $(this).hide();  //after fading, hide it
	  });
	}
	
	$('#rName').magictf({watermarkCSS: 'tf-watermark',defaultString:'NAME'});
	$('#rTel').magictf({watermarkCSS: 'tf-watermark',defaultString:'TEL (inc International  code)'});
	$('#rCompany').magictf({watermarkCSS: 'tf-watermark',defaultString:'Company Name'});

	$('#request-form-e').submit(function(){

		if($('#rName').val() == '' || $('#rName').val() == 'NAME')
		{
			alert('Please enter your name.');
			//$('#rName').focus();
			return false;
		}else if($('#rTel').val() == '' || $('#rTel').val() == 'TEL (inc International  code)'){
			alert('Please enter your telephone no.');
			//$('#rTel').focus();
			return false;
		}else if($('#rCompany').val() == '' || $('#rCompany').val() == 'Company Name'){
			alert('Please enter your company name.');
			//$('#rWhen').focus();
			return false;
		}else{
			$name   = $('#rName').val();
			$tel    = $('#rTel').val(); 
			$company = $('#rCompany').val();
			$when = $('#when_select').val();
			$.post("http://www.inform-consult.com/wp-content/themes/inform/send.php", { name: $name, tel: $tel, company: $company, when: $when },
			   function(data){
			    if(data==1)
				{
					$('#rName').val('NAME');
					$('#rTel').val('TEL (inc International  code)');
					$('#rCompany').val('Company Name');
					
					alert('Thankyou for contacting us. We will be in touch with you.')
					return false;
				}else if(data==2)
				{
					alert('There was an error sending message.Please try again later.');
					return false;
				}else{
					return false;
				}
			});
		}
		
		return false;
	});
});
 
function switch_tabs(obj)
{
	$('.tab-content').hide();
	$('.tabs a').removeClass("selected");
	var id = obj.attr("rel");
 
	$('#'+id).fadeIn('fast');
	obj.addClass("selected");
}
