$(function() {
	// login form
	var _loginForm = $('#header .login').hide();
	$('#header .btn-login').click(function() {
			if ( _loginForm.is(':visible') )
				_loginForm.slideUp();
			else
				_loginForm.slideDown();
			return false;
	});
	
	_loginForm.find('.logout').click( function() {
			_loginForm.slideUp();
			return false;
	});
	
	// vertical slide news
	var _upScroll = $('div.minibox a.up-scroll');
	var _dowScroll = $('div.minibox a.down-scroll');
	
	( new wdm_slide_vertical({
		'elemCarousel' : _upScroll.next('div').find('> ul').css('position', 'relative'),
		'elemUp' : _upScroll,
		'elemDown' : _dowScroll,
		'perMove' : 70,
		'offsetMove' : 3
		}) );
	
	// tab
	$('div.tab ul li > a').click( function() {
		$(this).parents('ul').eq(0).find('> li.active').removeClass('active').find('div.tab-content').addClass('hidden');
		
		$(this).parent('li').addClass('active');
		$(this).next('div.tab-content').removeClass('hidden');
		
		return false;
	});
});

/**
 * params
 *	elemCarousel
 *	elemUp
 * 	elemDown
 *	perMove
 *  offsetMove
 */
function wdm_slide_vertical( objParams ) {
	objParams = objParams || {};
	
	var _iterator;
	for (_iterator in objParams) {
		this[_iterator] = objParams[_iterator];
	}
	
	this.maxMove = this.elemCarousel.find('li').length - this.offsetMove;
	this.perMove = this.perMove * -1;
	this.currentMove = 1;
	var _self = this;
	
	this.move = function(moveTo) {
		
		if ( moveTo > this.maxMove || moveTo < 0 ) return;
		
		var _moveToPx = moveTo * this.perMove;
		this.elemCarousel.animate({
				top : _moveToPx
			});
		this.currentMove = moveTo;
	};
	
	this.elemUp.click( function() {
		var _moveTo = _self.currentMove - 1;
		_self.move(_moveTo);
		return false;
	});
	
	this.elemDown.click( function() {
		var _moveTo = _self.currentMove + 1;
		_self.move(_moveTo);
		return false;
	});
	
	
	var _timerUnHover = null;
	
	$('#header .main-nav > li').each( function() {
		var _me = $(this),
			_timer = null;
		_me.hover(
			function() {
				if ( _timer ) clearTimeout(_timer);
				_me.addClass('active');
			},
			
			function() {
				_timer = setTimeout(function() { _me.removeClass('active'); }, 100);
			}
		);
	});
		$(".casts li").hover(
		  function () {
			if ($(this).attr('clicked') != '1')
			{
				//get id
				var className = $(this).attr('id');
				var pid = className.substr(7,1);
				$(this).contents('a').addClass("activep"+pid);
			}
		},
		  function () {
			if ($(this).attr('clicked') != '1')
			{
				var className = $(this).attr('id');
				var pid = className.substr(7,1);
				$(this).contents('a').removeClass("activep"+pid);
				//$(this).find("span:last").remove();
			}
		  }
		);
		
		$('.casts ul li').click(function(e) {
			e.preventDefault();
			$('.casts ul li').attr('clicked', 0).find("span:last").hide();
			$(this).attr('clicked', 1);
			$('.casts ul li[clicked!=1] a').attr('class','');
			$(this).contents('span').show();
		});
		
		var _moveTo = _self.currentMove - 1;
		_self.move(_moveTo);

}
