jQuery(function($) {
  $('.gallery_demo_unstyled').addClass('gallery_demo'); // adds new class name to maintain degradability
  $('ul.gallery_demo').galleria({
	history   : false, // activates the history object for bookmarking, back-button etc.
	clickNext : true, // helper for making the image clickable
	insert    : '#main_image', // the containing selector for our main image
	onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
		// fade in the image & caption
		if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
			image.css('display','none').fadeIn(1000);
		}
		caption.css('display','none').fadeIn(1000);
		// fetch the thumbnail container
		var _li = thumb.parents('li');
		// fade out inactive thumbnail
		_li.siblings().children('img.selected').fadeTo(500,0.7);
		// fade in active thumbnail
		thumb.fadeTo('fast',1).addClass('selected');
		// add a title for the clickable image
		image.attr('title','Next image >>');
	},
	onThumb : function(thumb) { // thumbnail effects goes here
		// fetch the thumbnail container
		var _li = thumb.parents('li');
		// if thumbnail is active, fade all the way.
		var _fadeTo = _li.is('.active') ? '1' : '0.7';
		// fade in the thumbnail when finnished loading
		thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
		// hover effects
		thumb.hover(
			function() { thumb.fadeTo('fast',1); },
			function() { _li.not('.active').children('img').fadeTo('fast',0.7); } // don't fade out if the parent is active
			)
		}
	});       
});
ads=[];
$(document).ready(function(){
  $('#div-forthcoming').each(function(){
            var links=["http://www.icegov.org", "http://www.egov.iist.unu.edu/index.php?/cegov/projects/EGOV.AF-e-Government-in-Afghanistan","http://www.emacao.gov.mo","http://egov.iist.unu.edu/cegov/Events/Events-2009/UNeGov.net-25th-School-Leadership-Retreat-Maldives"]
	$.preloadImages("/design/cegov/images/highlight1.jpg","/design/cegov/images/highlight2.jpg","/design/cegov/images/highlight3.jpg","/design/cegov/images/highlight4.jpg");
	    var $container=$(this);
		$container.empty();	
		for(var i=0; i<ads.length; i++){
		  var $link=$('<a></a>').attr({href:links[i], target:'_blank'}).append(ads[i])
		  $('<div></div>').addClass('headline').append($link).appendTo($container);
		}
		var currentHeadline=0, oldHeadline=0;
		var hiddenPosition=$container.height()+10;
		$('div.headline').eq(currentHeadline).css('top',0);
		var headlineCount=$('div.headline').length;
		var pause=false;
		var rotateInProgress=false;
		var headlineRotate=function(){
		   if(!rotateInProgress){
		      rotateInProgress=true;
			  pause=false;
			  currentHeadline=(oldHeadline+1)%headlineCount;
			  $('div.headline').eq(oldHeadline).animate({top: -hiddenPosition},'slow',function(){
			     $(this).css('top', hiddenPosition);
			  });
			  $('div.headline').eq(currentHeadline).animate({top:0},'slow',function(){
			     rotateInProgress=false;
				 if(!pause){
				   pause=setTimeout(headlineRotate,5000);
				 }
			  });
			  oldHeadline=currentHeadline;
		   }
		};
		if(!pause){
		  pause=setTimeout(headlineRotate,5000);
		}
		
		$container.hover(function(){
		    clearTimeout(pause);
			pause=false;}, function(){
			  if(!pause){
			    pause=setTimeout(headlineRotate,2000);
			  }
			}
		);
	});
  $("div#rolling-container").each(function(){
     var $pcontainer=$(this);
     //$("div.abstract-td").each(function(){$(this).html($(this).html().substring(0,700)+" ...");});
    //$("div.abstract-td").each(function(){$(this).html("<p>"+$("p:first-child",$(this))).text().substring(0,750)+" ...</p>");});
      $("div.abstract-td").each(function(){
         var $this=$(this);
         var $p=$("p:first-child",$this).text();
         $this.empty();
         $this.append($("<p></p>").text($p.substring(0,650)+" ..."));
      });
     //$container.empty();
     var pcurrentHeadline=0, poldHeadline=0;
     var phiddenPosition=$pcontainer.width()+10;
     $('div.event-content',$pcontainer).css('left',phiddenPosition);
     $('div.event-content',$pcontainer).eq(pcurrentHeadline).css('left',0);
     var pheadlineCount=$('div.event-content',$pcontainer).length;
     var ppause=false;
     var protateInProgress=false;    
     var pheadlineRotate=function(){
       if(!protateInProgress){
         protateInProgress=true;
         ppause=false;
         pcurrentHeadline=(poldHeadline+1)%pheadlineCount;
         $('div.event-content',$pcontainer).eq(poldHeadline).animate({left: -phiddenPosition},'slow',function(){$(this).css('left', phiddenPosition);});
         $('div.event-content',$pcontainer).eq(pcurrentHeadline).animate({left:0},'slow',function(){
           protateInProgress=false;
           if(!ppause){
             ppause=setTimeout(pheadlineRotate,5000);
           }
         });
         poldHeadline=pcurrentHeadline;
       } 
     };
     if(!ppause){
       ppause=setTimeout(pheadlineRotate,5000);
     }
     $pcontainer.hover(function(){
     clearTimeout(ppause);
     ppause=false;}, function(){
       if(!ppause){
         ppause=setTimeout(pheadlineRotate,2000);
       }
     });    
  });
   $('#achievement-tabs').tabs({
   show: function(event, ui) { $('#event-media').height($('#event-content').height()); }
});    
        $('#assessment-tabs').tabs({
   show: function(event, ui) { $('#event-media').height($('#event-content').height()); }
});  
        $('#output-tabs').tabs({
   show: function(event, ui) { $('#event-media').height($('#event-content').height()); }
});     
        $('#event-media').height($('#event-content').height());
});
jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    ads[i]=jQuery("<img>").attr("src", arguments[i]);
  }
}
