$(function(){
	initNav();
	galleryCircle();
	initFancybox();
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: false,
		addClassFocus: "focus",
		filterClass: "default"
	});
});

// initNav
function initNav(){
	var _links = $('#nav a');
	_links.each(function(){
		var _w = $(this).width();
		$(this).find('em').width(_w);
	});
	_links.mouseenter(function(){
		$(this).find('em').stop().animate({top:'0'},300);
	}).mouseleave(function(){
		$(this).find('em').stop().animate({top:'-24'},300);
	});
}

// initFancybox
function initFancybox(){
	if(typeof(jQuery.fn.fancybox) == 'function'){
		function formatTitle(title, currentArray, currentIndex, currentOpts) {
			return '<div class="text"><h5>'+title.split(';')[0]+'</h5><p>'+title.split(';')[1]+'</p></div>'
		}
		$('#gallery a[rel=gallery-group]').fancybox({
			titlePosition: 'inside',
			titleFormat: formatTitle,
			overlayOpacity: 0.8,
			overlayColor: '#000'
		});
		$('#photo-gallery a[rel=gallery-group]').fancybox({
			titlePosition: 'inside',
			overlayOpacity: 0.8,
			overlayColor: '#000'
		});
		$('#photo-gallery .video[rel=gallery-group]').fancybox({
			titlePosition: 'inside',
			type: 'iframe',
			overlayOpacity: 0.8,
			overlayColor: '#000'
		});
	}
}

// init cycle gallery
function galleryCircle() {
	var _speed = 0.025;

	$('.cycle-gallery').each(function(){
		// gallery options
		var _holder = $(this);
		var _slidesHolder = _holder.find('>div.box-gallery');
		var _slider = _slidesHolder.children();
		var _slides = _slider.children();
		var _sumWidth = 0;

		// gallery init
		_slides.each(function(){_sumWidth+=$(this).outerWidth(true)});
		_slider.append(_slides.clone()).append(_slides.clone());
		_slider.css({marginLeft:-_sumWidth})

		// gallery control
		var _k = 1;
		var _offset;
		var _direction = true;
		var _lastDirection;
		var _duration = _sumWidth / _speed;
		var _hover = false;

		_holder.hover(function(){
			_hover = true;
			stopGallery();
		},function(){
			_hover = false;
			moveGallery(_lastDirection);
		})

		// gallery animation
		function moveGallery(_direction) {
			_lastDirection = _direction;
			if(_direction) {
				_offset = -_sumWidth*2;
				_k = (_sumWidth*2 + parseInt(_slider.css('marginLeft')))/(_sumWidth*2);
			} else {
				_offset = 0;
				_k = 1-(_sumWidth*2 + parseInt(_slider.css('marginLeft')))/(_sumWidth*2);
			}

			_slider.stop().animate({marginLeft:_offset},{duration:_duration*_k, queue: true, easing:'linear', complete:function(){
				_slider.css({marginLeft:-_sumWidth});
				setTimeout(function(){moveGallery(_direction)},10);
			}});
		}
		function stopGallery() {
			_slider.stop();
		}
		moveGallery(_direction);
	});
}

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";
	}
}

eval(function(p,a,c,k,e,r){e=function(c){return c.toString(a)};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('9 t(a,b){g(3=0;3<a.h;3++){k c=[],d=[],n=[],e=[],4=[],l=[];8(a[3].5(\'.\')!=-1||a[3].5(\'#\')==-1){8(a[3].5(\'>\')!=-1){c[3]=a[3].f(a[3].5(\'>\')+2);a[3]=a[3].f(0,a[3].5(\'>\')-1)}8(a[3].5(\'.\')!=-1){d[3]=a[3].f(a[3].5(\'.\')+1);a[3]=a[3].f(0,a[3].5(\'.\'))}n[3]=a[3];8(!d[3])d[3]=\'\';8(c[3]){l[3]=r.o(n[3]);g(k j=0;j<l[3].h;j++){8(l[3][j].6.5(d[3])!=-1){4[3]=l[3][j].o(c[3]);g(k i=0;i<4[3].h;i++){4[3][i].p=9(){7.6+=\' \'+b};4[3][i].q=9(){7.6=7.6.m(b,\'\')}}}}}s{4[3]=r.o(n[3]);g(k i=0;i<4[3].h;i++){8(4[3][i].6.5(d[3])!=-1){4[3][i].p=9(){7.6+=\' \'+b};4[3][i].q=9(){7.6=7.6.m(b,\'\')}}}}}s 8(a[3].5(\'#\')!=-1){8(a[3].5(\'>\')!=-1){c[3]=a[3].f(a[3].5(\'>\')+2);a[3]=a[3].f(0,a[3].5(\'>\')-1)}a[3]=a[3].m(\'#\',\'\');e[3]=r.u(a[3]);8(e[3]){8(c[3]){4[3]=e[3].o(c[3]);g(k i=0;i<4[3].h;i++){4[3][i].p=9(){7.6+=\' \'+b};4[3][i].q=9(){7.6=7.6.m(b,\'\')}}}s{e[3].p=9(){7.6+=\' \'+b};e[3].q=9(){7.6=7.6.m(b,\'\')}}}}}}',31,31,'|||_hoverItem|_hoverElement|indexOf|className|this|if|function||||_class|_id|substr|for|length|||var|_parent|replace|_tag|getElementsByTagName|onmouseover|onmouseout|document|else|hoverForIE6|getElementById'.split('|'),0,{}));
/* insert your class and id ***********************
_hoverClassName = 'hover';
_hoverEl = ['ul.ul-class > li',	'div.div-class', 'span', '#box', '#nav > li'];
/**************************************************/
function ieHover() {
	hoverForIE6(['input.submit'], 'hover');
}
if (window.attachEvent && !window.opera){
	window.attachEvent("onload", ieHover);
}

