jQuery(function(){
	initPlugins();
	jQuery('select').customSelect();
	initGallery();
	jQuery('.items > ul > li').mouseenter(function(){
		jQuery(this).addClass('hover');
	}).mouseleave(function(){
		jQuery(this).removeClass('hover');
	});
	initNav();
});

function initNav(){
	var holder = jQuery('#nav').css({position:'relative'});
	holder.find('>li').each(function (){
		var hold = jQuery(this);
		var drop = hold.find('.drop');
		var _beg = hold.position().left + parseInt(drop.css('left'));
		var _end = holder.width() - _beg - drop.width();
		if (_beg < 0) drop.css('left', parseInt(drop.css('left'))-_beg);
		if (_end < 0) drop.css('left', -(hold.position().left + drop.width())+ holder.width()+6);
	});
};

function initGallery(){
	var products_per_page = window.products_per_page;
	
	jQuery('div.g1').each(function(){
		
		var _this = jQuery(this);
		var list = _this.find('div.long'),
			prev = _this.find('ul.switcher a.prev').attr('rel', 'prev'),
			next = _this.find('ul.switcher a.next').attr('rel', 'next'),
			count = list.children().length,
			w = list.children().eq(0).outerWidth(true),
			_t,
			a = 0;
		
		var _fL = true;
		var _fR = true;
		
		disableBtn();
		
		function disableBtn(){
			if (a == 0 && count == 1) {
				prev.addClass('disabled');
				next.addClass('disabled');
				_fL = false;
				_fR = false;
			} else if(a == 0) {
				next.removeClass('disabled');
				prev.addClass('disabled');
				_fL = false;
				_fR = true;
			}
			else if(a == count-1) {
				next.addClass('disabled');
				prev.removeClass('disabled');
				_fL = true;
				_fR = false;
			}
			else{
				prev.removeClass('disabled');
				next.removeClass('disabled');
				_fL = true;
				_fR = true;
			}
		}
		
		var switcher = _this.find('ul.paging-list');
		switcher.each(function(){
			var _list = jQuery(this);
			_list.children().click(function(){
				var ind = _list.children().index(jQuery(this));
				switcher.children().removeClass('active');
				switcher.each(function(){
					jQuery(this).children().eq(ind).addClass('active');
				});
				animateSlide(ind);
				
				var max = parseInt(jQuery(".number .all").html());
				jQuery(".number .start").html(Math.min(max,(parseInt(jQuery(this).find("a").html())-1)*products_per_page+1));
				jQuery(".number .end").html(Math.min(max,(parseInt(jQuery(this).find("a").html()))*products_per_page));
				
				return false;
			});
		});
		
		next.click(animateSlide);
		prev.click(animateSlide);
		
		
		function animateSlide(e){
			if(typeof e == 'number') a=e;
			else if(typeof e == 'string' && e == 'next') a++
			else{
				if(e.target.rel == 'next' && _fR) a++;
				else if(e.target.rel == 'prev' && _fL) a--;
			}
			if(_t) clearTimeout(_t);
			if(a == count) a=0;
			else if(a == -1) a=count-1;
			list.animate({marginLeft:-w*a}, {queue:false, duration:700});
			disableBtn();
			
			switcher.children().removeClass('active');
			switcher.each(function(){
				jQuery(this).children().eq(a).addClass('active');
				var max = parseInt(jQuery(".number .all").html());
				jQuery(".number .start").html(Math.min(max,(parseInt(jQuery(this).children().eq(a).text())-1)*products_per_page+1));
				jQuery(".number .end").html(Math.min(max,(parseInt(jQuery(this).children().eq(a).text()))*products_per_page));
			});
			return false;
		}
	});
}

function initPlugins(){
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: false,
		addClassFocus: "focus",
		filterClass: "default"
	});
	jQuery('body').NVhoverIE({
		hoverItem:'.nav > li, .text-p p'
	});
}

jQuery.fn.NVhoverIE = function(_options){
	var _options = jQuery.extend({
		hoverItem:'div',
		hoverClass: 'hover'
		
	},_options);

	return this.each(function(){
		var _this = jQuery(this);
		var _hoverItem = jQuery(_options.hoverItem, _this);
		var _hoverClass = _options.hoverClass;
		if (jQuery.browser.msie && jQuery.browser.version < 7) {
			_hoverItem.each(function(){
				jQuery(this).hover(
					function(){
						jQuery(this).addClass(_hoverClass)
					},
					function(){
						jQuery(this).removeClass(_hoverClass)
					}
				)
			})
		}
	});
}

function clearFormFields(o){
	if (o.clearInputs == null) o.clearInputs = true;
	if (o.clearTextareas == null) o.clearTextareas = true;
	if (o.passwordFieldText == null) o.passwordFieldText = false;
	if (o.addClassFocus == null) o.addClassFocus = false;
	if (!o.filter) o.filter = "default";
	if(o.clearInputs) {
		var inputs = document.getElementsByTagName("input");
		for (var i = 0; i < inputs.length; i++ ) {
			if((inputs[i].type == "text" || inputs[i].type == "password") && inputs[i].className.indexOf(o.filterClass)) {
				inputs[i].valueHtml = inputs[i].value;
				inputs[i].onfocus = function ()	{
					if(this.valueHtml == this.value) this.value = "";
					if(this.fake) {
						inputsSwap(this, this.previousSibling);
						this.previousSibling.focus();
					}
					if(o.addClassFocus && !this.fake) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				inputs[i].onblur = function () {
					if(this.value == "") {
						this.value = this.valueHtml;
						if(o.passwordFieldText && this.type == "password") inputsSwap(this, this.nextSibling);
					}
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
				if(o.passwordFieldText && inputs[i].type == "password") {
					var fakeInput = document.createElement("input");
					fakeInput.type = "text";
					fakeInput.value = inputs[i].value;
					fakeInput.className = inputs[i].className;
					fakeInput.fake = true;
					inputs[i].parentNode.insertBefore(fakeInput, inputs[i].nextSibling);
					inputsSwap(inputs[i], null);
				}
			}
		}
	}
	if(o.clearTextareas) {
		var textareas = document.getElementsByTagName("textarea");
		for(var i=0; i<textareas.length; i++) {
			if(textareas[i].className.indexOf(o.filterClass)) {
				textareas[i].valueHtml = textareas[i].value;
				textareas[i].onfocus = function() {
					if(this.value == this.valueHtml) this.value = "";
					if(o.addClassFocus) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				textareas[i].onblur = function() {
					if(this.value == "") this.value = this.valueHtml;
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
			}
		}
	}
	function inputsSwap(el, el2) {
		if(el) el.style.display = "none";
		if(el2) el2.style.display = "inline";
	}
}

jQuery.fn.customSelect = function(_options) {
	var _options = jQuery.extend({
		selectStructure: '<div class="selectArea"><div class="left"></div><div class="center"></div><a href="#" class="selectButton">&nbsp;</a><div class="disabled"></div></div>',
		hideOnMouseOut: true,
		copyClass: false,
		selectText: '.center',
		selectBtn: '.selectButton',
		selectDisabled: '.disabled',
		optStructure: '<div class="selectOptions"><div class="select-top"></div><ul></ul><div class="select-bottom"></div></div>',
		optList: 'ul'
	}, _options);
	return this.each(function() {
		var select = jQuery(this);
		if(!select.hasClass('outtaHere')) {
			if(select.is(':visible')) {
				var hideOnMouseOut = _options.hideOnMouseOut;
				var copyClass = _options.copyClass;
				var replaced = jQuery(_options.selectStructure);
				var selectText = replaced.find(_options.selectText);
				var selectBtn = replaced.find(_options.selectBtn);
				var selectDisabled = replaced.find(_options.selectDisabled).hide();
				var optHolder = jQuery(_options.optStructure);
				var optList = optHolder.find(_options.optList);
				if(copyClass) optHolder.addClass(select.attr('class'));


				if(select.attr('disabled')) selectDisabled.show();
				select.find('option').each(function() {
					var selOpt = jQuery(this);
					var _opt = jQuery('<li><a href="#">' + selOpt.html() + '</a></li>');
					if(selOpt.attr('selected')) {
						selectText.html(selOpt.html());
						_opt.addClass('selected');
					}
					_opt.children('a').click(function() {
						optList.find('li').removeClass('selected');
						select.find('option').removeAttr('selected');
						jQuery(this).parent().addClass('selected');
						selOpt.attr('selected', 'selected');
						selectText.html(selOpt.html());
						select.change();
						optHolder.hide();
						return false;
					});
					optList.append(_opt);
				});
				replaced.width(select.outerWidth());
				replaced.insertBefore(select);
				optHolder.css({
					width: select.outerWidth(),
					display: 'none',
					position: 'absolute'
				});
				jQuery(document.body).append(optHolder);
				
				var optTimer;
				replaced.hover(function() {
					if(optTimer) clearTimeout(optTimer);
				}, function() {
					if(hideOnMouseOut) {
						optTimer = setTimeout(function() {
							optHolder.hide();
						}, 200);
					}
				});
				optHolder.hover(function(){
					if(optTimer) clearTimeout(optTimer);
				}, function() {
					if(hideOnMouseOut) {
						optTimer = setTimeout(function() {
							optHolder.hide();
						}, 200);
					}
				});
				selectBtn.click(function() {
					if(optHolder.is(':visible')) {
						optHolder.hide().removeClass('optionsDivVisible');
					}
					else{
						optHolder.children('ul').css({height:'auto', overflow:'hidden'});
						optHolder.css({
							top: replaced.offset().top + replaced.outerHeight(),
							left: replaced.offset().left,
							display: 'block'
						}).addClass('optionsDivVisible');
						if(optHolder.children('ul').height() > 200) optHolder.children('ul').css({height:200, overflow:'auto'});
					}
					return false;
				});
				select.addClass('outtaHere');
			}
		}
	});
}
