/*******************************************************************************
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 *
 *
 * Copyright (c) 2008 - 2010 Sun Microsystems Inc. All Rights Reserved
 *
 * This file is available and licensed under the following license:
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * * Neither the name of Sun Microsystems nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ******************************************************************************/

/*
 *Require:
 *- jquery.js - 1.2.6 and greater
 *- jquery.template.js
 *- time_renderer.js
 *- listing_templates.js
 *
 *Usage:
 * jQuery.noConflict();
 * jQuery.listing_table = new jQuery.InformationListing("listing_table");
 * jQuery.listing_table.deploy();
 */
;(function( $ ) {
    $.SiteConnect = function(id){
	    this.ID = 1;
	    if (id!=null) {
	        this.ID = id;
	    }

	    this.CONTAINER_ID = "siteconnect_" + this.ID;
	    this.CS = "#" + this.CONTAINER_ID + " ";
	    this.WEBSERVICE_URL = WEBSERVICE_RESSOURCE_PATH+"/jersey/Feeds/SiteFeeds";
	    this.WEBSERVICE_FEED_URL = WEBSERVICE_RESSOURCE_PATH+"/jersey/Feeds";
	    this.WEBSERVICE_PERSONAL_URL = WEBSERVICE_RESSOURCE_PATH+"/jersey/Feeds/PersonalFeeds";

        this.EMPTY_FEEDS_RESPONSE = "EMPTY";
        this.template_table = $.template(TEMPLATE_SITECONNECT_TABLE);

	    this.template_row = $.template(TEMPLATE_SITECONNECT_ROW);
	    this.template_row_content = $.template(TEMPLATE_SITECONNECT_ROW_CONTENT);
	    this.template_empty_row = $.template(TEMPLATE_SITECONNECT_EMPTY_ROW);

	    this.template_personal_row = $.template(TEMPLATE_SITECONNECT_PERSONAL_ROW);
	    this.template_personal_row_content = $.template(TEMPLATE_SITECONNECT_PERSONAL_ROW_CONTENT);
	    this.template_personal_empty_row = $.template(TEMPLATE_SITECONNECT_PERSONAL_EMPTY_ROW);


	    this.tmp_feeds_response = null;
	    this.tmp_personal_feeds_response = null;

        this.deploy = function(CS){
            var manager = this;
            if (CS){
                this.CS = CS;
            }else{
                throw 'Container Selector not valid!';
            }
            var itemObj = {};
            itemObj.WIDGET_RESSOURCE_PATH = WIDGET_RESSOURCE_PATH;
            $(this.CS).append(this.template_table, itemObj);

	        var fbconnect = new $.FbConnect();
	        fbconnect.deploy('ceq_siteconnect_personal_icons');

	        var cliqset = new $.CliqsetConnect(manager);
	        cliqset.deploy('ceq_siteconnect_personal_icons');

            $.ajax({
                type: "GET",
                url: this.WEBSERVICE_URL + "?format=json",
                success: function(data){
                    manager.handleGetAllFeedsResponse(data);
                },
                error: function(xhr, textStatus, errorThrown){
                        alert("Error getting the list of feeds: " + textStatus + " " + xhr.responseText);
                },
                dataType:"json"
            });


            $.ajax({
                type: "GET",
                url: this.WEBSERVICE_PERSONAL_URL + "?format=json",
                success: function(data){
                    manager.handleGetAllPersonalFeedsResponse(data);
                },
                error: function(xhr, textStatus, errorThrown){
                        alert("Error getting the list of personal feeds: " + textStatus + " " + xhr.responseText);
                },
                dataType:"json"
            });



        };

        this.setWebserviceURL = function(urlvar) {
             this.WEBSERVICE_URL = urlvar;
        }

        this.handleGetAllFeedsResponse = function(data){
            if (data != null){
                this.tmp_feeds_response = data;
            }else{
                this.tmp_feeds_response = this.EMPTY_FEEDS_RESPONSE;
            }
            this.handleGetFeedsResponse();
        };

	    this.handleGetAllPersonalFeedsResponse = function(data){
	        if (data != null){
	            this.tmp_personal_feeds_response = data;
	        }else{
	            this.tmp_personal_feeds_response = this.EMPTY_FEEDS_RESPONSE;
	        }
	        this.handleGetPersonalFeedsResponse();
	    };

        this.handleGetFeedsResponse = function(){
            var manager = this;
            manager.clearContent();
            if (manager.tmp_feeds_response == manager.EMPTY_FEEDS_RESPONSE){
                $("#ceq_siteconnect_table_content").append(manager.template_empty_row);
            }else{//there are some feeds defined already...
                var feeds = manager.tmp_feeds_response.Feeds;
                if (!feeds.applicationName){
                    $.each(feeds, function(i, feed){
                        manager.addFeedRow(feed);
                    });
                }else{//just one result
                    manager.addFeedRow(feeds);
                }
            }

        };

	    this.handleGetPersonalFeedsResponse = function(){
	        var manager = this;
	        manager.clearPersonalContent();
	        if (manager.tmp_personal_feeds_response == manager.EMPTY_FEEDS_RESPONSE){
	            $("#ceq_siteconnect_personal_table_content").append(manager.template_personal_empty_row);
	        }else{//there are some feeds defined already...
	            var feeds = manager.tmp_personal_feeds_response.Feeds;
	            if (!feeds.feedId){
	                $.each(feeds, function(i, feed){
	                    manager.addPersonalFeedRow(feed);
	                });
	            }else{//just one result
	                manager.addPersonalFeedRow(feeds);
	            }
	        }

	    };

        this.updateFeedRow = function(feed){
            this.addFeedRow(feed);
        };

        this.addFeedRow = function(feed){
            var manager = this;
            if ($("#ceq_siteconnect_table_content #ceq_siteconnect_empty_row").length > 0){
                $("#ceq_siteconnect_table_content #ceq_siteconnect_empty_row").remove();
            }

            if ($("[id=ceq_siteconnect_table_row_" + feed.applicationName + "]").length > 0){
                $("[id=ceq_siteconnect_table_row_" + feed.applicationName + "]").empty();
            }else{
                $("#ceq_siteconnect_table_content").append(manager.template_row, feed);
            }
            $("[id=ceq_siteconnect_table_row_" + feed.applicationName + "]").append(manager.template_row_content, feed);

            //assignhandler for the Edit.. button
            $("[id=ceq_siteconnect_button_update_" + feed.applicationName + "]").click(function(){
	            var toEdit = {};
                toEdit.applicationName = feed.applicationName;
	            toEdit.siteNames = feed.userAlias;
                var overlay = new $.UpdateSiteOverlay(manager, toEdit);
                overlay.deploy(manager.CS);
            });
        };

	    this.updatePersonalFeedRow = function(feed){
	        this.addPersonalFeedRow(feed);
	    };

	    this.addPersonalFeedRow = function(feed){
	        var manager = this;
	        if ($("#ceq_siteconnect_personal_table_content #ceq_siteconnect_personal_empty_row").length > 0){
	            $("#ceq_siteconnect_personal_table_content #ceq_siteconnect_personal_empty_row").remove();
	        }

	        if ($("#ceq_siteconnect_personal_table_row_" + feed.feedId).length > 0){
	            $("#ceq_siteconnect_personal_table_row_" + feed.feedId).empty();
	        }else{
	            $("#ceq_siteconnect_personal_table_content").append(manager.template_personal_row, feed);
	        }
	        $("#ceq_siteconnect_personal_table_row_" + feed.feedId).append(manager.template_personal_row_content, feed);

	        //assignhandler for the Delete button
	        $("#ceq_siteconnect_personal_button_delete_" + feed.feedId).click(function(){
		       $.ajax({
                    type: "DELETE",
                    url: manager.WEBSERVICE_FEED_URL + "/" + feed.feedId,
                    success: function(msg){
                        $("#ceq_siteconnect_personal_table_row_" + feed.feedId).remove();
                        if ($("#ceq_siteconnect_personal_table_content tr").length == 0){
                            $("#ceq_siteconnect_personal_table_content").empty().append(manager.template_personal_empty_row);
                        }
                    },
                    error: function(xhr, textStatus, errorThrown){
                        alert("Error deleting personal feed:" + textStatus + " " + xhr.responseText);
                    }
                });
	        });
	    };

	    this.clearContent = function(){
	        $("#ceq_siteconnect_table_content").empty();
	    };
	    this.clearPersonalContent = function(){
	        $("#ceq_siteconnect_personal_table_content").empty();
	    };
    };
})(jQuery);

