/****************************************************************************************
* This file is part of the "New Nestle Web Application / Framework".
* Written by Sogeti Swizerland for Nestlé Corporate.
****************************************************************************************/
SlideShow = {
	
	SetSlidesCount:function(count){this.SlidesCount=count;},	
	GetSlidesCount:function(){return this.SlidesCount;},
	
	SetThumbnailsCount:function(count){this.ThumbnailsCount=count;},	
	GetThumbnailsCount:function(){return this.ThumbnailsCount;},
		
	OnPageLoad:function(func) 
	{
		var a,b=navigator.userAgent,d=document,w=window,
		c="__onContent__",e="addEventListener",o="opera",r="readyState",
		s="<scr".concat("ipt defer src='//:' on",r,"change='if(this.",r,"==\"complete\"){this.parentNode.removeChild(this);",c,"()}'></scr","ipt>");
		w[c]=(function(o){return function(){w[c]=function(){};for(a=arguments.callee;!a.done;a.done=1)func(o?o():o)}})(w[c]);
		if(d[e])d[e]("DOMContentLoaded",w[c],false);
		if(/WebKit|Khtml/i.test(b)||(w[o]&&parseInt(w[o].version())<9))
		(function(){/loaded|complete/.test(d[r])?w[c]():setTimeout(arguments.callee,1)})();
		else if(/MSIE/i.test(b))d.write(s);	
	},
	
	DetermineVisibleThumbnail:function(index)
	{
      var CNT_DELAY=1;
      var CNT_DISPLAYABLE_SLIDES=7;
		var slideCount=SlideShow.GetSlidesCount();
      var slideState=new Array(SlideShow.GetThumbnailsCount());
      if((slideCount<=CNT_DISPLAYABLE_SLIDES)||(index<(CNT_DISPLAYABLE_SLIDES-CNT_DELAY)))
      {
         for(var offset=0;offset<CNT_DISPLAYABLE_SLIDES;offset++){slideState[offset]=true;}
         for(var offset=CNT_DISPLAYABLE_SLIDES;offset<slideState.length; offset++){slideState[offset]=false;}
         return slideState;
      }
      var currentPage=Math.ceil((index/CNT_DISPLAYABLE_SLIDES)+0.5);
      if(((index%CNT_DISPLAYABLE_SLIDES)!=0)&&((currentPage*CNT_DISPLAYABLE_SLIDES)>=(CNT_DISPLAYABLE_SLIDES+index))){currentPage--;}    
      var slideOffset=((currentPage-1)*CNT_DISPLAYABLE_SLIDES);
      var readjustIndex=((index+CNT_DELAY)%CNT_DISPLAYABLE_SLIDES);
      if(readjustIndex==0){slideOffset++;}
      if((slideOffset>0)&&(readjustIndex==1)){slideOffset--;}
      for(var offset=0;offset<slideOffset; offset++){slideState[offset]=false;}
      var maxDisplay=slideOffset+CNT_DISPLAYABLE_SLIDES;
      for(var offset=slideOffset;offset<maxDisplay;offset++){slideState[offset]=true;}
      for(var offset=maxDisplay;offset<slideState.length; offset++){slideState[offset]=false;}
      return slideState;
	},

	SetVisible:function(identifier)
	{
	   if(identifier<0){this.VisibleIdentifier=identifier;return;}	   
      if(identifier==this.VisibleIdentifier){return;}
		var slideId = document.getElementById("Slide"+identifier);
		if(! slideId){return;}		   		   
		var slideState=SlideShow.DetermineVisibleThumbnail(identifier);
		var slideCount=slideState.length;
      for(var offset = 0;offset<slideCount; offset++) 
      {
         var idAnchor=document.getElementById("SlideAnchor"+offset);
         var styleAnchor=((! idAnchor)?null: idAnchor.style);
      	if(styleAnchor){styleAnchor.display=(slideState[offset]?'block':'none');}
         var idSelectedAnchor=document.getElementById("SlideSelectedAnchor"+offset);
         var styleSelectedAnchor=((! idSelectedAnchor)?null:idSelectedAnchor.style);
      	if(styleSelectedAnchor){styleSelectedAnchor.display='none';}        
         var id=document.getElementById("Slide"+offset);
         if(! id){continue;}         
         var style=id.style;
         if(offset==identifier)
         {
            style.display='block';		
      	   this.VisibleIdentifier=identifier;      	   
         	if(styleAnchor){styleAnchor.display='none';}
         	if(styleSelectedAnchor){styleSelectedAnchor.display='block';}
            continue;
         } 
         style.display='none';		
		}
	},

	GetVisible:function(){return this.VisibleIdentifier;},
   
   PreviousVisible:function()
   {
	   var id=SlideShow.GetVisible();
	   if(id>0){SlideShow.SetVisible(id-1);}
   },

   NextVisible:function()
   {
	   var id=SlideShow.GetVisible();
	   if(id<(SlideShow.GetSlidesCount() - 1)){SlideShow.SetVisible(id+1);}
   },

   SetVisibleElementId:function(id, visible)
   {
		var elementId=document.getElementById(id);
		if(! elementId){return;}		
		var style=elementId.style;
		if(visible)
		{
		   style.display='block';
		   style.visibility='visible';
		   return;
		}		
		style.display='none';
		style.visibility='hidden';
   },
   
	Init:function() 
	{
		SlideShow.SetVisible(-1);
	   SlideShow.SetVisibleElementId("LoadingGalery", false);
	   SlideShow.SetVisibleElementId("thumbnails", true);
	   SlideShow.SetVisibleElementId("navigation", true);	  
		SlideShow.SetVisible(0);
	}
}

SlideShow.OnPageLoad(SlideShow.Init);
