var basedir = "http://www.volleyball-movies.pl/";

function menu_hover() {
	li = $(".top_menu li:not(li li)");
	
	//for (i = 0; i < li.length; i++) if (li.eq(i).find("ul").length) li.eq(i).find("a").eq(0).append("▼");

	li.hover(
			function(){
				$("a.on").removeClass('active');
				$(this).children("a").addClass('active');
				sub = $(this).children("ul");
				if (sub.length) {
					pos = $(this).position();
					//sub.css({'left':pos.left+'px','top':pos.top+30+'px','display':'block'});
					sub.css({'left':pos.left+'px','top':pos.top+30+'px'});
					sub.slideDown("fast");
				}				
			},
			function(){
				//$(this).children("ul").css('display', 'none');
				$(this).children("ul").slideUp("fast");
				$(this).children("a").removeClass('active');
				$("a.on").addClass('active');
			}
		);
	

}

function input_focus(what, text, cl) {
	
	if ($(what).val() == "") $(what).val(text);
	if ($(what).val() != text && $(what).val() != "") $(what).addClass(cl);
	
	$(what).focus(function()
			{
				if ($(this).val() == text) {
					$(this).val("");
					$(this).addClass(cl);
				}
			}).blur(function()
			{
				if ($(this).val() == "") {
					$(this).val(text);
					$(this).removeClass(cl);
				}
			});	
}

function search_setup() {
	
	$("#search").parent().submit(function()
			{	
				q = $(".search_q").val();
				if (q == "search movie" || q == "") return false;
				$("#search_b").attr('disabled','disabled');
				window.location = $(this).attr('action')+"/"+q.replace(/ /g, '+');
				return false;
			});	
	
	input_focus(".search_q", "search movie", "search_q2");
}

function star(d) {
	var v = $("#movie_vote div");
	v.removeClass("vote_half vote_full");
	var a = d+"";
	if (a.indexOf(".") > -1) {
		var half = true;
		d -= 1;
	}
	for (i = 0; i < 5; i++) {
		if (i<d) {			
			v.eq(i).addClass("vote_full");
		} else if (half) {
			v.eq(i).addClass("vote_half");
			half = false;
		}
	}
	
}

function voting(voted) {
	var v = $("#movie_vote div");
	v.addClass("vote_gray");
	if (!voted) {						
		v.mouseover(function(){
				star($(this).attr("title"));
			}).mouseout(function(){
				star(rate);
			}).click(function(){
				rate = $(this).attr("title");
				
				$.post(basedir+"ajax/movie_vote", "vote="+$(this).attr("title"), function(data){
					code = $("response code", data).text();
					  switch (code) {
					  	case '0':
					  		$("#vote_info").text("You've allready rated this movie to "+$("response old_vote", data).text()+".");
					  	break;
					  	default:
					  		votes = $("#votes");
					  		votes.text(parseInt(votes.text())+1);
					  		$("#rating").text($("response rating", data).text());
					  		$("#vote_info").text('Thanks for rating!');
						break;
					  }
					}, 'xml');
	
				v.unbind().css("cursor", "default");
			});
	} else {
		v.css("cursor", "default");
	}
	star(rate);
}

function ie_bg_fix() {
	if( ! jQuery.support.cssFloat) {
		a = $("a.nick").width();
		a = a < 46 ? 46 : a;
		$("div.bg").css('width', a+45+'px');
	}
}

function set_onsubmit() {
	$("#type_comment").parent().submit(
		function() {
			ta = $("#type_comment").val();
			if (ta.replace(/ /g,'').length < 2) {
				alert('Comment too short.');
				return false;
			}
			
			$("#post_comment").attr("disabled", "disabled");
			
			$.post(basedir+"ajax/add_comment", { comment: ta, movie_id: mid },
				function(data){
					$("#type_comment").val('');
					$("#post_comment").removeAttr("disabled");
				    $("#comments").prepend('<div class="new_comment">'+data+'</div>');
				    $("#comments .new_comment").eq(0).fadeIn(2000);
				    set_comment($("div.new_comment").eq(0));				    
				});
			
			return false;
		}
	);
}

function set_comment(parent) {
	boxes = parent.children(".comment_box");
	for (i = 0; i < boxes.length; i++) {
		box = boxes.eq(i);
		$(".plus", box).css('cursor', 'pointer').click(function(){ 
			comment_id = $(this).parents(".comment_box").attr('id').substring(8); 
			rate_comment(comment_id,1);			
		});
		$(".minus", box).css('cursor', 'pointer').click(function(){ 
			comment_id = $(this).parents(".comment_box").attr('id').substring(8); 
			rate_comment(comment_id,0);
		});
	}
	
	$(".comment_edit .delete", parent).click(
		function() {
			answer = confirm('Are you sure you want to delete this comment?');
			if (answer) {
				comment_id = $(this).parents(".comment_edit").attr('id').substring(5);
				$.post("ajax/delete_comment", "comment_id="+comment_id, 
					function(data){
						$("#comment_"+comment_id+", #edit_"+comment_id).slideUp(1000);
					});
			}
		});
	
	edit_lock = 'off';
		
	$(".comment_edit img.edit", parent).click(
		function() {
			if (edit_lock == 'on') {
				alert('End comment edition first.');
				return false;
			}
			edit_lock = 'on';
			
			comment_id = $(this).parents(".comment_edit").attr('id').substring(5);
			$.post(basedir+"ajax/get_edit_comment", "comment_id="+comment_id, 
				function(data){
					//$("#type_comment").val(data);
					comment = $("#comment_"+comment_id+" .comment_body");
					height = comment.height();
					height = height < 80 ? 80 : height-10;
					comment.toggle();
					
					editbox = $("<div></div>").addClass("edit");
					tarea = $("<textarea></textarea>").css('height', height+'px').val(data);

					editbox.append(tarea);					
					comment.after(editbox);
					
					editbuttons = $("#edit_"+comment_id);
					editbuttons.toggle();
					
					eb2 = $("<div></div>").addClass('comment_edit');
					cancel = $("<img />").attr('src', 'http://'+location.host+'/media/img/button_cancel.jpg').click(
							function() {
								editbox.remove();
								comment.toggle();
								eb2.remove();
								editbuttons.toggle();
								edit_lock = 'off';
							});
					save = $("<img />").attr('src', 'http://'+location.host+'/media/img/button_save.jpg').click(
							function() {
								$.post(basedir+"ajax/save_comment", { comment_id: comment_id, comment: tarea.val() }, 
									function(data){
										comment.html(data);
										
										editbox.remove();
										comment.toggle();
										eb2.remove();
										editbuttons.toggle();
										edit_lock = 'off';
									});

								
							});
					eb2.append(cancel).append(save);
					editbuttons.after(eb2);
				});
		});
	
	$(".pagination a").click(function(){
		h = $(this).attr('href');
		page = h.substring(h.lastIndexOf('/')+1);
		get_comments(page);	
		return false;
	});
}

function rate_comment(comment_id, vote) {
	$.post(basedir+"ajax/comment_vote", "comment_id="+comment_id+"&vote="+vote, function(data){
		if (data != "1") {
			span = $("#comment_"+comment_id+" .comment_rating");
			rating = parseInt(span.text());
			if (vote == 1) {
				rating += 1;
			} else {
				rating -= 1;
			}
			
			if (rating > 0) {
				rating = "+"+rating;
			}
			span.text(rating);
		}	
		imgs = $("#comment_"+comment_id+" .plus_minus img");
		imgs.css('cursor', 'default').unbind();
		$.each(imgs, function(){
			$(this).attr('src', $(this).attr('src').replace(".png", "_gray.png"));
		});
	});	
}

function get_comments(p) {
	$("#comments").addClass('comments_transparent');
	$.post(basedir+"ajax/get_comments/"+p, { movie_id: mid },
		function(data){
			$("#comments").html(data);
		    set_comment($("#comments"));
			$.scrollTo(".header_comment", 1000, {easing:'easeOutBounce', onAfter: 
				function() {
					$("#comments").removeClass('comments_transparent');
				}
			});
			
	});
}

function get_stats(mid) {
	$("#show_stats").click(
			function () {
				if ($("#chart").length > 0) {
					$("#stats").toggle()
				} else {
					$("#stats").toggle();
					$.post(basedir+"ajax/get_movie_stats", { movie_id: mid },
						function(data){							
							$("#stats").html(data);			
						});
				}
				return false;
			});
}

function readpm(id) {
	tr = $("#pmread_"+id);
	tr2 = $("#pm_"+id);
	td = tr.children("td");
	
	if (tr.css('display') == 'none') {
		td.css('border-top-width', '0');
		tr2.children("td").css('border-bottom-width', '0');		
	} else {
		td.css('border-top-width', '1px');
		tr2.children("td").css('border-bottom-width', '1px');	
	}
	
	if (td.html() == "") {		
		loader = $('<div></div>').addClass("pm_loader");
		td.append(loader);
		tr.toggle();
			
		$.get(basedir+"ajax/get_pm/"+id, 
			function (data) {	
				td.html(data);
				
				tr2.children(".title").removeClass("new");
			});
		
	} else {
		tr.toggle();
	}
}

function pm_del_setup() {
	$("a.delete").click(
		function () {
			answer = confirm('Are you sure?');
			if ( ! answer) {
				return false;
			}
		});
}
