// DOM Ready
jQuery(function(){
	initDropDown();
	hideFormText();
	jQuery('div.gallery').fadeGallery({
		listSelector: 'div.gallery-holder > ul > li',
		navHolder:'.switcher',
		swichTime:7000
	});
	initLangDrop();
});

function initLangDrop(){
	jQuery('.lang-nav').each(function(){
		var _this = jQuery(this);
		var link = _this.find('.opener > a');
		var drop = _this.find('> ul').hide();
		var dropLinks = drop.children();
		var selected = dropLinks.filter('.selected').eq(0);
		
		if(selected.length) link.text(selected.find('a').text());
		else link.text(dropLinks.eq(0).find('a').text());
		
		link.click(function(){
			if(_this.hasClass('active')) hideDrop(_this, drop);
			else showDrop(_this, drop)
			return false;
		});
		drop.find('a').click(function(){
			dropLinks.removeClass('selected');
			jQuery(this).parent().addClass('selected');
			link.text(jQuery(this).text());
			hideDrop(_this, drop);
		});
		
		function showDrop(_this, drop){
			drop.show();
			_this.addClass('active');
			jQuery('body').bind('click', closeBoby);
		}
		
		function hideDrop(_this, drop){
			drop.hide();
			_this.removeClass('active');
			jQuery('body').unbind('click', closeBoby);
		}
		
		function closeBoby(e){
			if(jQuery(e.target) != _this && jQuery(e.target).parents('.lang-nav').get(0) !== _this.get(0)) hideDrop(_this, drop);
		}
	});
}

function initAutoScalingNav(o) {
	if (!o.menuId) o.menuId = "nav";
	if (!o.tag) o.tag = "a";
	if (!o.spacing) o.spacing = 0;
	if (!o.constant) o.constant = 0;
	if (!o.minPaddings) o.minPaddings = 0;
	if (!o.liHovering) o.liHovering = false;
	if (!o.sideClasses) o.sideClasses = false;
	if (!o.equalLinks) o.equalLinks = false;
	if (!o.flexible) o.flexible = false;
	var nav = document.getElementById(o.menuId);
	if(nav) {
		nav.className += " scaling-active";
		var lis = nav.getElementsByTagName("li");
		var asFl = [];
		var lisFl = [];
		var width = 0;
		for (var i=0, j=0; i<lis.length; i++) {
			if(lis[i].parentNode == nav) {
				var t = lis[i].getElementsByTagName(o.tag).item(0);
				asFl.push(t);
				asFl[j++].width = t.offsetWidth;
				lisFl.push(lis[i]);
				if(width < t.offsetWidth) width = t.offsetWidth;
			}
			if(o.liHovering) {
				lis[i].onmouseover = function() {
					this.className += " hover";
				}
				lis[i].onmouseout = function() {
					this.className = this.className.replace("hover", "");
				}
			}
		}
		var menuWidth = nav.clientWidth - asFl.length*o.spacing - o.constant;
		if(o.equalLinks && width * asFl.length < menuWidth) {
			for (var i=0; i<asFl.length; i++) {
				asFl[i].width = width;
			}
		}
		width = getItemsWidth(asFl);
		if(width < menuWidth) {
			var version = navigator.userAgent.toLowerCase();
			for (var i=0; getItemsWidth(asFl) < menuWidth; i++) {
				asFl[i].width++;
				if(!o.flexible) {
					asFl[i].style.width = asFl[i].width + "px";
				}
				if(i >= asFl.length-1) i=-1;
			}
			if(o.flexible) {
				for (var i=0; i<asFl.length; i++) {
					width = (asFl[i].width - o.spacing - o.constant/asFl.length)/menuWidth*100;
					if(i != asFl.length-1) {
						lisFl[i].style.width = width + "%";
					}
					else {
						if(navigator.appName.indexOf("Microsoft Internet Explorer") == -1 || version.indexOf("msie 8") != -1 || version.indexOf("msie 9") != -1)
							lisFl[i].style.width = width + "%";
					}
				}
			}
		}
		else if(o.minPaddings > 0) {
			for (var i=0; i<asFl.length; i++) {
				asFl[i].style.paddingLeft = o.minPaddings + "px";
				asFl[i].style.paddingRight = o.minPaddings + "px";
			}
		}
		if(o.sideClasses) {
			lisFl[0].className += " first-child";
			lisFl[0].getElementsByTagName(o.tag).item(0).className += " first-child-a";
			lisFl[lisFl.length-1].className += " last-child";
			lisFl[lisFl.length-1].getElementsByTagName(o.tag).item(0).className += " last-child-a";
		}
		nav.className += " scaling-ready";
	}
	function getItemsWidth(a) {
		var w = 0;
		for(var q=0; q<a.length; q++) {
			w += a[q].width;
		}
		return w;
	}
}

function hideFormText() {
	var _inputs = document.getElementsByTagName('input');
	var _txt = document.getElementsByTagName('textarea');
	var _value = [];
	
	if (_inputs) {
		for(var i=0; i<_inputs.length; i++) {
			if (_inputs[i].type == 'text' || _inputs[i].type == 'password') {
				
				_inputs[i].index = i;
				_value[i] = _inputs[i].value;
				
				_inputs[i].onfocus = function(){
					if (this.value == _value[this.index])
						this.value = '';
				}
				_inputs[i].onblur = function(){
					if (this.value == '')
						this.value = _value[this.index];
				}
			}
		}
	}
	if (_txt) {
		for(var i=0; i<_txt.length; i++) {
			_txt[i].index = i;
			_value['txt'+i] = _txt[i].value;
			
			_txt[i].onfocus = function(){
				if (this.value == _value['txt'+this.index])
					this.value = '';
			}
			_txt[i].onblur = function(){
				if (this.value == '')
					this.value = _value['txt'+this.index];
			}
		}
	}
}

function ieHover(h_list, h_class){
	if(!h_class) h_class = 'hover'; 
	if (jQuery.browser.msie && jQuery.browser.version < 7){
		jQuery(h_list).mouseenter(function(){
			jQuery(this).addClass(h_class);
		}).mouseleave(function(){
			jQuery(this).removeClass(h_class);
		});
	}
}

/*fadeGallery*/
jQuery.fn.fadeGallery = function(_options){
	var _options = jQuery.extend({
		listSelector: ' > li',
		navHolder:		false,
		navCreate:		false,
		thumbsSelector: 'li',
		prev:			'a.prev',
		next:			'a.next',
		swichTime:		false,
		delay:			800,
		fadeIEfix:		false,
		onChange:		null
	},_options);
	return this.each(function(){
		var _swichTime = _options.swichTime;
		var _d = (_options.fadeIEfix) ? (jQuery.browser.msie ? 0 : _options.delay) : (_options.delay);
		var _this = jQuery(this);
		var _list = jQuery(_options.listSelector, _this);
		var _linksHold = jQuery(_options.navHolder, _this);

		if(_options.navCreate){
			var _htmlNav ='<ul>';
			for(var i=0; i<_list.length; i++) {
				_htmlNav += '<li><a href="#">'+(i+1)+'</a></li>';
			}
			_htmlNav +='</ul>';
			_linksHold.html(_htmlNav);
		}
		if(_options.navHolder) var _links = jQuery(_options.thumbsSelector, _linksHold);
		else var _links = jQuery(_options.thumbsSelector, _this);
		var _btnPrev = jQuery(_options.prev , _this);
		var _btnNext = jQuery(_options.next , _this);
		var _a = _list.index(_list.filter('.active:eq(0)'));
		if(_a == -1) _a = 0;
		var _t;
		_list.removeClass('active').css({display: 'none', opacity: 0}).eq(_a).addClass('active').css({display: 'block', opacity: 1}).css('opacity', 'auto');
		_links.eq(_a).addClass('active');

		autoSwitch();
		function autoSwitch(){
			if (_swichTime){
				_t = setTimeout(function(){
					if(_a < _list.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, _swichTime);
			}
		}

		if (_btnPrev){
			_btnPrev.click(function(){
				var _prevItem = 0;
				if (_a > 0) _prevItem = _a-1;
				else _prevItem = _list.length-1;
				changeEl(_prevItem);
				return false;
			})
		}
		if (_btnNext){
			_btnNext.click(function(){
				var _nextItem = 0;
				if (_a < _list.length - 1) _nextItem = _a+1;
				else _nextItem = 0;
				changeEl(_nextItem);
				return false;
			})
		}

		if(_links){
			_links.click(function(){
				var _ind = _links.removeClass('active').index(jQuery(this).addClass('active'));
				changeEl(_ind);
				return false;
			})
		}
		function changeEl(_ind){
			if(_t) clearTimeout(_t);
			if(_list.is(':animated')) _list.stop(true, true);
			if(_ind != _a){
				_links.removeClass('active').eq(_ind).addClass('active');
				_list.eq(_a).removeClass('active').animate({opacity: 0}, {queue:false, duration:_d, complete: function(){jQuery(this).css({display:'none'})}});
				_list.eq(_ind).addClass('active').css({opacity: 0, display:'block'}).animate({opacity: 1}, {queue:false, duration:_d,complete:function(){
					jQuery(this).css('opacity', 'auto');
					_a = _ind;
					autoSwitch();
					if (jQuery.isFunction(_options.onChange)) {
						_options.onChange.apply(_this);
					};
				}});
			}
		}
	});
};
function initDropDown(){
	var nav = document.getElementById("nav");
	var _nav = jQuery('#nav'),
		_li = jQuery('> li', _nav);
	
	_li.each(function(){
		var $this = jQuery(this),
			_drop = jQuery('> .drop', $this),
			_link = jQuery('a', _drop),
			_w = 0;
		$this.mouseenter(function(){
			$this.addClass('hover');
			setTimeout(function(){Cufon.refresh('#nav > li > a')}, 10);
		}).mouseleave(function(){
			$this.removeClass('hover');
			setTimeout(function(){Cufon.refresh('#nav > li > a')}, 10);
		})
	});
	
	var innerNav = jQuery('#nav > li'),
		_active = jQuery('#nav > li').filter('.current-menu-ancestor');
		_active2 = jQuery('#nav > li').filter('.current-menu-item');
	
	innerNav.each(function(){
		jQuery(this).mouseenter(function(){
			innerNav.removeClass('current-menu-ancestor');
			innerNav.removeClass('current-menu-item');
			setTimeout(function(){Cufon.refresh('#nav > li > a')}, 10);
		}).mouseleave(function(){
			_active.addClass('current-menu-ancestor');
			_active2.addClass('current-menu-item');
			setTimeout(function(){Cufon.refresh('#nav > li > a')}, 10);
		})
	})
}
