var carousel = new ( function($) { 
	var me = this;
	
	$(function() {
		$(window).load(function() {
			$(".carouselLong .jCarouselLite").jCarouselLite({
				btnNext: ".carouselLong .nextBtn",
				btnPrev: ".carouselLong .prevBtn",
				scroll: 1,
				visible: 5,
				speed: 500,
				circular: true
			});
			
			$(".carouselWLongAd .jCarouselLite").jCarouselLite({
				btnNext: ".carouselWLongAd .nextBtn",
				btnPrev: ".carouselWLongAd .prevBtn",
				scroll: 1,
				visible: 1,
				speed: 500,
				circular: true
			});
		});
	}); 
	
	
})(jQuery);


;
/*
 * main navigation
 */
var navOverlay = {
	currentID: null,
	mainNavElement: '#region-menu .block-system-main-menu',
	mainNavTimer: null,
  subNavWrapper: this.mainNavElement + ' ul.menu li .subNav',
  currentChannelName: null,
	disableSubnavHide:0,
	subChannelTimer: null,
	currentSubChannelID: null,
  magazineImg: '/sites/all/modules/custom/circmag_float/images/circ_magazine_cover.png',
  magazineLink: 'https://secure.palmcoastd.com/pcd/eSv?iMagId=0815B&i4Ky=I652',
  sponsorImg: '',
  sponsorLink: ''
};

/*
 * init function
 */
navOverlay.init = function(id){
	if(typeof id != 'undefined'){
		this.mainNavElement = id;
	}
  this.formatMenu();

	jQuery(this.mainNavElement+" ul.menu li").not(this.mainNavElement+" ul.menu li ul li").mouseenter(
		function(){
      this.currentID = navOverlay.getMenuIdFromClasses(jQuery(this).attr('class'));
      navOverlay.currentChannelName = jQuery('a', this).html();
      navOverlay.showSubnav(this.currentID);
		}
	).mouseleave( function(){
		navOverlay.delayHideSubnav();
  });

	jQuery(this.subNavWrapper).mouseenter(
		function(){ clearTimeout(navOverlay.mainNavTimer); }
	).mouseleave(
		function(){	navOverlay.delayHideSubnav(); }
	);
};

/*
 * Format menu
 *
 */
navOverlay.formatMenu = function(){
  var $this=this;
  jQuery(this.mainNavElement + " ul.menu li ul").wrap('<div class="subNav" />');
  jQuery(this.mainNavElement + " ul.menu li ul").prepend("<h6>Subchannels:</h6>");
  jQuery(this.mainNavElement + " ul.menu li ul").show();
  jQuery(this.mainNavElement + " ul.menu li ul li").prepend("<span></span>");
  jQuery(this.mainNavElement + " ul.menu li.expanded").each(
    function(){
      var circAd = '<div class="circ-ad"><a href="'+$this.magazineLink+'" target="_blank"><div class="cover-img"><img src="'+$this.magazineImg+'" /></div><p>&raquo; Subscribe to Maxim Magazine</p></a></div>';
      var sponsorAd = '';
      if($this.sponsorImg.length){
        sponsorAd = '<div class="sponsor-ad"><a href="'+$this.sponsorLink+'" target="_blank"><img src="'+$this.sponsorImg+'" /></a></div>';
      }
      var channel = jQuery('a:first', this).html();
      jQuery('ul', this).after(circAd + sponsorAd + '<div class="subnav_subchannel_feature"></div><ul class="nav_articles"><h3>Featured ' + channel + ':</h3></ul><ul class="nav_tv"><h3>Maxim TV:</h3></ul>');
    }
  );
}

/*
 * Get menu name and link from ID
 */
function getMenuLink(id){
  return [jQuery(".mtid-"+id + ' a:first').html(), jQuery(".mtid-"+id + ' a:first').attr('href')];
}

/*
 * Show menu dropdown
 */
navOverlay.showSubnav = function(id){
  clearTimeout(this.mainNavTimer);
 	this.hideSubnav();
	this.currentID = id;
  jQuery(".mtid-"+id).addClass('over');
  jQuery(".region-header-second").css('visibility','hidden'); // Hide top nav to prevent bleed thru
  jQuery(this.mainNavElement+" ul.menu li.over .subNav").show();
	jQuery(this.mainNavElement+" ul.menu li.over .subNav li").removeClass("selected");
  jQuery(this.mainNavElement+" ul.menu li.over .subNav li:eq(0)").addClass("selected");
  this.getChannelData(id);

  // Get first subchannel featured article
  if(jQuery(this.mainNavElement+" ul.menu li.over .subNav").length != 0){
    subChannelID = this.getMenuIdFromClasses(jQuery(this.mainNavElement+" ul.menu li.over .subNav li.selected").attr('class'));
    this.getSubchannelData(subChannelID, 0);
  }

	jQuery(this.mainNavElement+" ul.menu li.over .subNav ul li").mouseenter(
		function(){
			jQuery(this).parent().children('li').removeClass('selected');
			jQuery(this).addClass('selected');
      subChannelID = navOverlay.getMenuIdFromClasses(jQuery(this).attr('class'));
      navOverlay.getSubchannelData(subChannelID, 1);
		}
	);

	// fix for mouse over maxim logo (IE)
	jQuery(".logo-img img").mouseenter(
		function(){	navOverlay.disableSubnavHide=1;	}
	).mouseleave(
		function(){ navOverlay.disableSubnavHide=0;	}
	);
}

navOverlay.delayHideSubnav = function(){
	this.mainNavTimer = setTimeout(function(){
		navOverlay.hideSubnav();
	}, 250);
};

navOverlay.hideSubnav = function(){
	if(!this.disableSubnavHide){
		jQuery(this.mainNavElement + " li.over").removeClass('over');
		jQuery(this.mainNavElement + " ul.menu li .subNav").hide();
    jQuery(".region-header-second").css('visibility','visible');
	}
};

/*
 * Get menu id from class
 */
navOverlay.getMenuIdFromClasses = function($classes){
  var menuID='';
  var classList = $classes.split(/\s+/);
  for (i = 0; i < classList.length; i++) {
    if(classList[i].indexOf('mtid-') > -1){
      var menuID=classList[i].replace('mtid-','');
      break;
    }
  }
  return menuID;
}

/*
 * Get channel data using ajax
 */
navOverlay.getChannelData = function(id, navName){
  if(jQuery(".mtid-"+id).data("nav_featured_list")){
    jQuery(".mtid-"+id+" .nav_articles").html(jQuery(".mtid-"+id).data("nav_featured_list"));
  } else {
    //console.log('ajax: /menu-channel/'+id);
	  jQuery.ajax({
		  url: '/menu-channel/'+id,
		  dataType: 'JSON',
		  success: function(resp){
        str = navOverlay.buildChannelFeatured(resp, id);
        jQuery(".mtid-"+id).data("nav_featured_list", str);
        jQuery(".mtid-"+id+" .nav_articles").html(str);
      }
	  });
  }
  if(jQuery(".mtid-"+id).data("nav_tv_list")){
    jQuery(".mtid-"+id+" .nav_tv").html(jQuery(".mtid-"+id).data("nav_tv_list"));
  } else {
/*     console.log('ajax: /menu-video/'+id); */
	  jQuery.ajax({
		  url: '/menu-video/'+id,
		  dataType: 'JSON',
		  success: function(resp, textstatus){
        str = navOverlay.buildChannelTV(resp);
        jQuery(".mtid-"+id).data("nav_tv_list", str);
        jQuery(".mtid-"+id+" .nav_tv").html(str);
		  }
	  });
  }
}

/*
 * Build Channel tv list
 */
navOverlay.buildChannelTV = function(data){
  str = "<h3>Maxim TV:</h3>";
  if(data.featured && data.featured.length){
    for(var i=0; i < data.featured.length; i++){
      if(i==0){
        str += '<li class="feature">';
        str += '<a href="' + data.featured[i].item.path + '">';
        str += '<figure><img src="' + data.featured[i].item.thumb + '" /></figure>';
        str += '<h2>' + data.featured[i].item.title + '</h2></a>';
      } else {
        str += '<li>';
        str += '<a href="' + data.featured[i].item.path + '">' + data.featured[i].item.title + '</a>';
      }
      str += '</li>';
    }
  }
  return str;
}

/*
 * Build Channel featured list
 */
navOverlay.buildChannelFeatured = function(data, id){
  var channelData = getMenuLink(id);
  str = "<h3>Featured " + channelData[0] + ":</h3>";
  if(data.featured && data.featured.length){
    for(var i=0; i < data.featured.length; i++){
      if(i==0){ str+='<li class="clearfix">'; } else { str+='<li>'; }
      str += '<a href="' + data.featured[i].item.path + '"><figure><img src="' + data.featured[i].item.thumb + '" /></figure><h2>' + data.featured[i].item.title + '</h2></a></li>';
    }
    str += '<div class="morelink">&raquo; <a href="' + channelData[1] + '">more ' + channelData[0].toLowerCase() + ' articles</a></div>';
  }
  return str;
}

/*
 * Get subchannel data using ajax
 */
navOverlay.getSubchannelData = function(id, level){
  if(typeof level == 'undefined') { level = 0; }
  var mainChannelID = this.currentID;

  if(this.currentSubChannelID!=id){
		this.currentSubChannelID=id;
    if(jQuery(".mtid-"+id).data("subchannel_feature")){
      jQuery(".mtid-"+mainChannelID+" .subnav_subchannel_feature").html(jQuery(".mtid-"+id).data("subchannel_feature"));
    } else {
      // if top level, don't delay loading of featured article
		  if(level != 0){
			  clearTimeout(this.subChannelTimer);
			  var timeout=100;
		  } else {
			  var timeout=0;
		  }
      this.subChannelTimer = setTimeout(function(){
        //console.log('ajax: /menu-subchannel/'+id);
			  jQuery.ajax({
				  url: '/menu-subchannel/'+id,
				  dataType: 'JSON',
				  success: function(resp){
            str = '';
            if(resp.featured && resp.featured.length){
    					str = navOverlay.buildSubchannelFeatured(resp);
            }
            jQuery(".mtid-"+mainChannelID+" .subnav_subchannel_feature").html(str);
            jQuery(".mtid-"+id).data("subchannel_feature", str);
				  }
			  });
		  }, timeout);
    }
  }
};

/* Display subchannel featured block */
navOverlay.buildSubchannelFeatured = function(data){
  str = '';
  if(data.featured.length){
    str += '<a href="' + data.featured[0].item.path + '"><figure><img src="' + data.featured[0].item.thumb + '" /></figure><h2>' + data.featured[0].item.title + '</h2></a><p>' + data.featured[0].item.summary + '</p>';
    //str += '<h2>' + data.featured[0].item.title + '</h2><figure><img src="' + data.featured[0].item.thumb + '" /></figure><p>Lorem Ipsum</p>';
  }
  return str;
}

jQuery(document).ready(function(jQuery) {
  navOverlay.init('#region-menu .block-system-main-menu');
})
;
(function($){$.fn.jCarouselLite=function(o){o=$.extend({btnPrev:null,btnNext:null,btnGo:null,mouseWheel:false,auto:null,speed:200,easing:null,vertical:false,circular:true,visible:3,start:0,scroll:1,beforeStart:null,afterEnd:null},o||{});return this.each(function(){var b=false,animCss=o.vertical?"top":"left",sizeCss=o.vertical?"height":"width";var c=$(this),ul=$("ul",c),tLi=$("li",ul),tl=tLi.size(),v=o.visible;if(o.circular){ul.prepend(tLi.slice(tl-v-1+1).clone()).append(tLi.slice(0,v).clone());o.start+=v}var f=$("li",ul),itemLength=f.size(),curr=o.start;c.css("visibility","visible");f.css({overflow:"hidden",float:o.vertical?"none":"left"});ul.css({margin:"0",padding:"0",position:"relative","list-style-type":"none","z-index":"1"});c.css({overflow:"hidden",position:"relative","z-index":"2",left:"0px"});var g=o.vertical?height(f):width(f);var h=g*itemLength;var j=g*v;f.css({width:f.width(),height:f.height()});ul.css(sizeCss,h+"px").css(animCss,-(curr*g));c.css(sizeCss,j+"px");if(o.btnPrev)$(o.btnPrev).click(function(){return go(curr-o.scroll)});if(o.btnNext)$(o.btnNext).click(function(){return go(curr+o.scroll)});if(o.btnGo)$.each(o.btnGo,function(i,a){$(a).click(function(){return go(o.circular?o.visible+i:i)})});if(o.mouseWheel&&c.mousewheel)c.mousewheel(function(e,d){return d>0?go(curr-o.scroll):go(curr+o.scroll)});if(o.auto)setInterval(function(){go(curr+o.scroll)},o.auto+o.speed);function vis(){return f.slice(curr).slice(0,v)};function go(a){if(!b){if(o.beforeStart)o.beforeStart.call(this,vis());if(o.circular){if(a<=o.start-v-1){ul.css(animCss,-((itemLength-(v*2))*g)+"px");curr=a==o.start-v-1?itemLength-(v*2)-1:itemLength-(v*2)-o.scroll}else if(a>=itemLength-v+1){ul.css(animCss,-((v)*g)+"px");curr=a==itemLength-v+1?v+1:v+o.scroll}else curr=a}else{if(a<0||a>itemLength-v)return;else curr=a}b=true;ul.animate(animCss=="left"?{left:-(curr*g)}:{top:-(curr*g)},o.speed,o.easing,function(){if(o.afterEnd)o.afterEnd.call(this,vis());b=false});if(!o.circular){$(o.btnPrev+","+o.btnNext).removeClass("disabled");$((curr-o.scroll<0&&o.btnPrev)||(curr+o.scroll>itemLength-v&&o.btnNext)||[]).addClass("disabled")}}return false}})};function css(a,b){return parseInt($.css(a[0],b))||0};function width(a){return a[0].offsetWidth+css(a,'marginLeft')+css(a,'marginRight')};function height(a){return a[0].offsetHeight+css(a,'marginTop')+css(a,'marginBottom')}})(jQuery);;

(function($) {

/**
 * Drupal FieldGroup object.
 */
Drupal.FieldGroup = Drupal.FieldGroup || {};
Drupal.FieldGroup.Effects = Drupal.FieldGroup.Effects || {};
Drupal.FieldGroup.groupWithfocus = null;

Drupal.FieldGroup.setGroupWithfocus = function(element) {
  element.css({display: 'block'});
  Drupal.FieldGroup.groupWithfocus = element;
}

/**
 * Implements Drupal.FieldGroup.processHook().
 */
Drupal.FieldGroup.Effects.processFieldset = {
  execute: function (context, settings, type) {
    if (type == 'form') {
      // Add required fields mark to any fieldsets containing required fields
      $('fieldset.fieldset', context).once('fieldgroup-effects', function(i) {
        if ($(this).is('.required-fields') && $(this).find('.form-required').length > 0) {
          $('legend span.fieldset-legend', $(this)).eq(0).append(' ').append($('.form-required').eq(0).clone());
        }
        if ($('.error', $(this)).length) {
          $('legend span.fieldset-legend', $(this)).eq(0).addClass('error');
          Drupal.FieldGroup.setGroupWithfocus($(this));
        }
      });
    }
  }
}

/**
 * Implements Drupal.FieldGroup.processHook().
 */
Drupal.FieldGroup.Effects.processAccordion = {
  execute: function (context, settings, type) {
    $('div.field-group-accordion-wrapper', context).once('fieldgroup-effects', function () {
      var wrapper = $(this);

      wrapper.accordion({
        autoHeight: false,
        active: '.field-group-accordion-active',
        collapsible: true,
        changestart: function(event, ui) {
          if ($(this).hasClass('effect-none')) {
            ui.options.animated = false;
          }
          else {
            ui.options.animated = 'slide';
          }
        }
      });

      if (type == 'form') {
        // Add required fields mark to any element containing required fields
        wrapper.find('div.accordion-item').each(function(i){
          if ($(this).is('.required-fields') && $(this).find('.form-required').length > 0) {
            $('h3.ui-accordion-header').eq(i).append(' ').append($('.form-required').eq(0).clone());
          }
          if ($('.error', $(this)).length) {
            $('h3.ui-accordion-header').eq(i).addClass('error');
            var activeOne = $(this).parent().accordion("activate" , i);
            $('.ui-accordion-content-active', activeOne).css({height: 'auto', width: 'auto', display: 'block'});
          }
        });
      }
    });
  }
}

/**
 * Implements Drupal.FieldGroup.processHook().
 */
Drupal.FieldGroup.Effects.processHtabs = {
  execute: function (context, settings, type) {
    if (type == 'form') {
      // Add required fields mark to any element containing required fields
      $('fieldset.horizontal-tabs-pane', context).once('fieldgroup-effects', function(i) {
        if ($(this).is('.required-fields') && $(this).find('.form-required').length > 0) {
          $(this).data('horizontalTab').link.find('strong:first').after($('.form-required').eq(0).clone()).after(' ');
        }
        if ($('.error', $(this)).length) {
          $(this).data('horizontalTab').link.parent().addClass('error');
          Drupal.FieldGroup.setGroupWithfocus($(this));
          $(this).data('horizontalTab').focus();
        }
      });
    }
  }
}

/**
 * Implements Drupal.FieldGroup.processHook().
 */
Drupal.FieldGroup.Effects.processTabs = {
  execute: function (context, settings, type) {
    if (type == 'form') {
      // Add required fields mark to any fieldsets containing required fields
      $('fieldset.vertical-tabs-pane', context).once('fieldgroup-effects', function(i) {
        if ($(this).is('.required-fields') && $(this).find('.form-required').length > 0) {
          $(this).data('verticalTab').link.find('strong:first').after($('.form-required').eq(0).clone()).after(' ');
        }
        if ($('.error', $(this)).length) {
          $(this).data('verticalTab').link.parent().addClass('error');
          Drupal.FieldGroup.setGroupWithfocus($(this));
          $(this).data('verticalTab').focus();
        }
      });
    }
  }
}

/**
 * Implements Drupal.FieldGroup.processHook().
 *
 * TODO clean this up meaning check if this is really
 *      necessary.
 */
Drupal.FieldGroup.Effects.processDiv = {
  execute: function (context, settings, type) {

    $('div.collapsible', context).once('fieldgroup-effects', function() {
      var $wrapper = $(this);

      // Turn the legend into a clickable link, but retain span.field-group-format-toggler
      // for CSS positioning.

      var $toggler = $('span.field-group-format-toggler:first', $wrapper);
      var $link = $('<a class="field-group-format-title" href="#"></a>');
      $link.prepend($toggler.contents());

      // Add required field markers if needed
      if ($(this).is('.required-fields') && $(this).find('.form-required').length > 0) {
        $link.append(' ').append($('.form-required').eq(0).clone());
      }

      $link.appendTo($toggler);

      // .wrapInner() does not retain bound events.
      $link.click(function () {
        var wrapper = $wrapper.get(0);
        // Don't animate multiple times.
        if (!wrapper.animating) {
          wrapper.animating = true;
          var speed = $wrapper.hasClass('speed-fast') ? 300 : 1000;
          if ($wrapper.hasClass('effect-none') && $wrapper.hasClass('speed-none')) {
            $('> .field-group-format-wrapper', wrapper).toggle();
          }
          else if ($wrapper.hasClass('effect-blind')) {
            $('> .field-group-format-wrapper', wrapper).toggle('blind', {}, speed);
          }
          else {
            $('> .field-group-format-wrapper', wrapper).toggle(speed);
          }
          wrapper.animating = false;
        }
        $wrapper.toggleClass('collapsed');
        return false;
      });

    });
  }
};

/**
 * Behaviors.
 */
Drupal.behaviors.fieldGroup = {
  attach: function (context, settings) {
    if (settings.field_group == undefined) {
      return;
    }

    // Execute all of them.
    $.each(Drupal.FieldGroup.Effects, function (func) {
      // We check for a wrapper function in Drupal.field_group as
      // alternative for dynamic string function calls.
      var type = func.toLowerCase().replace("process", "");
      if (settings.field_group[type] != undefined && $.isFunction(this.execute)) {
        this.execute(context, settings, settings.field_group[type]);
      }
    });

    // Fixes css for fieldgroups under vertical tabs.
    $('.fieldset-wrapper .fieldset > legend').css({display: 'block'});
    $('.vertical-tabs fieldset.fieldset').addClass('default-fallback');

  }
};

})(jQuery);;

