

function profileIsPremium() {
	return ($("input[@account_type]:checked").val() == 'premium')
}

function applyPromoCode() {
	var promoCode = $("#PromoCodeTxt").val();
	$.post("/jQueryAjax/set_promotion_code.php", {promo_code: promoCode}, promoResults);
}

function promoResults(responseText, param2) {
	if(responseText.substring(0,6) != 'Error:') {
		$("#PromoCodeDisplay").slideUp();
		$("#PromoCodeError").slideUp();
		$("#PromoResults").html(responseText);
		$("#PromoResults").show();
	}
	else {
		$("#PromoCodeError").html(responseText.substring(6));
		$("#PromoCodeError").slideDown();
	}
}

function usernameExists() {
	var options = {
		url: '/jQueryAjax/username_exists.php',
		success:       showUsernameExistsResponse
	};
	$('#RegistrationFormAdvisor').ajaxSubmit(options);
}

function showUsernameExistsResponse(responseText, statusText) {
	if(responseText.substring(0,6) != 'Error:') {
	  $(".Content").slideUp();
	  $(".Footer").hide();
	  $(".Header").removeClass('Open');
	  $("#LicenseInfo .Header").addClass('Open');
	  $("#LicenseInfo .Content").slideDown();
	  $("#LicenseInfo .Footer").show();
	  $('#UserNameExits').slideUp();
	}
	else {
		$("#UserNameExists").html(responseText.substring(6));
		$('#UserNameExists').slideDown();
	}
}

$(function(){
	
	$('#MainNav li').hover(
		function(){
			var $this = $(this);
			clearTimeout($this.data('removeHoverTimer'));
			$this.addClass('hover');
		},
		function(){
			var $this = $(this);
			var removeHover = function(){
				$this.removeClass('hover');
			};
			$this.data('removeHoverTimer',setTimeout(removeHover,150));
		}
	);
	
	$('#LeaveFeedbackJumpLink').click(function(){
		$("#FeedbackForm").slideDown();
	});

	/*
	$('.HomeBackgroundImages').cycle({
		fx: 'fade',
		timeout: 6000
	});
	*/
	
	if($.browser.msie) {
		$(":input").focus(function(){
			$(this).addClass("focus");
		});
		$(":input").blur(function(){
			$(this).removeClass("focus");
		});
	}
	$.each($(".tipMe"), function(){
		var st_content = $(this).attr('content');
		$(this).simpletip({
			content: st_content,
			position: 'bottom',
	 		showEffect: 'fadeIn',
	 		hideEffect: 'fadeOut'

		});
	});
		
	$('textarea.expanding').vertigro();
	
	$('#FlashNotice .close').live("click",function(e){
		e.preventDefault();
		$(this).closest('#FlashNotice').slideUp('slow');
	});

	if($('#FlashNotice').length > 0){
		setTimeout(function(){$('#FlashNotice').slideUp('slow');},15000);
	}

	var displayMore = false;
	$(".more").click(function() {
		
		if(displayMore) {
			var newContent = $(this).parent().find(".short_comment").html();
			$(this).parent().parent().find(".LatestComment").html(newContent);			
		}
		else {
			var newContent = $(this).parent().find(".full_comment").html();
			$(this).parent().parent().find(".LatestComment").html(newContent);			
		}

		displayMore = (displayMore == true) ? false : true;
	});
	
	$("#FeedbackDisputeForm .disputeRadio").click(function(){
		if(this.value == 1) {
			$(this).closest(".feedbackGroup").find(".disputeTextareaDiv").slideDown();
		}
		else {
			$(this).closest(".feedbackGroup").find(".disputeTextareaDiv").slideUp();
		}
	});
	
	$("#RegistrationFormUser").validate({
		errorElement: "p"
	});
	
	$("#ContactUs").validate({
		errorElement: "p"
	});


	$('#s6').cycle({ 
	    fx:     'scrollUp', 
	    timeout: 6000, 
	    delay:  -2000 
	});
	
	$("#s6").hover(
		function(){
			$('#s6').cycle('pause');
		},
		function(){
			$('#s6').cycle('resume');
		}
	);
	
	$('#s5').cycle({ 
	    fx:     'scrollUp', 
	    timeout: 6000, 
	    delay:  -2000 
	});
	
	$("#s5").hover(
		function(){
			$('#s5').cycle('pause');
		},
		function(){
			$('#s5').cycle('resume');
		}
	);


	$(".greyedOut").click(function(){
		$("#greyLinkThickbox").trigger("click");	
	});
	
	$(".greyedOut").css({"cursor":"pointer"});


	
	$("#RegistrationFormAdvisor").validate({
		errorElement: "p"
	});
	
	$("#UpgradeSubmit").click(function(){
		var isValid = true;	
	
		$("#ValidateForm").find(":input").each(function(e){
			if(!$(this).valid()) {
				isValid = false;
			}
		});
		if(isValid) {
			$("#ValidateForm").submit();
		}
		else {
			return false;
		}
		
	});
	
	
	$("#FeedbackLoginSubmit").click(function() {
		var isValid = true;
		$("#FeedbackLogin").find(":input").each(function(e){
			if(!$(this).valid()) {
				isValid = false;
			}
		});
		if(isValid) {
			feedbackLogin();		
		}
		return false;
	});
	
	$("#UserRegistrationComplete").click(function() {
		var isValid = true;
		var formName = $(this).parents("form");
		$(formName).find(":input").each(function(e) {
			if(!$(this).valid()) {
				isValid = false;
			}
		});
		if(isValid) {
			formName.submit();
		}
		else {
			return false;
		}
	});
	

	$("#LeaveFeedbackExpand").click(function(e) {
		//$(".FeedbackContent").slideUp();
		$("#FeedbackForm").slideToggle();
	});
	
	$("#FeedbackRegistration").submit(function(e){
		var isValid = true;
		$("#FeedbackRegistration :input").each(function(e) {
			if(!$(this).valid()) {
				isValid = false;
			}
		});
		if(isValid) {
			submitFeedbackRegistration();
		}
		return false;
	});

	$("#AdvisorAccountTypeNext").click(function() {
		var isValid = true;
		$("#AccountType .Content :input").each(function(e) {
			if(!$(this).valid()) {
				isValid = false;
			}			
		});
		if(isValid) {
		  $(".Content").slideUp();
		  $(".Footer").hide();
		  $(".Header").removeClass('Open');
		  $("#RegistrationInfo .Header").addClass('Open');
		  $("#RegistrationInfo .Content").slideDown();
		  $("#RegistrationInfo .Footer").show();

		  if(profileIsPremium()) {
		  	$("#Disclosure").slideDown();
		  	$("#CreditCardInfo").slideDown();
		  	$("#Captcha .Header").html("6. Are You A Human?");
		  	/*$("#Disclosure .Header").html("5. Release of Information")*/
		  }
		  else {
		  	$("#Captcha .Header").html("4. Are You A Human?");
		  	$("#CreditCardInfo").hide();
		  	$("#Disclosure").hide();
		  }
		}		
	});
	
	$("#DisclosureNext").click(function() {
		var isValid = true;
		$("#Disclosure .Content :input").each(function(e) {
			if(!$(this).valid()) {
				isValid = false;
			}			
		});
		if(isValid) {
		  $(".Content").slideUp();
		  $(".Footer").hide();
		  $(".Header").removeClass('Open');
		  $("#Captcha .Header").addClass('Open');
		  $("#Captcha .Content").slideDown();
		  $("#Captcha .Footer").show();
//
//		  if(profileIsPremium()) {
//		  	$("#Disclosure").slideDown();
//		  	$("#CreditCardInfo").slideDown();
//		  	/*$("#Disclosure .Header").html("5. Release of Information")*/
//		  }
//		  else {
//		  	$("#CreditCardInfo").hide();
//		  	$("#Disclosure").hide();
//		  }
		}		
	});

	

	
	

	
	$("#RegistrationNext").click(function() {
		$('#UserNameExits').slideUp();
		var isValid = true;
		$("#RegistrationInfo .Content :input").each(function(e) {
			if(!$(this).valid()) {
				isValid = false;
			}
		});
		
		if(isValid) {
			usernameExists()
		}
	});
	
	$("#CompleteRegistration").click(function() {
		var isValid = true;
		if(isValid) {
			$("#RegistrationFormAdvisor .Content :input").each(function(e) {
				if(!$(this).valid()) {
					if(!profileIsPremium() && $(this).hasClass("cc_stuff")) {
						
					}
					else {
						isValid = false;
					}
				}
			});
			if(isValid) {
				$("#RegistrationFormAdvisor")[0].submit();
			}
			else {
				$(".Content").slideDown();
				$(".Header").addClass('Open');
				$(".Footer").show();
			}
		}
	});
	
	$("#LicenseNext").click(function() {
		var isValid = true;
		
		if($("#LicenseInfo .Content .security:checked").length > 0) {
			$(".crd").addClass("required");
		}
		else {
			$(".crd").removeClass("required");
		}
		
		if($("#LicenseInfo .Content .insurance:checked").length > 0) {
			$("#InsuranceLicense").addClass("required");
		}
		else {
			$("#InsuranceLicense").removeClass("required");
		}
		
		$("#LicenseInfo .Content :input").each(function(e) {
			if(!$(this).valid()) {
				isValid = false;
			}
		});
		

	
		
		if(isValid) {
			
		  if(profileIsPremium()) {
			  $(".Content").slideUp();
			  $(".Footer").hide();
			  $(".Header").removeClass('Open');
			  $("#CreditCardInfo .Header").addClass('Open');
			  $("#CreditCardInfo .Content").slideDown();	
			  $("#CreditCardInfo .Footer").show();		  	
		  }
		  else {
			  $(".Content").slideUp();
			  $(".Footer").hide();
			  $(".Header").removeClass('Open');
			  $("#Captcha .Header").addClass('Open');
			  $("#Captcha .Content").slideDown();
			  $("#Captcha .Footer").show();    	
		  }
		}
	});

	$("#CreditCardNext").click(function() {
		var isValid = true;
		$("#CreditCardInfo .Content :input").each(function(e) {
			if(!$(this).valid()) {
				isValid = false;
			}
		});
		if(isValid) {
		  $(".Content").slideUp();
		  $(".Footer").hide();
		  $(".Header").removeClass('Open');
		  $("#Disclosure .Header").addClass('Open');
		  $("#Disclosure .Content").slideDown();
		  $("#Disclosure .Footer").show();  	
		}
	});
	$(".RegistrationForm .Header").click(function() {
		if(!$(this).hasClass('Open')) {
			$(".Content").slideUp();
			$(".Footer").hide();
			$(".Header").removeClass('Open');
			$(this).addClass('Open').parent().find('.Content').slideDown();
			$(this).parent().find('.Footer').show();
		}
	});
	
	$("#foundUs").change(function(){
		if(this.value == 'Other') {
			$("#OtherTR").show();
			$("#how_did_you_find_us_other").addClass("required");
		}
		else {
			$("#OtherTR").hide();
			$("#how_did_you_find_us_other").removeClass("required");
		}
	});

	
    $.each($('select.ac-select'), function(){
        var $this = $(this);
        var $options = $this.find('option');
        var $acSelect = $('<div class="ac-select"><span></span><ul></ul></div>');
        $acSelect.find('ul').css({'color':'#333'});
        $.each($options,function(){
            $acSelect.find('ul').append('<li rel="'+$(this).val()+'">'+$(this).text()+'</li>');
            if(this.selected){
                $acSelect.find('span').text($(this).text());
            }
        });
        $acSelect.data('acSelect',$this);
        $this.hide().before($acSelect);
    });

    $('div.ac-select, div.ac-select li').live('click',function(){
    	var $this = $(this);
        $this.closest('div.ac-select').toggleClass('open');
    	$this
    		.find('li').andSelf()
    		.bind('mouseover',function(){
    			$this.closest('div.ac-select').andSelf().addClass('hover');
    		})
    		.bind('mouseout',function(){
    			$this.removeClass('hover');
    			if($this.is('div.ac-select')){
        			var closeSelect = function(){
        				if(! $this.hasClass('hover')){
        					$this.removeClass('open').find('ul').hide();
        				}
        			}
        			this.closeSelectTimer = setTimeout(closeSelect,200);
    			}
    		});
    });

    $('div.ac-select li').live('click',function(){
        $(this)
            .removeClass('hover')
            .closest('div.ac-select')
                .removeClass('open')
                .find('span')
                    .text($(this).text())
                .end()
            .removeClass('open')
            .data('acSelect') // the saved select object
                .val($(this).attr('rel'));
    });
    
    $(':input[name=remain_anonymous]').click(function(){
    	if(this.value == 'Yes') {
    		$(".ccField").fadeIn();
    	}
       	if(this.value == 'No') {
    		$('.ccField').fadeOut();
    	}
    });

    
    $('#BackgroundChecksImage a').click(function(e){
    	e.preventDefault();
    });
	
    $('.defaultMessageSelect').change(function(){
		var target = "#comment_"+$(this).attr('rel');
		$(target).val(this.value);    	
    });

    
    $('div.tooltip').bgiframe();
    
	var $newsScroller = $('.news-story-scroller');
	$newsScroller.data('currentNewsItem',1);
	
	var newsScroller = function(){
		$newsScroller.each(function(){
			var $this = $(this);
			var $newsItems = $('li',this);
			var newsItemHeight = $newsItems.eq(0).outerHeight(true);
			if($this.data('currentNewsItem') < $newsItems.length){
				$this.stop(true,true).animate({'marginTop':'-='+newsItemHeight});
				$this.data('currentNewsItem',$this.data('currentNewsItem')+1);
			}else{
				$this.stop(true,true).animate({'marginTop':0});
				$this.data('currentNewsItem',1);
			}
		});
		setTimeout(newsScroller,5000);
	};
	setTimeout(newsScroller,5000); 
	
	
	
	
});
