$(document).ready(function(){
	initGallery();
	initHighLight();
	initHoverState();
	initLoading();
});

function initHighLight(){
	var _speed = 350;
	$('.slider .holder a').find('span').hide();
	//$('.slider .holder span a.active').find('span').show();
	$('.slider .holder a').each(function(){
		$(this).mouseenter(function(){
			$(this).find('span').fadeIn(_speed);
		}).mouseleave(function(){
			if (!$(this).parent().hasClass('active')){
				$(this).find('span').fadeOut(_speed);
			}
		})
		$(this).parent().click(function(){
			$('.slider .holder a').find('span').hide();
			$(this).find('span').show();
		});
	});
	
	var nav = $('div.nav-holder');
	var frame = $('div.frame',nav).css({opacity:0});
	
	nav.mouseenter(function(){
		show(frame);
	}).mouseleave(function(){
		hide(frame);
	});
	
	$('#nav > li').each(function(){
		var _this = $(this);
		var drop = $('div.drop',_this).css({opacity:0});
		
		if (drop.length) {
			_this.mouseenter(function(){
				show(drop);
			}).mouseleave(function(){
				hide(drop);
			})
		}
	});
	
	function show(elem) {
		elem.stop().css({display:'block'}).animate({opacity:1},_speed);
	}
	function hide(elem,callback) {
		elem.stop().animate({opacity:0},_speed,function(){
			elem.css({display:'none'});
		})
	}
}
function initLoading(){
	var main = $('#main');
	main.css('opacity',0);
	setTimeout(function(){
		main.animate({opacity: 1}, {queue:false, duration:500});
	}, 600);
};
function initHoverState(){
	var _speed = 200;
	$('.btn-next, .btn-prev').each(function(){
		$(this).css('opacity',0.3);
		$(this).mouseenter(function(){
			$(this).animate({opacity: 1}, {queue:false, duration:_speed});
		}).mouseleave(function(){
			$(this).animate({opacity: 0.3}, {queue:false, duration:_speed});
		});
	});
}
function initGallery(){
	$('.slideshow').SlideShow({
		speed: 800,
		list: '.image-holder >ul>li',
		pager: '.slider ul ul li'
	});
	$('.slideshow').Gallery({
		speed: 800,
		holder: '.slider .holder',
		slide: '>ul',
		list: '>li',
		next: 'a.btn-next',
		prev: 'a.btn-prev'
	});
	$('.gallery').Gallery({
		speed: 800,
		holder: '.gallery-holder',
		slide: '>ul',
		list: '>li',
		next: 'a.btn-next',
		prev: 'a.btn-prev',
		pager: 'ul.listing'
	});
};
jQuery.fn.Gallery = function(_options){
	// default options
	var _options = jQuery.extend({
		speed: 1200,
		duration: 4000,
		holder: '.holder',
		slide: '>ul',
		list: '>li',
		prev: 'a.prev',
		next: 'a.next',
		pager: 'ul.switcher'
	},_options);
	
	return this.each(function(){
		// options
		var _hold = jQuery(this);
		var _speed = _options.speed;
		var _duration = _options.duration;
		var _holder = _hold.find(_options.holder);
		var _slide = _holder.find(_options.slide);
		var _list = _slide.find(_options.list);
		var _prev = _hold.find(_options.prev);
		var _next = _hold.find(_options.next);
	
		var _d = _list.eq(0).outerWidth(true);
		var _x=0;
		var _wh = _holder.width();
		var _ws = _list.length*_d;
		var _margin = parseInt(_list.eq(0).css('marginRight'));
		var _vis = Math.ceil(_holder.width()/_d);
	/*--------CREATING THUMBNAILS----------*/
		var _num = _hold.find(_options.pager).empty();
		_list.each(function(i){
			$('<li><a href="#">'+(i+1)+'</a></li>').appendTo(_num);
		});
	/*-------------------------------------*/
		var _thumb = _num.find('li');
		var _a = _list.index(_list.filter('.active:eq(0)'));
		if(_a == -1) {
			_a = 0; 
			_thumb.eq(_a).addClass('active');
			_list.eq(_a).addClass('active');
		}
		function Slide(_x){
			_list.removeClass('active').eq(_a).addClass('active');
			_thumb.removeClass('active').eq(_a).addClass('active');
			_slide.animate({left: -_x}, {queue:false, duration:_speed});
		};
		_thumb.each(function(j){
			$(this).click(function(){
				_next.fadeIn(_speed);
				_prev.fadeIn(_speed);
				_x = (j)*_d;
				_a = j;
				if (_x<=0){_prev.fadeOut(_speed);}
				if (_x>=_ws-_d){_next.fadeOut(_speed);}
				Slide(_x);
				return false;
			});
		});

		_next.click(function(){
			_next.fadeIn(_speed);
			_prev.fadeIn(_speed);
			if (_x<_ws-_d){
				_x = _x + _d;
				_a++
			}
			if (_x>=_ws-_d){_next.fadeOut(_speed);}
			Slide(_x);
			return false;
		});
		_prev.hide();
		_prev.click(function(){
			_next.fadeIn(_speed);
			_prev.fadeIn(_speed);
			if (_x>0){
				_x = _x - _d;
				_a--
			}
			if (_x<=0){_prev.fadeOut(_speed);}
			Slide(_x);
			return false;
		});
	});
};
jQuery.fn.SlideShow = function(_options){
	// default options
	var _options = jQuery.extend({
		speed: 1200,
		duration: 4000,
		list: 'ul.fade>li',
		prev: 'a.prev',
		next: 'a.next',
		pager: 'ul.switcher',
		pause: ''
	},_options);
	
	return this.each(function(){
		// options
		var _hold = jQuery(this);
		var _speed = _options.speed;
		var _duration = _options.duration;
		var _list = _hold.find(_options.list);
		var _prev = _hold.find(_options.prev);
		var _next = _hold.find(_options.next);
		var _pause = _hold.find(_options.pause);
		var _f = false;
		var _a = _list.index(_list.filter('.active:eq(0)'));
		if(_a == -1) {_a = 0;_list.eq(_a).addClass('active')}
		var _i, _old = _a, _t;
	/*--------CREATING THUMBNAILS----------*/
		var  _thumb= _hold.find(_options.pager);

	/*-------------------------------------------------*/
		if (jQuery.browser.msie && jQuery.browser.version < 6){		_list.hide().eq(_a).show();
		}else{		_list.show().css({opacity:0}).eq(_a).css({opacity:1});	}
		/*Run(_a);
		
		function Run(_a){
			_t = setTimeout(function(){
				_a++; if (_a >= _list.length){_a=0}
				ChangeFade(_a);
			}, _duration);
		};*/
		function ChangeFade(_new){
			if(_new != _old){
				if(jQuery.browser.msie && jQuery.browser.version < 6){
					_list.eq(_old).removeClass('active').hide();
					_list.eq(_new).addClass('active').show();
				}else{
					_list.eq(_old).removeClass('active').animate({opacity:0}, {queue:false, duration:_speed});
					_list.eq(_new).addClass('active').animate({opacity:1}, {queue:false, duration:_speed});
				}
				_thumb.eq(_old).removeClass('active');
				_thumb.eq(_new).addClass('active');
				_old=_new;_a=_new;
				if(_t) clearTimeout(_t);
				//if (_f){Run(_new);}
			};
		};
		_pause.click(function(){
			_f = false;
			clearTimeout(_t);
			return false;
		});
		_thumb.click(function(){
			_i = _thumb.index($(this));
			ChangeFade(_i);
			_a = _i;
			return false;
		});
		_next.click(function(){
			_a++; if (_a == _list.length){_a=0}
			ChangeFade(_a);
			return false;
		});
		_prev.click(function(){
			_a--; if (_a == -1){_a = _list.length-1}
			ChangeFade(_a);
			return false;
		});
	});
};

eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};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 u=k(){9 g=/^([^#.>`]*)(#|\\.|\\>|\\`)(.+)$/;k u(a,b){9 c=a.J(/\\s*\\,\\s*/);9 d=[];n(9 i=0;i<c.l;i++){d=d.v(o(c[i],b))};6 d};k o(a,b,c){a=a.z(" ","`");9 d=a.r(g);9 e,5,m,7,i,h;9 f=[];4(d==8){d=[a,a]};4(d[1]==""){d[1]="*"};4(c==8){c="`"};4(b==8){b=E};K(d[2]){w"#":7=d[3].r(g);4(7==8){7=[8,d[3]]};e=E.L(7[1]);4(e==8||(d[1]!="*"&&!x(e,d[1]))){6 f};4(7.l==2){f.A(e);6 f};6 o(7[3],e,7[2]);w".":4(c!=">"){5=p(b,d[1])}y{5=b.B};n(i=0,h=5.l;i<h;i++){e=5[i];4(e.C!=1){q};7=d[3].r(g);4(7!=8){4(e.j==8||e.j.r("(\\\\s|^)"+7[1]+"(\\\\s|$)")==8){q};m=o(7[3],e,7[2]);f=f.v(m)}y 4(e.j!=8&&e.j.r("(\\\\s|^)"+d[3]+"(\\\\s|$)")!=8){f.A(e)}};6 f;w">":4(c!=">"){5=p(b,d[1])}y{5=b.B};n(i=0,h=5.l;i<h;i++){e=5[i];4(e.C!=1){q};4(!x(e,d[1])){q};m=o(d[3],e,">");f=f.v(m)};6 f;w"`":5=p(b,d[1]);n(i=0,h=5.l;i<h;i++){e=5[i];m=o(d[3],e,"`");f=f.v(m)};6 f;M:4(c!=">"){5=p(b,d[1])}y{5=b.B};n(i=0,h=5.l;i<h;i++){e=5[i];4(e.C!=1){q};4(!x(e,d[1])){q};f.A(e)};6 f}};k p(a,b){4(b=="*"&&a.F!=8){6 a.F};6 a.p(b)};k x(a,b){4(b=="*"){6 N};6 a.O.G().z("P:","")==b.G()};6 u}();k Q(a,b){9 c=u(a);n(9 i=0;i<c.l;i++){c[i].R=k(){4(t.j.H(b)==-1){t.j+=" "+b}};c[i].S=k(){4(t.j.H(b)!=-1){t.j=t.j.z(b,"")}}}}4(D.I&&!D.T){D.I("U",V)}',58,58,'||||if|listNodes|return|subselector|null|var||||||||limit||className|function|length|listSubNodes|for|doParse|getElementsByTagName|continue|match||this|parseSelector|concat|case|matchNodeNames|else|replace|push|childNodes|nodeType|window|document|all|toLowerCase|indexOf|attachEvent|split|switch|getElementById|default|true|nodeName|html|hoverForIE6|onmouseover|onmouseout|opera|onload|ieHover'.split('|'),0,{}))
/*parametrs [selector, hover_class]*/
function ieHover() {
	hoverForIE6("ul#nav li", "hover");
}
