﻿/*
File Name: CLNYC_UserFunctions.Js 
Creator: Uday Vyas
Created On: May 24th, 2009
*/

function ctrMax(id, val, upDiv) {
    var txtid = document.getElementById(id).value;

    if (txtid.length <= val) {
        document.getElementById(upDiv).innerHTML = val - parseInt(txtid.length);
        return true;
    }
    else {
        if (txtid.length > val) {
            document.getElementById(id).value = txtid.substring(0, val);
            txtid = document.getElementById(id).value;
        }
        document.getElementById(upDiv).innerHTML = val - parseInt(txtid.length)
        return false;
    }
}

$(document).ready(function() {
    var popupStatus = 0;
    var popType = 1;

    function loadPopup() {
        //debugger;
        //loads popup only if it is disabled  
        if (popupStatus == 0) {
            $("#backgroundPopup").css({ "opacity": "0.75" });
            $("#backgroundPopup").fadeIn("slow");
            if (popType == 1) {
                $("#popupContact").fadeIn("slow");
                clearAll();
            }
            else if (popType == 2) {
                $("#popupSubscribe").fadeIn("slow");
                clrAll();
            }
            popupStatus = 1;
        }
    }

    //disabling popup with jQuery !
    function disablePopup() {
        //disables popup only if it is enabled  
        if (popupStatus == 1) {
            $("#backgroundPopup").fadeOut("slow");
            popupStatus = 0;
            if (popType == 1) {
                $("#popupContact").fadeOut("slow");
                $("#popupContactClose").css({ "cursor": "default" });
                clearAll();
            }
            else if (popType == 2) {
                $("#popupSubscribe").fadeOut("slow");
                $("#subcribClose").css({ "cursor": "default" });
                $('#errorSummary').html('');
            }
        }
    }
    //centering popup
    function centerPopup() {
        //request data for centering  
        var windowWidth = document.documentElement.clientWidth;
        var windowHeight = document.documentElement.clientHeight;
        if (popType == 1) {
            var popupHeight = $("#popupContact").height();
            var popupWidth = $("#popupContact").width();
            //centering
            $("#popupContact").css({ "position": "absolute", "top": windowHeight / 2 - popupHeight / 2, "left": windowWidth / 2 - popupWidth / 2 });
            popType = 1;
        }
        else if (popType == 2) {
            var popupHeight = $("#popupSubscribe").height();
            var popupWidth = $("#popupSubscribe").width();
            //centering  
            $("#popupSubscribe").css({ "position": "absolute", "top": windowHeight / 2 - popupHeight / 2, "left": windowWidth / 2 - popupWidth / 2 });
        }

        //only need force for IE6 
        $("#backgroundPopup").css({ "height": windowHeight });

    }

    //Link button Contact
    $("#lnkContact").click(function() {
        //set the Popup Type
        popType = 1;
        //centering with css    
        centerPopup();
        //load popup
        loadPopup();
    });

    //Link button Subcribe
    $("#lnkSubCribe").click(function() {
        //set the Popup Type
        popType = 2;
        //centering with css    
        centerPopup();
        //load popup
        loadPopup();
    });

    //Resize The BG based on the Resize
    $(window).resize(function() { centerPopup(); });

    //Mouse Over Functions
    $("#lnkContact").mouseover(function() { $("#lnkContact").css({ "cursor": "pointer" }); });
    $("#popupContactClose").mouseover(function() { $("#popupContactClose").css({ "cursor": "pointer" }); });
    $("#subcribClose").mouseover(function() { $("#popupContactClose").css({ "cursor": "pointer" }); });
    $("#lnkSubCribe").mouseover(function() { $("#lnkSubCribe").css({ "cursor": "pointer" }); });

    //Close the POPUP  

    //Click the x event!
    $("#popupContactClose").click(function() { disablePopup(); });
    $("#subcribClose").click(function() { disablePopup(); });

    //Click out event!  
    $("#backgroundPopup").click(function() { disablePopup(); });

    //Press Escape event!
    $(document).keypress(function(e) {
        if (e.keyCode == 27 && popupStatus == 1) { disablePopup(); }
    });


});