(function($) {
	
	var jqsliderForBrand = function(sliderForBrand, opt)
	{
		var self = this;
		
		this.index = null;
		this.left_wrapper = null;
		this.count = 0;
		
		this.opt = opt;
		this.query_ul = sliderForBrand;
		this.query_li = $("li", this.query_ul);
		this.busy = false;
		this.svg_index = null;
		
		this.query_ul.parent().mouseenter(function(){self.stopTimer();});
		this.query_ul.parent().mouseleave(function(){
			self.startTimer();
			self.highLight(self.index);
		});
		this.query_li.each(function(){
			$(this).mouseover(function(){
				self.highLight(self.getIndex($(this)));
			});
		});
		
		if (typeof($.fn.mousewheel) == "function") {
			this.query_ul.mousewheel(function(event, delta) {
				if (!self.busy) {
					if (delta > 0) self.goAnimate("next", self.index + self.opt.jump);
					else self.goAnimate("prev", self.index - self.opt.jump);
				}
			});
		}
		
		this.templateUl();
		this.templateLi();
		this.templateWrapper();
		this.templateParent();
		
		var random_index = parseInt(Math.floor(Math.random() * this.query_li.length));
		
		if (random_index < 0) random_index = 0;
		if (random_index > this.query_li.length) random_index = this.query_li.length - 1;		
		this.firstAnime(random_index);
	};
	
/***************************************************************
 * 						TEMPLATE	UL							
 ***************************************************************/
	jqsliderForBrand.prototype.templateUl = function()
	{
		var self = this;
	
		this.query_ul.css({
			width: self.countLi(),
			height: self.query_li.height(),
			position: "absolute"
		});
	};
	
/***************************************************************
 * 						TEMPLATE	LI							
 ***************************************************************/
	jqsliderForBrand.prototype.templateLi = function()
	{
		var self = this;
		
		this.query_li.css({	
			float: "left",
			opacity: self.opt.opacity
		});
	};
	
/***************************************************************
 * 					TEMPLATE	WRAPPER							
 ***************************************************************/
	jqsliderForBrand.prototype.templateWrapper = function()
	{
		var self = this;
				
		$("<div class=\"wrapper_ul\"></div>").insertBefore(this.query_ul);
		$(".wrapper_ul").css({
			width: self.countLi(),
			height: self.query_li.height(),
			position: "relative"
		});
		this.query_ul.appendTo(".wrapper_ul");
	};
	
/***************************************************************
 * 				TEMPLATE	PARENT	WRAPPER							
 ***************************************************************/
	jqsliderForBrand.prototype.templateParent = function()
	{
		var self = this;
		
		this.width_parent = $(".wrapper_ul").parent().width();
		$(".wrapper_ul").parent().css({ overflow: "hidden" });
	};
	
/***************************************************************
 * 						COUNT	LI							
 ***************************************************************/
	jqsliderForBrand.prototype.countLi = function()
	{
		var self = this;
		
		var count = 0;
		var width_li = 0;
		
		for (count; count < this.query_li.length; count++)
		{
			width_li += this.widthLi(count);
		}
		
		return width_li;
	};
	
/***************************************************************
 * 						WIDTH	LI							
 ***************************************************************/	
	jqsliderForBrand.prototype.widthLi = function(index)
	{
		var self = this;
		
		return this.query_li.eq(index).children().outerWidth(true) 
				+ parseInt(this.query_li.eq(index).css("marginLeft"))
				+ parseInt(this.query_li.eq(index).css("marginRight"));
	};
	
/***************************************************************
 * 						POSITION	LI							
 ***************************************************************/
	jqsliderForBrand.prototype.positionLi = function(index)
	{
		var self = this;
		
		return this.query_li.eq(index).position();
	};
	
/***************************************************************
 * 						POSITION	Ul							
 ***************************************************************/
	jqsliderForBrand.prototype.positionUl = function()
	{
		var self = this;
		
		return this.query_ul.position();
	};
	
/***************************************************************
 * 						FIRST	ANIME							
 ***************************************************************/
	jqsliderForBrand.prototype.firstAnime = function(index)
	{
		var self = this;
		
		this.index = index;
		
		var left = (this.positionLi(index).left + (this.widthLi(index) / 2)) - (this.width_parent / 2);
		this.left_wrapper = ($(".wrapper_ul").width() - this.width_parent) / 2;
		$(".wrapper_ul").css("left", -this.left_wrapper);
		
		this.query_ul.css("left", left);
		this.addButton("prev");
		this.addButton("next");
		this.startTimer();
		this.highLight(index);
		this.goAnimate("next", index, true);
	};
	
/***************************************************************
 * 						GO	ANIMATE							
 ***************************************************************/
	jqsliderForBrand.prototype.goAnimate = function (action, index, is_first)
	{		
		var self = this;
		
		this.index = index;
		this.busy = true;
		
		var info = new Object();
		var pos_wrapper_ul = $(".wrapper_ul").position();
		var pos_ul = $(".wrapper_ul").children().position();
		var new_pos_li = this.positionLi(self.index).left - (-pos_wrapper_ul.left - this.positionUl().left);
		
		switch (action)
		{
			case "next":
				info.move = ((new_pos_li + (this.widthLi(index) / 2)) - (this.width_parent / 2)) - this.positionUl().left;
				info.signe = info.move < 0 ? "" : "-";
				break;
			case "prev":
				info.signe = "";
				info.move = (this.width_parent / 2) - (new_pos_li + (this.widthLi(index) / 2)) + this.positionUl().left;
				break;
			default:
				return;
		}
				
		this.query_ul.animate(
			{left: info.signe + Math.abs(info.move) },
			self.opt.speed,
			self.opt.easein,
			function ()
			{
				self.highLight(index);
				if (is_first === true) {
					self.adjustLi("next", is_first);
					self.adjustLi("prev", is_first);
				}
				else {
					self.adjustLi(action);
				}
				self.busy = false;
			}
		);
		
		this.svg_index = index;
	};
	
/***************************************************************
 * 						ADJUST LI							
 ***************************************************************/
	jqsliderForBrand.prototype.adjustLi = function (action, is_first)
	{
		var self = this;
		
		var filter1 = (action == "next") ? ":first" : ":last";
		var filter2 = null;
		var width_li = this.query_li.filter(filter1).outerWidth(true) 
			+ parseInt(this.query_li.filter(filter1).css("marginLeft"))
			+ parseInt(this.query_li.filter(filter1).css("marginRight"));
		var css_left = null;
		var condition = null;
				
		switch (action)
		{
			case "next":
				filter2 = ":last";
				css_left = this.positionUl().left + width_li;
				condition = (-this.positionUl().left > width_li / 2) ? true : false;
				break;
			case "prev":
				filter2 = ":first";
				css_left = this.positionUl().left - width_li;
				condition = (this.positionUl().left > width_li / 2) ? true : false;
				break;
			default:
				return;
		}
		
		if (condition === true) {
			if (action == "next") this.query_li.filter(filter1).insertAfter(self.query_li.filter(filter2));
			else this.query_li.filter(filter1).insertBefore(self.query_li.filter(filter2));
			
			this.query_ul.css("left", css_left);
			this.query_li = $("li", this.query_ul);
			
			if (is_first === undefined) action == "next" ? this.index-- : this.index++;
			
			if (is_first === true)	this.adjustLi(action, undefined);
			else this.adjustLi(action, undefined);
		}
	};
	
/***************************************************************
 * 						OPACITY MOD							
 ***************************************************************/
	jqsliderForBrand.prototype.highLight = function(index)
	{
		var self = this;
		
		this.query_li.css("opacity", self.opt.opacity);
		this.query_li.eq(index).css("opacity", 1);
	};
	
/***************************************************************
 * 						ADD BUTTON							
 ***************************************************************/
	jqsliderForBrand.prototype.addButton = function (action)
	{
		var self = this;
		
		var info = new Object();
		
		switch (action)
		{
			case "prev":
				info.css = {
					height: $(".wrapper_ul").height(),
					position: "absolute",
					top: 0,
					left: 0
				};
				info.txt = this.opt.txt_prev;
				break;
			case "next":
				info.css = {
					height: $(".wrapper_ul").height(),
					position: "absolute",
					top: 0,
					right: 0
				};
				info.txt = this.opt.txt_next;
				break;
			default:
				return;
		}
		
		$("<a href=\"javascript:void(0)\" class=\"button-" + action + "\"><span>" + info.txt + "</span></a>")
			.insertAfter(".wrapper_ul")
			.css( info.css );
		
		$(".button-" + action).click(function() { 
			if (!self.busy) 
				self.goAnimate(action, action == "next" ? self.index + self.opt.jump : self.index - self.opt.jump) 
			});
	};
	
/***************************************************************
 * 						START TIMER							
 ***************************************************************/
	jqsliderForBrand.prototype.startTimer = function()
	{
		var self = this;
		
		this.interval = setInterval(function(){
			self.goAnimate(self.opt.sens_auto, self.opt.sens_auto == "next" ? self.index + self.opt.jump : self.index - self.opt.jump) },
			self.opt.time_auto);
	};
	
/***************************************************************
 * 						STOP TIMER							
 ***************************************************************/
	jqsliderForBrand.prototype.stopTimer = function()
	{
		var self = this;
		
		clearInterval(this.interval);
	};

/***************************************************************
 * 						GET INDEX							
 ***************************************************************/
	jqsliderForBrand.prototype.getIndex = function(elem)
	{
		return this.query_li.index(elem);
	};
	
/***************************************************************
 * 							MAIN							
 ***************************************************************/
	$.fn.sliderForBrand = function(options)
	{
		if(this.length < 1) return this;
		var options = $.extend($.fn.sliderForBrand.defaults, options);
		new jqsliderForBrand(this, options);
		return this;
	};
	
/***************************************************************
 * 							DEFAULT							
 ***************************************************************/
	$.fn.sliderForBrand.defaults = {
		speed: 250,
		easein: "linear",
		jump: 1,
		time_auto: 5000,
		sens_auto: "next",
		txt_prev: "Preview",
		txt_next: "Next",
		opacity: 0.25,
		cookie_name: "sliderForBrand_index"
	};
	
})(jQuery);


