/******************************
 *  linkuri externe
 ******************************/

function fixExternalLinks(element) {
    if(element == null)
        element = document;
    
    var child = element.firstChild;
    while(child) {
        if(child.nodeType == 1) {
            fixExternalLinks(child);
            if(child.nodeName == "A" && child.getAttribute("rel") == "certsignExternalLink") {
                child.target = "_blank";
            }
        }
        child = child.nextSibling;
    }
}

fixExternalLinks();

/******************************
 *  PASTILE: initializare
 ******************************/

var visiblePastila = null;
var initialPastila = null;
var pastileUsingAnchor = false;
if(window.pastileForceAnchor)
    pastileUsingAnchor = pastileForceAnchor;

if(location.hash.indexOf("#Q") > -1) {
    pastileUsingAnchor = true;
    //var nume = location.hash.substr(2);
    var nume = location.hash.substring(2,location.hash.indexOf("-"));
    initialPastila = "pastila_rosie_" + nume;
    }

if(window.pastileRegister)
for(var c = 0; c < pastileRegister.length; c++) {
    var pastilaName = pastileRegister[c];
    var autoScroll = true;
    
    if(initialPastila == null) {
        initialPastila = pastilaName;
        autoScroll = false;
    }
    
    if(pastilaName == initialPastila)
        pastilaInitialOpen(pastilaName, autoScroll);
}

function pastilaInitialOpen(pastilaName, autoScroll) {
    setTimeout(function() {
        var title = document.getElementById(pastilaName + "_title");
        pastilaClick(title.parentNode);
        if(autoScroll)
            window.scrollTo(0, title.offsetTop);
    }, 0);
}

function pastilaOnLoad(pastilaName) {
}

/******************************
 *  PASTILE: handlere
 ******************************/

function pastilaClick(title) {
    var pastila = title.parentNode.parentNode;
    if(visiblePastila == pastila.id) {
        visiblePastila = null;
        makePastilaSmall(pastila);
        return;
    }
    
    if(visiblePastila)
        makePastilaSmall(document.getElementById(visiblePastila));
    
    visiblePastila = pastila.id;
    if(pastileUsingAnchor)
    {
        //location.hash = "Q" + visiblePastila.substr(14);
	if (visiblePastila.substr(14) == "1")
	    location.hash = "Q1-certificat-digital";
	if (visiblePastila.substr(14) == "2")
	    location.hash = "Q2-pasi-obtinere-certificat-digital";
	if (visiblePastila.substr(14) == "3")
	    location.hash = "Q3-utilizare-certificat-digital";
	if (visiblePastila.substr(14) == "4")
	    location.hash = "Q4-certificat-calificat";
	if (visiblePastila.substr(14) == "5")
	    location.hash = "Q5-obtinere-certificat-calificat";
	if (visiblePastila.substr(14) == "6")
	    location.hash = "Q6-semnatura-electronica";
	if (visiblePastila.substr(14) == "7")
	    location.hash = "Q7-semnatura-electronica-extinsa";
	if (visiblePastila.substr(14) == "8")
	    location.hash = "Q8-furnizor-servicii-certificare-acreditat";
	if (visiblePastila.substr(14) == "9")
	    location.hash = "Q9-dispozitiv-criptografic";
	if (visiblePastila.substr(14) == "10")
	    location.hash = "Q10-valabilitate-certificat-digital";
	if (visiblePastila.substr(14) == "11")
	    location.hash = "Q11-certificat-ssl";
	if (visiblePastila.substr(14) == "12")
	    location.hash = "Q12-utilizare-certificat-ssl";
	if (visiblePastila.substr(14) == "13")
	    location.hash = "Q13-documente-semnate-electronic";
	if (visiblePastila.substr(14) == "14")
	    location.hash = "Q14-criptare-documente-electronice";
	if (visiblePastila.substr(14) == "15")
	    location.hash = "Q15-marca-temporala";
	if (visiblePastila.substr(14) == "16")
	    location.hash = "Q16-utilizare-marca-temporala";
	if (visiblePastila.substr(14) == "17")
	    location.hash = "Q17-pki";
	if (visiblePastila.substr(14) == "18")
	    location.hash = "Q18-autoritate-de-certificare";
	if (visiblePastila.substr(14) == "19")
	    location.hash = "Q19-lant-de-incredere";
	if (visiblePastila.substr(14) == "20")
	    location.hash = "Q20-certificat-semnare-cod";
    }
    makePastilaBig(pastila);
}

function pastilaMouseOver(element) {
    var pastila = element.parentNode.parentNode;
    makePastilaMouseOver(pastila);
}

function pastilaMouseOut(element) {
    var pastila = element.parentNode.parentNode;
    makePastilaMouseOut(pastila);
}

function goTo(address) {
    window.location = address;
}

function getPastilaSub(pastila) {
    return document.getElementById(pastila.id + "_sub");
}

function getPastilaTitle(pastila) {
    return document.getElementById(pastila.id + "_title");
}

function getPastilaTitleImg(pastila) {
    return document.getElementById(pastila.id + "_img");
}

function getPastilaTitleImgRed(pastila) {
    return document.getElementById(pastila.id + "_img_red");
}


/******************************
 *  PASTILE: vizual
 ******************************/

function makePastilaBig(pastila) {
    getPastilaSub(pastila).style.display = "block";
    //getPastilaTitle(pastila).className = "rosu"
    getPastilaTitleImg(pastila).style.display = "none";
    getPastilaTitleImgRed(pastila).style.display = "inline";
    pastila.className="red";
}

function makePastilaSmall(pastila) {
    getPastilaSub(pastila).style.display = "none";
    //getPastilaTitle(pastila).className = "gri"
    getPastilaTitleImg(pastila).style.display = "inline";
    getPastilaTitleImgRed(pastila).style.display = "none";
    pastila.className="grey";
}

function makePastilaMouseOver(pastila) {
    getPastilaTitle(pastila).style.textDecoration = "underline";
}

function makePastilaMouseOut(pastila) {
    getPastilaTitle(pastila).style.textDecoration = "none";
}



/******************************
 *  CONTACT
 ******************************/

function emailValid(email) {
    var a = email.indexOf("@");
    if(a < 0)
        return false;
    email = email.substr(a+1);
    var b = email.indexOf(".");
    if(b < 1 || b == email.length-1)
        return false;
    return true;
}

var contactForm = document.getElementById("contact-mail-page");
if(contactForm) contactForm.onsubmit = function() {
    var fields = [ "edit-name", "edit-mail", "edit-subject", "edit-message" ];
    for(var c = 0; c < fields.length; c++) {
        var value = document.getElementById(fields[c]).value;
        if(! value.length > 0) {
            alert("Vă rugăm completaţi toate câmpurile!");
            return false;
        }
    }
    if(! emailValid(document.getElementById("edit-mail").value)) {
        alert("Adresa de e-mail introdusă nu este validă!");
        return false;
    }
    return true;
}


var newsForm = document.getElementById("simplenews-subscription-manager-form");
if(newsForm) newsForm.onsubmit = function() {
    var email = document.getElementById("edit-mail").value;
    if(! email.length > 0) {
        alert("Vă rugăm completaţi adresa de e-mail!");
        return false;
    }
    if(! emailValid(email)) {
        alert("Adresa de e-mail introdusă nu este validă!");
        return false;
    }
    return true;
}

/******************************
 *  Galerie foto
 ******************************/

var galerieList;
var galerieListOffset;
var galerieListY = 0;
var galerieListYMax;
var galerieListIndex = [];
var galeriePozaMare;
var galeriePozaId = 0;
var galerieListTimer;
var galerieButtons = {};

galerieInit();

function galerieInit() {
    galerieList = document.getElementById("galerie_list");
    if(! galerieList) return;
    
    galeriePozaMare = document.getElementById("gallery_poza_mare");
    
    galerieBlessButton("galerie_scroll_up", function() {
            galerieScrollList("up");
    }, false, "galerie_scroll_up_img");
    galerieBlessButton("galerie_scroll_dn", function() {
            galerieScrollList("dn");
    }, true, "galerie_scroll_dn_img");
    
    galerieBlessButton("galerie_poza_prev", function() {
            galeriePrevNextImage("prev");
    }, false);
    galerieBlessButton("galerie_poza_next", function() {
            galeriePrevNextImage("next");
    }, true);
    
    galerieWalkList(function(element) {
            var id = galerieListIndex.length;
            var link = element.getAttribute("src");
            var linkExtension = link.substr(link.lastIndexOf("."));
            var linkBig = link.substr(0, link.length - 10 - linkExtension.length) + linkExtension;
            link = link.substr(0, link.length - 10 - linkExtension.length) + ".preview" + linkExtension;
            var title = element.getAttribute("title");
            galerieListIndex[id] = {
                    'link' : link,
                    'linkBig' : linkBig,
                    'title' : title
            }
            element.parentNode.parentNode.onmousedown = function(event) {
                    galerieSelectImage(id);
            }
    });
    
    galerieListOffset = galerieGetItem(0).offsetTop;
    galerieListYMax = galerieListIndex.length - 4;
    
    document.getElementById("galerie_link_big").target = "_blank";
}

function galerieBlessButton(elementName, action, enabled, imageName) {
    var element = document.getElementById(elementName);
    var image = document.getElementById(imageName);
    
    galerieButtons[elementName] = { 'isEnabled':true, 'image':image, 'element':element };
    galerieButtons[elementName]['enable'] = function(enable) {
        if(enable == this.isEnabled)
            return;
        this.isEnabled = enable;
        
        if(! enable)
            this.element.style.textDecoration = "none";
        
        this.element.style.color = enable ? "" : "#ccc";
        this.element.style.cursor = enable ? "" : "default";
        if(this.image)
            this.image.style.display = enable ? "inline" : "none";
    }
    
    if(! enabled)
        galerieButtons[elementName].enable(false);
    
    element.onmouseover = function() {
        if(! galerieButtons[elementName]['isEnabled']) return;
        element.style.textDecoration = "underline";
    }
    element.onmouseout = function() {
        if(! galerieButtons[elementName]['isEnabled']) return;
        element.style.textDecoration = "none";
    }
    element.onmousedown = function(e) {
        try { e.stopPropagation(); e.preventDefault(); } catch(ex) {}
        if(! galerieButtons[elementName]['isEnabled']) return;
        action();
    }
}

function galerieEnableButton(elementName, enabled) {
    galerieButtons[elementName]['isEnabled'] = enabled;
}

function galeriePrevNextImage(dir) {
    if(dir == "prev") {
        galerieSelectImage(galeriePozaId - 1);
    }
    if(dir == "next") {
        galerieSelectImage(galeriePozaId + 1);
    }
}

function galerieSelectImage(id) {
    var first = false; last = false;
    if(id <= 0) {
        id = 0;
        first = true;
    }
    if(id >= galerieListIndex.length - 1) {
        id = galerieListIndex.length-1;
        last = true;
    }
    if(id == galeriePozaId)
        return;
    
    galerieButtons["galerie_poza_prev"].enable(! first);
    galerieButtons["galerie_poza_next"].enable(! last);
    
    galerieGetItem(galeriePozaId).className = "";
    galeriePozaId = id;
    galerieGetItem(galeriePozaId).className = "activ";
    
    document.getElementById("galerie_titlu_poza").innerHTML = galerieListIndex[galeriePozaId]['title'];
    document.getElementById("galerie_link_big").setAttribute("href", galerieListIndex[galeriePozaId]['linkBig']);
    
    galeriePozaMare.src = galerieListIndex[galeriePozaId]['link'];
    galerieScrollListToShow(galeriePozaId);
}

function galerieGetItem(id) {
    var li = galerieList.firstChild;
    var c = 0;
    while(li) {
        if(li.nodeType == 1 && li.tagName == "LI") {
            if(id == c)
                return li;
            c++;
        }
        li = li.nextSibling;
    }
    return null;
}

function galerieWalkList(callback) {
    var li = galerieList.firstChild;
    while(li) {
        if(li.nodeType == 1 && li.tagName == "LI") {
            var div = li.firstChild;
            while(div) {
                if(div.nodeType == 1 && div.tagName == "DIV") {
                    var img = div.firstChild;
                    while(img) {
                        if(img.nodeType == 1 && img.nodeName == "IMG")
                            callback(img);
                        img = img.nextSibling;
                    }
                }
                div = div.nextSibling;
            }
        }
        li = li.nextSibling;
    }
}

function galerieScrollList(direction) {
    var y = galerieListY;
    
    if(direction == "up")
        y -= 3;
    if(direction == "dn")
        y += 3;
    
    galerieScroll(y);
}

function galerieScrollListToShow(id) {
    if(galerieListY > id)
        galerieScroll(id);
    else if(galerieListY < id - 3)
        galerieScroll(id - 3);
}

function galerieScroll(y) {
    var top=false, bottom=false;
    if(y <= 0) {
        y = 0;
        top = true;
    }
    if(y >= galerieListYMax) {
        y = galerieListYMax;
        bottom = true;
    }
    if(y == galerieListY) return;
    galerieListY = y;
    
    galerieButtons["galerie_scroll_up"].enable(! top);
    galerieButtons["galerie_scroll_dn"].enable(! bottom);
    
    galerieSmoothScroll(galerieGetItem(galerieListY).offsetTop);
}

function galerieSmoothScroll(y) {
    var count = 30;
    var increment = (y - galerieListOffset) / count;
    
    clearTimeout(galerieListTimer);
    galerieSmoothScrollLoop(increment, count);
}

function galerieSmoothScrollLoop(increment, count) {
    var delay = 10;
    
    galerieListOffset += increment;
    galerieList.scrollTop = galerieListOffset;
    if(count > 1)
        galerieListTimer = setTimeout(function() { galerieSmoothScrollLoop(increment, count-1) }, delay);
}

