$(document).ready(function(){
	// IE6 hover function
	hoverForIE6('.submit-row input')
	//cufon
	initCufon();
	//clearFormFields
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: false,
		addClassFocus: "focus",
		filterClass: "default"
	});
	initGallery();
});

//cufon function
function initCufon() {
	Cufon.replace('.header-frame .date', { fontFamily: 'Skia'});
	Cufon.replace('.social-txt', { fontFamily: 'Skia'});
	Cufon.replace('#content h1', { fontFamily: 'Skia'});
	Cufon.replace('#content h2', { fontFamily: 'Skia'});
	Cufon.replace('.contact-block dt', { fontFamily: 'Skia'});
	Cufon.replace('#sidebar h3', { fontFamily: 'Skia'});
	Cufon.replace('.box-form-heading p', { fontFamily: 'Skia'});
	Cufon.replace('.home h2', { fontFamily: 'Skia'});
	Cufon.replace('.twitter-heading', { fontFamily: 'Skia'});
	Cufon.replace('#nav a', { fontFamily: 'Skia', hover: true});
	Cufon.replace('.gallery-holder h1', { fontFamily: 'Skia'});
	Cufon.replace('.twitter-heading', { fontFamily: 'Skia'});
	Cufon.replace('.gallery-holder .title', { fontFamily: 'Skia'});
	Cufon.replace('.box-holder .title', { fontFamily: 'Skia'});
	Cufon.replace('.gallery-links strong', { fontFamily: 'Skia', hover: true});
	Cufon.replace('.items .case .text', { fontFamily: 'Skia'});
}

function initGallery(){
	$('.gallery-links a').each(function(){
		$(this).mouseenter(function(){
			if($.browser.msie) $(this).addClass('hover');
			setTimeout(function(){
				Cufon.replace('.gallery-links strong', { fontFamily: 'Skia', hover: true });
			},5)
		}).mouseleave(function(){
			if($.browser.msie) $(this).removeClass('hover');
			setTimeout(function(){
				Cufon.replace('.gallery-links strong', { fontFamily: 'Skia', hover: true });
			},5)
		}).bind('click', function(){
			setTimeout(function(){
				Cufon.replace('.gallery-links strong', { fontFamily: 'Skia', hover: true });
			},5)
		})
	});
	/*var gal1 = $('div.gallery').gallery({
		duration: 700,
		listOfSlides: 'ul.gallery-frame > li',
		switcher: '.gallery-links > li',
		effect: true,
		IE: true
	});*/
	$('div.gallery .gallery-links a').mouseenter( function() {
		$('.gallery-links li').removeClass('active');
		$(this).parent('li').addClass('active');
		$('div.gallery .gallery-frame li').fadeOut('slow');
		index = $('div.gallery .gallery-links li').index($('div.gallery .gallery-links .active')); 
		$('div.gallery .gallery-frame li').eq(index).addClass('active').fadeIn('slow');
	});
}

/* IE6 hover function */
function hoverForIE6(_list, _class) {
	var _hoverClass = 'hover';
		if(_class) _hoverClass = _class;
			if (jQuery.browser.msie && jQuery.browser.version < 7) {
				jQuery(_list).hover(function(){
					jQuery(this).addClass(_hoverClass);
				},function(){
			jQuery(this).removeClass(_hoverClass);
		});
	}
}

//clearFormFields function
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.filterClass) 
		o.filterClass = "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) == -1) {
				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) == -1) {
				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";
	}
}

(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({
				infinite: false,								//true = infinite gallery
				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, a.next-link',		//button "next"
				prevBtn: 'a.link-prev, a.btn-prev, a.prev, a.prev-link',		//button "prev"
				circle: true,									//true = cyclic gallery; false = not cyclic gallery
				direction: false,								//false = horizontal; true = vertical
				event: 'mouseenter',									//event for the buttons and switcher
				IE: false,										//forced off effect it "fade" in IE
				autoHeight: true								//auto height on fade
			}, options || {});
			var _el = $(context).find(this.options.listOfSlides);
			if (this.options.effect) this.list = _el;
			else this.list = _el.parent();
			if(this.options.switcher) 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');
				if(this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
				if(this.options.autoHeight) this.list.parent().css({height: this.list.eq(this.active).outerHeight()});
			}
			this.flag = true;
			if (this.options.infinite){
				this.count++;
				this.active += this.count;
				this.list.append(_el.clone());
				this.list.append(_el.clone());
				eval('this.list.css('+this.anim+')');
			}
			
			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.autoHeight) this.list.parent().animate({height: this.list.eq(this.active).outerHeight()}, {queue:false, duration: this.options.duration});
			if (this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
			this.last = this.active;
		},
		scrollElement: function($this){
			if (!$this.options.infinite) eval('$this.list.animate('+$this.anim+', {queue:false, duration: $this.options.duration});');
			else eval('$this.list.animate('+$this.anim+', $this.options.duration, function(){ $this.flag = true });');
			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(){
				if ($this.options.infinite) $this.flag = false;
				$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($this);
				else $this.fadeElement();
				if ($this.options.autoRotation) $this.runTimer($this);
				return false;
			});
		},
		initEvent: function($this, addEventEl, dir){
			addEventEl.bind($this.options.event, function(){
				if ($this.flag){
					if ($this.options.infinite) $this.flag = false;
					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.options.infinite){
				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--; }
				};
			}
			else{
				if ($this.active >= $this.count + $this.count && side) $this.active -= $this.count;
				if ($this.active <= $this.count-1 && !side) $this.active += $this.count;
				eval('$this.list.css('+$this.anim+')');
				if (side) $this.active += $this.options.slideElement;
				else $this.active -= $this.options.slideElement;
			}
			if (this.options.disableBtn) this.initDisableBtn();
			if (!$this.options.effect) $this.scrollElement($this);
			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));
