$(document).ready( function() {

  function carouselInit(carousel) {
    $('.level0-hero-carousel .carousel-control a').bind('click', function() {
      carousel.scroll( parseInt( $(this).attr('rel') ) );
      carousel.stopAuto();
      return false;
    });

    carousel.clip.hover(function() {
        //carousel.stopAuto();
    }, function() {
        //carousel.startAuto();
    });

  };

  function firstItemAction(carousel, item, idx, state) {
    $('.level0-hero-carousel .carousel-control a').removeClass('active').eq(idx-1).addClass('active');
  };

  $('.level0-hero-carousel').jcarousel({
    scroll: 1,
    initCallback: carouselInit,
    itemFirstInCallback:  firstItemAction,

    auto: 10,
    wrap: 'last',
    animation: 1500,

    buttonNextHTML: null,
    buttonPrevHTML: null
  });

  if( $('.level0-hero-carousel ul li').size() > 1 )
    $('.level0-hero-carousel .carousel-control').show();

  $('.level0-hero-carousel .carousel-control a').hover(function() {
    $(this).addClass('hover');
  }, function() {
    $(this).removeClass('hover');
  });
  
  /* Login */
  $(".level0-hero-login fieldset.credentials input.text").placeholder();
//  $(".level0-hero-login fieldset.credentials input#password").placeholder();



  /* Alerts */
  $('.level0-alerts ul li').eq(0).show();

  var alertsInterval = null;
  var alertsIndex = 0;
  var alertsIntervalLength = 6500;
  var alertsCount = $('.level0-alerts ul li').size();

  var expandedSizeClass = 'level0-alerts-expanded-' + alertsCount;
  
  // Update NumAlerts Text
  $('.level0-alerts .alerts-toggle span').text(
    alertsCount + ' Alerts'
  );

  $('.level0-alerts .alerts-controls .alerts-toggle').bind('click', function() {
    var itemsCount = $('.level0-alerts ul li').size();
    if( $('.level0-alerts').hasClass('level0-alerts-expanded') ) {
      $('.level0-alerts').removeClass('level0-alerts-expanded');
      $('.level0-alerts').removeClass(expandedSizeClass);
      switchAlert();
      startAlertsInterval();
    } else {
      $('.level0-alerts').addClass('level0-alerts-expanded');
      $('.level0-alerts').addClass(expandedSizeClass);
      $('.level0-alerts ul li').show();
      stopAlertsInterval();
    }
    return false;
  });
  
  var switchAlert = function() {
    $('.level0-alerts ul li').hide();
    alertsIndex++;
    $('.level0-alerts ul li').eq(alertsIndex % alertsCount).show();
  }

  var startAlertsInterval = function() {
    if(alertsInterval == null)
      alertsInterval = setInterval(switchAlert, alertsIntervalLength);
  }
  
  var stopAlertsInterval = function() {
    if(alertsInterval != null) {
      clearInterval(alertsInterval);
      alertsInterval = null;
    }
  }

  if(alertsCount > 1) {
    $('.level0-alerts .alerts-controls').show();
    startAlertsInterval();
  }
  
});

/*
 * jQuery Placeholder Plug-in
 *
 * Copyright (c) 2010 Max Davis
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * Revision: 1
 * Version: 0.1
 *
 * v0.1
 * - First public release
 *
*/

(function($){
	
	$.fn.placeholder = function() {
	  $(this).each( function() {
  		if($(this).attr("type") == "password") {
		
  			var original_pass_field = $(this);

  			if(original_pass_field.val() == "") {
  				$(this).parent().append("<input type=\"text\" value=\""+ $(this).attr("placeholder") +"\" name=\"pass_placeholder\" class=\"form-text placeholder-inactive\" id=\"pass_placeholder\">");
  				$(this).css("display","none");
  			}

  			var original_pass_field = $(this);

  			$("#pass_placeholder").focus(function() {
  				if(original_pass_field.val() == "") {
  					$("#pass_placeholder").css("display","none");
  					original_pass_field.css("display","");
  					original_pass_field.focus();
  				}
  			});

  			original_pass_field.blur(function() {
  				if(original_pass_field.val() == "") {
  					$("#pass_placeholder").css("display","");
  					original_pass_field.css("display","none");
  				}
  			});

  		} else {

  			if($(this).val() === "") {
  				$(this).val($(this).attr("placeholder"));
  				$(this).addClass('placeholder-inactive');
  			}

  			$(this).focus(function() {
  				if($(this).val() === $(this).attr("placeholder")) {
  					$(this).removeClass('placeholder-inactive').addClass('placeholder-active');
  					$(this).val("");
  				}
  			}).blur(function() {
  				if($(this).val() === "") {
  					$(this).removeClass('placeholder-active').addClass('placeholder-inactive');
  					$(this).val($(this).attr("placeholder"));
  				}
  			});
  		}
  	} )

	} 
	
})(jQuery);
