

// tieto tri fcie sa mzou volat z HTML kodu ----------------- -----------

// spustenei prezentacie - povinny len prvy  (a pri prvom volani aj druhy) parameter
function faderStart(id,totalCount,frameDelay,stepSize,stepDelay,divUnderParent)
{
	if(totalCount==undefined && !allFaders[id])
        return false;
    if(!allFaders[id])
        faderInit(id,totalCount,frameDelay,stepSize,stepDelay,divUnderParent);    
    if(allFaders[id]['running'] == false)
    {
    	//alert('start');
    	allFaders[id]['running']=true;
        faderSwitchTo(id); 
    }      
}
// zastavenie prezentacie  ---
function faderStop(id)
{
	if(!allFaders[id])
        return false;
    allFaders[id]['running']=false;
}
// nastavenei prezentacie na dany snimok
function faderSet(id,itemNumber,stopForTime)
{
	if(!allFaders[id])
        return false;           
   faderSwitchTo(id,true,itemNumber,true,stopForTime);  
   //faderStop(id);                               
}




// TOTO dalej nevolat z kodu - keby to bolo OOP bolo by to protected :)  ----------------------------------------------

var allFaders = new Array();

function faderSwitchTo(id,now,toNumber,forceOne,stopForTime)
{	
    //gE('vypis').innerHTML=gE('vypis').innerHTML+'switchTostart<br>';
     
    if(!allFaders[id])
        return false;
     
     if(forceOne==undefined)
        forceOne=false;
     if(now==undefined)
        now=false;
     if(stopForTime==undefined)
        stopForTime=false;
     
     if(toNumber == undefined || !toNumber)
        toNumber=allFaders[id]['actual']+1;
     
     if(toNumber>allFaders[id]['count'])
        toNumber=1;
                  
     
     
     if(now && (forceOne==true || allFaders[id]['running']))
     {     	
        allFaders[id]['actual']=toNumber;        
        var nextSwitchCall='';
        
        if(stopForTime > 0) 
        	nextSwitchCall="faderSetNextSwitch('"+id+"',"+stopForTime+");";
        else if(stopForTime == 0) 
        	nextSwitchCall="faderSetNextSwitch('"+id+"');";
        	
        // nacitaj si ajaxom htmlko	
        runPhpScript(allFaders[id]['ajax'],'&number='+toNumber,allFaders[id]['secondElmId'],"faderButtonsClass('"+id+"');"+nextSwitchCall+"faderRunSteps('"+id+"');",true);                 
     }
	 else if(allFaders[id]['running'])
        faderSetNextSwitch(id);   
	 
     
    
    //gE('vypis').innerHTML=gE('vypis').innerHTML+'running is '+allFaders[id]['running']+'<br>';
        
    
}

function faderSetNextSwitch(id,time)
{
	// nastav timer na dalsi prechod
    window.clearTimeout(allFaders[id]['timer']);
    if(time == undefined || !time)
    	time=allFaders[id]['frameDelay']
    	
    allFaders[id]['timer'] = window.setTimeout("faderSwitchTo('"+id+"',true)", time );
}

// spusta sa po ajaxe ako afterScript
function faderRunSteps(id)
{
    faderStopSteps(id);    
    //  volanie faderStep
    allFaders[id]['opacity']=0;  
    faderStep(id);  
    allFaders[id]['secondElm'].style.display='block';
    allFaders[id]['steptimer']= window.setInterval("faderStep('"+id+"')",   allFaders[id]['stepDelay']     );
}

function faderStep(id)
{
   if (allFaders[id]['opacity']>1) 
   {
        // vymena obsahu, aby sa dalo pokracovat dalej
        allFaders[id]['elm'].innerHTML=allFaders[id]['secondElm'].innerHTML;
        faderStopSteps(id);
   } 
   allFaders[id]['secondElm'].style.opacity=String(   allFaders[id]['opacity']    );    
   allFaders[id]['secondElm'].style.filter='alpha(opacity='+String(     allFaders[id]['opacity']*100   )+')';  
      
   allFaders[id]['opacity']+=allFaders[id]['stepSize'];
      
    
}
  
function faderStopSteps(id)
{                      
	if(allFaders[id]['steptimer'])
         window.clearInterval(allFaders[id]['steptimer']);
         
    allFaders[id]['secondElm'].style.display='none';       
        
}
  
function faderButtonsClass(id)
{                      
	var i;
    for(i=1;i<=allFaders[id]['count'];i++)
        if(allFaders[id]['buttons'][i])
        {
        	if(i==allFaders[id]['actual'])
               addCls(allFaders[id]['buttons'][i],'act');
            else    
                remCls(allFaders[id]['buttons'][i],'act');
        }        
        
}

function faderInit(id,totalCount,frameDelay,stepSize,stepDelay,divUnderParent)
{
     var mainElm=gE(id);
    if(!mainElm)
        return false;
    var ajaxFile=mainElm.getAttribute('fd');
    if(!ajaxFile)
        return false;
        
        
    allFaders[id]=new Object();
    if(divUnderParent == undefined)
        divUnderParent=false;
    if(stepSize == undefined || stepSize<=0)
        stepSize=0.05;
    if(stepDelay == undefined || stepDelay<=10)
        stepDelay=30;
    if(frameDelay == undefined || frameDelay<=800)
        frameDelay=4000;
    
    // TODO creat second div
    var outputDiv=document.createElement('DIV');
    var pos=getElmPosition(mainElm);
    var size=getElmSize(mainElm);    
            
              
    var outputDivId=id+'-second';
    outputDiv.className=mainElm.className ;
    outputDiv.id=outputDivId ;
    if(divUnderParent)
    {
    	mainElm.parentNode.appendChild(outputDiv);
        //alert('under parent');
    }
    else
    {
        var cssProps = 'position:absolute;overflow:hidden;display:block;z-index:5;width:'+size.x+'px;height:'+size.y+'px;top:'+(pos.y)+'px;left:'+(pos.x)+'px;';         
        outputDiv.style.cssText =   cssProps ;             
        document.body.appendChild(outputDiv);
    } 
   outputDiv.style.display='none';
   
    allFaders[id]['secondElmId']=outputDivId;    
    allFaders[id]['secondElm']=outputDiv;    
    allFaders[id]['frameDelay']=frameDelay;    
    allFaders[id]['stepSize']=stepSize;    
    allFaders[id]['stepDelay']=stepDelay;    
    allFaders[id]['elm']=mainElm;    
    allFaders[id]['status']=1;    
    allFaders[id]['count']=totalCount;    
    allFaders[id]['actual']=1;
    allFaders[id]['running']=false;
    allFaders[id]['ajax']=ajaxFile;
    var i;
    allFaders[id]['buttons']=new Array();
    for(i=1;i<=allFaders[id]['count'];i++)
        allFaders[id]['buttons'][i]=gE(id+'-'+i);
        
    return allFaders[id];
    
}


                    
