$(function() {

// adjust css if header is minimized
	 if ($.cookie('headerMinimized')) {
		$("#brandWrapper").css("top", "-80px");
		$("#wrapper").css("padding-top","25px");
		$("#headerHide").css("display", "none");
		$("#headerShow").css("display", "block");
	}
// show the header on click
	$("#headerShow").click(function(){
		$("#wrapper").animate({paddingTop: '100px'}, { duration:100, queue: false });
		$("#brandWrapper").animate({top: '+=80'}, { duration:100, queue: false });
		$(this).hide();
		$("#headerHide").show();
		$.cookie('headerMinimized', null, { path: '/'});
	});
// hide the header on click
	$("#headerHide").click(function(){
		$("#brandWrapper").animate({top: '-=80'}, { duration:100, queue: false });
		$("#wrapper").animate({paddingTop: '25px'}, { duration:100, queue: false });
		$(this).hide();
		$("#headerShow").show();
		$.cookie('headerMinimized', true, { path: '/'});
	});

// initialize nivoslider for banner rotation
$('#bannerScrollerBox').nivoSlider({
	effect:	'fold',
	pauseOnHover: true,
	animSpeed: 250,
	pauseTime: 7000,
	controlNav: false,
	directionNav: true,
	directionNavHide: true,
	keyboardNav: false
});

// make toolbar links have tooltip bubble based on link title
  $("#toolbar a[title]").tooltip({offset: [9, 6]});

// make notification bubble pop up after page load
  if (!$.cookie('notificationMinimized')) {
    setTimeout(function(){
      $("div.toolbarNotification").animate({
        bottom: '+=178'
      }, 500, 'easeOutBack', function() {});
    }, 2000);
  }

  // when clicking notification bubble, make it go away and then set cookie to make it stay hidden
  $("div.toolbarNotification").click(function(){
    $("div.toolbarNotification")
      .css("z-index","0")
      .animate({bottom: '-=178'}, 100);
    $.cookie('notificationMinimized', true, { expires: 7, path: '/'});
  });


	// learn more menu
	$('#learnMoreMenu').hide(); 
	$("#learnMoreMenuItem").hover(function(){ 
		$("#learnMoreMenu").slideDown(150); 
		}, function() { 
		$("#learnMoreMenu").slideUp(90); 
	});

	// referral menu
// uncomment the function below to make the referral menu function - ALSO IN FOOTER.PHP REMOVE THE TITLE FROM THE REFERRALMENUTRIGGER A TAG
		//	$('#referralMenu').hide(); 
		//	$("#referralMenuItem").hover(function(){ 
		//		$("#referralMenu").slideDown(150); 
		//		}, function() { 
		//		$("#referralMenu").slideUp(90); 
		//	});

  // Helper functions for callouts
  function displayDefaultCallout() {
    activateCallout($('#calloutDisplayDefault'));
    resetCalloutHovers();
  }

  function resetCalloutHovers() {
    var $ctr = $('.calloutCtr');
    $ctr.find('.calloutHover').removeClass('calloutHover');
    $ctr.find('.callout_tagline').show();
    $ctr.find('.btnLearnMore').hide();
	$ctr.find('.hoverArrow').hide();
  }

  function activateCallout($to_activate) {
    $('#calloutDisplay').find('.calloutDisplayPanel').hide();
    $to_activate.show();
  }

  function clearCalloutTimeout() {
    clearTimeout(window.calloutTimeout);
  }

  function setCalloutTimeout() {
    window.calloutTimeout = setTimeout(displayDefaultCallout, 700);
  }

  function bindCallout(trigger_sel, target_sel) {
    $(trigger_sel).hover(
      function() {
        var $trigger = $(trigger_sel);
        clearCalloutTimeout();
        resetCalloutHovers();
        activateCallout($(target_sel));
        //Hide the text, show the button
        $trigger.find('.callout_tagline').hide();
        $trigger.find('.btnLearnMore').show();
		$trigger.find('.hoverArrow').show();
        $trigger.addClass('calloutHover');
      },
      setCalloutTimeout
    );
  }

  bindCallout('#ecommerceCallout', '#ecommercePanel');
  bindCallout('#graphicDesignCallout', '#graphicDesignPanel');
  bindCallout('#freePosCallout', '#freePosPanel');
  bindCallout('#marketplacesCallout', '#marketplacesPanel');

  $('#calloutDisplay').hover(clearCalloutTimeout, setCalloutTimeout);

// Initialize scrollable plugin for various items

	// video scrollable
//	$("#slider2Ctr").scrollable();
	
	// testimonials scrollable
	$("#slider1Ctr").scrollable({
		circular: true,
		keyboard: false,
		vertical: true
	}).autoscroll({ autoplay: true, interval: 8000 });;
	
	// client logos scrollable
	$("#sliderLogoCtr").scrollable({
		circular: true,
		keyboard: false,
		vertical: false
	}).autoscroll({ autoplay: true, interval: 5000 });
	
	// display client logo nav arrows on hover
	$("#clientLogoScrollerBox").hover(
		function(){
			$(this).find(".next, .prev").show();
		},
		function(){
			$(this).find(".next, .prev").hide();		
		}
	);

	// page scrollable
	$("#sliderPageCtr").scrollable({
	  circular: false,
	  keyboard: false,
	  speed: 200,
	  onSeek: function() {
		aclass = this.getConf().activeClass;
		// Find all the buttons, remove active from all of them, then add it back to the currently selected one
		$($('#main_navi li').removeClass(aclass)[this.getIndex()]).addClass(aclass);
	  }
	}).navigator({
	  navi: "#main_navi",
	  naviItem: 'li > a',
	  activeClass: 'active',
	  history: true
	});

	// scroll page animation to same page anchor (in html add class scrollPage to the link)
	$('.scrollPage').click(function() {
	   var elementClicked = $(this).attr("href");
	   var destination = $(elementClicked).offset().top;
	 // if the header is minimized, scroll the destination distance minus the minimized header height
	if ($.cookie('headerMinimized')) {
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination - 25}, 500 );
	}
	// if the header is not minimized, scroll the destination distance minus the regular header height
	else {
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination - 100}, 500 );
	}
	   return false;
	});

	// Keep the previous and next buttons on the screen
 	   if ($('.follow').length > 0) { // if the selector exists on the page, run the following script
	   
		   $('.follow').scroll_with_page({
				container : '.modBoxCenter',
				buffer : {
					top    : 220,
					bottom : 120
				}
			});

 		}

// initialize the overlay jquery tool (links with class of overlay_link will trigger the overlay- MUST PUT OVERLAY DIV AND CONTENTS AT BOTTOM OF FILE AND TARGET WITH ANCHOR ID

$(".overlay_link").overlay({
	closeOnClick: false,
	closeOnEsc: true,
	mask: {
		color: '#000',
		loadspeed: 100,
		opacity: 0.8
	},
	top: 'center',
	onClose: function() {
		// when an overlay is clicked, stop the video playback
		$f("*").each(function() {
			(this).stop();
		});
	}
	
});

// initialize jquery ui datepicker, with timepicker addon
	$('#datetimepicker').datetimepicker({
		ampm: true,
		beforeShowDay: $.datepicker.noWeekends,
		showOn: "both",
		buttonImage: "http://crystalcommerce.com/assets/img/btnCalendarTimeMenu.png",
		buttonImageOnly: true,
		hourMin: 10,
		hourMax: 16,
		stepMinute: 15,
		minDate: 1,
		maxDate: 30,
		separator: ' @ ',
		timeText: "Time (PST)"
	
	});

	// validate signup page form
	$("#signup").validate({
		errorElement: "span",
			rules: {
				call_phone: {
					required: true,
					phoneUS: true
				},
				radBudget: {
					required: true
				},
				datetimepicker: {
					required: true
				}
			},
		invalidHandler: function(form, validator) { 
			var errors = validator.numberOfInvalids();
			if (errors) { // display error message if required form elements are not filled in
				var message = errors == 1
				? 'You missed 1 field. It has been highlighted'
				: 'You missed ' + errors + ' fields. They have been highlighted';
				$("div.errorNote span").html(message);
				$("div.errorNote").fadeIn();
			} else {
				$("div.errorNote").hide();
			}

			if ( $("#budget input").is(":checked")) 
				{} // if a budget is selected do nothing
				else
				{ // if a budget is NOT selected, scroll up page to show error message on the buget selection area
				$("html:not(:animated),body:not(:animated)").animate({ scrollTop: 100});
				}

			}
	});

	// validate various website forms
	$("#serviceCallForm, #questionnaire").validate({
		errorElement: "span",
			rules: {
				call_phone: {
					required: true,
					phoneUS: true
				},
				datetimepicker: {
					required: true
				}
			}
	});
	
	// validate various website forms
	$("#formReferMerchant").validate({
		errorElement: "span",
			rules: {
				referrer_phone: {
					required: true,
					phoneUS: true
				},
				phone: {
					required: true,
					phoneUS: true
				},
				web: {
				required: false,
				url: true
    			}
			}
	});

// sign-up page platform collapse and expand
	$("#platform").organicTabs({
		"speed": 100
	});
	
// sign-up page - if CMS and or Forum addons are selected and regular crystalcommerce option (without addon) is then selected, clear the selected addons
	$("#store_cc").click(function(){
		if ( $("#multisite input").is(":checked"))
		{
		$("#multisite input").attr("checked", false); // clear actual radio
		$("#multisite label").removeClass("r_on"); // clear custom radio image selection
		}
		else
		{}
	});

// sign up page - when addons option is selected, switch the support plans. switch back when standard crystal commerce platform is selected again
	$("#platform .label_radio:not('#store_cc')").click(function(){
		$("#support").addClass("support_addon");
	});
	$("#store_cc").click(function(){
		$("#support").removeClass("support_addon");
	});

// unobtrusive javascript print link (won't show for people without javascript)
var $printlink = $('<a class="printlink" href="#print"></a>')
$('.printme').prepend($printlink); // insert the printlink into any container with class printme
$('.printme a.printlink').click(function() { // add print dialog functionality
  window.print();
  return false;
 });
});
