<!-- hide
var speed = 15;
var y = 1;
var x = speed + y;
var time = x - y;
var now;
campicture = new Image();
var autoCycleIdx = 0;
var autoCycleIdx2 = 0;
var autoCycle = false;
var imgCounter = 0;
var acc = null;

var zeroImg = new Image(); zeroImg.src = "images/zero.gif";
var waitIco = new Image(); waitIco.src = "images/tiny_red.gif";
var loadIco = new Image(); loadIco.src = "images/ajax-loader.gif";


var imgPlay = new Image();imgPlay.src = "images/player_controls.gif";
var imgPause = new Image();imgPause.src = "images/pause.gif";

var imgPlayInner = new Image();imgPlayInner.src = "images/inner_player_controls.gif";
var imgPauseInner = new Image();imgPauseInner.src = "images/pause_inner.gif";

var historyLength = 8;
var preloadHistoryObj = new Array();

var g_iStep = 0;

var  t=0;
var t2=0;

var hours = 0;
var mins = 0;
var secs = 0;

var imageTime = 0;
var imageTimeObj = new Array(2);

var imageHistoryTimeObj = new Array();
var queryUrlTmp = new Array();
var HistoryUrl = new Array();

var loc = "";



function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}

function debug(txt) {
	if(document.getElementById("debug")) {
		document.getElementById("debug").innerHTML = txt;
	}
	else {
		  debugObj = document.body.appendChild(document.createElement('div'));
          debugObj.className = 'debug';
          debugObj.id = 'debug';
		  debugObj.innerHTML = txt;
	}
}

function stopClock() {
  x = "off";
  document.forms[0].clock.value = x;
}

function startAutoCycleClock() {
	if (autoCycle) {
		autoCycleIdx--;
		autoCycleIdx2++;
		if (autoCycleIdx>=0 && preloadHistoryObj.length>0) {
			ShowHistoryImg( autoCycleIdx );
			autoCycle = getElement('autocycle').checked;
			s.setValue(autoCycleIdx2-8);
			acc = setTimeout("startAutoCycleClock()", 1000);
		} else {
			clearTimeout(acc);acc=null;
			getElement('autocycle').checked = false;
			autoCycle = false;
			x=speed + y
			timerID2 = setTimeout("startClock()", 1000);
			document.getElementById('playerControls').src = imgPlay.src;
			s.setValue(0);
		}
	}
}

function startClock() {
t += 3;

     if (x != "off" && !autoCycle) {
        x--;
        
        document.forms[0].clock.value = x;
        if (x ==0) {
           //document.getElementById("loadStatus").style.backgroundImage = "url("+waitIco.src+")";
           timerID = setTimeout("reload()", 1000);
        }
        document.getElementById("playerScale2").style.backgroundPosition = -t;
        timerID2 = setTimeout("startClock()", 1000);
     }

}


function OnImgUpdate( iProgress, m_aImages ) {   
TurnOff();
   if( (iProgress >= 0) && (iProgress <= 10) && (iProgress > g_iStep) )
   {   
       var oSpan = document.getElementById( "playerHistoryProgress");
		
	   newWidth = parseInt((g_iStep)*(document.getElementById("playerScale").offsetWidth/28)) + "px";
       //if(g_iStep>4) {
	   oSpan.style.width = newWidth;
	   //}
	   preloadHistoryObj[g_iStep].src = m_aImages[g_iStep].src;
	   g_iStep++;
	   
	   if(g_iStep==2) {
			ShowHistoryImg(0)
			document.getElementById('preloadStatus').innerHTML = "";
			document.getElementById('campicture').className = "campictureLoadDone";
			
	   }
   }
}

function OnCompletion() {
	combinedHistoryXMLQuery.init(queryUrlTmp);   
	g_iStep = 0;
}

function preloadHistory(loc) {
	document.getElementById('campicture').className = "campictureLoad";
	if(!loc) loc=document.getElementById("locationTxt").innerHTML;
	document.getElementById('preloadStatus').innerHTML = "<h2>загрузка камеры<br>\"" + loc + "\"</h2>";
	
	clearTimeout(acc);
	s.setValue(-7);
	getElement('autocycle').checked = false;
	var now = new Date();
	var tmpUrl = document.getElementById('currentCam').value;
	
	
	
	for (l=0;l<=historyLength;l++) {
		HistoryUrl[l] = tmpUrl + "&refresh_time=" + now.getTime()+"&skip_frames="+l;
		preloadHistoryObj[l] = new Image();
		
		queryUrlTmp[l] = ("./service.aspx?action=get_descriptor&skip_frames="+l+"&session_id="+getQueryVariable(document.getElementById('currentCam').value, "session_id"));
    }
	
	var oPreload = new ImagePreload( HistoryUrl, OnImgUpdate, OnCompletion );
}


function animatePlay() {
  t2 += 1;
  document.getElementById("playerScale").style.backgroundPosition = -t2;
  animID3 = setTimeout("animatePlay()", 400);
}

function reload() {
imageTimeObj = loadXMLDoc("./service.aspx?action=get_descriptor&session_id="+getQueryVariable(document.getElementById('currentCam').value, "session_id"));

		now = new Date();
		tmpCampicture = document.getElementById('currentCam').value;
		var camImg = tmpCampicture + "&refresh_time=" + now.getTime();
		clearTimeout(timerID);timerID=null;
		
	if(imageTimeObj[1] && imageTimeObj[0]>imageTimeObj[1]) {
		cc = new Image(320,240);
		cc.src = camImg;

		cc.onload = function() {
			document.getElementById('campicture').src = cc.src;
			IncImgCounter();
		
			x = speed;
			document.forms[0].clock.value = x;
			//document.getElementById("loadStatus").style.backgroundImage = "url("+zeroImg.src+")";
			
			imageHistoryTimeObj.pop();
			imageHistoryTimeObj.unshift(imageTimeObj[0]);
			
			preloadHistoryObj.pop();
			preloadHistoryObj.unshift(cc);
			
			buildPlayerTimes();
		}
	}
	else {
		x = speed;
		document.forms[0].clock.value = x;
		//document.getElementById("loadStatus").style.backgroundImage = "url("+zeroImg.src+")";
	}
}

function IncImgCounter() {
  imgCounter++;
  secs++;

  now = new Date(imageTimeObj[0]);

  day = now.getDate(); day<=9?preDay="0":preDay="";
  month = now.getMonth()+1; month<=9?preMonth="0":preMonth="";
  year = now.getFullYear();
  
  nowDate = preDay+day + "." + preMonth+month + "." + year;
  
  hours = now.getHours();
  mins  = now.getMinutes();
  secs  = now.getSeconds();

  hours<=9?pre1="0":pre1="";
  mins<=9?pre2="0":pre2="";
  secs<=9?pre3="0":pre3="";
	
	getElement('imgCounterDiv').innerHTML = nowDate + "&nbsp;" + pre1+hours+":"+pre2+mins+":"+pre3+secs;
}

function ShowHistoryImg(idx) {
	now = new Date(imageHistoryTimeObj[idx]);
	
		day = now.getDate(); day<=9?preDay="0":preDay="";
		month = now.getMonth()+1; month<=9?preMonth="0":preMonth="";
		year = now.getFullYear();
	
		hours = now.getHours();
		mins  = now.getMinutes();
		secs  = now.getSeconds();

		hours<=9?pre1="0":pre1="";
		mins<=9?pre2="0":pre2="";
		secs<=9?pre3="0":pre3="";
		
		nowDate = preDay+day + "." + preMonth+month + "." + year;
  
	tmpCampicture = document.getElementById('currentCam').value;
  
	if(preloadHistoryObj[idx]) {
		document.getElementById('campicture').src = preloadHistoryObj[idx].src;
		if(day) {
			getElement('imgCounterDiv').innerHTML = nowDate + "&nbsp;" + pre1+hours+":"+pre2+mins+":"+pre3+secs;
		}
	}
}

function ChangeSpeed (new_speed) {
	speed = new_speed;
	speed>1?freqTxt="секунд":freqTxt="секунду";
	document.getElementById('cameraFreqSelect').innerHTML = new_speed + " " + freqTxt;
	hideFreqSelect();
}

function RefreshImage () {
	x = 0;
}

function SwitchAutoCycle(chkbox) {
	autoCycle = getElement('autocycle').checked;
	autoCycleIdx = 8;
	autoCycleIdx2 = 0;
	if (autoCycle) {
		startAutoCycleClock();
	}
}

function TurnOn() {
autocycle = true;
  if(document.getElementById('autocycle').checked != true) {
       stopClock();
       document.getElementById('autocycle').checked = true;
       document.getElementById('playerControls').src = imgPause.src;
       SwitchAutoCycle(autocycle);
  }
  else {
       x = parseInt(document.getElementById('cameraFreqSelect').innerHTML);
       document.forms[0].clock.value = x;
       
       getElement('autocycle').checked = false;
       document.getElementById('playerControls').src = imgPlay.src;
       SwitchAutoCycle(false);
       startClock();
  }
}

function TurnOff() {
  autocycle = false;
  stopClock();
  getElement('autocycle').checked = false;
  document.getElementById('playerControls').src = imgPlay.src;
}

function showFreqSelect() {
     it = document.getElementById('cameraFreqMenu');
     it.style.display = "block"
}

function hideFreqSelect() {
     it = document.getElementById('cameraFreqMenu');
     it.style.display = "none"
}

function loadXMLDoc(url) {
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
		    if (req.status == 200) {
				response = req.responseXML.documentElement;
				imageTime = response.getElementsByTagName('time')[0].firstChild.data;
		    } else {
		        //debug("There was a problem retrieving the XML data:\n" + req.statusText);
		    }
		}
	}
    req.open("GET", url, true);
    req.send(null);
	
	imageTimeObj.pop();
	imageTimeObj.unshift(imageTime);
	return imageTimeObj;
}



combinedHistoryXMLQuery = {
    init: function(urlArray) {
        theContent = "";
        combinedHistoryXMLQuery.requestWrapper(urlArray, theContent);
    },
        
    requestWrapper: function(urlArray, theContent) {
        requestObject = makeRequestObject();
        requestObject.onreadystatechange = processRequest;
                
        var url = urlArray[0];
                
        requestObject.open("GET", url, true);
        requestObject.send(null);
                
        function makeRequestObject() {
            if (window.XMLHttpRequest) {
                    return new XMLHttpRequest();
            } else if (window.ActiveXObject) {
                    return new ActiveXObject("Microsoft.XMLHTTP");
            }
        };
            
        function processRequest() {
            if (requestObject.readyState == 4) {
                if (requestObject.status == 200) {
						response = requestObject.responseXML.documentElement;
						if(response.getElementsByTagName('time')[0].firstChild) {
							combinedHistoryXMLQuery.takeText(urlArray, theContent, response.getElementsByTagName('time')[0].firstChild.data);
						}
						else {
							combinedHistoryXMLQuery.takeText(urlArray, theContent, "00:00:00");
						}
                }
            }
        };
    },
        
    takeText: function(urlArray, theContent, responseText) {
        imageHistoryTimeObj[-urlArray.length+9] = responseText;
        urlArray.shift();
        if (urlArray.length > 0) {
				var oSpan = document.getElementById( "playerHistoryProgress");
				if(urlArray.length<5) {
					newWidth = parseInt((-urlArray.length+8)*(document.getElementById("playerScale").offsetWidth/14)) + "px";
					oSpan.style.width = newWidth;
				}
                combinedHistoryXMLQuery.requestWrapper(urlArray, theContent);
        } else {
                combinedHistoryXMLQuery.doAlert(theContent);
        }
    },
    
    doAlert: function(theContent) {
        TurnOn();
		buildPlayerTimes();
    }
}

function buildPlayerTimes() {
	h = "";
	for(tm=1;tm<imageHistoryTimeObj.length;tm++) {
		newL = ((tm-1) * document.getElementById("playerScale").offsetWidth/16);
		tmpTimeObj = new Date(imageHistoryTimeObj[-tm+8]);
		tmpTimeObj.getHours()<=9?pre1="0":pre1="";
		tmpTimeObj.getMinutes()<=9?pre2="0":pre2="";
		tmpTime = pre1 + tmpTimeObj.getHours() + ":" + pre2 + tmpTimeObj.getMinutes();
		if(tm==1 || tm==4 || tm==8) {
			if(tm==1) newL = newL-16;
			h += "<div class='playerTimesItem' style='left:"+newL+"px'>"+tmpTime+"</div>";
		}
	}
	document.getElementById("playerTimes").innerHTML = h;
}

function timestamp2date(timestamp) {
  var theDate = new Date(timestamp * 1000);
  return theDate;
}

function getQueryVariable(str, variable) {
	var query = str.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	}
} 


function showCamPopup(moo, islist) {
	var mb = document.getElementById("popup");
	if(!islist) {
		moo = moo.split("&");
	} else {
		moo = moo + "?size=medium";
		moo = moo.split("?");
	}
	mb.style.display = "block";
	popupImage = new Image();
	popupImage.src = "service.aspx?action=download&" + moo[1] + "&size=medium";
	popupImage.onload = function() {
		document.getElementById("popupImage").src = popupImage.src;
		
	}
}

function hideCamPopup() {
	var mb = document.getElementById("popup");
	mb.style.display = "none";
	document.getElementById("popupImage").src = "images/ajax-loader2.gif";
}

function popupMove(evt, moo, islist) {
	
	if(!islist) {
		moo = moo.split("&");
	} else {
		moo = moo + "?size=medium";
		moo = moo.split("?");
	}
	var x=0, y=0;
	if (document.all) {//IE
		x = (document.documentElement && document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
		y = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
		x += window.event.clientX;
		y += window.event.clientY;
		
	} else {//Good Browsers
		x = evt.pageX;
		y = evt.pageY;
	}
	
	moo = "service.aspx?action=download&" + moo[1] + "&size=medium";
	
	if(moo != document.getElementById("popupImage").src) {
		document.getElementById("popupImage").src = moo;
	}
	document.getElementById("popup").style.left = x-160 + "px";//(x + this.offsetX) + "px";
	document.getElementById("popup").style.top = y + 30 + "px";//(y + this.offsetY) + "px";
}


//end hide -->