var Widget = {
	collection: "apple",
	width: 250,
	bgColor: "#000000",
	titleColor: "#ffffff",
	titleSize: 12,
	sourceColor: "#b1b2b2",
	sourceSize: 9,
	roundedCorners: 1,
	limit: 10,
	logo: "white",
	
	setWidth: function(width) {
		width = parseInt(width);
		
		if(width >= 100 && width <= 500)
			this.width = width;
		
		$("#clctd-widget").css("width", this.width);
		this.generateCode();
			
		return this;
	},
	
	setCollection: function(collection) {
		if(this.collection != collection) {
			this.collection = collection;
			this.generateCode();
			$("#clctd-widget").html("Loading...");
			$.get("/widget/" + this.collection + "/1", function(data) { $("#clctd-widget").html(data); Widget.setTitleColor(Widget.titleColor, 1).setSourceColor(Widget.sourceColor, 1).setTitleSize(Widget.titleSize).setSourceSize(Widget.sourceSize).setLimit(Widget.limit).setBgColor(Widget.bgColor); });
			
		}
		
		return this;
	},
	
	setCorners: function(corners) {
		this.roundedCorners = parseInt(corners);
		$("#clctd-widget").css("-moz-border-radius", (this.roundedCorners * 10) + "px").css("-webkit-border-radius", (this.roundedCorners * 10) + "px");
		
		this.generateCode();
		
		return this;
	},
	
	setBgColor: function(color, simple) {
		if(color == "Custom") { 
			$("#bgcolor-advanced").fadeIn("slow"); 
		} else {
			Widget.bgColor = color; 
			
			if(simple == 1) {
				$("#bgcolor-advanced").hide(); 
				$.farbtastic("#bgcolor-advanced").setColor(Widget.bgColor).linkTo(Widget.setBgColor);
			}
			
			$("#clctd-widget").css("background-color", Widget.bgColor);
			Widget.generateCode();
			
			/*
			if(Widget.bgColor == "#000000")
				$("#clctd-widget-img").attr("src", "http://s.clctd.com/img/collectedb.gif");
			else
				$("#clctd-widget-img").attr("src", "http://s.clctd.com/img/collectedw.gif");
			*/
		}
		
		return Widget;
	},
	
	setTitleColor: function(color, simple) {
		if(color == "Custom") { 
			$("#titlecolor-advanced").fadeIn("slow"); 
		} else {
			Widget.titleColor = color; 
			
			if(simple == 1) {
				$("#titlecolor-advanced").hide(); 
				$.farbtastic("#titlecolor-advanced").setColor(Widget.titleColor).linkTo(Widget.setTitleColor);
			}
			
			$("#clctd-widget > ul > li > a").css("color", Widget.titleColor);
			Widget.generateCode();
		}
		
		return Widget;
	},
	
	setTitleSize: function(size) {
		$("#clctd-widget > ul > li > a").css("font-size", size + "px");	
		this.generateCode();
		return this;
	},
	
	setSourceColor: function(color, simple) {
		if(color == "Custom") { 
			$("#sourcecolor-advanced").fadeIn("slow"); 
		} else {
			Widget.sourceColor = color; 
			
			if(simple == 1) {
				$("#sourcecolor-advanced").hide(); 
				$.farbtastic("#sourcecolor-advanced").setColor(Widget.sourceColor).linkTo(Widget.setSourceColor);
			}
			
			$("#clctd-widget").css("color", Widget.sourceColor);
			Widget.generateCode();
		}
		
		return Widget;
	},
	
	setSourceSize: function(size) {
		$("#clctd-widget").css("font-size", size + "px");
		this.generateCode();
		return this;
	},
	
	setLimit: function(limit) {
		if(limit > 0 && limit <= 50) {
			this.limit = limit;
			$("#clctd-widget > ul > li").hide();
			for(var i = 0; i < this.limit; i++) {
				$("#clctd-widget > ul > li:eq(" + i + ")").show();	
			}
			this.generateCode();
			
			if(this.limit < $("#clctd-widget > ul > li").length)
				$("#clctd-widget-showmore").show();
			else
				$("#clctd-widget-showmore").hide();
		}
		
		
		return this;
	},
	
	showMore: function() {
		var v = $("#clctd-widget > ul > li:visible").length;
		$("#clctd-widget > ul > li").hide();
		
		for(var i = 0; i < v + 10; i++) {
			if(i == 50) {
				$("#clctd-widget-showmore").hide();
				break;
			}
			
			if(i < v)
				$("#clctd-widget > ul > li:eq(" + i + ")").show();
			else
				$("#clctd-widget > ul > li:eq(" + i + ")").fadeIn("fast");
		}
	},
	
	setLogo: function(v) {
		this.logo = v;
		
		if(this.logo == "white")
			$("#clctd-widget-img").attr("src", "http://s.clctd.com/img/collectedww.png");
		else
			$("#clctd-widget-img").attr("src", "http://s.clctd.com/img/collectedwb.png");
		this.generateCode();			
		return this;
	},
	
	generateCode: function() {
			$("#widget-code").html('&lt;script type="text/javascript" src="http://w.collected.info/' + this.collection + '"&gt;&lt;/script&gt;\n\
&lt;script type="text/javascript"&gt;\nCollected.widget({width:' + this.width + ', bgColor: "' + this.bgColor + '", titleColor: "' + this.titleColor + '", titleSize: ' + this.titleSize + ', sourceColor: "' + this.sourceColor + '", sourceSize: ' + this.sourceSize + ', roundedCorners: ' + this.roundedCorners + ', items: ' + this.limit + ', logo: "' + this.logo + '"});\n\
&lt;/script&gt;');
		},
	
	show: function() {
		this.setWidth(this.width).setCorners(this.roundedCorners).setBgColor(this.bgColor, 1).setTitleColor(this.titleColor, 1).setSourceColor(this.sourceColor, 1).setTitleSize(this.titleSize).setSourceSize(this.sourceSize).setLimit(this.limit).setLogo(this.logo);
		$("#clctd-widget").fadeIn("slow");
		this.generateCode();
	}
};