jQuery(document).ready(function () {
	jQuery('a.lightbox').attr('rel', 'shadowbox');
	Shadowbox.init();
	
	//$("div.stars").tooltip({ position: "bottom center", effect: 'slide'});//, events: {def: 'click, blur'}});
	
	$('.stars').live('mouseenter', function(e) {
		tooltip=$(this).next();
		if(tooltip.css('display')=='none') {
			tooltip.css({
				display: 'block',
				opacity: 0
			}).animate({opacity: 1}, 400);
		}
	});
	
	$('.tooltip').live('mouseleave', function() {
		$(this).animate({opacity: 0}, 400, 'swing', function() {
			$(this).css({
				display: 'none'
			});
		});
	});
		
	$(document).ready(function(){
		$("a.toggle").click(function(){
			$("#catalog").slideToggle("fast");
			$(this).toggleClass("active"); return false;
		});
	});
	
	var visiblePhoto=3;
	var totalPhoto=$('.slider div img').length;
	var lastIndex=totalPhoto-1;
	var imageWidth=$('.slider div a').eq(0).width()+2;

	$('.slider div a').eq(0).addClass('current');
	$('.arrow-back').css('display', 'none');
	
	if(totalPhoto<=visiblePhoto) {
		$('.arrow-forward, .arrow-back').css('display', 'none');
	}

	$('.arrow-forward a').click(function() {
		currIndex=Number($('.slider .current').attr('id').replace('gallery-', ''));
		if(currIndex==(totalPhoto-visiblePhoto)) {
			return false;
		}
		left=Number($('.slider div').css('left').replace('px', ''));
		if(left<0) {
			left=left*(-1);
		}
		
		if((currIndex+1)==(totalPhoto-visiblePhoto)) {
			$('.arrow-forward').css('display', 'none');
		}
		if(currIndex==0) {
			$('.arrow-back').css('display', 'inline');
		}
		
		$('.slider div').animate({'left': '-'+(left+imageWidth)+'px'}, 400, 'swing', function() {
			$('.slider div a').eq(currIndex).removeClass('current');
			$('.slider div a').eq(currIndex+1).addClass('current');
		});
		return false;
	});

	$('.arrow-back a').click(function() {
		currIndex=Number($('.slider .current').attr('id').replace('gallery-', ''));
		if(currIndex==0) {
			return false;
		}
		left=Number($('.slider div').css('left').replace('px', ''));
		
		if((currIndex-1)==0) {
			$('.arrow-back').css('display', 'none');
		}
		if((currIndex-1)==(lastIndex-visiblePhoto)) {
			$('.arrow-forward').css('display', 'inline');
		}
		
		$('.slider div').animate({'left': (left+imageWidth)+'px'}, 400, 'swing', function() {
			$('.slider div a').eq(currIndex).removeClass('current');
			$('.slider div a').eq(currIndex-1).addClass('current');
		});
		return false;
	});
	
	$('.top-search a').click(function() {
		$('.top-search form').submit();
		return false;
	});
	
	$('.star-rating-control div').live('click', function(e) {
		if(!$(this).hasClass('star-rating-readonly')) {
			video=$(this).attr('id').replace(/^[^-]+-/, '');
			rating=$(this).attr('id')=='' ? 0 : $(this).attr('id');
			$.get('/contest/updaterating/'+rating, function(response) {
				if(response.result==0) {
					alert('Вы уже голосовали');
				} else {
					$.fn.addAlert(e);
					$('input[name=star-'+video+']').rating('readOnly', true);
					$.get(window.location.href, function(response) {
						$('.container').html(response);
						$('.video-list input').rating();
						//$('div.stars').tooltip({ position: "bottom center", effect: 'slide'});
					});
				}
			}, 'json');
		}
	});
	
	$.fn.addAlert=function(e) {
		$('body').append('<div class="tooltip" id="p-tooltip"><p>Ваш голос учтен</p></div>');
		$('#p-tooltip').css({
			top: e.pageY+20,
			left: Math.round(e.pageX-($(this).width()/2)-21),
			display: 'block',
			opacity: 0
		});
		$('#p-tooltip').animate({opacity: 1}, 300);
		setTimeout("$('#p-tooltip').animate({opacity: 0}, 300, 'swing', function() {$(this).remove()})", 1400);
	}
	
	function createOverlay() {
		$('body').append('<div id="process-overlay"></div>');
		$('#process-overlay').css({
			top: 0,
			width: $(document).width(),
			height: $(document).height(),
			"background-color": '#000',
			"z-index": 98,
			position: 'absolute'
		});
		$('#process-overlay').fadeTo(0, 0.7, function() {$(this).css('display', 'none');});
		$('#process-overlay').fadeIn();
	}
	
	function deleteOverlay() {
		$('#process-overlay').fadeOut();
		$('#process-overlay').remove();
	}
});
