
var $featured_content = $('#featured'),
  et_disable_toptier = $("meta[name=et_disable_toptier]").attr('content'),
  et_featured_slider_auto = $("meta[name=et_featured_slider_auto]").attr('content'),
  et_featured_auto_speed = $("meta[name=et_featured_auto_speed]").attr('content');

$('ul.nav').superfish({
  delay:       200,                            // one second delay on mouseout
  animation:   {opacity:'show',height:'show'},  // fade-in and slide-down animation
  speed:       'fast',                          // faster animation speed
  autoArrows:  true,                           // disable generation of arrow mark-up
  dropShadows: false                            // disable drop shadows
});

$('ul.nav > li > a.sf-with-ul').parent('li').addClass('sf-ul');

if ( $featured_content.length ) {
  $.fn.et_switcher = function(options) {
    var defaults = {
       slides: '>div',
       activeClass: 'active',
       linksNav: '',
       findParent: true, //use parent elements in defining lengths
       lengthElement: 'li', //parent element, used only if findParent is set to true
       useArrows: false,
       arrowLeft: 'prevlink',
       arrowRight: 'nextlink',
       auto: false,
       autoSpeed: 5000
    };

    var options = $.extend(defaults, options);

    return this.each(function()
    {
      var slidesContainer = $(this),
          thumbMaxWidth = 570,
          thumbMaxHeight = 300;

      slidesContainer.find(options.slides).find('.thumbnail').each(function(){
        var $this = $(this),
            side = 'right';
        if ($this.innerWidth() <= thumbMaxWidth) {
          var marginSide = (thumbMaxWidth - $this.innerWidth()) / 2;

          if($this.parent().is('.descright')) {
            side = 'left';
          }
          $this.css('margin-' + side,marginSide);
        }
        if ($this.innerHeight() <= thumbMaxHeight) {
          var marginTop = (thumbMaxHeight - $this.innerHeight()) / 2;
          $this.css('margin-top',marginTop + 120);
        }
      });

      slidesContainer.find(options.slides).hide().end().find(options.slides).filter(':first').css('display','block');

      if (options.linksNav != '') {
        var linkSwitcher = $(options.linksNav);
              
        linkSwitcher.click(function(){
          var targetElement;

          if (options.findParent) targetElement = $(this).parent();
          else targetElement = $(this);
                    
          if (targetElement.hasClass('active')) return false;
        
          targetElement.siblings('.active').removeClass('active');
          targetElement.addClass('active');
          
          var ordernum = targetElement.prevAll(options.lengthElement).length;
      
          slidesContainer.find(options.slides).filter(':visible').hide().end().end().find(options.slides).filter(':eq('+ordernum+')').stop(true,true).fadeIn(700);
                    
          if (typeof interval != 'undefined') {
            clearInterval(interval);
            auto_rotate();
          };
        
          return false;
        });
      };
    
      $('#'+options.arrowRight+', #'+options.arrowLeft).click(function(){
      
        var slideActive = slidesContainer.find(options.slides).filter(":visible"),
          nextSlide = slideActive.next(),
          prevSlide = slideActive.prev();

        if ($(this).attr("id") == options.arrowRight) {
          if (nextSlide.length) {
            var ordernum = nextSlide.prevAll().length;
          } else { var ordernum = 0; }
        };

        if ($(this).attr("id") == options.arrowLeft) {
          if (prevSlide.length) {
            var ordernum = prevSlide.prevAll().length;
          } else { var ordernum = slidesContainer.find(options.slides).length-1; }
        };

        slidesContainer.find(options.slides).filter(':visible').hide().end().end().find(options.slides).filter(':eq('+ordernum+')').stop(true,true).fadeIn(700);

        if (typeof interval != 'undefined') {
          clearInterval(interval);
          auto_rotate();
        };

        return false;
      });

      if (options.auto) {
        auto_rotate();
      };
    
      function auto_rotate(){
        interval = setInterval(function(){
          var slideActive = slidesContainer.find(options.slides).filter(":visible"),
            nextSlide = slideActive.next();
       
          if (nextSlide.length) {
            var ordernum = nextSlide.prevAll().length;
          } else { var ordernum = 0; }
       
          if (options.linksNav === '')
            $('#'+options.arrowRight).trigger("click");
          else
            linkSwitcher.filter(':eq('+ordernum+')').trigger("click");
        },options.autoSpeed);
      };
    });
  }

  var et_featured_options = {
    linksNav: '#switcher a',
    auto: false,
    autoSpeed: et_featured_auto_speed,
    findParent: false,
    lengthElement: 'a'
  }

  if ( et_featured_slider_auto == 1 ) et_featured_options.auto = true;

  $(window).bind('load', function(){
    $featured_content.et_switcher(et_featured_options);
    $featured_content.find('img').fadeIn();
  });

  var $controllers_box = $('#controllers #switcher'),
    controllers_offset = Math.round( ( 960 - ( $controllers_box.width() ) ) / 2 );

  $controllers_box.css('marginLeft',controllers_offset);
}

var $footer_widget = $("#footer-widgets .footer-widget");
if ( $footer_widget.length ) {
  $footer_widget.each(function (index, domEle) {
    if ((index+1)%3 == 0) $(domEle).addClass("last").after("<div class='clear'></div>");
  });
}

et_search_bar();

function et_search_bar(){
  var $searchform = $('div#search-form'),
    $searchinput = $searchform.find("input#searchinput"),
    searchvalue = $searchinput.val();
  
  $searchinput.focus(function(){
    if ($(this).val() === searchvalue) $(this).val("");
  }).blur(function(){
    if ($(this).val() === "") $(this).val(searchvalue);
  });
}

if ( et_disable_toptier == 1 ) $("ul.nav > li > ul").prev("a").attr("href","#");

