/*******************************************************************************
 * 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
 *- login_templates.js
 *
 *Usage:
 */


;
(function($) {

	$.CliqsetConnect = function(manager) {
		//--------MEMBER VARS
		this.ID = 1;
		this.CONTAINER_ID = "cliqsetconnect_" + this.ID;
		this.CS = "#" + this.CONTAINER_ID + " ";
		this.template_cliqset = $.template(TEMPLATE_CLIQSETCONNECT_WIDGET);
		this.manager = manager;

		this.deploy = function(widgetid, manager) {
			var overlay = this;

			$('#'+widgetid).append(this.template_cliqset);

			$("#cliqsetconnect_register").click(function() {
				jQuery("#cliqsetconnect_overlay #messages").empty();
				jQuery("#add_cliqset_feed").attr("disabled", false);
				$("#cliqsetconnect_overlay").overlay({
					top: 100,
					api: true,
					finish: {
						top: 100,
						left: 'center',
						absolute: false
					}
				}).load();
			});

			// register submit button
			$("#add_cliqset_feed").click(function() {
				$("#cliqsetconnect_content #messages").empty();
				var applicationName = "Cliqset";
				var feedType = "activitystream";
				var feedUrl = "http://cliqset.com/feed/atom?uid=" + $("#cliqsetid").val();
				var ownerAlias = $("#cliqsetid").val();
				var defaultTags = '';

				//clear messages and show wait
				jQuery("#cliqsetconnect_overlay #messages").empty().append("Please Wait...<img src=\""+WIDGET_RESSOURCE_PATH+"/ceq_siteconnect/images/wait.gif\">");
				jQuery("#add_cliqset_feed").attr("disabled", true);

				var newFeed = {};
				newFeed.applicationName = applicationName;
				newFeed.feedType = feedType;
				newFeed.feedUrl = feedUrl;
				newFeed.defaultTags = defaultTags;
				newFeed.ownerAlias = ownerAlias;
				newFeed.siteFeed = false;
				jQuery.ajax({
					type: "POST",
					url: "/ceq-ws/jersey/Feeds",
					data: newFeed,
					success: function(response, textStatus) {
						jQuery("#cliqsetconnect_overlay #messages").empty().append("Feed Saved!");
						jQuery("#cliqsetconnect_overlay").overlay({
							api: true
						}).close();
						overlay.manager.updatePersonalFeedRow(response);
					},
					error: function(xhr, textStatus, errorThrown) {
						jQuery("#cliqsetconnect_overlay #messages").empty().append("<font color=\"red\">Error registering feed - " + textStatus + " " + xhr.responseText + "</font><br/>");
						jQuery("#add_cliqset_feed").attr("disabled", false);
					},
					dataType:"json"
				});
			});

		};
	};

})(jQuery);


