(function($) {
	$.fn.product_target = function(settings) {
		
		settings = jQuery.extend({
			path: './products/',
			on: 'target_on.png',
			off: 'target_off.png',
			top:0,
			top_start:-20,
			left:0,
			animate: true,
			place: '',
			product: '',
			class_main: 'product_target',
			class_off: 'product_target_off',
			class_on: 'product_target_on',
			click_href: '',
			click_js: '',
			speed: 500,
			change_on_hover: false
		}, settings);

		var container = $(this).eq(0);

		function _initialize()
		{
			var s = settings;
			var html = '<div class="' + s.class_main + ' ' + s.class_off + '" style="left:' + s.left + 'px; top:' + s.top_start + 'px;"></div>';

			container.append(html);
			//alert('Placing: ' + s.top + '|' + s.left);
			var _t = container.children('div:last');
			
			_t.animate({top: s.top}, s.speed);
			
			if(!_apple_device())
			{
				_t.hover(function () {
					$(this).toggleClass('' + s.class_off + ' ' + s.class_on + '');
				});
				
				if(s.change_on_hover && s.click_js)
				{
					_t.bind('mouseenter',{_pl:s.place, _pr:s.product}, function(event) {
							eval(s.click_js + "('" + event.data._pl + "','" + event.data._pr + "')");
					});
				}
			}
			else
			{
				//-- mouseenter and mouseleave don't really work on iP*ds or iPhone
				_t.bind('touchstart', function() {
					$(this).css('background-image', 'url(' + s.path + s.on + ')');
				});
				_t.bind('touchend', function() {
					$(this).css('background-image', 'url(' + s.path + s.off + ')');
				});
			}
	
			//-- bind to click
			_t.click({_pl:s.place, _pr:s.product}, function(event) {
				if(s.click_js)
					eval(s.click_js + "('" + event.data._pl + "','" + event.data._pr + "')");
				else
				{
					if(s.click_href)
						window.open('' + s.click_href + '?p=' + event.data._pl + '&s=' + event.data._pr, '_self');
				}
			});

			return false;
		}
		
		function _apple_device()
		{
			return navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPod/i);
		}
		
		_initialize();
	};
})(jQuery);
