﻿(function($) {

jQuery.extend(jQuery.expr[":"], {
	image: function(a) { return !a.tagName == "IMG" || jQuery.css(a, "background-image") != 'none'; }
});

var AdCanvasItem = function(parent, index, url) {
	this._index = index;
	this.load(parent, url);
}

jQuery.extend(AdCanvasItem.prototype, {
	_html: '',
	_ready: false,

	getIndex: function() { return this._index; },
	getHtml: function() { return this._html; },
	getReady: function() { return this._ready; },

	load: function(parent, url) {
		var item = this;
		$.get(url, null, function(data) {
			var images = $(item._html = data).parent().find(':image');
			if (item._ready = images.length != 0)
				images.each(function(index) {
					var image = $(new Image());
					var e = $(this);
					image.bind('load', function() {
						if (item._ready = (index == images.length - 1))
							parent.completed(item);
					}).attr('src', e.is('img') ? e.attr('src') : e.css('background-image').replace(/^url\(("|')?|("|')?\)$/g, ''));
				});
			else
				parent.completed(item);
		});
	}
});

var AdCanvas = {}, AdCanvasSlider = {}, AdCanvasStatic = {};

jQuery.extend(AdCanvas, {
	_items: null,

	getItems: function() { return this._items; },

	_init: function() {
		this.element.addClass("container").addClass("loading");
		this._items = [];
		for (var i = this.options.items.length - 1; i >= 0; i--) {
			this._items.push(new AdCanvasItem(this, i, this.options.url.replace('#{id}', escape(this.options.items[i]))));
		}
	}
});

jQuery.extend(AdCanvasSlider, AdCanvas, {
	_delay: 5000,
	_selectedIndex: 0,
	_paused: false,
	_playExecutor: null,
	_fadeExecutor: null,

	getDelay: function() { return this._delay; },
	setDelay: function(value) {
		if (this._delay != value) {
			this._delay = value;
			this._startPlayExecutor();
		}
	},

	getSelectedIndex: function() { return this._selectedIndex; },
	setSelectedIndex: function(value) {
		if (this._selectedIndex != value)
			this._activateItem(this.getItems()[this._selectedIndex = value]);
	},

	getPaused: function() { return this._paused; },
	setPaused: function(value) { this._paused = value; },

	_init: function() {
		AdCanvas._init.apply(this);
		this._startPlayExecutor();
	},

	movePrev: function() {
		this.setSelectedIndex((this.getSelectedIndex() > 0 ? this.getSelectedIndex() : this.getItems().length) - 1);
	},

	moveNext: function() {
		this.setSelectedIndex(this.getSelectedIndex() < this.getItems().length - 1 ? this.getSelectedIndex() + 1 : 0);
	},

	switchPaused: function() {
		this.setPaused(!this.getPaused());
		return this.getPaused();
	},

	completed: function(item) {
		var items = this.getItems();
		if (this._selectedIndex < items.length && items[this._selectedIndex] == item)
			this._activateItem(item);
		if (!this._playExecutor)
			this._startPlayExecutor();
	},

	_activateItem: function(item) {
		var owner = this.element;
		if (item && item.getReady()) {
			owner.removeClass('loading').fadeOut("fast", function() {
				owner.html($(item.getHtml())).fadeIn("fast");
			});
		} else
			owner.addClass('loading').empty().fadeIn("fast");
		if (this.options.activateItem)
			this.options.activateItem.apply(this, [item]);
	},

	_startPlayExecutor: function() {
		if (this._playExecutor) {
			clearTimeout(this._playExecutor);
			this._playExecutor = null;
		}
		var self = this;
		this._playExecutor = setInterval(function() { if (!self.getPaused()) self.moveNext(); }, this.getDelay());
	}
});

$.widget("ui.adcanvasslider", AdCanvasSlider);

$.ui.adcanvasslider.getter = ["getDelay", "getSelectedIndex", "getPaused", "switchPaused"];

jQuery.extend(AdCanvasStatic, AdCanvas, {
	completed: function(item) {
		this.element.removeClass('loading').html($(item.getHtml()));
	}
});

$.widget("ui.adcanvasstatic", AdCanvasStatic);

})(jQuery);
