﻿function isNull(obj) {
    return (obj == null || obj === "undefined")
}

function addEventHandler(obj, event, func, capture) {
    if (!isNull(obj) && obj.addEventListener) {
        obj.addEventListener(event, func, (isNull(capture)) ? false : capture);
        return true;
    } else if (!isNull(obj) && obj.attachEvent) {
        return obj.attachEvent("on" + event, func);
    }
    return false;
}

function parseEvent(e) {
    var event = window.event || e;
    if (!event) {
        return null;
    }

    if (event.stopPropagation) {
        e.stopPropagation();
    } else {
        event.cancelBubble = true;
    }

    if (event.preventDefault) {
        event.preventDefault();
    }

    if (!event.target) {
        event.target = event.srcElement;
    }

    return event;
}

function imageLoadEventHanler(e) {
    e = parseEvent(e);
    imgsLoaded++;

    var progress = (imgsLoaded * 100) / imgUrls.length;
    progressPreloadImages(progress);
    if (progress >= 100) {
        finishPreloadImages();
    }
}

var imgsLoaded = 0;
var imgUrls = new Array();

function preloadImages() {
    if (document.images) {
        startPreloadImages();
        for (var i = 0; imgUrls[i]; i++) {
            var img = new Image(25, 25);
            img.style.visibility = "hidden";
            img.style.display = "none";
            img.style.width = "25px";
            img.style.height = "25px";
            addEventHandler(img, "load", imageLoadEventHanler);
            img.src = imgUrls[i];
            if (document.body.appendChild) {
                document.body.appendChild(img);
            }
        }
    }
}

function startPreloadImages() {
    window.status = "Cargando imágenes...";
}

function progressPreloadImages(progress) {
    window.status = "Cargando imágenes: " + parseInt(progress) + "%";
}

function finishPreloadImages() {
    window.status = "Listo";
}


function changeSection(section) {
    if (__doPostBack) {
        __doPostBack('ctl00_btnAjax', 'changeSection;' + section);
    }
}

function performSubmit(section) {
    if (section == 3) {
        window.alert("El plazo de inscripción ha finalizado.");
    } else if (section == 444) {
        window.alert("Esta sección se abrirá a lo largo del día.");
    } else if (__doPostBack) {
        __doPostBack('ctl00_btnAjax', 'performSubmit;' + section);
    }
}

function closePopup(section) {
    changeSection(section);
}

function ideaSave() {
    if (__doPostBack) {
        __doPostBack('ctl00_btnAjax', 'ideaSave;4');
    }
}

function ideaSend() {
    if (window.confirm("Se va a confirmar la idea. Después de esta operación no se podrá realizar ninguna modificación.\n\n¿Seguro que quieres confirmar la idea?") &&
        __doPostBack) {
        __doPostBack('ctl00_btnAjax', 'ideaSend;4');
    }
}

function ideaRefresh() {
    if (__doPostBack) {
        __doPostBack('ctl00_btnAjax', 'ideaRefresh;4');
    }
}

addEventHandler(window, "load", function(e) {
    preloadImages();

    var manager = Sys.WebForms.PageRequestManager.getInstance();
    if (manager) {
        manager.add_pageLoaded(function(sender, args) {
            if (!isNull(document.getElementById("btnAjaxUpload")) && !isSent) {
                new AjaxUpload("btnAjaxUpload", {
                    action: "Upload.aspx",
                    onSubmit: function(file, extension) { if (window.confirm("Se va a proceder a subir el archivo siguiente: \n\n" + file + "\n\nNota: Aceptar y esperar a recibir la confirmación de guardado.")) { return true; } else { return false; } },
                    onComplete: function(file, response) { if (response.indexOf("OK") >= 0) { window.alert("El archivo se ha guardado correctamente."); ideaRefresh(); } else { window.alert("Ha ocurrido un error al intentar subir el archivo."); } }
                });
            }
        });
    }
});

addEventHandler(window, "resize", function(e) {
});

imgUrls.push("empty.png");

imgUrls.push("finalistas.png");
imgUrls.push("ganadores.png");
imgUrls.push("finalistas_background.png");

imgUrls.push("background.png");
imgUrls.push("background_puzzle.png");
imgUrls.push("background_puzzle_empty.png");
imgUrls.push("background_puzzle1.png");
imgUrls.push("background_puzzle2.png");
imgUrls.push("background_puzzle3.png");
imgUrls.push("background_puzzle4.png");
imgUrls.push("background_puzzle5.png");
imgUrls.push("background_puzzle6.png");

imgUrls.push("logo_syc.png");
imgUrls.push("admin_login.png");

imgUrls.push("menu.png");
imgUrls.push("menu_empty.png");
imgUrls.push("menu1.png");
imgUrls.push("menu2.png");
imgUrls.push("menu3.png");
imgUrls.push("menu4.png");
imgUrls.push("menu5.png");
imgUrls.push("menu1_single.png");
imgUrls.push("menu2_single.png");
imgUrls.push("menu3_single.png");
imgUrls.push("menu4_single.png");
imgUrls.push("menu5_single.png");

imgUrls.push("table_shadow_bottom.png");
imgUrls.push("table_shadow_left.png");
imgUrls.push("table_shadow_top.png");

imgUrls.push("puzzle_small.png");
imgUrls.push("puzzle_big.png");

imgUrls.push("page_text1.png");
imgUrls.push("page_title.png");

imgUrls.push("page1_text1.png");
imgUrls.push("page1_text2.png");

imgUrls.push("page2_text1.png");

imgUrls.push("page3_text1.png");
imgUrls.push("page3_form1_field1.png");
imgUrls.push("page3_form1_field2.png");
imgUrls.push("page3_form1_field3.png");
imgUrls.push("page3_form1_field4.png");
imgUrls.push("page3_form2_field1.png");
imgUrls.push("page3_form2_field2.png");
imgUrls.push("page3_form2_field3.png");
imgUrls.push("page3_form2_field4.png");
imgUrls.push("page3_member1.png");
imgUrls.push("page3_member2.png");
imgUrls.push("page3_member3.png");
imgUrls.push("page3_member4.png");
imgUrls.push("page3_member5.png");
imgUrls.push("page3_member6.png");
imgUrls.push("page3_required_fields.png");
imgUrls.push("page3_submit_button.png");
imgUrls.push("page3_popup1.png");
imgUrls.push("page3_popup2.png");
imgUrls.push("page3_popup3.png");

imgUrls.push("page4_text1.png");
imgUrls.push("page4_text2.png");
imgUrls.push("page4_form1_field1.png");
imgUrls.push("page4_form1_field2.png");
imgUrls.push("page4_form2_field1.png");
imgUrls.push("page4_form2_field2.png");
imgUrls.push("page4_form2_field2_background.png");
imgUrls.push("page4_form2_field3.png");
imgUrls.push("page4_form2_submit.png");
imgUrls.push("page4_submit_button.png");
imgUrls.push("page4_popup1.png");
imgUrls.push("team_welcome.png");
imgUrls.push("download.png");

imgUrls.push("page5_form1_field1.png");
imgUrls.push("page5_form1_field2.png");
imgUrls.push("page5_form1_field3.png");
imgUrls.push("page5_form1_field4.png");
imgUrls.push("page5_form1_field4_background.png");
imgUrls.push("page5_form1_field4_shadow_bottom.png");
imgUrls.push("page5_form1_field4_shadow_left.png");
imgUrls.push("page5_submit_button.png");

imgUrls.push("page6_form1_field1.png");
imgUrls.push("page6_form1_field1_shadow_left.png");
imgUrls.push("page6_submit_button.png");


var isSent = false;

