$( function() {
	// check header navs
	$('div.header .menu-cont li.p').each( function() {
			var _me = $(this);
			
			if ( !_me.find('ul:first').get(0) )
				_me.addClass('nddown');
	});

	// search
	var _defVal = 'Search',
		_search = $('div.header div.site-actions input[value=Search]');
		_search.focus( function() {
			if (_search.val() == _defVal ) _search.val('');
		})
			.focusout( function() {
				if (_search.val() == '' ) _search.val(_defVal);
			});
    // fade effect buttons
	$('div.menu-cont a.btn-bookonline span.hover, div.rightcolumn form a.btn-submit span.hover, div.rightcolumn .fb a.btn-join span.hover').css('opacity', 0);
    $('div.menu-cont a.btn-bookonline, div.rightcolumn form a.btn-submit, div.rightcolumn .fb a.btn-join')
        .hover(function () {
			$(this).find('span.hover').fadeTo(500, 1);
		}, function () {
			$(this).find('span.hover').fadeTo(500, 0);
		});
		
	// dropdown
	$('div.header ul.mainmenu li.p:has(div)').each( function() {
		var _me = $(this),
			_child = _me.find('div:first'),
			_nextCurrent = _me.next('li.current'),
			_timer = null;
		
		_me.hover( function() {
				if (_timer) clearTimeout(_timer);
				
				if ( _nextCurrent.length > 0 )
						_nextCurrent.removeClass('current');
				
				_me.addClass('hover');
				_child.slideDown();
			},
			function() {
				_timer = setTimeout(function() { 
					_child.slideUp('normal', function() { 
						if ( _nextCurrent.length > 0 )
							_nextCurrent.addClass('current');
						_me.removeClass('hover'); 
					} ); }, 500);
			});
	});
    
    $('div.header ul.mainmenu li ul').each( function() {
        $(this).find('li:last').addClass('l');
    });
	
	// tabs
	var _tabDivs = $('#feats div.feat-cont'),
		_tabLi = $('#tab-items li');
	
	_tabLi.each( function(i) {
			$(this).click( function() {
					if ( $(this).hasClass('active') ) return false;
					WmTabber.show(i);
					WmTabber.stop();
					return false;
				});
	});
	
    $('#feats div.feat-cont:not(:first)').find('p, h2').hide();
    
	var WmTabber = {
		lis : $('#tab-items li'),
		divs : $('#feats div.feat-cont'),
		current : 0,
		maxLi : $('#tab-items li').length - 1,
		timer : null,
		time : 5000,
        transtime : 400,
		show : function(key) {
			var _self = this;
			
            this.lis.filter('.active').removeClass('active');
            this.lis.eq(key).addClass('active');   
            
            var _prevActive = this.divs.filter('.active'),
                _nowActive =  this.divs.eq(key);
            
            //this.stop();
            
            _prevActive.find('h2, p').fadeOut(this.transtime);
            
            setTimeout(function() {
                    _prevActive.removeClass('active');
                    _nowActive.addClass('active').find('h2, p').fadeIn(_self.transtime);
                }, this.transtime);
                
            /*setTimeout(function() {
                _self.current = key;
				_self.start();
            }, this.transtime * 2);*/
            this.current = key;
		},
		start : function() {
			var _self = this;
			this.timer= setInterval(function() {
				var _key = _self.current + 1;
				if ( _key > _self.maxLi) _key = 0;
				_self.show(_key);
			}, this.time);
		},
		stop : function() {
			if ( !this.timer ) return;
			clearInterval(this.timer);
		}
	};
	
	WmTabber.start();
	
	// tag clouds
	var _tagCloud = $('#TagClouds').hide();
	
	$('div.footer .site-info a.btn-searchbytag').click( function() {
			var _span = $(this).find('span');
			if ( _tagCloud.is(':visible') ) {
				_tagCloud.slideUp();
				_span.addClass('hide');
			} else {
				_tagCloud.slideDown('normal', function() { $(window).scrollTo('#TagClouds', 1000) });
				_span.removeClass('hide');
			}
			
			return false;
		});
	
});
