(function(jQuery){
  jQuery.fn.initApppletSwish = function (options) {

    var rotateInProgress = false;
    var animationInProgress = false;
    var pause = false;
    var splashRotator;
    
    var sObj = this;
    var sObjId = this.attr('class');
    
    var defaults = [];
      defaults = {
        speed : '350',
        ease : 'easeInQuad',
        delay : 8000,
        hoverDelay : 8000,
        activeMenu : sObjId+'-menu-active',
        autoStart : 1,
        animateClass : sObjId+'-animate',
        current : sObjId+'-active',
        backgrounds : sObjId+'-backgrounds',
        background : sObjId+'-background',
        menus : sObjId+'-menus',
        menu : sObjId+'-menu'
      }

    o = $.extend(defaults, options);

    var myObject = [];
    myObject = {

      // Set CSS class active for first background in DOM and set CSS display none for all
      initSplash : function() {
        // Find all slide backgrounds in background container - set display:none
        this.find('#'+o.backgrounds+' .'+o.background).css('display','none');
        // Find first slide background in background container - set display:block and add active class
        this.find('#'+o.backgrounds+' .'+o.background+':first').css('display','block').addClass(o.current);
        // Find the menu item in menus container related to current active background
        var activeId = this.currentSplash().id;
        var activeMenu = this.getSplash(activeId).menu;
        activeMenu.addClass(o.activeMenu);
      },
      
      // Function to return a slide and menu item of splash elements
      getSplash : function(id) {
        var slide = jQuery('#'+o.background+'-'+id);
        var menu = jQuery('#'+o.menu+'-'+id);
        var obj = {
          slide : slide,
          menu : menu,
          id : id}
        return obj;
      },
      
      // Function to find the current background shown
      currentSplash : function(){
        var id = this.find('#'+o.backgrounds+' .'+o.current).attr('id').replace(o.background+'-','');
        var splash = this.getSplash(id);
        var obj = {
          slide : splash.slide,
          menu : splash.menu,
          id : id
        }
        return obj;
      },
      
      nextSplash : function() {
        var current = this.currentSplash().menu;
        var last = current.parent().find('.'+o.menu+':last');
        if (current.attr('id') === last.attr('id')) {
          var object = this.currentSplash().menu.parent().find('.'+o.menu+':first');
        } else {
          var object = this.currentSplash().menu.next('.'+o.menu);
        }
        var nextId = object.attr('id').replace(o.menu+'-','');
        var next = this.getSplash(nextId);
        return next;
      },    

      aniDim : function() {
        var screenWidth = wSize().w;
        var current = this.currentSplash();
        var currentOffset = current.slide.offset().left;
        var slideWidth = current.slide.width();
        var marginRight = screenWidth - slideWidth - currentOffset;
        var obj = {
          currentTo : currentOffset + slideWidth,
          nextFrom : screenWidth - currentOffset
        }
        return obj;
      }

    }

    var splashObj = [];
    splashObj = $.extend(myObject, sObj);

    // Rotator control
    var rotator = function() {
      if (!rotateInProgress) {
        rotateInProgress = true;
        pause = false;
        
        var currentTo = splashObj.aniDim().currentTo;
        var nextFrom = splashObj.aniDim().nextFrom;
        var currentSplash = splashObj.currentSplash();
        var nextSplash = splashObj.nextSplash();

        // Position current & next
        currentSplash.slide.css({'position':'absolute'});
        nextSplash.slide.css({'left':nextFrom+'px','display':'block','position':'absolute'});
        
        // Slide current to left
        currentSplash.slide.animate({
          left: '-'+currentTo+'px'
        }, o.speed, o.ease, function() {
          // Animation complete - hide it & remove current class
          jQuery(this).css('display','none').removeClass(o.current).css('left','0px');
          currentSplash.menu.removeClass(o.activeMenu);
        });
        // Slide next to left
        nextSplash.slide.animate({
          left: '0px'
        }, o.speed, o.ease, function() {
          jQuery(this).addClass(o.current);
          nextSplash.menu.addClass(o.activeMenu);
          rotateInProgress = false;
          if (!pause) {
            pause = setTimeout(rotator, o.delay);
          }
        });
      }
    }

    var animateSlider = function(currentSplash, selectedSplash) {
      if (!animationInProgress) {
        animationInProgress = true;
        animationPause = false;

        currentSplash.slide.css({'position':'absolute'});
        selectedSplash.slide.css({'left':splashObj.aniDim().nextFrom+'px','display':'block','position':'absolute'});

        currentSplash.slide.animate({
          left: '-'+splashObj.aniDim().currentTo+'px'
        }, o.speed, o.ease, function() {
          jQuery(this).css('display','none').removeClass(o.current);
        });
        // Slide next to left
        selectedSplash.slide.animate({
          left: '0px'
        }, o.speed, o.ease, function() {
          jQuery(this).addClass(o.current);
          animationInProgress = false;
        });
      }
    }
        
    splashObj.initSplash();

    if (!pause) {
      pause = setTimeout(rotator, o.delay);
    }

    hiConfig = {
      sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)
      interval: 150, // number = milliseconds for onMouseOver polling interval
      timeout: 150, // number = milliseconds delay before onMouseOut
      over: function() {
        var selectedId = jQuery(this).attr('id').replace(o.menu+'-','')
        clearTimeout(pause);
        pause = false;
      
        var currentSplash = splashObj.currentSplash();
        var selectedSplash = splashObj.getSplash(selectedId);  
        if (currentSplash.id !== selectedId) {
          jQuery(this).parent().find('.'+o.activeMenu).removeClass(o.activeMenu);
          jQuery(this).addClass(o.activeMenu);
          animationPause = false;
          animateSlider(currentSplash, selectedSplash);
        }
      }, // function = onMouseOver callback (REQUIRED)
      out: function() {
        if (!pause) {
          pause = setTimeout(rotator, o.hoverDelay);
        }
      } // function = onMouseOut callback (REQUIRED)
    }

    splashObj.find('.'+o.menu).hoverIntent(hiConfig)
 
    return this;

  }
})(jQuery);


(function(jQuery){
  jQuery.fn.expProgramSlide = function (options) {

    programConfig = {
      sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)
      interval: 150, // number = milliseconds for onMouseOver polling interval
      timeout: 150, // number = milliseconds delay before onMouseOut
      over: function() {
        $('#body-intro').animate({
          height : '500px'
        }, 'slow', function() {});
      }, // function = onMouseOver callback (REQUIRED)
      out: function() {
        $('#body-intro').animate({
          height : '349px'
        }, 'fast', function() {});
      } // function = onMouseOut callback (REQUIRED)
    }

    $(this).hoverIntent(programConfig);

  }
})(jQuery);


function expFormInit(el) {
    $(el).wellforms({
        errorPlugin : function() {
          $.each(this.errorTips, function() {
            var obj = this;
            this.wrapper.attr('id', this.father.$form.attr('name')+'_'+this.father.name+'_errorloc');
            this.father.$original.focus(function() {
              obj.wrapper.css('visibility','hidden');
            });
          });
        }
      });
}


function expFullHeight() {
  var docH = bSize().h;
  var winH = wSize().h;
  var cont = $('#body-content');
  var contH = cont.height();
  if ( winH >= docH ) {
    cont.height((winH - docH) + cont.height());
  }
  $(window).resize(function() {
    if (wSize().h >= docH) {
      cont.height((wSize().h - bSize().h) + cont.height());
    } else {

    }
  });
}


function bSize() {
  var a = [];
  a.h = $('body').height();
  a.w = $('body').width();
  return a;
}


function wSize() {
  var a = [];
  a.h = $(window).height();
  a.w = $(window).width();
  return a;
}
