jQuery(document).ready(function(){
   // jQuery("#presenter").visualSlider();

   	Cufon.replace('h2', { fontFamily: 'Helvetica Neue LT Pro' });
	Cufon.replace('h3.label', { fontFamily: 'Helvetica Neue LT Pro' });
	
	$(".marginallistitem").hover( addHover, removeHover );

	$("tbody tr").hover( addHover, removeHover );

	if ( $('.slide').length > 1 ) {
	    new Carousel();
	}

   if ( $('#morenews').length > 0 ) {
       function setControls() {
           if ( position > addcount + 1 ) {
               $('#showLess').css({'opacity': 1});
           }
           if ( position + addcount > items.length ) {
               $('#showMore').css({'opacity': 0});
           }
           if ( position <= addcount + 1 ) {
               $('#showLess').css({'opacity': 0});
           }
           if ( position + addcount <= items.length ) {
               $('#showMore').css({'opacity': 1});
           }
       }

       function setUrlPosition( urlPosition ) {
           $('#additional .marginallistitem a').each(function(i, el) {
               var url = $(el).attr('href');
               if ( url.split('&c=').length > 1 ) {
                   url = url.split('&c=')[0];
               }
               $(el).attr('href', url + "&c=" + urlPosition );
           });
       }

       var items = $('#additional .marginallistitem');
       var position = 0;
       var height = 0;
       var addcount = 6;
       var startshow = 6;
       /* Calculate Height on reload */
       if ( location.href.split("?").length > 1 ) {
           var params = location.href.split("?")[1];
           var params = params.split("&");
           for ( var i = 0; i < params.length; i++ ) {
               if ( params[i].substring(0,1) == "c" ) {
                   var param = parseInt(params[i].split("=")[1]);
                   if ( param > startshow ) {
                       var ceiled = Math.ceil( parseFloat( param ) / parseFloat( addcount ) ) * addcount;
                       if ( $(items[ceiled]).length > 0 ) {
                           var startshow = ceiled;
                       }
                       else {
                           var startshow = items.length;
                       }
                   }
               }
           }
       }
       /* Get and Set Container Width */
       while ( position < startshow ) {
           height = height + parseInt($(items[position]).height()) + parseInt($(items[position]).css('margin-bottom').split('px')[0]);
           position++;
       }
       setControls();
       $('#additional').css('height', height);
       
       /* Bind Link Events */
       $('#showMore').bind('click', function(e) {
           e.preventDefault();
           var addition = position + addcount;

           while ( position < addition && ( $(items[position]).length > 0 ) ) {
               height = height + $(items[position]).outerHeight() + parseInt($(items[position]).css('margin-bottom').split('px')[0]);
               position++;
           }

           setControls();
           setUrlPosition( position - 1 );

           $('#additional').animate({ 'height':height });
           $(document.body).animate({ scrollTop: $(document.body).attr('scrollHeight') });
       });

       $('#showLess').bind('click', function(e) {
           e.preventDefault();
           var substraction = position - addcount;

           while ( position > substraction && position >= addcount + 1 ) {
               position--;
               height = height - ( $(items[position]).outerHeight() + parseInt($(items[position]).css('margin-bottom').split('px')[0]) );
           }

           setControls();
           setUrlPosition( position - 1 );

           $('#additional').animate({ 'height':height });
       });
       

   }
});

function addHover(e) {
	$(this).addClass('hoverbg');
	$(this).addClass('hoverbg');
	
	$(this).bind('click', function(e) {
		window.location.href = $("a:first", this).attr('href');
		return false;
	});
}

function removeHover(e) {
	$(this).removeClass('hoverbg');
}
