var isHandlerActive = true;
$(document).ready(function () {
    $('div.header-gallery').gallery({
        duration: 2000,
        //autorotation. 5000 = 5 sec
        autoRotation: 12000,
        listOfSlides: '.slide',
        switcher: '.swicher>li',
        effect: false,
        direction: false
    });
    $('div.gallery').gallery({
        duration: 500,
        listOfSlides: '.slide',
        //autorotation. 5000 = 5 sec

        effect: true
    });

    var countryDDL = $('select[name=m_field_id_11]');
    if (countryDDL.attr('selectedIndex') == 0) {
        countryDDL.attr('value', 'United States');
    }

    $('.linkselect').change(function () {
        window.location = $(this + "option:selected").attr('location');
    });
    arrowsPosition();
});
function arrowsPosition(){
	var holder = $('.header-gallery');
	holder.each(function(){
		var _this = $(this);
		var slides = _this.find('.slide-area');
		var slidesWidth = slides.outerWidth();
		var prev = _this.find('.prev-holder');
		var next = _this.find('.next-holder');
		setPosition();
		$(window).resize(function(){
			setPosition();
		});
		function setPosition(){
			prev.css({
				left: (_this.outerWidth() - slidesWidth)/4
			});
			next.css({
				right: (_this.outerWidth() - slidesWidth)/4
			});
		}
	});
}
(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,
				slideElement: 1,
				autoRotation: false,
				effect: false,
				listOfSlides: 'ul > li',
				switcher: false,
				disableBtn: false,
				nextBtn: 'a.link-next, a.btn-next, a.next',
				prevBtn: 'a.link-prev, a.btn-prev, a.prev',
				circle: true,
				direction: false,
				event: 'click',
				IE: false
			}, 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;
				//if (!this.options.direction) this.list.css({marginLeft: -(this.woh * this.active)});
				//else this.list.css({marginTop: -(this.woh * this.active)});
			}
			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');
			}
			
			if (this.options.disableBtn) {
				if (this.count < this.wrapHolderW) this.nextBtn.addClass(this.options.disableBtn);
				if (this.active == 0) this.prevBtn.addClass(this.options.disableBtn);
			}

			this.initEvent(this, this.nextBtn, this.prevBtn, true);
			this.initEvent(this, this.prevBtn, this.nextBtn, false);
			this.initPauseEvent(this, _el);
			
			if (this.options.autoRotation) this.runTimer(this);
			
			if (this.options.switcher) this.initEventSwitcher(this, this.switcher);
		},
		installDirections: function(temp){
			this.wrapHolderW = Math.ceil(temp / this.woh);
			if (((this.wrapHolderW - 1) * this.woh + this.woh / 2) > temp) this.wrapHolderWwrapHolderW--;
		},
		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(){			
			if(this.last != this.active && isHandlerActive)
			{							
				isHandlerActive = false;
				var activeList = this.list.find('.slide');
				var totalelements = activeList.size() - 1;
				var marginLeftFinish = -2000;
				var marginLeftStart = 2000;									
				
				if((this.active < this.last && this.last != totalelements) || (this.active == totalelements && this.last == 0) || (this.last==totalelements && this.active == totalelements-1))
				{
					marginLeftFinish = 2000;
					marginLeftStart = -2000;
				}
	// 			if (!this.options.direction) this.activeList.eq(this.active-1).animate({marginLeft: -960}, {queue:false, duration: this.options.duration});
		//		else this.list.animate({marginTop: -(this.woh * this.active)}, {queue:false, duration: this.options.duration});
							
				
				activeList.eq(this.last).animate({marginLeft:marginLeftFinish}, {queue:false, duration: this.options.duration, complete:(function () {			       
            			$(this).removeClass('active');
						$(this).css("margin-left", "960px");	
     			})});
				
				
				activeList.eq(this.active).css("margin-left", marginLeftStart);
				activeList.eq(this.active).addClass('active').animate({marginLeft:0}, {queue:true, duration: this.options.duration, complete:function() { isHandlerActive=true; }});
			
				
				if (this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
				this.last = this.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));
				if($this._t) clearTimeout($this._t);
				if (!$this.options.effect) $this.scrollElement();
				else $this.fadeElement();
				if ($this.options.autoRotation) $this.runTimer($this);
				return false;
			});
		},
		initPauseEvent: function($this, el)
		{
			if($this.options.autoRotation){
				el.bind('mouseenter', function(){
					clearTimeout($this._t); 
				});
				el.bind('mouseleave', function(){
					$this.runTimer($this);
				});
			}
		},
		initEvent: function($this, addEventEl, addDisClass, dir){
			addEventEl.bind($this.options.event, function(){
				if(isHandlerActive){					
					if($this._t) clearTimeout($this._t);
					if ($this.options.disableBtn &&($this.count > $this.wrapHolderW)) addDisClass.removeClass($this.options.disableBtn);
					$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) {
						if ($this.options.disableBtn && ($this.count > $this.wrapHolderW)) $this.nextBtn.addClass($this.options.disableBtn);
					}
					else $this.active++;
				}
				else{
					if ($this.active - 1 < 0) {
						if ($this.options.disableBtn && ($this.count > $this.wrapHolderW)) $this.prevBtn.addClass($this.options.disableBtn);
					}
					else $this.active--;
				}
			};
			if ($this.active == $this.rew && side) if ($this.options.disableBtn &&($this.count > $this.wrapHolderW)) $this.nextBtn.addClass($this.options.disableBtn);
			if ($this.active == 0 && !side) if ($this.options.disableBtn &&($this.count > $this.wrapHolderW)) $this.prevBtn.addClass($this.options.disableBtn);
			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));
