﻿function loadPopup(id){
    $("#popupBackground").css({ "opacity": "0.7" });
    $("#popupBackground").show();
    $("#" + id).show();
    $("#" + id).attr('name',id);
    $("#" + id).attr('id', 'opened');
    return false;
}
function disablePopup(){
    $(".popupLayer").hide();
    $("#opened").attr('id', $("#opened").attr('name'));
	    $("#popupBackground").hide();
	    return false;
}
function centerPopup(id) {
    if (id == 'undefined') return false;
    var windowWidth = $(window).width();
    var windowHeight = $(window).height();
    var popupHeight = $("#" + id).height();
    var popupWidth = $("#" + id).width();
    if (popupHeight == null || popupWidth == null) return false;
	var py = (windowHeight / 2 - popupHeight / 2) < 15 ? 15 : (windowHeight / 2 - popupHeight / 2);
	var px = (windowWidth / 2 - popupWidth / 2) < 0 ? 0 : (windowWidth / 2 - popupWidth / 2);
	$("#popupBackground").css({
	"height": windowHeight, "width": windowWidth
    });
	$(".popupLayer").css({
		"position": "absolute",
		"top": py,
		"left": px
    });
    return false;
}
function resizeMask() {
    $('#popupBackground').height($(document).height());
    $('#popupBackground').width($(window).width() < $(document.body).width() ? $(document.body).width() : $(window).width());
    centerPopup('opened');
    return false;
}

function SetFileName(id,file) {
    reWin = /.*\\(.*)/;
    var fileTitle = file.replace(reWin, "$1");
    reUnix = /.*\/(.*)/;
    fileTitle = fileTitle.replace(reUnix, "$1");
    $(id).html(fileTitle);
    var RegExExt = /.*\.(.*)/;
    var ext = fileTitle.replace(RegExExt, "$1");

    var pos;
    if (ext) {
        switch (ext.toLowerCase()) {
            case 'doc': pos = '0'; break;
            case 'bmp': pos = '16'; break;
            case 'jpg': pos = '32'; break;
            case 'jpeg': pos = '32'; break;
            case 'png': pos = '48'; break;
            case 'gif': pos = '64'; break;
            case 'xls': pos = '80'; break;
            case 'xlsx': pos = '96'; break;
            case 'docx': pos = '112'; break;
            case 'pdf': pos = '128'; break;
            case 'tif': pos = '144'; break;
            case 'tiff': pos = '160'; break;
            case 'txt': pos = '176'; break;
            case 'rar': pos = '192'; break;
            case 'zip': pos = '192'; break;
            case '7z': pos = '192'; break;
            default: pos = '208'; break;
        };
        $(id).css({ "display": "block", "background": "url(../_images/icons.png) no-repeat 0 -" + pos + "px" });
    };
    return false;
}

$(document).ready(function() {
    if (window.location.search.indexOf("contactus=1") != -1) { centerPopup("p1000"); loadPopup("p1000"); }
    $(".popupLink").click(function() {
        var id = "p" + String($(this).attr('id')).split('popup').join('');
        centerPopup(id);
        loadPopup(id);
        return false;
    });
    $(window).resize(function() {
        resizeMask();
        return false;
    });
    $(".popupClose").click(function() {
        disablePopup();
        return false;
    });
    $("#popupBackground").click(function() {
        disablePopup();
        return false;
    });
    $(document).keypress(function(e) {
        if (e.keyCode == 27) {
            disablePopup();
        } 
    });
    $("a.anchorLink").click(function() {
        $("html:not(:animated)").animate({ scrollTop: 0 }, 600);
        return false;
    });
    $("a.anchorLinkru").click(function() {
        $("html:not(:animated)").animate({ scrollTop: 0 }, 600);
        return false;
    });
    $("a.anchorLinkfr").click(function() {
        $("html:not(:animated)").animate({ scrollTop: 0 }, 600);
        return false;
    });

    $.dpText = {
        TEXT_PREV_YEAR: "Annee précédente",
        TEXT_PREV_MONTH: "Mois précéden",
        TEXT_NEXT_YEAR: "Année suivante",
        TEXT_NEXT_MONTH: "Mois suivant",
        TEXT_CLOSE: "Fermer",
        TEXT_CHOOSE_DATE: "Choisir une date"
    }
    $(".date-pick").datePicker({ startDate: "01/01/1900" }).trigger("change"); //, clickInput: true
    //$('.date-pick-disabled').datePicker({ startDate: '01/01/1900' }).dpSetDisabled(true);

    $(".customfileupload").each(function() {
        $(this).addClass("customFile").wrap($(document.createElement("div")).addClass("customfileuploadwraper"));
        $(this).after($(document.createElement("div")).addClass("FileName").css({ "display": "none", "background": "url(../_images/icons.png)" }).attr("id", String($(this).attr('id')) + "_filename"));
        $(this).after($(document.createElement("div")).addClass("blocker"));
        $(this).after($(document.createElement("div")).html("Attacher").addClass("fakeButton"));
    })
    $(".customfileupload").change(function() {
        file = $(this).val();
        id = "#" + String($(this).attr('id')) + "_filename";
        SetFileName(id, file);
    });
    $(".popupLayer").bgiframe();
    $("#popupBackground").bgiframe();
});
