/**
 * Plugin: jquery.related
 * 
 * Dependencies:
 * content.js
 * routine.js
 *
 * Version: 1.1.0
 * (c) Copyright 2010-2011, Igor
 **/

(function ($) {
    $.fn.collector = function (options, fn) {
        var defaults = Routine.Model.extend({
			limit: 10,
			title: "No title",			
			keyword: "flowers",
			contenttype: "wikipedia-related-keywords",
			spinner: "<img src=\"images/spinner.gif\" border=\"0\" />",
            parameter: "key=G7kg81m893nmwa16o8ofvn243tj25ijh&type={0}&keyword={1}",
            url: "../../services/contentsupplier.aspx",
			timeout: 3800,
			container: $(".related-topics.widget"),
			width: "300px",
            bordercolor: "#ccc",
            backgroundcolor: "#ccc",
            titlecolor: "#000",
            linkcolor: "#142DE3",
            contentcolor: "#333",
            titlefont: "bold 12px arial",
            linkfont: "normal 12px arial",
            contentfont: "normal 12px arial", 
			wrapped: true
		});		
		
        var options = $.extend(defaults.pluck(), options);	

		//iterate through target elements
        return this.each(function (i, e) {
            var $e = $(e);

			remoteCall(e, options);					
        });
    };
	
	var remoteCall = function(e, options){			
		Routine.Remote.extend({
			method: Routine.CallType.Read,
			model: String.format(options.parameter, options.contenttype, options.keyword),
			url: options.url,
			timeout: options.timeout,
			success: function (data) {					
				var output = eval("data=" + data);
				if (output.result != "") {					
					$(e).html(generatehtml(output.result, options));
				}
				else{						
					$(e).empty();
				}
			},
			error: function (x, t, e) {
				console.log("error");	
				$(e).empty();
			}
		});
	}
	
	var generatehtml = function(data, options){		
		var html = "";
		
		// widget styles
        html += "<style>";
        html += "h4.widget{ padding:6px; font:" + options.titlefont + "; margin:0px }";
        html += "div.widget{ font:normal 12px arial; color:#333 }";
        html += "a.widget:link{ font:" + options.linkfont + "; color:" + options.linkcolor + "; text-decoration:none }";
        html += "a.widget:active{ font:" + options.linkfont + "; color:" + options.linkcolor + "; text-decoration:none }";
        html += "a.widget:hover{ font:" + options.linkfont + "; color:" + options.linkcolor + "; text-decoration:underline }";
        html += "a.widget:visited{ font:" + options.linkfont + "; color:" + options.linkcolor + "; text-decoration:none }";
        html += "div.widget-content{ margin-top:3px; margin-bottom:10px; color:" + options.linkcolor + "; ";
		html += "font:" + options.contentfont + " }";
        html += "div.widget-content a:link{ font:normal 12px arial; color:green; text-decoration:none }";
        html += "</style>";
		
		//widget actual html
		if(options.wrapped){
			html += "<div class=\"widget\" style=\"clear:both;border:1px solid " + options.bordercolor + ";";
			html += "width:" + options.width + "\"><h4 class=\"widget\" style=\"color:" + options.titlecolor + ";";
			html += "background-color:" + options.backgroundcolor + ";padding-left:8px\">" + options.title + "</h4>";
		}
		html += data;
		if(options.wrapped){
			html += "</div>";
		}
		
		return html;
	};
   
    function getUrlParam(name) {
		name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
		var regexS = "[\\?&]" + name + "=([^&#]*)";
		var regex = new RegExp(regexS);
		var results = regex.exec(window.location.href);
		if (results == null)
			return "";
		else
			return results[1];
	}

})(jQuery);

