//waits until dom loaded
$(function(){

	var g = $('div.visual').gallery({
		duration: 800,
		effect:'fade',
		listOfSlides: 'ul.img-holder > li',
		switcher: '.item-list > li'
		, IE: false
		, autoRotation: 6000
	});
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: true,
		addClassFocus: "focus",
		filterClass: "default"
	});
	addclass();
	
	$('.item-list').hover( function() {g.stop();}, function(){g.play();});
	$('.article-holder').hover( function() {g.stop();}, function(){g.play();});
	
});

(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,
				playing: true,
				IE: true
			}, 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.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.list.css({display: 'none'}).removeClass('active').eq(this.active).addClass('active').css({display: 'block'});
				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);
			
			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.css({display: 'none'}).removeClass('active').eq(this.active).addClass('active').css({display: 'block'});
			}
			else{
			
				var p = this.list.eq(this.last);
				var d = this.options.duration;
				
				var l = this.list.removeClass('active').eq(this.active).addClass('active'); //set active
				var c = l.children();
				
				c.eq(0).css({opacity:0}); //hide silh
				c.eq(1).css({opacity:0}); //hide people
				c.eq(2).css({left: -384}); //start off screen
				l.css({display: 'block'}); //show parent
				
				//transition in children
				//bg
				var layer2 = c.eq(1);
				var layer3 = c.eq(2);
				
				//slide off article 
				p.children().eq(2).animate({left:-384}, {queue:false, duration: 500
					, complete: function(){
							//fade out
							p.children().eq(0).animate({opacity:0}, {queue:false, duration: 500}); 
							p.children().eq(1).animate({opacity:0}, {queue:false, duration: 500}); 
							
							c.eq(0).animate({
									opacity:1
								}, {queue:false, duration: 500, complete: function(){
								
									$(this).css('opacity','auto');
									
									layer2.delay(250).animate({
										opacity:1
											}, {queue:false, duration: 500, complete: function(){
												$(this).css('opacity','auto');
												layer2 = null;
												
											}
										});
										
									layer3.delay(500).animate({
										left:1
											}, {queue:false, duration: 500, complete: function(){
												$(this).css('opacity','auto');
												layer3 = null;
											}
										});
								}
							});
							
						}
					});
				
				
			}
			
			if (this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
			this.last = this.active;
		},
		
		/*scrollElement: function(){
			if (!this.options.direction) this.list.animate({marginLeft: -(this.woh * this.active)}, {queue:false, duration: this.options.duration})
			else this.list.animate({marginTop: -(this.woh * this.active)}, {queue:false, duration: this.options.duration})
			if (this.options.switcher) this.switcher.removeClass('active').eq(this.active).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.click(function(){
				$this.active = $this.switcher.index($(this));
				if ($this.active == $this.last) return; //ignore if already there
				
				if($this._t) clearTimeout($this._t);
				/*if (!$this.options.effect) $this.scrollElement();
				else*/ $this.fadeElement();
				if ($this.options.autoRotation && $this.playing) $this.runTimer($this);
				return false;
			});
		},
		/*initEvent: function($this, addEventEl, addDisClass, dir){
			addEventEl.click(function(){
				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(){
			this.playing = false;
			if (this._t) clearTimeout(this._t);
		},
		play: function(){
			this.playing = true;
			if (this._t) clearTimeout(this._t);
			if (this.options.autoRotation) this.runTimer(this);
		}
	}
}(jQuery));

// 
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";
	}
}

//join our mailing list box
function addclass(){
	var _link = $('div.box-login').find('a.a-class');
	var _box = $('div.drop-login');
	var _holder = $('div.box-login');
		_link.click(function(){
		$(this).parent('div.box-login').toggleClass('open');
		return false;
	});
	$(document).bind('click', function(e){
		if(_holder.hasClass('open')) {
			e = e || event;
			var t = e.target || e.srcElement;
			t = $(t);
			if(!(t.parents().hasClass('open')) && (t.attr('class') != _box.attr('class'))){
		_holder.removeClass('open');
	}
	}
});
}
