/*
    (c) UAB Edukacines Sistemos 2009
*/

var time = 10;
var gal_imgs;
var gal_poz = 0;
var gal_plotis = 800;

function GalleryShow(num)
{
    var x = document.body.offsetWidth;
    var y = document.body.offsetHeight;
    var gal_poz_x = 200;
    var gal_poz_y = 60;
    if(y < 400)
        gal_plotis = 600;
    if(x > 40 + gal_plotis)
        gal_poz_x = (x - gal_plotis - 40) / 2;
    else
        gal_poz_x = 0;
    
    var obj = document.getElementById("galerija");
    obj.innerHTML = "<div style='position:absolute; left:" + gal_poz_x + "px; top:" + gal_poz_y + "px;'>" + GautiHttp("/gallery.htm") + "</div>";
    gal_imgs = GautiHttp("/gallery.aspx?id=" + num).split(";");
    gal_poz = 0;
    if(gal_imgs.length > 0){
        GalleryShift(0);
        GalleryImg(0);
    }
    window.scrollTo(0, 0);
}

function GalleryHide()
{
	document.getElementById("galerija").innerHTML = "";
}

function GalleryShift(kiek)
{
    gal_poz += kiek;
    if(gal_poz > gal_imgs.length - 4)
        gal_poz = gal_imgs.length - 4;
    if(gal_poz < 0)
        gal_poz = 0;
    var rez = "";
    for(var i = gal_poz; i < gal_poz + 4 && i < gal_imgs.length; i++)
        rez += "<a href='javascript:GalleryImg(" + i + ");'><img src='/gallery/" + gal_imgs[i].split(",")[1] + "' height=90px style='padding:5px; border:0px;' /></a>";
    document.getElementById("gal_sar").innerHTML = rez;
}

function GalleryImg(num)
{
    document.getElementById("gal_img").innerHTML = "<div align='center'><img id='paveiksliukas' src='/gallery/" + gal_imgs[num].split(",")[0] + "' onLoad='GalleryResize()' /></div>";
}

function GalleryResize()
{
    var pav = document.getElementById("paveiksliukas");
    if(pav.width > gal_plotis)
        pav.width = gal_plotis;
}

function GautiHttp(reqStr)
{
    var request;
    if (window.XMLHttpRequest)
        request = new XMLHttpRequest();
    else if (window.ActiveXObject)
        request = new ActiveXObject("Microsoft.XMLHTTP");
    else
        return "";
    request.open("GET", reqStr, false);
    request.setRequestHeader("User-Agent", navigator.userAgent);
    request.send(null);
    if (request.status==200)
        return request.responseText;
    else
        return "";
}

function StartTimer(_time, _time_max)
{
    time = _time_max - _time + 1;
    UpdateTimer();
}

function UpdateTimer()
{
    --time;
    var min = parseInt(time / 60);
    if(min < 10)
        min = "0" + min;
    
    var sek = time % 60;
    if(sek < 10)
        sek = "0" + sek;
    
    document.getElementById('timer').innerHTML = min + ":" + sek;
    if(time <= 0)
        window.location.reload();
    else
        setTimeout(UpdateTimer, 1000);
}
function countdown_clock(year, month, day, hour, minute, format)
{
	//I chose a div as the container for the timer, but
	//it can be an input tag inside a form, or anything
	//who's displayed content can be changed through
	//client-side scripting.
	html_code = '<div id="countdown"></div>';

	document.write(html_code);

	countdown(year, month, day, hour, minute, format);                
}
         
function countdown(year, month, day, hour, minute, format)
{
	Today = new Date();
	Todays_Year = Today.getFullYear() - 2000;
	Todays_Month = Today.getMonth();                  

	//Convert both today's date and the target date into miliseconds.                           
	Todays_Date = (new Date(Todays_Year, Todays_Month, Today.getDate(), 
						 Today.getHours(), Today.getMinutes(), Today.getSeconds())).getTime();                                 
	Target_Date = (new Date(year, month - 1, day, hour, minute, 00)).getTime();                  

	//Find their difference, and convert that into seconds.                  
	Time_Left = Math.round((Target_Date - Todays_Date) / 1000);

	if(Time_Left < 0)
	Time_Left = 0;

	switch(format)
	   {
	   case 0:
			//The simplest way to display the time left.
			document.all.countdown.innerHTML = Time_Left + ' seconds';
			break;
	   case 1:
			//More datailed.
			days = Math.floor(Time_Left / (60 * 60 * 24));
			Time_Left %= (60 * 60 * 24);
			hours = Math.floor(Time_Left / (60 * 60));
			Time_Left %= (60 * 60);
			minutes = Math.floor(Time_Left / 60);
			Time_Left %= 60;
			seconds = Time_Left;
			
			dps = 's'; hps = 's'; mps = 's'; sps = 's';
			//ps is short for plural suffix.
			if(days == 1) dps ='';
			if(hours == 1) hps ='';
			if(minutes == 1) mps ='';
			if(seconds == 1) sps ='';
			
			document.all.countdown.innerHTML = '<span class="days">'+ days + '</span><br><font class="text" style="font-size:10px;">';
			document.all.countdown.innerHTML += hours + ':' + minutes + ':' + seconds +'</font>';

			break;
	   default: 
			document.all.countdown.innerHTML = Time_Left + ' seconds';
	   }
	   
	//Recursive call, keeps the clock ticking.
	setTimeout('countdown(' + year + ',' + month + ',' + day + ',' + hour + ',' + minute + ',' + format + ');', 1000);
}
