/* tabs */
(function($,window,document,undefined){
  $.fn.navTabs = function(options) {  
    var settings = {
      currentClass: "nav-current",
      checkHash: true
    };
    if ( options ) { 
      $.extend( settings, options );
    }
    return this.each(function(){
      var tabs = [],
        activeTab = 0;
      // loop through all tabs
      $(this).find('a').each(function(index){
        var tab = $(this),
          hash = tab.attr('href');
        if(hash.indexOf('#') === 0){
          var content = $(hash);
          // push the elements for easy reference later
          tabs[index] = {tab:tab,content:content,hash:hash};
          // hide the linked content
          content.hide();
          // if the tab has a class current we set it as the default
          if(tab.parent().hasClass(settings.currentClass)){
            activeTab=index;
          }
          // click handler
          tab.click(function(){
            // show corrent content and hide old content
            tabs[activeTab].content.hide();
            tabs[index].content.show();
            // set the current class on the LI elements
            tabs[activeTab].tab.parent().removeClass(settings.currentClass);
            tabs[index].tab.parent().addClass(settings.currentClass);
            // save the active tab
            activeTab = index;
            return false;
          })
        }
        
      })
      // check hash
      if(settings.checkHash && document.location.hash){
        // loop through tabs
        for (var i = tabs.length - 1; i >= 0; i--) {
          // if the tabs hash reference (href attribute) matches the hash we set the default as that item
          if(tabs[i].hash === document.location.hash){
            tabs[activeTab].tab.parent().removeClass(settings.currentClass);//remove a possibly set current class
            activeTab = i; // set the active tab
          }
        };
      }
      // display the default tab
      try{
        tabs[activeTab].tab.click();
      }catch(e){}
      
    })
  }
})(jQuery,window,document);


/* accordion */
(function($,window,document,undefined){
  $.fn.navAccordion = function(options) {  
    var settings = {
      openClass: "nav-summary-open",
      closedClass: "nav-summary-closed",
      accordionClass: "nav-accordion",
      detailsClass: "nav-details",
      summaryClass: "nav-summary",
      toggleAllClass: "nav-toggle",
      toggleOpenClass: "gui-icon-expand",
      toggleCloseClass: "gui-icon-contract",
      // Custom Text for Collaps/Expand
      toggleCloseText: "Collapse All",
      toggleOpenText: "Expand All",
      openOne: true
    };
    if ( options ) { 
      $.extend( settings, options );
    }

    /* Added by JROCA to enable global togglin'*/
    /* Start Global Togglin'*/
    var masterToggle = $('#masterToggle');
    var masterToggleText = $('#masterToggleText');
    
    if ((masterToggle!=null) && (masterToggleText!=null))
    {
    	masterToggle.bind('click',function(){
            var children = $('.nav-summary');
            if(masterToggle.hasClass(settings.toggleCloseClass)){
            	masterToggleText.text(settings.toggleOpenText);
              children.trigger('close');
              masterToggle.removeClass(settings.toggleCloseClass).addClass(settings.toggleOpenClass);
            }else{
            	masterToggleText.text(settings.toggleCloseText);
              children.trigger('open');
              masterToggle.addClass(settings.toggleCloseClass).removeClass(settings.toggleOpenClass);
            }
            return false;
          });
          
     masterToggleText.bind('click', function()
       {
       	masterToggle.trigger('click');
       });
    }
    /* End Global Togglin'*/

    return this.each(function(){
      var summary = $(this),
          details = summary.next("."+settings.detailsClass),
          accordion = summary.parent(),
          isAccordion = accordion.hasClass(settings.accordionClass),
          siblings = summary.siblings("."+settings.summaryClass),
          toggle = summary.parent().find("."+settings.toggleAllClass);
        
        function open(){
          details.show();
          summary.removeClass(settings.closedClass).addClass(settings.openClass);
        }
        function close(){
          details.hide();
          summary.addClass(settings.closedClass).removeClass(settings.openClass);
        }

        summary.bind('open',open);
        summary.bind('close',close);

        summary.bind('click',function(){
          if(summary.hasClass(settings.closedClass)){
            open();
            if(isAccordion && settings.openOne){ 
              siblings.trigger('close');
            };
          }else{
            close();
          }
        });

        if(isAccordion){
          toggle.bind('click',function(){
            var children = accordion.children("."+settings.summaryClass);
            if(toggle.hasClass(settings.toggleCloseClass)){
              children.trigger('close');
              toggle.removeClass(settings.toggleCloseClass).addClass(settings.toggleOpenClass);
            }else{
              children.trigger('open');
              toggle.addClass(settings.toggleCloseClass).removeClass(settings.toggleOpenClass);
            }
            return false;
          })
        }

        if(summary.hasClass(settings.openClass)){
          open();
        }else{
          close();
        }
    })
  }
})(jQuery,window,document);



/* carroussel */
(function($,window,document,undefined){
  
  // detect touch support
  var support = {};
  var events = [
    'touchstart',
    'touchmove',
    'touchend'
  ];
  var el = document.createElement('div');
  for( i in events ) {
    var eventName = events[i];
    eventName = 'on' + eventName;
    var isSupported = (eventName in el);
    if (!isSupported) {
      el.setAttribute(eventName, 'return;');
      isSupported = typeof el[eventName] == 'function';
    }
    support[events[i]] = isSupported;
  }
  support.touches = 
    support.touchstart 
    && support.touchend 
    && support.touchmove;
  
  $.fn.browserTouchSupport = support;


  // setup swiping logic
  $.fn.swipe = function(options) {
    var args = arguments, 
      opts = $.extend({
        threshold: 45,
        speed: 350,
        turnpoint: $(this).width()/2,
        touch: $.fn.browserTouchSupport.touches,
        start: function(){},
        move: function(){},
        success: function(){},
        complete: function(){},
        click: function(){},
        failed: function(){}
      },options);
    return this.each(function() {
      var startX,
        endX,
        startT,
        endT,
        hasMoved = false,
        singleSwipe = false,
        element = this;
      
      function onTouchStart(e){
        if(!singleSwipe){
          var date= new Date();
          startT = date.getTime();
          tempT = startT;
          hasMoved = false;
          if (!opts.touch || e.touches.length >= 1) {
            if(opts.touch){
              startX = e.touches[0].pageX;
            }else{
              startX = e.clientX;
            }
            //console.log(startX)
            endX = startX;
            opts.start.call(this,e);
            singleSwipe = true;
          }else{
            singleSwipe = false;  
          }
        }
      }
      function onTouchMove(e){
        if(singleSwipe){
          e.preventDefault();
          var date= new Date();
          endT = date.getTime();
          if(opts.touch){
            endX = e.touches[0].pageX;
          }else{
            endX = e.clientX;
          }
          var dt = endT - startT;
          var dx = startX - endX;
          if(Math.abs(dx) > opts.threshold || hasMoved){
            opts.move.call(this,e,dx,dt);
            hasMoved = true;
          }
        }
      }
      function onTouchEnd(e){
        if(singleSwipe){
          var date= new Date();
          endT = date.getTime();
          var dx = startX - endX;
          var dt = endT - startT;
          opts.complete.call(this,e);
          if(Math.abs(dx) > opts.threshold && dt < opts.speed){
            // quick swipe
            opts.success.call(this,e,dx,dt);
          }else if(Math.abs(dx) > opts.turnpoint){
            // slow swipe
            opts.success.call(this,e,dx,dt);
          }else if(hasMoved){
            opts.failed.call(this,e);
          }else {
            opts.click.call(this,e);
          }
          singleSwipe = false;
        }
      }

      if(opts.touch){
        this.addEventListener('touchstart', onTouchStart, false);
        this.addEventListener('touchmove', onTouchMove, false);
        this.addEventListener('touchend', onTouchEnd, false);
      }else{

        $(this).mousedown(onTouchStart);
        // binding other events to the document (allow outside screen mouse tracking)
        $(document).mousemove(onTouchMove);
        $(document).mouseup(onTouchEnd);
        this.onselectstart = function() {return false;} // ie
        this.onmousedown = function() {return false;} // mozilla
      }

      $(window).resize(function(){
        opts.turnpoint = $(element).width()/2;
      });

    });
  };

  // setup slider
    $.fn.guiCarousel = function(options) {  
      var settings = {
        'speed'       : 0.5,
        'interval'    : 0,
        'navClass'    : 'gui-carousel-nav',
        'container'   : '.gui-carousel-slides',
        'pagingClass' : 'gui-carousel-icon',
        'next'        : '.gui-carousel-next',
        'previous'    : '.gui-carousel-prev'
      };
      
      return this.each(function() {
        if ( options ) { 
        $.extend( settings, options );
          }
      // global vars
        var $this = $(this),
          slidesContainer = $this.find(settings.container),
          slides = slidesContainer.children(),
          navBlock = $("<div class=\""+settings.navClass+"\"></div>").appendTo(this),
          current = 0,
          imageWidth = $this.width(),
          imageSum = slides.size(),
          navList = [],
          nextLink = $this.find(settings.next),
          prevLink = $this.find(settings.previous);

        if(slides.size() <= 1){
          navBlock.hide();
          return;
        }
        
      // setup slides
      slidesContainer.width(100 * imageSum +'%');

      slides.each(function(index){
        var dot = $('<span class="'+settings.pagingClass+'">'+'&bull;'+'</span>');
        dot.click(function(){
          goToItem(index,settings.speed)
        });
        navBlock.append(dot);
        navList.push(dot);
        $(this).css({
          position:'absolute',
          top: 0,
          left: index*(100/imageSum)+'%',
          width: 100/imageSum+'%'
        })
        
      })

      function goToItem(index,speed){
        navList[index].addClass('current').siblings().removeClass('current');
        setCasePosition(slidesContainer,-(index*imageWidth),speed);
        current = index;
        $this.trigger('slideChange',index);
      }

      // swipe animations
      function slideCases(dx){
        setCasePosition(slidesContainer,-(current*imageWidth)-dx,0);
      }
      
      function setCasePosition(el,x,duration){
        el.stop().animate({'left':x+'px'},duration*1000);
      }



      // setup swipe detaction
      $this.swipe({
        threshold:30,
        start:function(){
          
        },
        move: function(e,dx){
          slideCases(dx);
        },
        success: function(e,dx,dt){
          var direction = dx > 0;
          // slide to next
          var next = current;
          var speed = settings.speed;
          if( !(current == 0 && !direction) && !(current == imageSum-1 && direction) ){
            next += (direction)? 1:-1;
            speed = ((imageWidth - Math.abs(dx)) / (Math.abs(dx)/dt)) / 1000;
            speed = (speed > settings.speed)? settings.speed : speed;
            speed = (speed < .1)? 0.1 : speed;
          }
          // calculate time needed:

          goToItem(next,speed)
        },
        failed: function(){
          // slide back
          goToItem(current,settings.speed);
        }
      });

      $(window).resize(function(){
        imageWidth = $this.width();
        setHeight();
        goToItem(current,0);
      });

      function setHeight(){
        var height = 0;
        slides.each(function(){
          var thisHeight = $(this).outerHeight();
          if(thisHeight > height){
            height = thisHeight;
          }
        });
        slidesContainer.height(height);
        $this.height(height + navBlock.outerHeight());
      }

      function showHideNextPrev(){
        if(current >= imageSum-1){
          nextLink.addClass('inactive');
        }else{
          nextLink.removeClass('inactive');
        }
        if(current <= 0){
          prevLink.addClass('inactive');
        }else{
          prevLink.removeClass('inactive');
        }
      }

      nextLink.click(function(){
        if(current < imageSum){
          goToItem(current+1,settings.speed);
          showHideNextPrev();
        }
      });

      prevLink.click(function(){
        if(current > 0){
          goToItem(current-1,settings.speed);
          showHideNextPrev();
        }
      });
      showHideNextPrev();
      setHeight();
      goToItem(current,0);
        
      function goToNext(){
        var next = (current+1)%slides.length,
            speed = (next > 0)? settings.speed : 0;
        goToItem(next,speed);
      }

      if(settings.interval > 0){
        var interval = setInterval(goToNext,settings.interval);
        $this.mouseenter(function(){
          try{ clearInterval(interval); }catch(e){}
        }).mouseleave(function(){
          try{ clearInterval(interval); }catch(e){}
          interval = setInterval(goToNext,settings.interval);
        })
      }
        
    });
  }
  
})(jQuery,window,document);






(function($,window,document,undefined){
  $.fn.guiWindow = function(options) {  
    var settings = {
      mode: 'center'
    };
    if ( options ) { 
      $.extend( settings, options );
    }
    return this.each(function(){
      var self = $(this),
          href = self.attr('href'),
          content = $(href);
 
     content.appendTo('body').hide();
 
     function positionWindow(noanimation,e){
      //console.log(settings.mode)
      if(settings.mode === 'mouse'){
        content.css({
          position: 'absolute',
          margin: 'auto',
          left: e.pageX,
          top: e.pageY,
          display: '',
          zIndex: 1001
        })
      }else{
          content.css({
            position: 'absolute',
            margin: 'auto',
            left: '-999em',
            display: '',
            zIndex: 1001
          })
         var windowHeight = $(window).height(),
             contentHeight = content.height(),
             windowWidth = $(window).width(),
             contentWidth = content.width(),
             top = (windowHeight - contentHeight)/2 + $(window).scrollTop(),
             left = (windowWidth - contentWidth)/2;
         
         if(noanimation === true){
           content.css({
             left: (left > 0)? left : 0,
             top: (top > 0)? top : 0
           })
         }else{
          content.css({
             left: (left > 0)? left : 0
           }).stop().delay(100).animate({
             top: (top > 0)? top : 0
           },100);
         }
       }
     }
 
     self.click(function(e){
       $('.gui-window').hide();// hide all windows
       positionWindow(true,e);
       $(window).bind('resize scroll',positionWindow);
       return false;
     })
     content.find('.gui-window-close').click(function(){
       content.hide();
       $(window).unbind('resize scroll',positionWindow);
       return false;
     })
      
 
    })
  }
})(jQuery,window,document);
