﻿    var TimerIDArray = new Array()
    var TimerIDCount = 0;


        var lAuctions;
        var CountDowns;
        var moreAuctions=0;
        var lowestTime = 0;
        
        function formatTime(secondsRest){
			var text='';
			
			calc = secondsRest;
			
			days = Math.floor(calc / 86400);
			calc -= days * 86400;
			
			hours = Math.floor(calc / 3600);
			calc -= hours * 3600;
			
			minutes = Math.floor(calc / 60);
			calc -= minutes * 60;						
						
			if (secondsRest > 0) 
			{
			    if(secondsRest > 300){
				    if (days > 0) {text = text + days + ' d ';}
				    if (hours > 0) {text = text + hours + ' t ';}
				    if (minutes > 0) {text = text + minutes + ' min igjen';}				    
			    }else{
				    if (days > 0) {text = text + days + ' d ';}
				    if (hours > 0) {text = text + hours + ' t ';}
				    if (minutes > 0) {text = text + minutes + ' m ';}
				    text = text + calc + ' sek igjen';
			    }
			}
			else
			{
				text = 'Oppdateres...';
			}			
			return text;
        }
        
		function CountDown(id, secondsRest, TimerId) {
			rest = secondsRest;
			rest--;

			calc = rest;
			
			var text = formatTime(rest);
			
			
			if (document.getElementById("auc_list_prodtid_" + id)) {
				document.getElementById("auc_list_prodtid_" + id).innerHTML = text;
				if(rest<60){
				    document.getElementById("auc_list_prodtid_" + id).className = 'atr';
				}
			}
			
			if (rest > 0) {
			    if(TimerId==0){
			        NewTimerId = TimerIDCount++;
			    }else{
			        NewTimerId = TimerId;
			    }
			    
			    if(rest < 121){			        				    			        
				    TimerIDArray[NewTimerId] = setTimeout("CountDown("+ id + ", " + rest + ", " + NewTimerId + ")", 1000);
				}else{				   
				   TimerIDArray[NewTimerId] = setTimeout("CountDown("+ id + ", " + (parseInt(rest)-60) + ", " + NewTimerId + ")", 60000);
				}
			}
		}
			         
        
        function writeAuctions(Utvalg){
             var mainDiv = document.getElementById('auc_list');
            
            for (i = 0 ; i < TimerIDCount ; i++){
                clearTimeout(TimerIDArray[i]);
            }
            TimerIDArray.length=0;
            TimerIDCount=0;
                 
            mainDiv.innerHTML = lAuctions;            
            for(var c in CountDowns ) {     
                var currCountDown = CountDowns[c];
                CountDown(currCountDown.id, currCountDown.time, 0);
            }             
        }   
                        
        
        function getAuctionList(Utvalg, json){                             
          lAuctions = json.auctions;
          moreAuctions = json.moreAuctions;  
          lowestTime = json.lowestTime;    
          CountDowns = json.CountDowns;               
          if(lAuctions){
            writeAuctions(Utvalg);
          }
        }
        
        function getAuctions(OppdateringsFrekvens, Utvalg){                                               
            $.getJSON("felles/auksjonsliste_hent_v3.aspx?Utvalg=" + Utvalg, function(json){
                getAuctionList(Utvalg, json);
                var updateTime =0;                
                if(lowestTime < OppdateringsFrekvens){
                    if(lowestTime < 11){
                        updateTime = 10;
                    }else{
                        updateTime = lowestTime+1;
                    }                   
                }else{
                    updateTime = OppdateringsFrekvens;
                }                                 
                 setTimeout("getAuctions(" + OppdateringsFrekvens + "," + Utvalg + ");", (parseInt(updateTime)*1000));                                 
            });
        }
        
        function getAuctionsInCategory(OppdateringsFrekvens, Kategori){                                               
            $.getJSON("felles/auksjonsliste_hent_v3.aspx?Kategori=" + Kategori, function(json){
                getAuctionList(0, json);
                var updateTime =0;                
                if(lowestTime < OppdateringsFrekvens){
                    if(lowestTime < 11){
                        updateTime = 10;
                    }else{
                        updateTime = lowestTime+1;
                    }                   
                }else{
                    updateTime = OppdateringsFrekvens;
                }                                 
                 setTimeout("getAuctionsInCategory(" + OppdateringsFrekvens + "," + Kategori + ");", (parseInt(updateTime)*1000));                                 
            });
        }        

        function getAuctionsInProfilterte(OppdateringsFrekvens, pids){                                               
            $.getJSON("felles/auksjonsliste_hent_v3.aspx?pids=" + pids, function(json){
                getAuctionList(0, json);
                var updateTime =0;                
                if(lowestTime < OppdateringsFrekvens){
                    if(lowestTime < 11){
                        updateTime = 10;
                    }else{
                        updateTime = lowestTime+1;
                    }                   
                }else{
                    updateTime = OppdateringsFrekvens;
                }                                 
                 setTimeout("getAuctionsInProfilterte(" + OppdateringsFrekvens + ",'" + pids + "');", (parseInt(updateTime)*1000));                                 
            });
         }

         function getAuctionsInSearch(OppdateringsFrekvens, PST) {
             $.getJSON("felles/auksjonsliste_hent_v3.aspx?pst=" + PST, function (json) {
                 getAuctionList(0, json);
                 var updateTime = 0;
                 if (lowestTime < OppdateringsFrekvens) {
                     if (lowestTime < 11) {
                         updateTime = 10;
                     } else {
                         updateTime = lowestTime + 1;
                     }
                 } else {
                     updateTime = OppdateringsFrekvens;
                 }
                 setTimeout("getAuctionsInSearch(" + OppdateringsFrekvens + ",'" + PST + "');", (parseInt(updateTime) * 1000));
             });
         }
                   
        
        
