jQuery(document).ready(function() {

  // Menu and sidebar hover animations
  jQuery('a.menu.current').prev('span').addClass('current');
  jQuery('a.menu').each(function() {
    var item = jQuery(this);
    item.hover(function() {
      setTimeout(function() { item.prev('span').addClass('hover'); }, 0 );    
      setTimeout(function() { item.next('span').addClass('hover'); }, 0 );    
    }, function() {
      setTimeout(function() { item.prev('span').removeClass('hover'); }, 0 );    
      setTimeout(function() { item.next('span').removeClass('hover'); }, 0 );    
    });
  });

  // Fix padding for last menu item
  jQuery('#InnerContainer a.menu.last').each(function(){
    el = jQuery(this);
    elPos = el.position();
    elWidth = el.innerWidth();
    elPadding = parseInt(el.css('padding-right'));
    elRightPos = elPos.left+elWidth;
    elParentWidth = el.parent().innerWidth();
    el.css('padding-right', Math.floor(elPadding+elParentWidth-elRightPos));
  });

  // Initialise fancybox
  jQuery('a.iframe').fancybox({
    'hideOnContentClick': true,
    'overlayOpacity'    : 0.6,
    'overlayColor'      : '#000',
    'padding'           : 0,
    'width'             : 425,
    'height'            : 310
  });    

  // Initialise fancybox for paylogic links
  jQuery('a.iframe[href*="paylogic"]').fancybox({
    'width'             : 500,
    'height'            : 500
  });    

  // Gallery image swap
  if (jQuery('#GalleryContainer').length != 0) {
    jQuery('#GalleryThumbs img').click(function() {
      el1 = jQuery('#GalleryContainer img');
      el2 = jQuery(this);
      src1 = el1.attr('src');
      src2 = el2.attr('src');
      alt1 = el1.attr('alt');
      alt2 = el2.attr('alt');
      el1.attr({
        src : src2,
        alt : alt2
      });
      el2.attr({
        src : src1,
        alt : alt1
      });
      jQuery('#GalleryTitle').html(alt2);
    });
  }

  // Detect slider object
  if (jQuery('#Slider').length != 0) {
    
    // Get selected event
    function getSelectedEvent() {
      return jQuery('#EventSelection a.current').attr('id');
    }
    
    // Get slider max
    function getSliderMax() {
      if (jQuery('a.factsheet').length != 0) {
        max = 0;
        jQuery('a.factsheet').each(function() {
          range = jQuery(this).attr('rel').split('-');
          rangeMax = parseInt(range[1]);
          if (max < rangeMax) { max = rangeMax }
        });
        return max;
      } else {
        return jQuery('#EventSelection a.current').attr('rel');
      }
    }
  
    // Set slider max
    function setSliderMax() {
      slider.slider('option', 'max', getSliderMax());
    }
    
    // Get slider value
    function getSliderValue() {
      var URL = document.location.toString();
      if (URL.match('#')) {
        var anchor = URL.split('#')[1];
        return parseInt(anchor);
      } else { return 0; }
    }
    
    // Set slider value
    function setSliderValue() {
      slider.slider('option', 'value', getSliderValue());
    }
    
    // Update slider button
    function updateSliderButton(a) {
      a = typeof(a) != 'undefined' ? a : getSliderValue();
      jQuery('#SliderValue span').html(a);
    } 
    
    // Update more button
    function updateMoreButton(a) {
      a = typeof(a) != 'undefined' ? a : getSliderValue();
      jQuery('#ShowMeMore').attr('href', getSelectedEvent()+'#'+a);
    }
    
    // Update rooms
    function updateRooms(a) {
      a = typeof(a) != 'undefined' ? a : getSliderValue();
      if (jQuery('#SliderContainer').length != 0) {
        jQuery('#Center a.factsheet').each(function() {
          range = jQuery(this).attr('rel').split('-');
          if (range[0] > a || range[1] < a) {
            jQuery(this).parent().parent().slideUp();
          }
          else {
            jQuery(this).parent().parent().slideDown();
          }
        });
      }
    } 

    // Initiatilise slider
    slider = jQuery('#Slider');
    slider.slider({
      step  : 5,
      max   : getSliderMax(),
      value : getSliderValue(),
    	slide : function(event, ui) {
    		updateSliderButton(ui.value);
    		updateRooms(ui.value);
        if (jQuery('body.HomePage').length > 0 || jQuery('body.EventTypeHolder').length > 0) {
       		updateMoreButton(ui.value);
        }
    	}     
    });
    updateSliderButton();
    updateRooms();

     // Select event and activate slider
    jQuery('#EventSelection a').click(function() {
      jQuery('#EventSelection a').removeClass('current');
      jQuery(this).addClass('current');
      setSliderValue();
      setSliderMax();
      updateSliderButton(0);
      updateMoreButton();
    });
    
    // Select first item
    // Note: currently selected in HTML
    // jQuery('#EventSelection li:first a').click();
  }

  // Route widget
  jQuery('#Route a').click(function() {
    href = jQuery(this).attr('href')+'&saddr='+jQuery('#Route input').val();
    jQuery(this).attr('href',href);
  });
  
  // Add EventSelectionContainer border
  jQuery('#EventSelectionContainer li:lt(2)').addClass('borderTop');
  
  // Move EventSelectionContainer
  if (jQuery('#EventSelectionContainer').lenght != 0) {
    jQuery('#Left p:first').after(jQuery('#EventSelectionContainer'));
  }

  // Bold first paragraph of BlogEntry
  jQuery('body.BlogEntry #Left p:first').css('font-weight','bold');

  // Innerfade function
  if(jQuery('body.HomePage').length != 0 ) {
    jQuery('.innerfade span').show();
    var elements = jQuery('#MainMenuBackground .innerfade img').length;
    var secondLastElement = elements - 1;
    var counter = 0;
    function showNextImage() {
      jQuery('.innerfade').each(function() {
        if (counter > 0) {
          jQuery('span:nth-child('+secondLastElement+')', this).fadeOut(4000);
          var lastElement = jQuery('span:last', this).remove();
          jQuery('span:first', this).before(lastElement);
          jQuery('span:first', this).show();
        } else {
          jQuery('span:nth-child('+elements+')', this).fadeOut(4000);
        }
      });
      counter ++; 
      t = setTimeout(function() {
        showNextImage();
      }, 9500);
    }
    
    // Start innerfade
    setTimeout(function() {
      showNextImage();
    }, 5500);
  }
  
  // Move logo on smaller screens
  if (jQuery('body').innerWidth() < 1060) {
    jQuery('#Header a').css('left','0');
  }

  // IE6 PNG fix
  if(jQuery.browser.msie && jQuery.browser.version=="6.0") {
    /**
     * --------------------------------------------------------------------
     * jQuery-Plugin "pngFix"
     * Version: 1.1, 11.09.2007
     * by Andreas Eberhard, andreas.eberhard@gmail.com
     *                      http://jquery.andreaseberhard.de/
     *
     * Copyright (c) 2007 Andreas Eberhard
     * Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
     */
    eval(function(p,a,c,k,e,r){e=function(c){return(c<62?'':e(parseInt(c/62)))+((c=c%62)>35?String.fromCharCode(c+29):c.toString(36))};if('0'.replace(0,e)==0){while(c--)r[e(c)]=k[c];k=[function(e){return r[e]||e}];e=function(){return'([237-9n-zA-Z]|1\\w)'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('(s(m){3.fn.pngFix=s(c){c=3.extend({P:\'blank.gif\'},c);8 e=(o.Q=="t R S"&&T(o.u)==4&&o.u.A("U 5.5")!=-1);8 f=(o.Q=="t R S"&&T(o.u)==4&&o.u.A("U 6.0")!=-1);p(3.browser.msie&&(e||f)){3(2).B("img[n$=.C]").D(s(){3(2).7(\'q\',3(2).q());3(2).7(\'r\',3(2).r());8 a=\'\';8 b=\'\';8 g=(3(2).7(\'E\'))?\'E="\'+3(2).7(\'E\')+\'" \':\'\';8 h=(3(2).7(\'F\'))?\'F="\'+3(2).7(\'F\')+\'" \':\'\';8 i=(3(2).7(\'G\'))?\'G="\'+3(2).7(\'G\')+\'" \':\'\';8 j=(3(2).7(\'H\'))?\'H="\'+3(2).7(\'H\')+\'" \':\'\';8 k=(3(2).7(\'V\'))?\'float:\'+3(2).7(\'V\')+\';\':\'\';8 d=(3(2).parent().7(\'href\'))?\'cursor:hand;\':\'\';p(2.9.v){a+=\'v:\'+2.9.v+\';\';2.9.v=\'\'}p(2.9.w){a+=\'w:\'+2.9.w+\';\';2.9.w=\'\'}p(2.9.x){a+=\'x:\'+2.9.x+\';\';2.9.x=\'\'}8 l=(2.9.cssText);b+=\'<y \'+g+h+i+j;b+=\'9="W:X;white-space:pre-line;Y:Z-10;I:transparent;\'+k+d;b+=\'q:\'+3(2).q()+\'z;r:\'+3(2).r()+\'z;\';b+=\'J:K:L.t.M(n=\\\'\'+3(2).7(\'n\')+\'\\\', N=\\\'O\\\');\';b+=l+\'"></y>\';p(a!=\'\'){b=\'<y 9="W:X;Y:Z-10;\'+a+d+\'q:\'+3(2).q()+\'z;r:\'+3(2).r()+\'z;">\'+b+\'</y>\'}3(2).hide();3(2).after(b)});3(2).B("*").D(s(){8 a=3(2).11(\'I-12\');p(a.A(".C")!=-1){8 b=a.13(\'url("\')[1].13(\'")\')[0];3(2).11(\'I-12\',\'none\');3(2).14(0).15.J="K:L.t.M(n=\'"+b+"\',N=\'O\')"}});3(2).B("input[n$=.C]").D(s(){8 a=3(2).7(\'n\');3(2).14(0).15.J=\'K:L.t.M(n=\\\'\'+a+\'\\\', N=\\\'O\\\');\';3(2).7(\'n\',c.P)})}return 3}})(3);',[],68,'||this|jQuery||||attr|var|style||||||||||||||src|navigator|if|width|height|function|Microsoft|appVersion|border|padding|margin|span|px|indexOf|find|png|each|id|class|title|alt|background|filter|progid|DXImageTransform|AlphaImageLoader|sizingMethod|scale|blankgif|appName|Internet|Explorer|parseInt|MSIE|align|position|relative|display|inline|block|css|image|split|get|runtimeStyle'.split('|'),0,{}))
    jQuery(document).pngFix();

    // Add hover effect IE6    
    jQuery('#EventSelection a').hover(function() {
      jQuery(this).addClass('hover');  
    }, function() {
      jQuery(this).removeClass('hover');
    });
  }

  // Performance logging
  // Dont forget to include Javascript in <head> of page
  //var loadTime = (new Date()).getTime() - startTime;
  //jQuery('body').append('<div style="color:#fff;position:absolute">Load time: '+loadTime+'ms</div>');
});