; (function($){

    $.UpdateSiteOverlay = function(manager, site){
        this.CS = "";
        this.ID = "ceq_siteconnect_overlay_update";
        this.template_overlay_update = $.template(TEMPLATE_SITECONNECT_OVERLAY_UPDATE);

        if (!manager){
            throw "SiteConnect UpdateOverlay Init Error: manager parameter missing";
        }
        this.manager = manager;


        if (!site){
            throw "UpdateOverlay Init Error: site parameter missing";
        }
        this.site = site;

        this.getId = function(){
            return this.ID;
        };

        this.deploy = function(CS){
            var overlay = this;
            if (CS){
                this.CS = CS;
            }else{
                throw 'Container Selector not valid!';
            }

            var params = overlay.site;
            params.overlay_id = overlay.ID;
            $(overlay.CS).append(overlay.template_overlay_update, params);

            $("#" + overlay.ID + " #update_siteconnect_submit").click(function(){
                $("#" + overlay.ID + " #messages").empty();

                var siteNames = $("#" + overlay.ID + " #siteNames").val();
	            var applicationName = $("#" + overlay.ID + " #applicationName").val();

                //validate form values
                if(applicationName == ""){
                    $("#" + overlay.ID + " #messages").append("Application Name Cannot Be Empty!<br/>");
					return;
                }

                //clear messages and show wait
                $("#" + overlay.ID + " #messages").empty().append("Please Wait...<img src=\"" + WIDGET_RESSOURCE_PATH + "/ceq_siteconnect/images/wait.gif\">");
                $("#" + overlay.ID + " button").attr("disabled", true);
                $("#" + overlay.ID + " input").attr("disabled", true);
                $("#" + overlay.ID + " select").attr("disabled", true);

                var newSiteName = {};
	            newSiteName.applicationName = applicationName;
	            newSiteName.siteNames = siteNames;
                $.ajax({
                    type: "PUT",
                    url: overlay.manager.WEBSERVICE_URL,
                    data: newSiteName,
                    success: function(response, textStatus){
                        $("#" + overlay.ID + " #messages").empty().append("Site Names Updated!");
                        overlay.closeOverlay();
                        overlay.manager.updateFeedRow(response);
                    },
                    error: function(xhr, textStatus, errorThrown){
                        $("#" + overlay.ID + " #messages").empty().append("<font color=\"red\">Error Updating Site Names - " + textStatus  + " " + xhr.responseText + "</font><br/>");
                        $("#" + overlay.ID + " button").attr("disabled", false);
                        $("#" + overlay.ID + " input").attr("disabled", false);
                        $("#" + overlay.ID + " select").attr("disabled", false);
                    },
                    dataType:"json"
                });
            });

            $("#" + overlay.ID).overlay({
                top: 100,
                api:true,
                onClose:function(){
                    $("#" + overlay.ID).remove();
                },
                finish: {
                    top: 100,
                    left: 'center',
                    absolute: false
                }
            }).load();

        };

        this.closeOverlay = function(){
            $("#" + this.ID).overlay({
                api:true
            }).close();
        };
    };

})(jQuery);


