var numart = 5;                  
var hpatosel = 1;
// id, title, subtitle, img
var hpartArr = new Array(numart);

var delay = 5000                 
var timerID = null
                    
var startDateTime = "June 11 2010, 17:00:00";

function init(){
    setSel(hpatosel);    
}    
    
function setSel(tosel){
    // deselect all articles
    deselectAll();
    
    // set current article for selection
    hpatosel = tosel;
    setArticleSel();   
    
    // calculate next article for selection
    hpatosel = (numart == tosel) ? 1 : tosel+1;
    
    // call the function again                        
    if(null != timerID) clearTimeout(timerID);
    timerID = setTimeout(setSel, delay, hpatosel);
}    
    
function deselectAll(){
    for(var i=1; i<=numart; i++){
        document.getElementById("hpa" + i).className = "";
    }
}    
    
function setArticleSel(){ 
    // set selected in links
    document.getElementById("hpa" + hpatosel).className = "sel";
    
    // change picture
    document.getElementById("hpaimg").style.backgroundImage = "url(" + hpartArr[hpatosel][3] + ")";
    
    // change title
    hpatext = document.getElementById("hpatext");
    hpatext.innerHTML = "<a href='/news/" + hpartArr[hpatosel][0] + "' title='" + hpartArr[hpatosel][1] + "'>" +
                            "<span class='hpatitle'>" + hpartArr[hpatosel][1] + "</span>" + 
                            "<br/><br/><span class='hpasubtitle'>" + hpartArr[hpatosel][2] + "</span>" +
                        "</a>";                            
}

function login(toshow){
    document.getElementById("lf").style.display = toshow ? "block" : "none";
}

function checkElement(id, msg, istext){
    el = document.getElementById(id);
    
    value = (istext) ? el.value.trim() : parseInt(el.value);
    
    if(!value){
        alert(msg);
        el.style.borderColor = '#AC0000';
        el.focus();
        return false;
    }
    else {
        el.style.borderColor = '#CCC';
    }
    return true;
}

function checkreg(){
    
    if(!checkElement('name', 'Моля, въведете потребителско име!', true)) return false;
    if(!checkElement('pass', 'Моля, въведете парола!', true)) return false;
    if(!checkElement('realname', 'Моля, въведете име и фамилия!', true)) return false;
    if(!checkElement('email', 'Моля, въведете e-mail адрес!', true)) return false;
    
    if(!checkElement('year', 'Моля, посочете годината си на раждане!', false)) return false;
    if(!checkElement('region', 'Моля, посочете местоположение!', false)) return false;
    if(!checkElement('education', 'Моля, посочете образованието си!', false)) return false;
    
    if(!checkElement('termsOK', 'Регистрацията изисква съгласяване с общите условия', true)) return false;
    
    return true;
}

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function startCountDown(){
	var tDate = new Date(startDateTime);
	var nDate = new Date();
	var tDiff = (tDate.getTime() - nDate.getTime()) / 1000;
	
	// get days -> 1 day = 24 * 60 * 60 = 86400 seconds
	var days = Math.floor(tDiff / 86400);
	days = days < 10 ? "0" + days : days;
	days = new String(days);
	
	// get hours -> 1 hour = 60 * 60 = 3600 seconds
	var hours = Math.floor((tDiff - days * 86400) / 3600);
	hours = hours < 10 ? "0" + hours : hours;
	hours = new String(hours);
	
	// get minutes -> 1 minute = 60 seconds
	var minutes = Math.floor((tDiff - days * 86400 - hours * 3600) / 60);
	minutes = minutes < 10 ? "0" + minutes : minutes;
	minutes = new String(minutes);
	
	// get seconds
	var seconds = Math.floor(tDiff - days * 86400 - hours * 3600 - minutes * 60);
	seconds = seconds < 10 ? "0" + seconds : seconds;
	seconds = new String(seconds);
	
	document.getElementById("d1").className = "num" + days.charAt(0);
	document.getElementById("d2").className = "num" + days.charAt(1);
	
	document.getElementById("h1").className = "num" + hours.charAt(0);
	document.getElementById("h2").className = "num" + hours.charAt(1);
	
	document.getElementById("m1").className = "num" + minutes.charAt(0);
	document.getElementById("m2").className = "num" + minutes.charAt(1);
	
	document.getElementById("s1").className = "num" + seconds.charAt(0);
	document.getElementById("s2").className = "num" + seconds.charAt(1);
	
	setTimeout("startCountDown()", 500);
}

