var Site = {
  Common: {
    init: function(e) {
      $('body').addClass('js');
    },
    /* Label propagator */
    accessibleInputValues: function(e) {
      labeltxt = $(e).prev().html();
      if ($(e).val() == '') $(e).val(labeltxt);
      $(e)
        .focusin(function() {
          if ($(this).val() == labeltxt) $(e).val('');
        }).focusout(function() {
          if ($(e).val() == '') $(e).val(labeltxt);
        });
    },
    linkbehaviour: function() {
	  // make all links with rel external open in a new window
	  $('a[rel="external"]').attr('target', '_blank');
    },
    videoLinks: function(e) {
	  $(e + ' li:nth-child(1)').addClass('active');
      $(e + ' li:nth-child(1)').addClass('child1');
      $(e + ' li:nth-child(2)').addClass('child2');
      $(e + ' li:nth-child(3)').addClass('child3');
      $(e + ' li:nth-child(4)').addClass('child4');
      $(e + ' > li').hover(function() {
        if ($(this).hasClass('active')) {
          
        } else {
          $(e + ' li.active').removeClass('active');
          $(this).addClass('active');
        }
      })
    },
    tabs: function(e) {
      $(e + ' .panel').hide().filter($(e + ' .panel:first-child')).show();
      $(e + ' .rights li:first-child, ' + e + ' .pages li:nth-child(2)').addClass('current');
      prevpanel = $(e + ' .panel:last-child').attr('id');
      nextpanel = $(e + ' .panel:nth-child(2)').attr('id');
      $(e + ' .prev a').attr('href', '#' + prevpanel);
      $(e + ' .next a').attr('href', '#' + nextpanel);
      $(e + ' .rights a,' + e + ' .pages a').click(function() {
        var activated = $(this).attr('href');
        var currentnb = activated.substr(7,9);
        var nextnb = parseInt(currentnb) + 1;
        var prevnb = parseInt(currentnb) - 1;
        if (nextnb > 20) {nextnb = 1}
        if (prevnb < 1) {prevnb = 20}
        nextpanel = "#right_" + nextnb;
        prevpanel = "#right_" + prevnb;
        $(e + ' .prev a').attr('href',prevpanel);
        $(e + ' .next a').attr('href',nextpanel);
        $(e + ' .panel').fadeOut('slow').filter($(activated)).fadeIn('normal');
        $(activated + ' .jScrollPaneContainer').height('130px').width('100%');
        $(activated + ' .jScrollPaneContainer .scrollable').height('130px').width('100%');
        Site.Common.customScroll(activated + ' .scrollable');
        $(e + ' .current').removeClass('current');
        $(e + ' a[href="' + activated + '"]').parent().addClass('current');
      });
    },
    customScroll: function(e) {
      if (typeof $.fn.jScrollPane != 'undefined') {
        $(e).jScrollPane({showArrows:false, scrollbarWidth:11, scrollbarMargin:12});
      }
    }
  } 
};

$(document).ready(function() {
  Site.Common.init();
  Site.Common.accessibleInputValues($('#search input.text'));
  Site.Common.videoLinks('.videolinks');
  Site.Common.tabs('.tabs');
  Site.Common.customScroll('.scrollable');
  Site.Common.linkbehaviour();
  $('.hticker').vTicker({
     speed: 3000,
     pause: 3060,
     showItems: 3,
     animation: '',
     mousePause: true
  });
});

/*
* Based on 
* News Ticker
* by Tadas Juozapaitis
*/

(function($){
$.fn.vTicker = function(options) {
	var defaults = {
		speed: 700,
		pause: 4000,
		showItems: 2,
		animation: '',
		mousePause: true
	};

	var options = $.extend(defaults, options);

	moveUp = function(obj, height){
		obj = obj.children('ul');
		  first = obj.children('li:first');
    	first = obj.children('li:first').clone(true);
    	firstclone = obj.children('li:first');
		
    	obj.animate({left: '-=' + height + 'px'}, options.speed, 'linear', function() {
        	firstclone.remove();
        	$(this).css('left', '0px');
        });
		
		if(options.animation == 'fade')
		{
			obj.children('li:first').fadeOut(options.speed);
			obj.children('li:last').hide().fadeIn(options.speed);
		}

    	first.appendTo(obj);
	};
	
	return this.each(function() {
		obj = $(this);
		maxHeight = 0;

		obj.css({overflow: 'hidden', position: 'relative'})
			.children('ul').css({position: 'absolute', margin: 0, padding: 0})
			.children('li').css({margin: 0, padding: 0});

		obj.children('ul').children('li').each(function(){
			if($(this).width() > maxHeight)
			{
				maxHeight = $(this).width();
			}
		});

		obj.children('ul').children('li').each(function(){
			$(this).width(maxHeight);
		});
		
    	interval = setInterval('moveUp(obj, maxHeight)', options.pause);
		
		if(options.mousePause)
		{
			obj.bind("mouseenter",function(){
				clearInterval(interval);
			}).bind("mouseleave",function(){
				interval = setInterval('moveUp(obj, maxHeight)', options.pause);
			});
		}
	});
};
})(jQuery);