var SLIDE_SHOW_INTERVAL = 2500;


function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}


window.onload = function() {
  externalLinks();
  
  if (document.getElementById("submit")) {
	document.getElementById("submit").onclick = function() {
		document.getElementById("login-form").submit();
		return false;
	}
  }
  
  if (document.getElementById("tikrink-bilieta")) {
	document.getElementById("tikrink-bilieta").onclick = function() {
		document.getElementById("tikrinimo-forma").submit();
		return false;
	}
  }
  
  if (document.getElementById("pirk-internetu")) {
	document.getElementById("pirk-internetu").onclick = function() {
		document.getElementById("pirkimo-forma").submit();
		return false;
	}
  }
  if (document.getElementById("pirk-internetu-2")) {
	document.getElementById("pirk-internetu-2").onclick = function() {
		document.getElementById("pirkimo-forma-2").submit();
		return false;
	}
  }
  if (document.getElementById("pirk-internetu-3")) {
	document.getElementById("pirk-internetu-3").onclick = function() {
		document.getElementById("pirkimo-forma-3").submit();
		return false;
	}
  }
  
  // jeigu loto zaidimu puslapis
	if (document.getElementById("loto-bg-1")) {
		selectSlide(1);
		/* preloader, not working in IE6 */
		var lotobg2img = new Image(); 
		lotobg2img.src = "../images/loto-content/slide-2-lt.jpg";
		var lotobg3img = new Image(); 
		lotobg3img.src = "../images/loto-content/slide-3-lt.jpg";
		var lotobg4img = new Image(); 
		lotobg4img.src = "../images/loto-content/slide-4-lt.jpg";
		/*var lotobg5img = new Image(); 
		lotobg5img.src = "images/loto-content/slide-5.png";*/
		
		/* on mouse over/out */
		var lotobg1 = document.getElementById("loto-bg-1");
		lotobg1.onmouseover = function() {
			selectSlide(1);
			clearInterval(slideShowInterval);
		}
		lotobg1.onmouseout = function() {
			slideShowInterval = setInterval("slideShow()", SLIDE_SHOW_INTERVAL);
		}
		
		var lotobg2 = document.getElementById("loto-bg-2");
		lotobg2.onmouseover = function() {
			selectSlide(2);
			clearInterval(slideShowInterval);
		}
		lotobg2.onmouseout = function() {
			slideShowInterval = setInterval("slideShow()", SLIDE_SHOW_INTERVAL);
		}
		
		var lotobg3 = document.getElementById("loto-bg-3");
		lotobg3.onmouseover = function() {
			selectSlide(3);
			clearInterval(slideShowInterval);
		}
		lotobg3.onmouseout = function() {
			slideShowInterval = setInterval("slideShow()", SLIDE_SHOW_INTERVAL);
		}
		
		var lotobg4 = document.getElementById("loto-bg-4");
		lotobg4.onmouseover = function() {
			selectSlide(4);
			clearInterval(slideShowInterval);
		}
		lotobg4.onmouseout = function() {
			slideShowInterval = setInterval("slideShow()", SLIDE_SHOW_INTERVAL);
		}
		/*
		var lotobg5 = document.getElementById("loto-bg-5");
		lotobg5.onmouseover = function() {
			selectSlide(5);
			clearInterval(slideShowInterval);
		}
		lotobg5.onmouseout = function() {
			slideShowInterval = setInterval("slideShow()", SLIDE_SHOW_INTERVAL);
		}*/
		
		/* slide show */
		var slideShowInterval = setInterval("slideShow()", SLIDE_SHOW_INTERVAL);
	}
	
	/* jeigu loto zaidimu puslapis IR jame neuzpildyta login forma */
	var lotoZaidimaiPrisijunk = document.getElementById("loto-zaidimai-prisijunk");
	var username = document.getElementById("username");
	
	if (lotoZaidimaiPrisijunk && username) {
		lotoZaidimaiPrisijunk.onclick = function() {
			username.focus();
		}
	}  
}

function slideShow() {
	if (currentSlide != 4)
		selectSlide(currentSlide + 1);
	else
		selectSlide(1);
}

var currentSlide = 1;

/* unselect old "currentSlide", select new "index" slide */
function selectSlide(index) {
	document.getElementById("loto-bg-" + currentSlide).className = "";
	document.getElementById("loto-bg-" + index).className = "selected";
	document.getElementById("loto-zaidimai-content").style.backgroundImage = "url(../images/loto-content/slide-" + index + "-lt.jpg)";
	
	currentSlide = index;
}



/* SPORTO LOTERIJOS */


function changeSize(id) {
	var item = document.getElementById(id)
	
	if (item.intervalWorking)
		clearInterval(item.interval)
	else if (!item.enlarged)
		item.count = 0	// 0..9
	else
		item.count = 9	// 0..9
	
	if (!item.enlarged) {
		item.enlarged = true
		item.intervalWorking = true;
		item.interval = setInterval(enlargeStep, 40);
	}
	else {
		item.enlarged = false
		item.intervalWorking = true;
		item.interval = setInterval(reduceStep, 40);
		document.getElementById(id + "-bottom").style.display = "block"
	}
	
	function enlargeStep() {
		item.style.height = (267 + item.count * 20) + "px"
		item.count++
		
		if (item.count > 9) {
			clearInterval(item.interval)
			item.intervalWorking = false
			document.getElementById(id + "-bottom").style.display = "none"
		}
	}
	function reduceStep() {
		item.style.height = (267 + item.count * 20) + "px"
		item.count--
		
		if (item.count < 0) {
			clearInterval(item.interval)
			item.intervalWorking = false
		}
	}
}