﻿(function($) {
	$.fn.gallery = function(options) { return new Gallery(this.get(0), options); };
	
	function Gallery(context, options) { this.init(context, options); };
	
	Gallery.prototype = {
		options:{},
		init: function (context, options){
			this.options = $.extend({
				duration: 700,									//duration of effect it 1000 = 1sec
				slideElement: 1,								//number of elements for a slide
				autoRotation: false,							//false = option is disabled; 1000 = 1sec
				effect: false,									//false = slide; true = fade
				listOfSlides: 'ul > li',						//elements galleries
				switcher: false,								//false = option is disabled; 'ul > li' = elements switcher
				disableBtn: false,								//false = option is disabled; 'hidden' = class adds an buttons "prev" and "next"
				nextBtn: 'a.link-next, a.btn-next, a.next',		//button "next"
				prevBtn: 'a.link-prev, a.btn-prev, a.prev',		//button "prev"
				circle: true,									//true = cyclic gallery; false = not cyclic gallery
				direction: false,								//false = horizontal; true = vertical
				event: 'click',									//event for the buttons and switcher
				IE: false,										//forced off effect it "fade" in IE
				initSwitch: function(){}
			}, options || {});
			var _el = $(context).find(this.options.listOfSlides);
			if (this.options.effect) this.list = _el;
			else this.list = _el.parent();
			this.switcher = $(context).find(this.options.switcher);
			this.nextBtn = $(context).find(this.options.nextBtn);
			this.prevBtn = $(context).find(this.options.prevBtn);
			this.count = _el.index(_el.filter(':last'));
			
			if (this.options.switcher) this.active = this.switcher.index(this.switcher.filter('.active:eq(0)'));
			else this.active = _el.index(_el.filter('.active:eq(0)'));
			if (this.active < 0) this.active = 0;
			this.last = this.active;
			
			this.woh = _el.outerWidth(true);
			if (!this.options.direction) this.installDirections(this.list.parent().width());
			else {
				this.woh = _el.outerHeight(true);
				this.installDirections(this.list.parent().height());
			}
			
			if (!this.options.effect) {
				this.rew = this.count - this.wrapHolderW + 1;
				if (!this.options.direction) this.anim = '{marginLeft: -(this.woh * this.active)}';
				else this.anim = '{marginTop: -(this.woh * this.active)}';
				eval('this.list.css('+this.anim+')');
			}
			else {
				this.rew = this.count;
				this.list.css({opacity: 0}).removeClass('active').eq(this.active).addClass('active').css({opacity: 1}).css('opacity', 'auto');
				this.switcher.removeClass('active').eq(this.active).addClass('active');
			}
			
			this.initEvent(this, this.nextBtn, true);
			this.initEvent(this, this.prevBtn, false);
			if (this.options.disableBtn) this.initDisableBtn();
			if (this.options.autoRotation) this.runTimer(this);
			if (this.options.switcher) this.initEventSwitcher(this, this.switcher);
		},
		initDisableBtn: function(){
			this.prevBtn.removeClass('prev-'+this.options.disableBtn);
			this.nextBtn.removeClass('next-'+this.options.disableBtn);
			if (this.active == 0 || this.count+1 == this.wrapHolderW) this.prevBtn.addClass('prev-'+this.options.disableBtn);
			if (this.active == 0 && this.count == 1 || this.count+1 == this.wrapHolderW) this.nextBtn.addClass('next-'+this.options.disableBtn);
			if (this.active == this.rew) this.nextBtn.addClass('next-'+this.options.disableBtn);
		},
		installDirections: function(temp){
			this.wrapHolderW = Math.ceil(temp / this.woh);
			if (((this.wrapHolderW - 1) * this.woh + this.woh / 2) > temp) this.wrapHolderW--;
		},
		fadeElement: function(){
			if ($.browser.msie && this.options.IE){
				this.list.eq(this.last).css({opacity:0});
				this.list.removeClass('active').eq(this.active).addClass('active').css({opacity:'auto'});
			}
			else{
				this.list.eq(this.last).animate({opacity:0}, {queue:false, duration: this.options.duration});
				this.list.removeClass('active').eq(this.active).addClass('active').animate({
					opacity:1
				}, {queue:false, duration: this.options.duration, complete: function(){
					$(this).css('opacity','auto');
				}});
			}
			if (this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
			this.last = this.active;
		},
		scrollElement: function(){
			eval('this.list.animate('+this.anim+', {queue:false, duration: this.options.duration});');
			if (this.options.switcher) this.switcher.removeClass('active').eq(this.active / this.options.slideElement).addClass('active');
		},
		runTimer: function($this){
			if($this._t) clearTimeout($this._t);
			$this._t = setInterval(function(){
				$this.toPrepare($this, true);
			}, this.options.autoRotation);
		},
		initEventSwitcher: function($this, el){
			el.bind($this.options.event, function(){
				$this.active = $this.switcher.index($(this)) * $this.options.slideElement;
				if($this._t) clearTimeout($this._t);
				if ($this.options.disableBtn) $this.initDisableBtn();
				if (!$this.options.effect) $this.scrollElement();
				else $this.fadeElement();
				if ($this.options.autoRotation) $this.runTimer($this);
				$this.options.initSwitch();
				return false;
			});
		},
		initEvent: function($this, addEventEl, dir){
			addEventEl.bind($this.options.event, function(){
				if($this._t) clearTimeout($this._t);
				$this.toPrepare($this, dir);
				if ($this.options.autoRotation) $this.runTimer($this);
				return false;
			});
		},
		toPrepare: function($this, side){
			if (($this.active == $this.rew) && $this.options.circle && side) $this.active = -$this.options.slideElement;
			if (($this.active == 0) && $this.options.circle && !side) $this.active = $this.rew + $this.options.slideElement;
			for (var i = 0; i < $this.options.slideElement; i++){
				if (side) { if ($this.active + 1 <= $this.rew) $this.active++; }
				else { if ($this.active - 1 >= 0) $this.active--; }
			};
			if (this.options.disableBtn) this.initDisableBtn();
			if (!$this.options.effect) $this.scrollElement();
			else $this.fadeElement();
		},
		stop: function(){
			if (this._t) clearTimeout(this._t);
		},
		play: function(){
			if (this._t) clearTimeout(this._t);
			if (this.options.autoRotation) this.runTimer(this);
		}
	}
}(jQuery));

$(document).ready(function(){
	var autoRotate = 7000;
	var gal1 = $('div.header').gallery({
		duration: 500,
		effect:true,
		listOfSlides: 'div.slider1 div.hold ul > li',
		switcher:'div.slider2 div.hold ul > li',
		initSwitch: function(){
			$(window).trigger('nextSwitch');
		}
	});
	var gal2 = $('div.header').gallery({
		duration: 500,
		listOfSlides: 'div.slider2 div.hold ul > li'
	});
	$('div.header').each(function(){
		var hold = $(this);
		var linkNext = hold.find('div.next');
		var linkPrev = hold.find('div.prev');
		var slider = hold.find('div.slider1 ul.nav > li > span');
		var _time;
		linkNext.click(function(){
			linkNext.removeClass('next-dis');
			linkPrev.removeClass('prev-dis');
			if(_time) clearTimeout(_time);
			if (gal1.active > gal2.wrapHolderW - 2) {
				if(gal2.rew > gal2.active) gal2.toPrepare(gal2, true);
			}
			if (gal1.active == gal1.count) gal2.toPrepare(gal2, true);
			gal1.toPrepare(gal1, true);
			slider.removeClass('active').eq(gal1.active).addClass('active');
			if(gal1.active == 0) linkPrev.addClass('prev-dis');
			if(gal1.active == gal1.count) linkNext.addClass('next-dis');
			runInterval();
			return false;
		});
		linkPrev.click(function(){
			linkNext.removeClass('next-dis');
			linkPrev.removeClass('prev-dis');
			if(_time) clearTimeout(_time);
			if (gal1.active-1 < gal2.rew && gal1.active != 0) {
				if(gal2.active != 0 && gal2.active != 0) gal2.toPrepare(gal2, false);
			}
			if (gal1.active == 0) gal2.toPrepare(gal2, false);
			gal1.toPrepare(gal1, false);
			slider.removeClass('active').eq(gal1.active).addClass('active');
			if(gal1.active == gal1.count) linkNext.addClass('next-dis');
			if(gal1.active == 0) linkPrev.addClass('prev-dis');
			runInterval();
			return false;
		});
		$(window).bind('nextSwitch', function(){
			linkNext.removeClass('next-dis');
			linkPrev.removeClass('prev-dis');
			slider.removeClass('active').eq(gal1.active).addClass('active');
			if(gal1.active == 0) linkPrev.addClass('prev-dis');
			if(gal1.active == gal1.count) linkNext.addClass('next-dis');
		});
		function runInterval(){
			_time = setInterval(function(){
				linkNext.trigger('click');
			}, autoRotate);
		}
		runInterval();
	});
	$('select.customSelect').customSelect();
	gallery2();
});

function gallery2(){
	var gal3 = $('div.item-main').gallery({
		duration: 500,
		effect:true,
		listOfSlides: 'div.photo-big ul > li',
		switcher:'div.slider3 div.hold ul > li',
		initSwitch: function(){
			$(window).trigger('nextSwitch2');
		}
	});
	var gal4 = $('div.item-main').gallery({
		duration: 500,
		listOfSlides: 'div.slider3 div.hold ul > li'
	});
	$('div.item-main').each(function(){
		var hold = $(this);
		var linkNext = hold.find('div.next');
		var linkPrev = hold.find('div.prev');
		var slider = hold.find('div.photo-big ul > li');
		var _time;
		linkNext.click(function(){
			linkNext.removeClass('next-dis');
			linkPrev.removeClass('prev-dis');
			if(_time) clearTimeout(_time);
			if (gal3.active > gal4.wrapHolderW - 2) {
				if(gal4.rew > gal4.active) gal4.toPrepare(gal4, true);
			}
			if (gal3.active == gal3.count) gal4.toPrepare(gal4, true);
			gal3.toPrepare(gal3, true);
			slider.removeClass('active').eq(gal3.active).addClass('active');
			if(gal3.active == 0) linkPrev.addClass('prev-dis');
			if(gal3.active == gal3.count) linkNext.addClass('next-dis');
			return false;
		});
		linkPrev.click(function(){
			linkNext.removeClass('next-dis');
			linkPrev.removeClass('prev-dis');
			if(_time) clearTimeout(_time);
			if (gal3.active-1 < gal4.rew && gal3.active != 0) {
				if(gal4.active != 0 && gal4.active != 0) gal4.toPrepare(gal4, false);
			}
			if (gal3.active == 0) gal4.toPrepare(gal4, false);
			gal3.toPrepare(gal3, false);
			slider.removeClass('active').eq(gal3.active).addClass('active');
			if(gal3.active == gal3.count) linkNext.addClass('next-dis');
			if(gal3.active == 0) linkPrev.addClass('prev-dis');
			return false;
		});
		$(window).bind('nextSwitch2', function(){
			linkNext.removeClass('next-dis');
			linkPrev.removeClass('prev-dis');
			slider.removeClass('active').eq(gal3.active).addClass('active');
			if(gal3.active == 0) linkPrev.addClass('prev-dis');
			if(gal3.active == gal3.count) linkNext.addClass('next-dis');
		});
	});
}

jQuery.fn.customSelect = function(_options) {
var _options = jQuery.extend({
	selectStructure: '<div class="selectArea"><div class="selected"></div></div>',
	selectText: '.selected',
	selectBtn: '.selected',
	selectDisabled: '.disabled',
	optStructure: '<div class="select-sub"><ul></ul></div>',
	optList: 'ul'
}, _options);
return this.each(function() {
	var select = jQuery(this);
	if(!select.hasClass('outtaHere')) {
		if(select.is(':visible')) {
			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(select.attr('disabled')) selectDisabled.show();
			select.find('option').each(function() {
				var selOpt = $(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');
					$(this).parent().addClass('selected');
					selOpt.attr('selected', 'selected');
					selectText.html(selOpt.html());
					select.change();
					optHolder.hide();
					return false;
				});
				optList.append(_opt);
			});
			if (select.attr('title')) selectText.html(select.attr('title'));
			replaced.width(select.outerWidth()+12);
			replaced.insertBefore(select);
			replaced.addClass(select.attr('class'));
				optHolder.css({
					width: select.outerWidth()+14,
					display: 'none',
					position: 'absolute',
					zIndex: 999
				});
			optHolder.addClass(select.attr('class'));
			jQuery(document.body).append(optHolder);
			
			var optTimer;
			replaced.hover(function() {
				if(optTimer) clearTimeout(optTimer);
			}, function() {
				optTimer = setTimeout(function() {
					optHolder.hide();
				}, 200);
			});
			optHolder.hover(function(){
				if(optTimer) clearTimeout(optTimer);
			}, function() {
				optTimer = setTimeout(function() {
					optHolder.hide();
				}, 200);
			});
			selectBtn.click(function() {
				if(optHolder.is(':visible')) {
					optHolder.hide();
				}
				else{
					optHolder.children('ul').css({height:'auto', overflow:'hidden'});
					optHolder.css({
						top: replaced.offset().top + replaced.outerHeight() +2,
						left: replaced.offset().left,
						display: 'block'
					});
					//if(optHolder.children('ul').height() > 100) optHolder.children('ul').css({height:100, overflow:'auto'});
				}
				return false;
			});
			select.addClass('outtaHere');
		}
	}
});
}
jQuery.fn.customRadio = function(_options){
	var _options = jQuery.extend({
		radioStructure: '<span></span>',
		radioDisabled: 'disabled',
		radioDefault: 'radioArea',
		radioChecked: 'radioAreaChecked'
	}, _options);
	return this.each(function(){
		var radio = jQuery(this);
		if(!radio.hasClass('outtaHere') && radio.is(':radio')){
			var replaced = jQuery(_options.radioStructure);
			replaced.addClass(radio.attr('class'));
			this._replaced = replaced;
			if(radio.is(':disabled')) replaced.addClass(_options.radioDisabled);
			else if(radio.is(':checked')) replaced.addClass(_options.radioChecked);
			else replaced.addClass(_options.radioDefault);
			replaced.click(function(){
				if($(this).hasClass(_options.radioDefault)){
					radio.change();
					radio.attr('checked', 'checked');
					changeRadio(radio.get(0));
				}
			});
			radio.click(function(){
				changeRadio(this);
			});
			replaced.insertBefore(radio);
			radio.addClass('outtaHere');
		}
	});
	function changeRadio(_this){
		$('input:radio[name='+$(_this).attr("name")+']').not(_this).each(function(){
			if(this._replaced && !$(this).is(':disabled')) this._replaced.removeClass('radioAreaChecked').removeClass('radioArea').addClass(_options.radioDefault);
		});
		_this._replaced.removeClass('radioAreaChecked').removeClass('radioArea').addClass(_options.radioChecked);
	}
}
jQuery.fn.customCheckbox = function(_options){
	var _options = jQuery.extend({
		checkboxStructure: '<span></span>',
		checkboxDisabled: 'disabled',
		checkboxDefault: 'checkboxArea',
		checkboxChecked: 'checkboxAreaChecked'
	}, _options);
	return this.each(function(){
		var checkbox = jQuery(this);
		if(!checkbox.hasClass('outtaHere') && checkbox.is(':checkbox')){
			var replaced = jQuery(_options.checkboxStructure);
			replaced.addClass(checkbox.attr('class'));
			this._replaced = replaced;
			if(checkbox.is(':disabled')) replaced.addClass(_options.checkboxDisabled);
			else if(checkbox.is(':checked')) replaced.addClass(_options.checkboxChecked);
			else replaced.addClass(_options.checkboxDefault);
			
			replaced.click(function(){
				if(checkbox.is(':checked')) checkbox.removeAttr('checked');
				else checkbox.attr('checked', 'checked');
				changeCheckbox(checkbox);
			});
			checkbox.click(function(){
				changeCheckbox(checkbox);
			});
			replaced.insertBefore(checkbox);
			checkbox.addClass('outtaHere');
		}
	});
	function changeCheckbox(_this){
		if(_this.is(':checked')) _this.get(0)._replaced.removeClass('checkboxArea').removeClass('checkboxAreaChecked').addClass(_options.checkboxChecked);
		else _this.get(0)._replaced.removeClass('checkboxArea').removeClass('checkboxAreaChecked').addClass(_options.checkboxDefault);
	}
	
}

