/**
 * @author Artjom Kurapov
 * @since 26.08.11 16:25
 */

$(document).ready(function(){
	if($('#subheader SPAN.slide').length > 1){
		$('#subheader').append('<ul id="slidedots"></ul><a href="#" id="slideprev"></a><a href="#" id="slidenext"></a>');
		$('#slidenext').bind('click',function(){
			if( $('#subheader SPAN.slide:animated').length == 0 ){
				if($('#slidedots A.active').parents('LI').eq(0).nextAll('LI').length > 0){
					$('#slidedots A.active').parents('LI').eq(0).next('LI').find('A').trigger('click');
				} else {
					$('#slidedots LI:first A').trigger('click');
				}
			}
			return false;
		});
		$('#slideprev').bind('click',function(){
			if( $('#subheader SPAN.slide:animated').length == 0 ){
				if($('#slidedots A.active').parents('LI').eq(0).prevAll('LI').length > 0){
					$('#slidedots A.active').parents('LI').eq(0).prev('LI').find('A').trigger('click');
				} else {
					$('#slidedots LI:last A').trigger('click');
				}
			}
			return false;
		});
		var tmp01 = '';
		$('#subheader SPAN.slide').each(function(){ tmp01 += '<li><a href="#"></a></li>'; });
		$('#slidedots').append(tmp01);
		$('#slidedots').css({ marginLeft: 0 - $('#slidedots').outerWidth()/2 + 'px' });
		$('#slidedots A:first').addClass('active');
		$('#slidedots A').click(function(){
			if(!$(this).hasClass('active') && $('#subheader SPAN.slide:animated').length == 0){
				$(this).parents('UL').eq(0).find('A.active').removeClass('active');
				$(this).addClass('active');
				var tmp02 = $('#subheader SPAN.slide:not(.hidden)');
				$(tmp02).css({ zIndex: '1' });
				$('#subheader SPAN.slide:eq('+$(this).parents('LI').prevAll('LI').length+')')
					.removeClass('hidden')
					.css({ zIndex: '2', opacity: 0 })
					.animate({ opacity: 1 },{ duration: 200, complete: function(){
						$(tmp02).addClass('hidden').css({ zIndex: '-1' });
					}});
			}
			return false;
		});
	}
});
