var Site = {
  Common: {
    init: function(e) {
      $('body').addClass('js');
    },
    ie6pngFix: function(e) {
      if (typeof DD_belatedPNG != 'undefined') {
        DD_belatedPNG.fix('#navigation .mask, #sidebar .box-sponsor img, body.home #content .latest-news .cols3, #content .presentation .carousel-wrapper .jcarousel-control .jcarousel-control-inner, #content .presentation .carousel-wrapper .jcarousel-control .jcarousel-control-inner2, #content .partners .contents ul li img, #content a.readmore');
      }
    },
    centerdropdown: function(e) {
      parent_width = $(e).parent().width();
		  dropdown_width = $(e).siblings('.drop').find('.drop1').width();
			menu_pos = $('ul.menu').offset();
			parrent_pos = $(e).parent().offset();
			max_move = parrent_pos.left - menu_pos.left;
			max_move = max_move +  (parent_width / 2)
			dropdown_center = max_move - (dropdown_width / 2);
			if (dropdown_center < 0) {
			  dropdown_center = 1;
			}
			$(e).siblings('.drop').find('.drop1').css('left', dropdown_center +'px');
    }
  },
  Homepage: {
    carouselInit: function(e) {
      if (typeof $.jcarousel != 'undefined') {        
        function mycarousel_initCallback(carousel)
        {
          // Pause autoscrolling if the user moves with the cursor over the carousel
          carousel.clip.hover(function() {
              carousel.stopAuto();
          }, function() {
              carousel.startAuto();
          });
          
          // Bind the carousel step change on the prev-next buttons
          $('.jcarousel-control ul li a').bind('click', function() {
            carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
            return false;
          });
          $('#jcarousel-prev').bind('click', function() { carousel.prev(); return false; });
          $('#jcarousel-next').bind('click', function() { carousel.next(); return false; });
          
        };
        
        // Initialize with autoscrolling at 10 seconds, 1 item visible, wrapping around.
        jQuery('.jcarousel').jcarousel({
          auto: 10, // autoscrolling at 10 seconds
          visible: 1, // 1 item visible
          scroll: 1, // scrolling once at a time
          wrap: 'circular', // endless sliding
          initCallback: mycarousel_initCallback, // initialize the 'pausing-on-hover'
          buttonNextHTML: null,
          buttonPrevHTML: null // stop autobuilding prev/next buttons
        });
        
      }
    }
  }
};

var Navigation = (
	function() {
		var self = {
			baseElm: null,
			init: function(){
				self.baseElm = $('#navigation');
				self.baseElm.find(' > ul.menu > li > a' ).mouseover(self.handleLinkClick);
			},
			handleLinkClick: function() {
				self.baseElm.find('li.clicked').removeClass('clicked');
				$(this).parent().addClass('clicked');				
				/* centering the dropdown or moving to the leftmost point if not enough space is available */
				dropdown_left = $(this).siblings('.drop').find('.drop1').css('left');
				if ((dropdown_left == '0px') || (dropdown_left == 'auto')) {
				  parent_width = $(this).parent().width();
				  dropdown_width = $(this).siblings('.drop').find('.drop1').width();
  				menu_pos = $('ul.menu').offset();
  				parrent_pos = $(this).parent().offset();
  				max_move = parrent_pos.left - menu_pos.left;
  				max_move = max_move +  (parent_width / 2)
  				dropdown_center = max_move - (dropdown_width / 2);
  				if (dropdown_center < 0) {
  				  dropdown_center = 1;
  				}
  				/* Sentrering av undermeny er utkommentert pga at den ikke tar hensyn til mange punkter i undermeny av et hovedpunkt som er plassert langt til høyre.
				$(this).siblings('.drop').find('.drop1').css('left', dropdown_center +'px');
				}*/
				$(this).siblings('.drop').find('.drop1').css('left', '0px');
				}
				return false;
			}		
		};
		return self;
	}
)();

$(document).ready(function() {
  Site.Common.init();
  Navigation.init();
  Site.Common.ie6pngFix();
  Site.Common.centerdropdown('#navigation .menu li.clicked a')
  Site.Homepage.carouselInit();
});