﻿/// Home items slider
jQuery(function($) {
	$.fn.slide = function() {
		var slider = new $.fn.slide.slider(this);
		//return this.each(function() {
		//});
	}

	$.fn.slide.options = {
		changeTimeout: 3000
	}

	// Slider
	$.fn.slide.slider = function(items) {
		this.init(items);
	}

	$.fn.slide.slider.prototype = {
		init: function(items) {
			var me = this;
			this._ie = $.browser.msie;
			this._ie6 = $.browser.msie && $.browser.version.indexOf('6.') == 0;
			this._ie8 = $.browser.msie && $.browser.version.indexOf('8.') == 0;
			this._items = items;
			this._timer = null;
			this._nav = null;
			this._index = 0;
			this._width = 0;

			if (this._items.length == 0) return;

			var orgParent = this._items.parent();

			this._items
				.css('float', 'left')
				.addClass('slider_item');
			orgParent.css({ 'overflow': 'hidden', 'position': 'relative' });

			this._parent = $('<div class="slider">')
				.prependTo(this._items.parent())
				.width(10000)
				.append(this._items)
				.append('<div class="cleaner">');

			this._parent.hover(
				function(e) { clearTimeout(me._timer); },
				function(e) { me.startTimer(); }
			);

			// Items navigation
			this._nav = $('<div class="slider_nav">');
			orgParent.append(this._nav);

			this._nav.hover(
				function(e) { clearTimeout(me._timer); },
				function(e) { me.startTimer(); }
			);

			var navWidth = 0;
			$.each(this._items, function(index, item) {
				var l = $('<a>');
				me._nav.append(l);
				l.html(index + 1);
				l.click(function(e) {
					me.move(index);
				});

				// Save item position
				var $item = $(item);
				var pos = $item.position();
				$item.data('pos', pos);

				// Calculate max width (items and nav links)
				var w = $item.outerWidth();
				if (w > me._width) me._width = w;
				navWidth += l.outerWidth(true);
			});

			this._nav
				.css('position', 'absolute')
				.append('<div class="cleaner">')
				.css('top', this._parent.innerHeight() - this._nav.outerHeight())
				.css('left', this._width - navWidth)
				.width(navWidth);

			this.selectNavLink(this._index);
			this.startTimer();

			orgParent.css({ 'visibility': 'visible', 'opacity': 0 })
				.animate(
				{
					'opacity': 1
				}, 500, function() { $(this).css({ 'filter': '' }) });
		},

		slide: function() {
			var me = this;
			clearTimeout(this._timer);

			var index = this._index + 1;
			if (index >= this._items.length) {
				index = 0;
			}
			this.move(index);
			this.startTimer();
		},

		move: function(index) {
			if (index == this._index) return;
			this._index = index;
			var me = this;
			var animateMargin = $(this._items[this._index]).data('pos').left;
			var links = null;
			if (me._ie8) links = this._parent.find('a').css({ backgroundImage: 'none' });
			this._parent.stop(true, false);
			this._parent.animate(
				{ marginLeft: -animateMargin },
				1000,
				function() { if (me._ie8) links.css({ backgroundImage: '' }); }
			);

			this.selectNavLink(this._index);
		},

		startTimer: function() {
			var me = this;
			this._timer = setTimeout(function() { me.slide(); }, $.fn.slide.options.changeTimeout);
		},

		selectNavLink: function(index) {
			var navLinks = this._nav.children('a').removeClass('selected');
			$(navLinks[index]).addClass('selected');
		}
	}
});
