$(function() {
 	$('.comment')
  		.mouseover(function(){
			$(this).children().children('.mHidden').stop().css({visibility:"visible"})
												   .animate({top:"-19px", height:"20px"}, {queue:false, duration:100})
		})
  		.mouseout(function(){
			$(this).children().children('.mHidden').stop().animate({top:"1px", height:"0px"}, {queue:false, duration:100, complete:function(){
				$(this).css({visibility:"hidden"})
			}})
									
		});
	
	$('.mHidden img')
		.mouseover(function(){
			$(this).fadeTo(100, 0.6).css({cursor:"pointer"})
		})
		.mouseout(function(){
			$(this).fadeTo(100, 1)
		})
		.click(function(){
			id = $(this).parent().attr('id').replace('cmt-', '');
			rate = $(this).attr('alt');
			ctid = 0;
			r = parseInt($('div.comment div.'+ id +' span.CHrate').text());


			$.ajax({ 
				type: "GET",
				url: "/rating.php",
				data: "t=pm&c="+ id +"&ct="+ ctid +"&r=" + rate,
				success: function(msg){
					if(msg == 'ok'){
						if(rate == "-")
							r--;
						else
							r++;
						rr = (r > 0)? "+" + r.toString() : r.toString(); 
					$('div.comment div.'+ id +' span.CHrate').text(rr);
					}else
						$('div.comment div.'+ id +' span.CHrate').text(msg);
				}
			});

		});
		
		def = 'Zostaw swój komentarz';
		
		$('#newcmt')
			.val(def)
			.css({color:"#4a8b36", fontSize:"14px", textAlign:"center"})
			.focusin(function(){
				if($(this).val() == def){
					$(this).val('');
					$(this).css({color:"#555", fontSize:"12px", textAlign:"left"})
				}

			})
			.focusout(function(){
				if($(this).val() == ''){
					$(this).val(def);
					$(this).css({color:"#4a8b36", fontSize:"14px", textAlign:"center", textDecoration:"blink"})
				}
			})
			.elastic();
			
		$('#cmtform')
			.submit(function(){
				val = $('#newcmt').val();
				cid = $("input[name='cid']").val();
				
				if(val != '' && val != def){
					
					$.ajax({ 
						type: "POST",
						url: $(this).attr('action'),
						data: "ajax=1&cid="+ cid +"&body="+ val,
						success: function(msg){
							if(msg == '1'){
								$('#cmtlog').html('Dodano komentarz');
								$('#cmtlog').css({color:"green"});
								$('#newcmt').parent().html(val);
								$('#submitcmt').parent().css({visibility:"hidden !important"});
							}else{
								$('#cmtlog').html('Wystąpoł błąd, spróbuj ponownie');
								$('#cmtlog').css({color:"red"});
							}
						}
					});
			}else
				$('#cmtlog').html('Uzupełnij treść');
				$('#cmtlog').css({color:"red"});
				
			return false;
			});
		$("a[rel='lightBox']")
			.lightBox({
				fixedNavigation:false
			});
});
