﻿function sendmeplease(){
	$(this).removeClass().addClass("sendmeplease");
}
function dontsendme(){
	$(this).removeClass().addClass("fade");
}
function load_image_popup()
{	
	if ($('#idtoreset').length==0)
	{
		var output="";
		output='<form id="idtoreset" name="form" action="" method="post" enctype="multipart/form-data">';
		output+='<div style="height: 25px; margin-bottom: 6px;">';
		output+='<label class="avatar"> ';
		output+='<input type="file" id="send_file" class="file" name="photo_comp" />';
		output+='</label>';
		output+='<span id="fileadress"></span>';
		output+='</div>';
		output+='<span><input type="submit" class="subm_inp" value="" id="submit_file" onclick="return ajaxFileUpload();"/></span>';
		output+='<div style="display: none;" id="filename"></div>';
		output+='</form>';
			
		$('#load_imageContact').append(output);
		$('input[name=photo_comp]').bind('change',function(){
		$('#fileadress').html($(this).val());
		});
	}

	$(".load_image_popup").overlay({
		speed:200,
		top: 100,
		left: 100,
		expose: 
		{ 
			color: '#000', 
			loadSpeed: 200,
			opacity: 0,
			element:'add_recordContact'
		},
		api: true
		}).load();
		return false;
}
function load_cross_popup(id, title)
{	
	/*$('title').html(title);*/
	$("#cross_popup").overlay({
		speed:200,
		top:250,
		left: "center",
		expose: 
		{ 
       	color: '#000', 
       	loadSpeed: 200,
		opacity: 0.7,
		element:'body'
   		},
		api: true
	}).load();
	
	var url='http://'+location.host+'/blog/view/'+id+'/';
	
	var options='Toolbar=0, Scrollbars=0, Resizable=0, Width=600, resize=no, Height=400';
	$('#bz').click(function(){
		if($(this).attr("class")!="sendmeplease")
		{
			var href="http://www.google.com/buzz/post?url="+url;
			window.open(href, '_blank', options);
			
			if($('#bz_x').attr('class')!='already')
			{			
				$("#item_"+id+" #crossposting").append('<a style="margin-right: 3px;" href="http://www.google.com/buzz"><img src="/images/blog/buzz.png"></a>');
				$('#bz_x').removeClass().addClass('already');
			}
			$.post("/ajax/crosspost.php?rand="+Math.random(), {new_id: id, post_to: 'at_buzz'},
			function(data){
				if (data=='true')
				{
					return false;
				}
				else
				{
					alert("error");
				}
			});
		}
	});
	
	$('#bg').click(function(){
		if($(this).attr("class")!="sendmeplease")
		{
			var href='//www.blogger.com/blog_this.pyra?t='+title+'+&u='+url+'+&n=KENT repost';
			window.open(href, '_blank', options);
			
			if($('#bg_x').attr('class')!='already')
			{
				$("#item_"+id+" #crossposting").append('<a style="margin-right: 3px;" href="http://www.blogger.com"><img src="/images/blog/blogspot.png"></a>');
				$('#bg_x').removeClass().addClass('already');
			}
			
			$.post("/ajax/crosspost.php?rand="+Math.random(), {new_id: id, post_to: 'at_blogspot'},
				function(data){
					if (data=='true')
					{
						return false;
					}
					else
					{
						alert("error");
					}
			});
		}
	});
	$('#vk').click(function(){
		if($(this).attr("class")!="sendmeplease")
		{
			var href='http://vkontakte.ru/share.php?url='+url;
			window.open(href, '_blank', options);
			
			if($('#vk_x').attr('class')!='already')
			{
				$("#item_"+id+" #crossposting").append('<a style="margin-right: 3px;" href="http://vkontakte.ru"><img src="/images/blog/vkontakte.png"></a>');
				$('#vk_x').removeClass().addClass('already');
			}
			
			$.post("/ajax/crosspost.php?rand="+Math.random(), {new_id: id, post_to: 'at_vkontakte'},
				function(data){
					if (data=='true')
					{
						return false;
					}
					else
					{
						alert("error");
					}
			});
		}
	});
	$('#fb').click(function(){
		if($(this).attr("class")!="sendmeplease")
		{
			var href='http://www.facebook.com/sharer.php?u='+url+'&t='+title;
			window.open(href, '_blank', options);
			
			if($('#fb_x').attr('class')!='already')
			{			
				$("#item_"+id+" #crossposting").append('<a style="margin-right: 3px;" href="http://www.facebook.com/"><img src="/images/blog/facebook.png"></a>');
				$('#fb_x').removeClass().addClass('already');
			}
			
			$.post("/ajax/crosspost.php?rand="+Math.random(), {new_id: id, post_to: 'at_facebook'},
				function(data){
					if (data=='true')
					{
						return false;
					}
					else
					{
						alert("error");
					}
			});
		}
	});
	$('#lj').click(function(){
		if($(this).attr("class")!="sendmeplease")
		{
			var href='http://www.livejournal.com/update.bml?subject='+title+'&event='+url;
			window.open(href, '_blank', options);
			
			if($('#lj_x').attr('class')!='already')
			{
				$("#item_"+id+" #crossposting").append('<a style="margin-right: 3px;" href="http://livejournal.com"><img src="/images/blog/lj.png"></a>');
				$('#lj_x').removeClass().addClass('already');
			}
			
			$.post("/ajax/crosspost.php?rand="+Math.random(), {new_id: id, post_to: 'at_lj'},
				function(data){
					if (data=='true')
					{
						return false;
					}
					else
					{
						alert("error");
					}
			});
		}
	});
	$('#li').click(function(){
		if($(this).attr("class")!="sendmeplease")
		{
			var href='http://www.liveinternet.ru/journal_post.php?action=n_add&cnurl='+url+'&cntitle='+title;
			window.open(href, '_blank', options);
			
			if($('#li_x').attr('class')!='already')
			{
				$("#item_"+id+" #crossposting").append('<a style="margin-right: 3px;" href="http://www.liveinternet.ru/"><img src="/images/blog/li.png"></a>');
				$('#li_x').removeClass().addClass('already');
			}
			
			$.post("/ajax/crosspost.php?rand="+Math.random(), {new_id: id, post_to: 'at_li'},
				function(data){
					if (data=='true')
					{
						return false;
					}
					else
					{
						alert("error");
					}
			});
		}
	});
	
	$("#closer").click(function(){
		$("body").trigger("click");
	});
	$("body").click(function(){
		$('#bz').removeClass().addClass('fade');
		$('#bg').removeClass().addClass('fade');
		$('#vk').removeClass().addClass('fade');
		$('#fb').removeClass().addClass('fade');
		$('#lj').removeClass().addClass('fade');
		$('#li').removeClass().addClass('fade');
	});
}

function ajaxFileUpload()
{
	var photo_url = $("input[name=photo_url]").val();
	var photo_comp = $("input[name=photo_comp]").val();
	if (photo_url!='' && photo_comp!="")
	{
		load_ok_popup('Будь ласка, виберіть лише один файл.','','','',0.7);
		return false;
	}
	else if (photo_url!='')
	{
		//------------------------завантажуєм файл з урл----------------------------
		var dataString = 'photo_url='+photo_url;
		//alert (dataString);
		$.ajax({
				type: "POST",
				url: "/ajax/photo_url.php",
				data: dataString,
				dataType: "json",		
				success: function(message, status)
				{				
					if(typeof(message.error) != 'undefined')
					{
						if(message.error != '')
						{
							load_ok_popup(message.error);
						}
						else
						{
							$('#filename').html(message.msg);
							//$("#photo").css({"display": "none"});
						}
					}
				},
				error: function (message, status, e)
				{
					load_ok_popup(e);
				}
		});
		return false;
	}
	else if (photo_comp!="")
	{	
		//------------------------завантажуєм файл з компа----------------------------
		$.ajaxFileUpload(
		{
			url:'/ajax/photo_comp.php',
			secureuri:false,
			fileElementId:'send_file',
			dataType: 'json',
			success: function (data, status)
			{
				if(typeof(data.error) != 'undefined')
				{
					if(data.error != '')
					{
						load_ok_popup(data.error);
					}
					else
					{
						$('#filename').html(data.msg);
						//$("#photo").css({"display": "none"});
					}
				}
			},
			error: function (data, status, e)
			{
				load_ok_popup(e);
			}
		})
		
		return false;
	}
	else return false;
}

$(document).ready(function(){
	
	$(".add_recordContactClose").click(function(){
		$("#add_recordContact div:last").trigger("click");
		$("#idtoreset").remove();
		$('input[name=photo_url]').val('');
	});

	$(".add_record_background").click(function(){
		$("#add_recordContact div:last").trigger("click");
		$("#idtoreset").remove();
		$('input[name=photo_url]').val('');
	});
	
	$("#submit_file").live('click',function(){
		$("#add_recordContact div:last").trigger("click");
		//$("#photo").css({"display": "none"});
	});	
	$("#send_new").click(function(){
	
		var title = $("input[name=title]").val();
		var text = $("textarea[name=text]").val();
		var img = $('#filename').html();
		var type = $("input[name=type]").val();
		
		var dataString = 'type='+type+'&title='+title+'&text='+text+'&img='+img;
		//alert (dataString);
		$.ajax({
			type: "POST",
			url: "/ajax/new_post.php",
			data: dataString,
			success: function(msg){
				var output=msg.split("_");
				if (!isNaN(output[0]))
				{
					var id=output[0];
					
					if(type=='blog' || type=='blog_wall')
					{					
						if (img=="" || img==null) img='default.jpg';
						//add blog
						disablePopup(".add_record_background",".add_record_popup");
						$("#idtoreset").remove();
						$('input[name=photo_url]').val('');
						var user_id=$('#herewehideuserid').html();
						var user_login=$('#herewehideuserlogin').html();
						var date=$("#middle").html()+' / '+output[1];
						var np="";
						if(type=='blog')
						{	
							np='<div class="item" style="display: none;" id="item_'+id+'">';
							np+='<div class="image"><a href="/blog/view/'+id+'"><img src="/upload/'+img+'" height="100" width="150" /></a></div>';
							if(type=='blog')
							{
								np+='<h2 class="title"><a href="/blog/view/'+id+'">'+title.substring(0,50)+'...</a></h2>';
							}
							else
							{
								np+='<h2 class="title"><a href="/blog/view/'+id+'">'+title.substring(0,20)+'...</a></h2>';
							}
							np+='<div style="float: left; width: 480px;">';
							np+='<p class="date" style="width: auto;">'+date+'</p><div id="crossposting"></div>';
							np+='</div>';
							np+='<div class="item_content">';
							np+='<p class="item_desc">'+text.substring(0,200)+'...</p>';
							np+='<span class="comments">';
							np+='<a href="/blog/view/'+id+'#comments"><img src="/images/blog/cloud.png" /></a>';
							if(user_id!=getCookie('user_id'))
							{
								np+=' 0 / <a href="/user/view/'+user_id+'/">'+user_login+'</a>';
							}
							else
							{
								np+=' 0 / <a href="/profile/">'+user_login+'</a>';
							}
							np+='</span>';
							np+='</div>';
							np+='<div style="clear:both;"></div>';
							np+='</div>';
							$('#news').prepend(np);
							$("#item_"+id).fadeIn(1500);
							
							load_cross_popup(id, title);
						}
						else //type='blog_wall'
						{
							np='<div class="blog_list_item" id="item_'+id+'">';
							np+=	'<span class="list_item_topic">';
							np+=			'<a href="/blog/view/'+id+'/">'+title+'</a>';
							np+=	'</span><br>';
							np+=	'<span class="list_item_attr">'+date+'</span>';
							np+='</div>';
							
							$('#blog_list').prepend(np);
							//$("#item_"+id).fadeIn(1500);	
							//crosspostion ver. 0.2
							load_cross_popup(id, title);
							//load_ok_popup('Запис було успішно опубліковано.','','','',0.7);
								
						}
					}
					else
					{
						load_ok_popup("Ваша новина надiслана модератору i з‘явиться у роздiлi Новини пiсля попереднього перегляду.",'','','',0.7);
						disablePopup(".add_record_background",".add_record_popup");
						$("#idtoreset").remove();
						$('input[name=photo_url]').val('');
					}
				}
				else load_ok_popup(msg,'','','',0.7);
			}
		});
		  
		return false;
	});
	
	$("#photo").live('click',load_image_popup);
	$(".fade").live('click',sendmeplease);
	//$(".sendmeplease").live('click',dontsendme);
	
	//---------------------------delete post----------------------------------
	$(".dell_button").click(function(){
		$("#friend_confirm").overlay({
			speed:200,
			top:250,
			left: "center",
			expose: 
			{ 
			color: '#000', 
			loadSpeed: 200,
			opacity: 0.7,
			element:'body'
			},
			api: true
		}).load();
	});
	$("#noser").live("click",function(){
		$("body").trigger('click');
	});
	$("#yesser").click(function(){
		newarray=window.location.pathname.split("/");
		var del_id=newarray[3];
		
		//del_firstblog
		if(del_id=='1')
		{
			var ofcourse="ofcourse";
			$.post("/ajax/delete_firstpost.php?rand="+Math.random(), {yes: ofcourse},
			function(data){
				if (data=='true')
				{
					$("#dd").css('display','none');
					$("body").trigger('click');
					load_ok_popup('Пост було успішно видалено.','body','','',0.7);
					window.open('/blog/', '_parent');
				}
				else
				{
					$("body").trigger('click');
					load_ok_popup('Невірні дані для видалення.','','','',0.7);
				}
			});
		}
		else
		{
			if (!isNaN(del_id) && typeof(del_id)!='undefined')
			{
				var coded=$('#imsomaniac').html();
				
				$.post("/ajax/delete_post.php?rand="+Math.random(), {del_id: del_id, coded: coded},
				function(data){
					if (data=='true')
					{
						$("#dd").css('display','none');
						$("body").trigger('click');
						load_ok_popup('Пост було успішно видалено.','body','','',0.7);
						window.open('/blog/', '_parent');
					}
					else
					{
						$("body").trigger('click');
						load_ok_popup('Невірні дані для видалення.','','','',0.7);
					}
				});
				
			}
			else
			{
				$("body").trigger('click');
				load_ok_popup('Невірні дані для видалення.','','','',0.7);
				return false;
			}
		}
	});
	//---------------------------------------------------------------------
	
	//---------------------delete comment----------------------------------
	
	$(".del_comment").live("click",function(){
		var COM_ID=$(this).attr('id').split('_')[1];
		$('#imsomaniac2').html(COM_ID);
		$("#comment_confirm").overlay({
			speed:200,
			top:140,
			left:480,
			expose: 
			{ 
			color: '#000', 
			loadSpeed: 200,
			opacity: 0.7,
			element:'body'
			},
			api: true
		}).load();
	});
	$("#noser_c").live("click",function(){
		$("body").trigger('click');
	});
	$("#yesser_c").click(function(){
		var del_id=$('#imsomaniac2').html();
		
		newarray=window.location.pathname.split("/");
		var new_id=newarray[3];
		
		if (!isNaN(del_id) && typeof(del_id)!='undefined' && !isNaN(new_id) && typeof(new_id)!='undefined')
		{			
			$.post("/ajax/delete_comment.php?rand="+Math.random(), {del_id: del_id, new_id: new_id},
			function(data){
				if (data=='true')
				{
					$("body").trigger('click');
					$('#comid_'+del_id).parent().remove();
					load_ok_popup('Коментар було успішно видалено.','body','','',0.7);
				}
				else
				{
					$("#news div:last").trigger('click');
					load_ok_popup('Невірні дані для видалення.','body','','',0.7);
				}
			});
		}
		else
		{
			$("#news div:last").trigger('click');
			load_ok_popup('Невірні дані для видалення.','','',0.7);
			return false;
		}
	});	
});
