﻿//$('#toolbar_email_form_success').hide();
//$('#toolbar_email_form_failure').hide();

var benefix_mail = function () {
    var theObject = {};
    var servicePath = null;
    var cancelImage = null;
    var ajax_params = {
        url: "",
        data: "",
        success_handler: null,
        failure_handler: null
    };
    //setters + getters
    var setCancelImage = function (ci) {
        if (!ci) {
            return;
        }
        cancelImage = ci;
    };
    var setServicePath = function (sp) {
        if (!sp) {
            return;
        }
        servicePath = sp;
        servicePath += "/sendMail";
    };
    var getServicePath = function () {
        if (!servicePath) {
            alert("servicePath not set");
            return;
        }
        return servicePath;
    };
    var getData = function () {
        var to_address = $("#toolbar_email_to").val();
        var from_address = $("#toolbar_email_from").val();
        
        var from_name = $("#toolbar_email_to_name").val();

        return "to_address=" + escape(to_address) + "&from_address=" + escape(from_address) + "&from_name=" + escape(from_name) + "&location=" + escape(location.href);
    };
    //ajax functions
    var postData = function () {
        $.ajax({
            url: ajax_params.url,
            data: ajax_params.data,
            type: "GET",
            dataType: "xml",
            contentType: "application/xml",
            success: ajax_params.success_handler,
            error: ajax_params.failure_handler
        });
    };
    //event handlers
    var sendMail = function () {
        ajax_params.url = getServicePath();
        ajax_params.data = getData();
        ajax_params.success_handler = showSuccess;
        ajax_params.failure_handler = showFailure;
        postData();
    };
    var openMailForm = function () {
        benefix_UI.setPopupId("toolbar_email_form");
        benefix_UI.setBackgroundId("popup_bg");
        benefix_UI.showPopup();
    };
    var closeMailForm = function () {
        benefix_UI.setPopupId("toolbar_email_form");
        benefix_UI.setBackgroundId("popup_bg");
        benefix_UI.hidePopup();
    };
    var showSuccess = function () {
        $('#toolbar_email_form_default').hide();
        $('#toolbar_email_form_success').show();
    };
    var showFailure = function () {
        $('#toolbar_email_form_default').hide();
        $('#toolbar_email_form_failure').show();
    };
    var bindEvents = function () {
        $(".email_link").click(openMailForm);
        $("#toolbar_email_cancel_close").click(closeMailForm);
        $("#toolbar_email_cancel_link").click(benefix_UI.showDefault);
        $("#toolbar_email_send").click(sendMail);
    };
    theObject.bindEvents = bindEvents;
    theObject.sendMail = sendMail;
    theObject.openMailForm = openMailForm;
    theObject.closeMailForm = closeMailForm;
    theObject.setServicePath = setServicePath;
    theObject.setCancelImage = setCancelImage;
    return theObject;
} ();
var benefix_UI = function () {
    var theObject = {};
    var backgroundId = null;
    var popupId = null;
    var popupVisible = false;
    //getters + setters
    var getPopupVisible = function () {
        return popupVisible;
    };
    var setPopupId = function (id) {
        if (!id)
            return;
        popupId = id;
    }
    var setBackgroundId = function (id) {
        if (!id)
            return;
        backgroundId = id;
    }
    var setPopupVisible = function (val) {
        popupVisible = val;
    };
    
    
    
    var showPopup = function () {                   //VC 08/09/2011
        if(popupVisible)return; 
        $('.thisTVswf').hide();
        $('.thisTVswf_temp').show();


        $("#toolbar_email_to").val("");
        $("#toolbar_email_to_name").val("");
        $("#toolbar_email_from").val("");
       
        if($.browser.msie == true && $.browser.version == "6.0"){
            $('#stateDropDown').hide();
        }
        centerPopup();
        showDefault();
        $("#" + backgroundId).css({"opacity": "0.7"}).css({"z-index": "8000"}).fadeIn("slow");
        $("#" + popupId).fadeIn("slow");
        popupVisible = true;
    };
    
    var showDefault = function(){                   //VC 08/09/2011
        $('#toolbar_email_form_failure').hide();
        $('#toolbar_email_form_success').hide();
        $('#toolbar_email_form_default').show();
    };

    var hidePopup = function () {
        $('.thisTVswf').show();
        $('.thisTVswf_temp').hide();
        $('#stateDropDown').show();
        $("#" + backgroundId).fadeOut("slow");
        $("#" + popupId).fadeOut("slow");
        popupVisible = false;
    };
    var centerPopup = function () {
        var windowWidth = document.documentElement.clientWidth;
        var windowHeight = document.documentElement.clientHeight;
        var popupHeight = $("#" + popupId).height();
        var popupWidth = $("#" + popupId).width();
        $("#" + popupId).css({
            "position": "absolute",
            "top": windowHeight / 2 - popupHeight / 2 + $(window).scrollTop() + "px",
            "left": windowWidth / 2 - popupWidth / 2 + $(window).scrollLeft() + "px"
        });
    };
    theObject.setPopupId = setPopupId;
    theObject.setBackgroundId = setBackgroundId;
    theObject.showPopup = showPopup;
    theObject.hidePopup = hidePopup;
    theObject.showDefault = showDefault;
    return theObject;
} ();

