(function($){
	$.specialdeal = function(el, options){
	var base = this;

	// Access to jQuery and DOM versions of element
	base.$el = $(el);
	base.el = el;

	// Add a reverse reference to the DOM object
	base.$el.data("specialdeal", base);

	base.init = function(){
		base.options = $.extend({}, $.specialdeal.defaultOptions, options);
		// Put your initialization code here
		var data = base.options.data;
		$.each(data, function(id, obj){
			
			var holder = base.buildTag('div', {
					'class': 'specialdeal-graphic'
				},
				// draw background
				base.drawBackground(obj) +
				// add text
				base.drawTexts(obj)
			);
			// show in place
			var holderObj = $(holder);
			holderObj.data('link', obj.background.link || '/');

			base.$el.append(holderObj);
			holderObj.css({
				'cursor': 'pointer'
			}).click(function(){
				document.location = $(this).data('link') || '/';
			});
		});
	};

	base.drawBackground = function(obj){
		/**
		 * Build <img> tag with deal banner background
		*/
		return base.buildTag('img', {
			src: obj.background.url,
			width: obj.background.width,
			height: obj.background.height
		}, null, true);
	};

	base.drawTexts = function(obj){
		/**
		 * Build set of deal banner texts
		*/
		var chunks = [];
		var width = parseInt(obj.background.width);
		var height = parseInt(obj.background.height);
		$.each(obj.texts, function(index, text){
			var posX = parseInt(text.pos_x);
			var posY = parseInt(text.pos_y);
			var fontSize = parseInt(text.size);
			var textWidth = base.textWidth(text, fontSize);
			if(posX > (width - textWidth)){
				posX = width - textWidth;
			}
			if(posY > height){
				posY = height - fontSize + 2;
			}

			chunks.push(base.buildTag('li', {
				'style': base.buildCss({
					'font-size': text.size + 'px',
					'top': posY + 'px',
					'left': posX + 'px',
					'color': '#' + text.color,
					'font-family': text.font_family
					}),
				'class': 'specialdeal-text'
			}), text.text);
		});
		return base.buildTag('ul', {}, chunks.join('\n'));
	};

	base.textWidth = function(text, fontSize){
		/**
		 * Get width of the text in specified font size
		*/
		var data = $('<strong style="font-size:' + 
					fontSize + 'px;"><nobr>' + text + '</nobr></strong>');
		$('body').append(data);
		data.css({
			'position': 'absolute',
			'top': '-3000px',
			'left': '-3000px',
			'display': 'block'
		});
		var width = data.outerWidth();
		data.remove();
		return width;
	};

	base.buildTag = function(name, data, text, selfClosed){
		/**
		 * Build tag from arguments
		*/
		var tagEnd = '>';
		var tagBegin = '<' + name;
		if(selfClosed != null){
			tagEnd = ' />';
		}
		var pairs = [];
		$.each(data, function(key, val){
			if(val == null){
				val = '';
			}
			pairs.push(key + '="' + val + '"');
		});
		var rv = tagBegin + ' '+ pairs.join(' ') + tagEnd;
		// closing tag
		if(text != null && selfClosed == null){
			return rv + text + '</' + name + '>';
		}
		return rv;
	};

	base.buildCss = function(props){
		/**
		 * Build CSS properties from object
		*/
		var pairs = [];
		$.each(props, function(key, val){
			pairs.push(key + ': ' + val);
		});
		return pairs.join(";");
	};

	// Run initializer
		base.init();
	};

	$.specialdeal.defaultOptions = {
		data: []
	};

	$.fn.specialdeal = function(options){
		return this.each(function(){
				(new $.specialdeal(this, options));
				
		});
	};
})(jQuery);
