// JavaScript Document
$(function(){   
	
	$('.clsDemoForm').validate({
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit({
				target: "#idFormFields"
			});
			setTimeout(function() {
				$('.clsDemoPanel').slideUp('fast');
				$('.clsDemoTrigger').html("Schedule a Demo  <img src='/images/blank.gif' width='25' height='25' border='0' align='absmiddle' class='clsDemoPanelIndicator' />");
				$('#idFormFields').load('/demoform.asp')
			}, 4000 );
		}
	});

	$('#idContactForm').load('contactform.asp');
	$('.clsContactForm').validate();
	$('.clsAdminForm').validate();
	
	
		   
	// Tabs
	function offsetTab() {
		var tabWidth = $('li.clsActiveTab').width();
		var indicatorOffset=((tabWidth-16)/2);
		$('.clsTabIndicator').css('left', indicatorOffset + 'px');
	};
	
	var defaultTabNum=0;
	
	var hash = window.location.hash.substring(1);
	
	if (hash == parseInt(hash)) {
		
		if (hash <= $('ul.clsTabNav li').size()) {
			defaultTabNum=hash;									  
		}
	}
		
	$('ul.clsTabNav li:eq(' + defaultTabNum + ')').addClass('clsActiveTab');
	$('li.clsActiveTab').append('<div class="clsTabIndicator">&nbsp;</div>');
	offsetTab()
	$('.clsTabPanel:eq(' + defaultTabNum + ')').fadeIn('fast');
	$('ul.clsTabNav li a').click(function() {
		$('.clsTabIndicator').remove();
		$('.clsTabPanel').hide();
		$('ul.clsTabNav li').removeClass('clsActiveTab');
		$(this).parent('li').addClass('clsActiveTab');
		$('li.clsActiveTab').append('<div class="clsTabIndicator">&nbsp;</div>');
		offsetTab()
		var whichTab = $(this).attr('href');
		$(whichTab).fadeIn('fast');
		$(this).blur();
		return false;
	});
	
	
	var tallestUL = 0
	$('.cls2columnFloat ul').each(function(){
		if ($(this).height() > tallestUL) { tallestUL = $(this).height(); }									   
	});
	$('.cls2columnFloat ul').css({'height': tallestUL + 'px'});
	
	
	$('#idFormFields').load('/demoform.asp');
	$('.clsScheduleLink').click(function(){
		$('.clsDemoPanel').slideDown('fast');
		$('.clsDemoTrigger').html('Close the Drawer  <img src="/images/blank.gif" width="25" height="25" border="0" align="absmiddle" class="clsDemoPanelIndicatorActive" />');
		$("html, body").animate({ scrollTop: 0 }, "fast");
		return false;
	});
	$('.clsDemoTrigger').click(function(){
		$('.clsDemoPanel').slideToggle('fast', function(){
			if ( $(this).is(':visible') ) {
				$('.clsDemoTrigger').html('Close the Drawer  <img src="/images/blank.gif" width="25" height="25" border="0" align="absmiddle" class="clsDemoPanelIndicatorActive" />');  
			}
			if ( $(this).is(':hidden') ) {
				$('.clsDemoTrigger').html('Schedule a Demo  <img src="/images/blank.gif" width="25" height="25" border="0" align="absmiddle" class="clsDemoPanelIndicator" />');  
			}												
		});
	});
	
	$('#rotate')
	.before('<ul id="pager">')
	.cycle({
		fx:	'fade',
		speed: 400,
		timeout: 9000,
		pause: true,
		//random: 1,
		cleartype:  true,
		cleartypeNoBg:  true,
		pager: '#pager',
		
		// callback fn that creates a bullet to use as a page anchor
		pagerAnchorBuilder: function(idx, slide) {
			return '<li><a href="#" onclick="this.blur();">' + (idx + 1) + '</a></li>'
			}
		});
		var numHeaders=$('#pager li').size();
		$('#pager li').width(112/numHeaders);
		$('#pager a').click(function() {
			$('#rotate').cycle('pause')
			})
		
		$("#scroller").scrollable({circular: true}).autoscroll(4000, { autoplay: true });   
		$(".clsTickerHolder").newsticker();   
		// Homepage Module Animation
		if ($.browser.msie && $.browser.version.substr(0,1)<7) {
		// do not move modules for IE6
		} else {
			$('.clsModule').hover(
				function() {
					$(this).stop().animate({ marginTop: "-27px" }, 150 );
					$(this).children('.clsLearnMore').stop(true, true).fadeIn('fast');
					$(this).children('.clsModuleInset').addClass('clsActiveModuleInset');
					$(this).height(237);
				},
				function() {
					$(this).children('.clsLearnMore').stop(true, true).fadeOut('fast');	
					$(this).children('.clsModuleInset').removeClass('clsActiveModuleInset');
					$(this).animate({ marginTop: "0" }, 150 );
					$(this).height(200);
			});
		};
		$('.clsModule').click(function(){
			window.location=$(this).find("a").attr("href");return false;
		});
		
		// Ticker 
		$('.clsTickerHolder li').hide();
		$('.clsTickerHolder li:eq(0)').fadeIn();
		
		//Additional menu hover
		$('li.clsTopLevel').hover(
			function(){ 
				$(this).addClass("clsActive")
			},
    		function(){
				$(this).removeClass("clsActive")
			}					
		);
		
		// Testimonials	
		$.fn.cycleTestimonials = function(timeout){
			var $all_elem = $(this)
		
			show_cycle_elem = function(index){
				if(index == $all_elem.length) {
					index=0
				}; //you can make it start-over, if you want
				$all_elem.hide().eq(index).fadeIn()
				setTimeout(function(){show_cycle_elem(++index)}, timeout);
			}
			show_cycle_elem(0);
		}
		$("div.clsTestimonialHolder .clsTestimonialTopInt").cycleTestimonials(12000);
		
		// ADMIN functionality
		$(".clsLoginForm").validate();
		$(".clsForgotForm").validate();
		$(".clsResetPasswordForm").validate({
			rules: {
				Password: {
					required: true,
					minlength: 5
				},
				confirm_password: {
					required: true,
					minlength: 5,
					equalTo: "#Password"
				}
			},
			messages: {
				Password: {
					required: "Required",
					minlength: "At least 5 characters"
				},
				confirm_password: {
					required: "Required",
					minlength: "At least 5 characters",
					equalTo: "Must be the same"
				}
			}
		});
		$('.clsForgotLink').click(function() {
			$('.clsLoginDiv').slideUp(250, function(){
				$('.clsForgotDiv').slideDown(250);			
			});			
			return false;
		 });
		
		setTimeout(function(){
			$(".clsSuccessMsg").effect("bounce", { times: 3 }, 350);
		}, 1500);
		setTimeout(function(){
			$(".clsErrMsg").effect("shake", { times: 2 }, 100);
		}, 1500);
		
		setTimeout(function(){
		  $('div.clsSuccessMsg').fadeOut('slow', function () {
		  $('div.clsSuccessMsg').remove();
			  });
			
		}, 8000);
		$('.clsMsg').click(function() { 
			$(this).fadeOut('slow', function () { 
				$(this).remove(); 
			}); 
		});
		$("#sortable").tablesorter({ 
		widgets: ['zebra']
		});
		$(".clsAdminTable tr").mouseover(function() {$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");});

		
});
