$('document').ready(function(){
        $(function() {
            $("#1").lavaLamp({
                fx: "backout",
                speed: 700
            });
        });
	$("#lasts").mouseleave(function(){
		hidetip();
	});
	
	$('#savecheckbox').click(function(){
		if($('#savecheckbox').attr('ariaPressed') == 'false'){
			$('#savecheckboximg').css("background-image","url('images/no_checked.png')");
			$('#savecheckbox').attr("ariaPressed",'true');
			
		} else {
			$('#savecheckbox').attr("ariaPressed",'false');
			$('#savecheckboximg').css("background-image","url('images/checked.png')");
		}
	});
	$("#facebook").hover(function(){
		$("#facebook").stop(true, false).animate({left:"0px"},"medium");
	},function(){
		$("#facebook").stop(true, false).animate({left:"-230"},"medium");
	},500);
	
	$("#username").focus(function(){
		if($(this).val() == 'Потребител'){
			$(this).val('').css({'color':'black'});
		}
	}).blur(function(){
		if($(this).val() == ''){
			$(this).val('Потребител').css({'color':'silver'});
		}
	}).change(function(){
		$(this).css({'color':'black'});
	});
	
	$("#password").focus(function(){
		if($(this).val() == 'парола'){
			$(this).val('').css({'color':'black'});
		}
	}).blur(function(){
		if($(this).val() == ''){
			$(this).val('парола').css({'color':'silver'});
		}
	}).change(function(){
		$(this).css({'color':'black'});
	});
	
	var add_link = true;
	$('#add_link').click(function(){
		var pos = $(this).position();
		$("#add_items").css({'left': pos.left-7,'top': pos.top+15 });
		if(add_link){
			$('#add_items').slideDown('fast');
			add_link = false;
			$(this).css({'background-image':'url("images/up_arrow.png")'});
		} else {
			$('#add_items').slideUp('fast');
			add_link = true;
			$(this).css({'background-image':'url("images/down_arrow.png")'});
		}
		return false;
	});
});
function roll_lasts(){
	var selected = $('#lasts').tabs('option', 'selected');
	if(selected == 5){
		$('#lasts').tabs('option', 'selected',0);
	} else {
		$('#lasts').tabs('option', 'selected', (selected+1));
	}
	$("#tip").hide("fast");


}

function tip(type,uri){
	if(type=='img'){
		$('#tip').fadeIn('fast').html('<img src="'+uri+'" alt="pic" />');  
	}
	if(type=='text'){
		$('#tip').fadeIn('fast').html(uri); 
	}
     $(this).bind("mousemove",function(e){
           var y = e.pageY-30+"px";
           var x = e.pageX+25+"px";
           $('#tip').css({'left':x ,'top': y});
     });

}
function hidetip(){
     $('#tip').css({'display':'none'});
     $(this).unbind("mousemove");
}		
function del(url) {
	var check = confirm("Сигурни ли сте че искате да изтриете това?");
	if(check == true) {
		window.location=url;
	} 
}

function reportContent(type,id){
	$('#report_form').dialog({
			resizable: false,
			draggable: false,
			height:100,
			modal: true,
			buttons: {
				"Изпрати": function() {
					$('#problem').css({'display':'none'});
					var descr = $('#problem').val();
					$.post("report.php", {type: type, id: id,descr: descr},
						function(data){
							
							$('#report_response').html(data);
						}
					);
					$(this).dialog("option","buttons",{
						"Oк": function(){
							$( this ).dialog( "close" );
							$('#problem').css({'display':'block'}).val('');
							$('#report_response').html('');
						}
					});
				},
				"Отказ": function() {
					$( this ).dialog( "close" );
				}
			}
	});
/*	var descr = prompt("Опишете с няколко думи проблема:","");
	if(descr){

	}*/
}
function changepage(url) {
	var page =  document.forms.page.pages.value;
	window.location = url+page;
}

function showComment(){
	$('#comments .add').slideToggle('fast');
	document.forms.add.comment.disabled = false;
	document.forms.add.send.disabled = false;
	document.forms.add.comment.value = "";
}
function addComment(form,type,id){
var comment = form.comment.value;
form.comment.disabled = true;
form.send.disabled = true;
if(comment==""){
	alert("Напишете коментар преди да изпратите към сайта!");
	form.comment.disabled = false;
	form.send.disabled = false;
} else {
		$.post("comments.php", { act: 'add', type: type, id: id ,comm: comment},
		function(data){
			$('.list').html(data);
			});
}
 $("#comments .add").ajaxSuccess(function(evt, request, settings){
   	document.forms.add.comment.disabled = false;
	document.forms.add.comment.value = "";
	document.forms.add.send.disabled = false;
	$(this).slideUp('slow');
 });

	
}

function updateComment(form,cid,type,id,page){
var comment = form.comment.value;
form.comment.disabled = true;
form.send.disabled = true;
	if(comment==""){
		alert("Напишете коментар преди да изпратите към сайта!");
		form.comment.disabled = false;
		form.send.disabled = false;
	} else {
		$.post("comments.php", { act: 'update',cid: cid, type: type, id: id ,comm: comment, p: page},
			function(data){
				$('.list').html(data);
				});
	}

}
function getComment(type,id,page){
	$('.list').fadeTo('fast',0.1,function(){
			$.post("comments.php", { type: type, id: id ,p: page},
				function(data){
					$('.list').html(data);
				});
	}).fadeTo('fast',1);
}

function delComment(elm,cid,type,id,page){
	var get = confirm("Искате ли да изтриете мнението?");
	if(get){
		$(elm).parent().slideUp('slow',function(){
		$.post("comments.php", {act:'del', cid: cid, type: type, id: id ,p: page},
		function(data){
			$('.list').html(data);
		});
		});
	}
}
function unlockComment(cid){
	$.post("comments.php", {act:'unlock', cid: cid, die:'1'},
		function(data){
			//$('.list').html(data);
		});
}
function editComment(cid,type,id,page){
		$.post("comments.php", {act:'edit', cid: cid, type: type, id: id ,p: page},
		function(data){
			$('.list').html(data);
		});
	$(window).unload(function() {
		unlockComment(cid);
	});
}

function cancelEditComment(type,id,page,cid){
	$('.list').fadeTo('fast',0.1,function(){
			$.post("comments.php", { act: 'unlock',type: type, id: id ,p: page,cid: cid},
				function(data){
					$('.list').html(data);
				});
	}).fadeTo('fast',1);
	$(window).unbind('unload');
}
function over_rate(rate,type,id){
	for(var i = 1;i<=rate;i++){
		$('.'+i).addClass("hi");	
	}

	$('.'+rate).bind("click",function(){
			$("#rating").fadeTo('slow',0.5,function(){
			$.post("rating.php",{rate: rate,type:type,id: id},
				function(data){
					$('#rating').html(data);
				});
			});
	});
	$("#rating").ajaxSuccess(function(evt, request, settings){
		 $("#rating").fadeTo('slow',1);

	});
}

function out_rate(star){
	$('.star').removeClass("hi");
	$('.star_off').removeClass("hi");
	$('.'+star).unbind("click");
}

function switch_news(num){
	clearInterval(change);
	if(num == 4)
		num = 0;
	else
		num ++;
	get_news(num);
}	
function get_news(num){
	clearInterval(change);
	$('#last_news .img').fadeTo("slow",0.5, function(){
		$.post("last_news.php",{num: num},function(data){
			$('#last_news').html(data);
			$('#last_news .img').css({"opacity":"0.5"});
			$('#last_news .img').fadeTo("slow",1);
		});
	});
	change = setInterval("switch_news("+num+")",5000);
}

function fav(type,id,act){
			$("#favourite_add_rem").fadeTo('slow',0.5,function(){
				$.post("favourite.php",{type:type,id: id,act: act},
				function(data){
					$('#favourite_add_rem').html(data);
				});
			});
	$("#favourite_add_rem").ajaxSuccess(function(evt, request, settings){
		$("#favourite_add_rem").fadeTo('slow',1);
	});
}

function addGreet(type,id){
$.post("addgreet.php",{type:type,id: id},
				function(data){
						$('#greet').html(data);
				});
}

