jQuery(document).ready(function(){
	clearInputs();
	initGall();
	jQuery('#prev-next').each(function(){
		if(jQuery(this).find('a').length == 0) jQuery(this).remove();
	});
	scrollBtn();
	
	jQuery('#menu > li').each(function() {
		jQuery(this).hover(
			function() {
				if(jQuery(this).children().size() > 0 && jQuery(this).hasClass('current-page-ancestor')==false && jQuery(this).hasClass('current_page_item')==false) {
					jQuery('.current-page-ancestor ul').hide();
					jQuery('.current_page_item ul').hide();
				}
			},function() {
				jQuery('.current-page-ancestor ul').show();
				jQuery('.current_page_item ul').show();
			}
		);
	});
});
/*--- scrollBtn ---*/
function scrollBtn(){
	var _hold = jQuery('#btn-top');
	if(_hold.length){
		var _btn = _hold.find('a');
		var anim_f = true;
		_btn.click(function(){
			jQuery(window).scrollTo(jQuery('#water_box'), 400);
			anim_f = false;
			_btn.stop().animate({opacity: 0}, 200, function(){ _btn.css('display', 'none'); });
		});
		jQuery(window).scroll(function(){
			if(jQuery(window).scrollTop() > 100 && anim_f){
				_btn.animate({opacity: 0}, 200, function(){ _btn.css('display', 'none'); });
				anim_f = false;
			}
			else if(jQuery(window).scrollTop() < 100 && !anim_f){
				anim_f = true;
				_btn.stop().css('display', 'block').animate({opacity: 1}, 200);
			}
		});
	}
}
/*---- clear inputs ---*/
function clearInputs(){
	jQuery('input:text, input:password, textarea').each(function(){
		var _el = jQuery(this);
		_el.data('val', _el.val());
		_el.bind('focus', function(){
			if(_el.val() == _el.data('val')) _el.val('');
		}).bind('blur', function(){
			if(_el.val() == '') _el.val(_el.data('val'));
		});
	});
}
/*--- gallery function ---*/
function initGall(){
	var gall_fade = 500; //in ms for thumbnail change
	var change_speed = 500; //in ms for big images
	var move_speed = 300; //in ms for thumbnails
	
	if(jQuery('div.slider_thumb div.gallery-hold').length && jQuery('div.slider-hold').length){
		if(jQuery('#fader').length == 0) jQuery('body').append('<div id="fader"></div>');
		var _fader = jQuery('#fader');
		_fader.css({display:'none', opacity: 0});
		
		jQuery('div.slider_thumb div.nav-hold').customScroll();
		
		Gall();
	}
	/*--- global ---*/
	function Gall(){
		var thumb_hold = jQuery('div.slider_thumb div.gallery-hold');
		var big_hold = jQuery('div.slider-hold');
		var gall_btn = jQuery('div.slider_thumb div.nav-hold ul a');
		var gall_a = gall_btn.parent().index(gall_btn.parent().filter('.active:eq(0)')) != -1 ? gall_btn.parent().index(gall_btn.parent().filter('.active:eq(0)')) : 0;
		var gall_f = false;
		
		gall_btn.parent().removeClass('active').eq(gall_a).addClass('active');
		
		jQuery.ajax({
			url: gall_btn.eq(gall_a).attr('href'),
			dataType: 'html',
			success: function(_html){
				gall_btn.eq(gall_a).data('box', jQuery('<div class="gallery"><a href="#" class="btn_prev">prev</a><div class="gallery_holder">'+_html+'</div><a href="#" class="btn_next">next</a></div>'));
				gall_btn.eq(gall_a).data('box').data('big_hold', jQuery('<div class="slider"><a href="#" class="btn_prev">prev</a><a href="#" class="btn_next">next</a></div>'));
				gall_btn.eq(gall_a).data('box').appendTo(thumb_hold).addClass('active').data('big_hold').addClass('active').appendTo(big_hold);
				gall_f = true;
				inGall(gall_btn.eq(gall_a).data('box'));
			}
		});
		
		gall_btn.click(function(){
			changeGall(gall_btn.index(this));
			return false;
		});
		
		function changeGall(_ind){
			if(gall_f && _ind != gall_a){
				gall_f = false;
				gall_btn.eq(gall_a).parent().removeClass('active');
				gall_btn.eq(_ind).parent().addClass('active');
				gall_btn.eq(gall_a).data('box').removeClass('active').stop().animate({opacity:0}, gall_fade).data('big_hold').removeClass('active').stop().animate({opacity:0}, gall_fade);
				if(gall_btn.eq(_ind).data('box')){
					gall_btn.eq(_ind).data('box').addClass('active').stop().animate({opacity:1}, gall_fade).data('big_hold').addClass('active').stop().animate({opacity:1}, gall_fade);
					gall_f = true;
					gall_a = _ind;
				}
				else{
					_fader.stop().css({height: initH(), display: 'block'}).animate({opacity:0.5}, gall_fade/3);
					jQuery.ajax({
						url: gall_btn.eq(_ind).attr('href'),
						dataType: 'html',
						success: function(_html){
							_fader.stop().animate({opacity:0}, gall_fade/3, function(){ jQuery(this).css('display', 'none');});
							gall_btn.eq(_ind).data('box', jQuery('<div class="gallery"><a href="#" class="btn_prev">prev</a><div class="gallery_holder">'+_html+'</div><a href="#" class="btn_next">next</a></div>'));
							gall_btn.eq(_ind).data('box').data('big_hold', jQuery('<div class="slider"><a href="#" class="btn_prev">prev</a><a href="#" class="btn_next">next</a></div>'));
							gall_btn.eq(_ind).data('box').css('opacity', 0).appendTo(thumb_hold).data('big_hold').css('opacity', 0).appendTo(big_hold);
							gall_btn.eq(_ind).data('box').addClass('active').stop().animate({opacity:1}, gall_fade).data('big_hold').addClass('active').stop().animate({opacity:1}, gall_fade);
							gall_f = true;
							gall_a = _ind;
							inGall(gall_btn.eq(_ind).data('box'));
						}
					});
				}
			}
		}
	}
	/*--- in ---*/
	function inGall(m_hold){
		thumbSlide();
		fadeImg();
		/*--- thumbnails slide ---*/
		function thumbSlide(){
			var list_hold = m_hold.find('div.gallery_holder ul');
			var btn_prev = m_hold.find('a.btn_prev');
			var btn_next = m_hold.find('a.btn_next');
			var list_w = list_hold.children().outerWidth() * list_hold.children().length;
			var hold_w = list_hold.parent().width();
			var _step = list_hold.children().outerWidth();
			var _m = 0;
			
			if(list_w > hold_w){
				btn_prev.show();
				btn_next.show();
			}
			else{
				btn_prev.hide();
				btn_next.hide();
			}
			
			btn_prev.click(function(){
				moveList(false);
				return false;
			});
			btn_next.click(function(){
				moveList(true);
				return false;
			});
			
			function moveList(_f){
				if(_f){
					if(_m < list_w - hold_w) _m += _step;
					else _m = 0;
				}
				else{
					if(_m > 0) _m -= _step;
					else _m = Math.ceil((list_w - hold_w)/_step)*_step;
				}
				list_hold.animate({left:-_m},{queue:false, duration: move_speed});
			}
		}
		/*--- big img fade ---*/
		function fadeImg(){
			var img_hold = m_hold.data('big_hold');
			var _btn = m_hold.find('div.gallery_holder ul a');
			var btn_prev = img_hold.find('a.btn_prev');
			var btn_next = img_hold.find('a.btn_next');
			var _a = _btn.parent().index(_btn.parent().filter('.active:eq(0)')) != -1 ? _btn.parent().index(_btn.parent().filter('.active:eq(0)')) : 0;
			var anim_f = true;
			
			_btn.eq(_a).data('img', jQuery('<img src="'+_btn.eq(_a).attr('href')+'" />'));
			_btn.eq(_a).data('img').appendTo(img_hold).addClass('active');
			_btn.parent().removeClass('active').eq(_a).addClass('active');
			
			if(_btn.length <= 1){
				btn_prev.hide();
				btn_next.hide();
			}
			else{
				btn_prev.show();
				btn_next.show();
			}
			_btn.click(function(){
				changeEl(_btn.index(this));
				return false;
			});
			btn_prev.click(function(){
				if(_a > 0) changeEl(_a - 1);
				else changeEl(_btn.length - 1);
				return false;
			});
			btn_next.click(function(){
				if(_a < _btn.length - 1) changeEl(_a + 1);
				else changeEl(0);
				return false;
			})
			
			function changeEl(_ind){
				if(_ind != _a && anim_f){
					anim_f = false;
					_btn.eq(_a).parent().removeClass('active');
					_btn.eq(_ind).parent().addClass('active');
					_btn.eq(_a).data('img').removeClass('active').stop().animate({opacity:0}, change_speed);
					if(_btn.eq(_ind).data('img')){
						_btn.eq(_ind).data('img').addClass('active').stop().animate({opacity:1}, change_speed);
						_a = _ind;
						anim_f = true;
					}
					else{
						_fader.stop().css({height: initH(), display: 'block'}).animate({opacity:0.5}, change_speed/3);
						_btn.eq(_ind).data('img', jQuery('<img />'));
						_btn.eq(_ind).data('img').css('opacity', 0).appendTo(img_hold).load(function(){
							_fader.stop().animate({opacity:0}, change_speed/3, function(){ jQuery(this).css('display', 'none');});
							jQuery(this).addClass('active').animate({opacity: 1}, change_speed);
							_a = _ind;
							anim_f = true;
						});
						_btn.eq(_ind).data('img').attr('src', _btn.eq(_ind).attr('href'));
					}
				}
			}
		}
	}
	/*--- page height ---*/
	function initH(){
		var _h = jQuery('#wrapper').outerHeight();
		if(_h < jQuery(window).height()) _h = jQuery(window).height();
		if(_h < jQuery('body').height()) _h = jQuery('body').height();
		return _h;
	}
}
/*--- mouse whell function ---*/
(function($) {
var types = ['DOMMouseScroll', 'mousewheel'];
jQuery.event.special.mousewheel = {
	setup: function() {
		if ( this.addEventListener )
			for ( var i=types.length; i; )
				this.addEventListener( types[--i], handler, false );
		else
			this.onmousewheel = handler;
	},
	teardown: function() {
		if ( this.removeEventListener )
			for ( var i=types.length; i; )
				this.removeEventListener( types[--i], handler, false );
		else
			this.onmousewheel = null;
	}
};
jQuery.fn.extend({
	mousewheel: function(fn) {
		return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
	},
	unmousewheel: function(fn) {
		return this.unbind("mousewheel", fn);
	}
});
function handler(event) {
	var args = [].slice.call( arguments, 1 ), delta = 0, returnValue = true;
	event = jQuery.event.fix(event || window.event);
	event.type = "mousewheel";
	if ( event.wheelDelta ) delta = event.wheelDelta/120;
	if ( event.detail     ) delta = -event.detail/3;
	// Add events and delta to the front of the arguments
	args.unshift(event, delta);
	return jQuery.event.handle.apply(this, args);
}
})(jQuery);
/*--- function custom scroll ---*/
jQuery.fn.customScroll = function(_options) {
var _options = jQuery.extend({
	lineWidth: 24
}, _options);
return this.each(function(){
	var _box = jQuery(this);
	if(_box.is(':visible')){
		if(_box.children('.scroll-content').length == 0){
			var line_w = _options.lineWidth;
			/*--- init part ---*/
			var scrollBar = jQuery('<div class="scroll-bar"><div class="scroll-up"></div><div class="scroll-line"><div class="scroll-slider"><div></div></div></div><div class="scroll-down"></div></div>');
			_box.wrapInner('<div class="scroll-content"></div>').after(scrollBar);
			var scrollContent = _box.children('.scroll-content');
			var scrollSlider = scrollBar.find('.scroll-slider');
			var scrollSliderH = scrollSlider.parent();
			var scrollUp = scrollBar.find('.scroll-up');
			var scrollDown = scrollBar.find('.scroll-down');
			/*--- different variables ---*/
			var box_h = _box.height();
			var box_w = _box.width();
			var slider_h = 0;
			var slider_f = 0;
			var cont_h = scrollContent.height();
			var _f = false;
			var _f1 = false;
			var _f2 = true;
			var _t1, _t2, _s1, _s2;
			/*--- set styles ---*/
			_box.css({
				position: 'relative',
				overflow: 'hidden',
				width: box_w,
				height: box_h
			});
			scrollContent.css({
				position: 'absolute',
				top: 0,
				left: 0,
				zIndex: 1,
				width: box_w - line_w,
				height: 'auto'
			});
			scrollBar.css({
				position: 'absolute',
				top: 0,
				left: box_w - line_w,
				zIndex:2,
				width: line_w,
				height: box_h,
				overflow: 'hidden'
			});
			scrollUp.css({
				width: line_w,
				height: line_w,
				overflow: 'hidden',
				cursor: 'pointer'
			});
			scrollDown.css({
				width: line_w,
				height: line_w,
				overflow: 'hidden',
				cursor: 'pointer'
			});
			slider_h = scrollBar.height();
			if(scrollUp.is(':visible')) slider_h -= scrollUp.height();
			if(scrollDown.is(':visible')) slider_h -= scrollDown.height();
			scrollSliderH.css({
				position: 'relative',
				width: line_w,
				height: slider_h,
				overflow: 'hidden'
			});
			slider_h = 0;
			scrollSlider.css({
				position: 'absolute',
				top: 0,
				left: 0,
				width: line_w,
				height: slider_h,
				overflow: 'hidden',
				cursor: 'pointer'
			});
			box_h = _box.height();
			cont_h = scrollContent.height();
			if(box_h < cont_h){
				_f = true;
				slider_h = Math.round(box_h/cont_h*scrollSliderH.height());
				if(slider_h < 5) slider_h = 5;
				scrollSlider.height(slider_h);
				slider_h = scrollSlider.height();
				slider_f = (cont_h - box_h)/(scrollSliderH.height() - scrollSlider.height());
				_s1 = (scrollSliderH.height() - scrollSlider.height())/20;
				_s2 = (scrollSliderH.height() - scrollSlider.height())/3;
				if(_s1 < 3) _s1 = 3;
				if(_s2 < 10) _s2 = 10;
			}
			else{
				_f = false;
				scrollBar.hide();
				scrollContent.css({width: _box.width(), top: 0, left:0});
			}
			var _top = 0;
			/*--- element's events ---*/
			scrollUp.mousedown(function(){
				_top -= _s1;
				scrollCont();
				_t1 = setTimeout(function(){
					_t2 = setInterval(function(){
						_top -= 4/slider_f;
						scrollCont();
					}, 20);
				}, 500);
			}).mouseup(function(){
				if(_t1) clearTimeout(_t1);
				if(_t2) clearInterval(_t2);
			}).mouseleave(function(){
				if(_t1) clearTimeout(_t1);
				if(_t2) clearInterval(_t2);
			});
			scrollDown.mousedown(function(){
				_top += _s1;
				scrollCont();
				_t1 = setTimeout(function(){
					_t2 = setInterval(function(){
						_top += 4/slider_f;
						scrollCont();
					}, 20);
				}, 500);
			}).mouseup(function(){
				if(_t1) clearTimeout(_t1);
				if(_t2) clearInterval(_t2);
			}).mouseleave(function(){
				if(_t1) clearTimeout(_t1);
				if(_t2) clearInterval(_t2);
			});
			scrollSliderH.click(function(e){
				if(_f2){
					if(scrollSlider.offset().top + slider_h < e.pageY){
						_top += _s2;
					}
					else if(scrollSlider.offset().top > e.pageY){
						_top -= _s2;
					}
					scrollCont();
				}
				else{
					_f2 = true;
				}
			});
			var t_y = 0;
			scrollSlider.mousedown(function(e){
				t_y = e.pageY - jQuery(this).position().top;
				_f1 = true;
			}).mouseup(function(){
				_f1 = false;
			});
			jQuery('body').mousemove(function(e){
				if(_f1){
					_f2 = false;
					_top = e.pageY - t_y;
					scrollCont();
					this.onselectstart = function() { return false; };
					this.unselectable = "on";
					jQuery(this).css('-moz-user-select', 'none'); 
				}
				else{
					this.onselectstart = function(){};
					this.unselectable = "off";
					jQuery(this).css('-moz-user-select', 'auto'); 
				}
			});
			_box.bind('mousewheel', function(event, delta){
				if(_f){
					_top -=delta*_s1;
					scrollCont();
					//if((_top > 0) && (_top+slider_h < scrollSliderH.height()));
					return false;
				}
			});
			function scrollCont(){
				if(_top < 0) _top = 0;
				else if(_top+slider_h > scrollSliderH.height()) _top = scrollSliderH.height() - slider_h;
				scrollSlider.css('top', _top);
				scrollContent.css('top', -_top*slider_f);
			}
			this.scrollResize = function(){
				box_h = _box.height();
				cont_h = scrollContent.height();
				if(box_h < cont_h){
					_f = true;
					scrollBar.show();
					scrollContent.width(box_w - line_w);
					slider_h = Math.round(box_h/cont_h*scrollSliderH.height());
					if(slider_h < 5) slider_h = 5;
					scrollSlider.height(slider_h);
					slider_h = scrollSlider.height();
					slider_f = (cont_h - box_h)/(scrollSliderH.height() - scrollSlider.height());
					if(cont_h + scrollContent.position().top < box_h) scrollContent.css('top', -(cont_h - box_h));
					_top = - scrollContent.position().top/slider_f;
					scrollSlider.css('top', _top);
					_s1 = (scrollSliderH.height() - scrollSlider.height())/20;
					_s2 = (scrollSliderH.height() - scrollSlider.height())/3;
				}
				else{
					_f = false;
					scrollBar.hide();
					scrollContent.css({width: _box.width(), top: 0, left:0});
				}
			}
			/*
			setInterval(function(){
				if(_box.is(':visible') && cont_h != scrollContent.height()) _box.get(0).scrollResize();
			}, 200);
			*/
		}
		else{
			this.scrollResize();
		}
	}
});
}
