var interval;
$(document).ready(function(){




	$('#login_link').click(function(){
	
		$('.login_form').css('display', 'block');
																
	});

	
	
	$('.cinema_volumes li.plus').hover(function(){
		$(this).find('ul').css('display', 'block');
	}, function(){
		$(this).find('ul').css('display', 'none');	
	});
	
	// vote block
	$('.vote_block ul li, .stars-raiting li').mouseover (function(){
	
		$(this).prevAll().addClass('over');
		$(this).addClass('over');
		$(this).nextAll().removeClass('over');
	
	});
	
	$('.vote_block, .stars-raiting').mouseout(function(){
		$(this).find('li').removeClass('over');
	});

	
	$('.top_menu li').hover(function(){
		$(this).addClass('hover');
		$(this).find('ul').css('display','block');
	}, function(){
		$(this).removeClass('hover');
		$(this).find('ul').css('display','none');
	});
	
	$('.bookmark_button_wrap').hover(function(){
		$('.bookmarks_layer').css('display', 'block');
	}, function(){
		$('.bookmarks_layer').css('display', 'none');
	});
	
	$('.captcha').click(function(){
		
		$(this).attr('src','/public/site_img/google_captcha.php?'+Math.random());
		
	});

	
	$('.treyler_preview .image').hover(function(){
	
		$(this).find('img:eq(0)').css('display','none');
	
	}, function(){
	
		$(this).find('img:eq(0)').css('display','block');
	
	});
	
	$('#by_alpha').click(function(){
		$('.treyler_filters_wrap div:eq(0)').animate({marginTop: '0'},'fast');
		$(this).parent('li').attr('class','active');
		$('#by_year').parent('li').attr('class','');
	});
	
	$('#by_year').click(function(){
		$('.treyler_filters_wrap div:eq(0)').animate({marginTop: '-40px'},'fast');
		$(this).parent('li').attr('class','active');
		$('#by_alpha').parent('li').attr('class','');
	});
	
	currentFrame = 0;
	interval = setInterval("showFrame()", 3500); 
	
	
	$('.button a.link').click(function(){
		if($('.button .list').css('display') == 'block'){
			$('.button .list').slideUp('fast');
		}else{
			$('.button .list').slideDown('fast');
		}
	});
	
	$('*').live('click', function() {
		//alert($(this).attr('class'));													
		if(!$(this).hasClass('city_keep')){												
			$('.button .list').slideUp('fast');
		}
	});
	
	
	$('.small_tabs a').click(function(){
		
		oldId = $('.small_tabs a.active').attr('rel');
		$('.small_tabs a.active').removeClass('active');
		$(this).addClass('active');
		
		$('#'+oldId).css('display', 'none');
		$('#'+$(this).attr('rel')).css('display', 'block');
		
	});

	$('.small_tabs_yellow a').click(function(){
		
		oldId = $('.small_tabs_yellow a.active').attr('rel');
		$('.small_tabs_yellow a.active').removeClass('active');
		$(this).addClass('active');
		
		$('#'+oldId).css('display', 'none');
		$('#'+$(this).attr('rel')).css('display', 'block');
		
	});
	
	
	ifNext = true;
	ifPrev = true;
	
	$('.image_move_right').click(function(){
		
		if(!ifNext) return false;
		
		ifNext = false;
		
		obj = $(this).parent('td').parent('tr').find('td:eq(1)').find('div').find('ul');
		
		maxWidth = $(this).parent('td').parent('tr').find('td:eq(1)').find('div').find('ul').find('li').length*190;
		
		curLeft = $(this).parent('td').parent('tr').find('td:eq(1)').find('div').find('ul').css('margin-left').replace('px', '')*-1;
		
		liIndex = curLeft/190+1;
		
		if(curLeft >= maxWidth-190){
			height = obj.find('li:eq(0)').find('img').height();
			obj.animate({marginLeft: '0'}, 500, function(){ ifNext = true; });	
		}else{
			obj.animate({marginLeft: '-=190px'}, 500, function(){ ifNext = true; });	
			height = obj.find('li:eq('+liIndex+')').find('a').height();
		}
		
		//obj.parent('div').css('height', height+6+'px');
		
	});
	
	$('.image_move_left').click(function(){
		
		if(!ifPrev) return false;
		
		ifPrev = false;
		
		obj = $(this).parent('td').parent('tr').find('td:eq(1)').find('div').find('ul');
		
		maxWidth = $(this).parent('td').parent('tr').find('td:eq(1)').find('div').find('ul').find('li').length*190;
		
		curLeft = $(this).parent('td').parent('tr').find('td:eq(1)').find('div').find('ul').css('margin-left').replace('px', '')*-1;
		
		liIndex = curLeft/190-1;
		
		if(curLeft > 0){
			obj.animate({marginLeft: '+=190px'}, 500, function(){ ifPrev = true; });	
			
			height = obj.find('li:eq('+liIndex+')').find('a').height();
			//obj.parent('div').css('height', height+6+'px');			
		}
		
	});
	
	/*		COMMENTS VOTES 	*/
		
		$('.comment_votes a').focus(function(){
			$(this).blur();		
		});
		
		$('.comment_votes a').click(function(){
			
			linkObj = $(this);
			
			vote = eval($(this).text());														 
			
			comObj = eval(linkObj.parents('li[id*="comment_"]').attr('id').replace('comment_', ''));
			
			$.post("/comment/comment_vote/", { com_id: comObj, vote: vote, type: $('form#comment_form input[name="comment_type"]').val() },
				function(data){
					
					obj = linkObj.parent('td').parent('tr').parent('tbody').parent('table').parent('div').find('div');
					
					if(data == 0){
						obj.text('Вы уже голосовали за этот комментарий.');	
					}else{
						obj.text('Спасибо. Ваш голос учтен.');	
						
						commentVote = linkObj.parent('td').parent('tr').find('td:eq(0)').find('span');
						
						newVote = eval(commentVote.text()) + eval(data);
						
						if(newVote > 0){
							newVote = '+'+newVote;
							commentVote.attr('class', 'green');
						}else if(newVote == 0){
							commentVote.attr('class', 'gray');
						}else{
							commentVote.attr('class', 'red');
						}
						
						commentVote.text(newVote);
						
					}
					
					setTimeout('obj.text("");', 1000);
					
				}
			);
			
		});
	
	/************************/
	
	/* 	COMMENTS ACTIONS 		*/
	
		$('.comment_actions a').focus(function(){
			$(this).blur();
		});
		
		$('.comment_actions a').click(function(){
			
			comId = eval($(this).parent('div').parent('div').parent('li').attr('id').replace('comment_',''));
			
			linkObj = $(this);
			
			if($(this).attr('rel') == 'spoyling'){
				
				if(confirm('Вы уверены что хотите пожаловаться на спойлинг в комментарии?')){
					
					$.post("/comment/comment_spoyling/", { com_id: comId, type: $('form#comment_form input[name="comment_type"]').val() },
						function(data){
							
							linkObj.parent('div').find('div').text('Спасибо. Ваша жалоба была отправлены.');
							
							setTimeout('linkObj.parent("div").find("div").text("");', 1000);
							
						}
					);
					
				}
				
			}else if($(this).attr('rel') == 'answer'){
				
				$('#reply_to').css('display', 'block');
				$('#reply_source').text($(this).parent('div').parent('div').find('p').text());
				$('input#parent_id').val(comId);
				$('textarea[name="comment_text"]').focus();
				
			}
		
		});
		
		$('.comment_actions a').hover(function(){
		
			$(this).parent('div').parent('div').find('p').css('text-decoration', 'underline');
		
		}, function(){
		
			$(this).parent('div').parent('div').find('p').css('text-decoration', 'none');
		
		});
		
	/***************************/
	
	$('#film_short_actor_list').find('li').each(function(){
		
		$(this).hover(function(){
			$(this).find('.actor_preview').css('display', 'block');
		},function(){
			$(this).find('.actor_preview').css('display', 'none');
		});
		
		
	});
	
	
	
});


function sPC(winW, winH, winId)
{
	
	showDisable({width:winW, height:winH, id:winId});
	
}


function removeDisable(){
	//$('body').css('overflow','visible');
	$('.disable_screen').remove();
	$('.layer_content_wrap').remove();
}

function showDisable(options){
	
	if(options.width)
		popUpWidth = options.width;
	else
		popUpWidth = 100;
		
	if(options.height)
		popUpHeight = options.height;
	else
		popUpHeight = 100;	
		
	if(options.closeButton){
		if(options.closeButton == 1)
			closeButton = true;
		else
			closeButton = false;
			
	}else
		closeButton = false;
	
	var topScroll = document.body.scrollTop||document.documentElement.scrollTop;
	
	popUpLeft = (getDocumentWidth()/2)-(popUpWidth/2);
	popUpTop = (getDocumentHeight()/2)-(popUpHeight/2)+topScroll;
	
	content = '';
	content = $('#'+options.id).html();
	
	$('body').prepend('<div class="disable_screen" onclick="removeDisable()"></div>');
	
	tempContent = '<div class="layer_content_wrap" style="width:'+popUpWidth+'px;height:'+popUpHeight+'px; left:'+popUpLeft+'px; top:'+popUpTop+'px">';
	if(closeButton == true) tempContent += '<div class="close_window"><span onclick="removeDisable()">Закрыть</span></div>';
	tempContent += content+'</div>';
	
	$('body').prepend(tempContent);
	
	var yScroll = document.compatMode=='CSS1Compat'&&!window.opera?document.documentElement.scrollHeight:document.body.scrollHeight;
	var cliHeight = document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
	
	$('.disable_screen').css('height', yScroll+'px');
	
	$('.disable_screen').animate({opacity: 0.8}, 50);
	//$('body').css('overflow','hidden');
}

function getDocumentHeight(){
	
	return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
	
}

//Размер документа по горизонтали
function getDocumentWidth(){
	
	return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;

}



function showFrame(id){
	
	count = $('.items_list li').length;
	
	if(id){
		clearInterval(interval); 
		$('.items_list li.active').removeClass('active');
		$('.items_list li:eq('+(id-1)+')').addClass('active');
		
		cnt = 0;
		$('.items_list li').each(function(){
		
			if(!$(this).hasClass('active') && cnt != (id-1) && !$(this).hasClass('last')){
				$(this).css('border-bottom-color', '#FFF');
			}
			cnt++;
			
		});
		$('.items_list li:eq('+(id-2)+')').css('border-bottom-color', '#AAB8C7');
		
		$('.frame_item_wrap:eq('+(id-1)+')').fadeOut('slow');
		$('.frame_item_wrap:eq(0)').fadeIn('slow');
		
		cnt = 1;
		$('.frame_item_wrap').each(function(){
			if(cnt == id) $('.frame_item_wrap:eq('+(id-1)+')').fadeIn('slow');
			else $(this).css('display','none');
			cnt++;
		});
		
	}else{
	
		if(currentFrame == (count - 1)){
			$('.frame_item_wrap:eq('+currentFrame+')').fadeOut('slow');
			$('.frame_item_wrap:eq(0)').fadeIn('slow');
			
			$('.items_list li:eq('+currentFrame+')').removeClass('active');
			$('.items_list li:eq(0)').addClass('active');
			
			$('.items_list li:eq(0)').css('border-bottom-color', '#AAB8C7');
			$('.items_list li:eq(3)').css('border-bottom-color', '#FFF');
			
			currentFrame = 0;
		}else{
			$('.frame_item_wrap:eq('+currentFrame+')').fadeOut('slow');
			$('.frame_item_wrap:eq('+(currentFrame+1)+')').fadeIn('slow');
			
			$('.items_list li:eq('+currentFrame+')').removeClass('active');
			$('.items_list li:eq('+(currentFrame+1)+')').addClass('active');
			
			$('.items_list li:eq('+currentFrame+')').css('border-bottom-color', '#AAB8C7');
			$('.items_list li:eq('+(currentFrame-1)+')').css('border-bottom-color', '#FFF');
			//if(currentFrame > 1) $('.items_list li:eq(0)').css('border-bottom-color', '#FFF');		
			
			currentFrame++;
		}
		
	}
	
}


function voteFilm(filmId, point){
	
	$.post("/film/vote/", { film_id: filmId, point: point },
	  function(data){
			eval(data);
	  });
	
}



function voteCinema(cinemaId, point, voteType){
	
	$.post("/cinema/vote/", { cinema_id: cinemaId, point: point, vote_type: voteType },
		function(data){
			eval(data);
		});
	
}



function voteWaiting(film_id, mark){
	
	
	if(mark == 0){
		
		$.post("/film/wait_vote/", { point: 0, film_id: film_id },
			function(data){
				eval(data);
			}
		);
	
	}else if(mark == 1){
		
		$.post("/film/wait_vote/", { point: 1, film_id: film_id },
			function(data){
				eval(data);
			}
		);
		
	}

	
}



function getPopup(url){
	
	window.open(url, "popWindow","width=800,height=600,status=0,toolbar=0");
	
}


function addTreylerToList(id){

	$.post("/treylers/save_treyler/", { treyler_id: id },
		function(data){
			eval(data);
		}
	);
	
}


function reviewVote(obj, id, type){
	$(obj).attr('onclick','');
	$.post("/film/review_vote/", { type: type, id: id },
		function(data){
			eval(data);
			
			$(obj).parent('div').find('.message').text("Спасибо, Ваш голос учтен.");
			
			setTimeout("$('.message').text('')", 2000);
			
		}
	);
}


function biggerFontSize(elmId, fSize)
{
	
	if($("#"+elmId).css("font-size").replace("px", "") < fSize)
	$("#"+elmId).css("font-size", Number($("#"+elmId).css("font-size").replace("px", ""))+1+"px");
	
}


function smallerFontSize(elmId, fSize)
{
	
	if($("#"+elmId).css("font-size").replace("px", "") > fSize)
	$("#"+elmId).css("font-size", Number($("#"+elmId).css("font-size").replace("px", ""))-1+"px");
	
}


function replyToComment(cmntId)
{
	
	
	$("#parent_id").val(cmntId);
	$("#reply_source").text($("#comment_"+cmntId).find("p").text());
	$("#reply_to").css("display", "block");
	
}


function cancelReply()
{
	
	$("#parent_id").val("");
	$("#reply_source").text("");
	$("#reply_to").css("display", "none");
	
}



