// JavaScript Document
var i = location.href.indexOf('?')

var pageName ;
if ( i != -1)
{
	pageName = location.href.substring(0, i); //'gunluk.php';
}
else
{
	pageName = location.href;
}

var downArrowURL = 'http://www.e-yillik.net/yillik/modules/blog/images/downarrow.png';
var rightArrowURL = 'http://www.e-yillik.net/yillik/modules/blog/images/rightarrow.png';


$(document).ready(function() 
{
	//$('a.helpTip').cluetip();
	//$('a.essay-delete').cluetip();
	$('a.helpTip').cluetip({splitTitle:'|',  
						    dropShadow:false,
						    cluetipClass: 'rounded',						  
						    positionBy: 'mouse',
							showTitle:true
						   });
	
	$('a.rssInfo').cluetip({splitTitle:'|',  
						    dropShadow:false,
						    cluetipClass: 'rounded',						  
						    positionBy: 'mouse',
							showTitle:true,
							width:100
						   });	
	
	$('.blog .attachment_form .header').click(function(){
		$('.photo_paths', $(this).parent()).slideToggle();
		$('.add_photo', $(this).parent()).slideToggle();
	});
	
	$('.blog .attachment_form .add_photo_button').click(function(){
		$('.photo_paths', $(this).parents('.attachment_form')).append("<div>" + 
								"<input type='file' name='photo[]' class='photo_path'/><div class='delete_photo_variable'></div>"						
								+"</div>");
		
		$('.blog .attachment_form .delete_photo_variable').click(function(){
			$(this).parent().html("");
		});
		
	});
	
	$('.blog .attachment_form .delete_photo_variable').click(function(){
		$(this).parent().html("");
	});
	
	
	
});


function voteEssay(score, essayId, currentTotalScore, currentVoteCount)
{
	$.ajax({
   			type: 'POST',
			url: pageName,
		    data: 'ajaxReq=true&action=voteEssay&essayId='+essayId+'&score='+score+'&currentTotalScore='+currentTotalScore+'&currentVoteCount='+currentVoteCount,								
			beforeSend: function(){ $('#ajaxLoading').show(); },
			success: function(list){							
						$('#ajaxLoading').hide();
						$('#ratingblock').html(list);									
					  },
			failure: function(list) {
						$('#ajaxLoading').hide();
						alert('Ajax Error');
					  }
			});								
}

function loadComments(essayId, page)
{
	$.ajax({
				type: 'POST',
				url: pageName,
				data:'ajaxReq=true&action=getComments&essayId='+essayId+'&page='+page,
				dataType: 'script',
				beforeSend: function(){ 
						$('#ajaxLoading').show(); 
						location.href='#comments';
				},
				success: function(list){							
					$('#ajaxLoading').hide();
					//$(".comments").hide("fast");
					$('#comments').html(list);
					//$('#essay'+Id).show('slow');
				},
				failure: function(list) {
					$('#ajaxLoading').hide();
					alert('Ajax Error');
				}
		});		
}
function loadAttachmentComments(essayId, photoId, page) {
	$.ajax({
		type: 'POST',
		url: pageName,
		data:'ajaxReq=true&action=getComments&essayId='+essayId+'&attachmentId='+photoId+'&page='+page,
		dataType: 'script',
		beforeSend: function(){ 
				$('#ajaxLoading').show(); 
				location.href='#comments';
		},
		success: function(list){							
			$('#ajaxLoading').hide();
			//$(".comments").hide("fast");
			$('#comments').html(list);
			//$('#essay'+Id).show('slow');
		},
		failure: function(list) {
			$('#ajaxLoading').hide();
			alert('Ajax Error');
		}
});	
}
function deleteComment(commentId)
{
	if (confirm('Bu yorumu silmek istedi\u011Finize emin misiniz?'))
	{
		$.ajax({
				type: 'POST',
				url: pageName,
				data:'ajaxReq=true&action=deleteComment&commentId='+commentId,
				dataType: 'script',
				beforeSend: function(){ 
						$('#ajaxLoading').show(); 
						//location.href='#comments';
				},
				success: function(result){							
					$('#ajaxLoading').hide();					
					$('#comment'+commentId).html(result);
				},
				failure: function(list) {
					$('#ajaxLoading').hide();
					alert('Ajax Error');
				}
			});
	}
}
function deleteEssay(essayId)
{
	
	if (confirm('Bu yaz\u0131y\u0131 silmek istedi\u011Finize emin misiniz?'))
	{
		
		$.ajax({
				type: 'POST',
				url: pageName,
				data:'ajaxReq=true&action=deleteEssay&essayId='+essayId,
				dataType: 'script',
				beforeSend: function(){ 
						$('#ajaxLoading').show(); 
						//location.href='#comments';
				},
				success: function(result){		
				
					$('#ajaxLoading').hide();					
					$('#essay'+essayId).html(result);
					$('#comment-area').html("");
				
				},
				failure: function(list) {
					$('#ajaxLoading').hide();
					alert('Ajax Error');
				}
			});
	}
}
function deletePicture(pictureId, articleId) {
	
	if (confirm('Bu foto\u011Frafı silmek istedi\u011Finize emin misiniz?'))
	{
		
		$.ajax({
				type: 'POST',
				url: pageName,
				data:'ajaxReq=true&action=deletePicture&photoId='+ pictureId +'&articleId='+ articleId,
				dataType: 'script',
				beforeSend: function(){ 
						$('#ajaxLoading').show(); 
						//location.href='#comments';
				},
				success: function(result){		
				
					$('#ajaxLoading').hide();						
					$('#comments').html("");
					$('#writeComment').html("");
					$('#pictureList').html("");
					$('#essay'+pictureId).html(result);
					
				
				},
				failure: function(list) {
					$('#ajaxLoading').hide();
					alert('Ajax Error');
				}
			});
	}
}
function editEssay(essayId)
{
	location.href=pageName+'?action=editEssay&essayId='+essayId;
}

function editCategory(categoryId)
{
	$('#categoryName').attr('value',$('#category'+categoryId).text());
	$('#operationButton').attr('value','Kaydet');
	$('#operationButton').unbind('click');
	$('#operationButton').bind('click',updateCategoryName);
	$('#categoryIdInput').attr('value',categoryId);
	$('#operation-area').show();
}
function cancelEditCategoryName()
{
	$('#operation-area').hide();	
}
function updateCategoryName()
{
	var categoryName = $('#categoryName').attr('value');
	var categoryId = $('#categoryIdInput').attr('value');
	$.ajax({
				type: 'POST',
				url: pageName,
				data:'ajaxReq=true&action=updateCategoryName&categoryId='+categoryId+'&categoryName='+encodeURI(categoryName) ,
				dataType: 'script',
				beforeSend: function(){ 
						$('#ajaxLoading').show(); 
				},
				success: function(result){							
					$('#ajaxLoading').hide();					
					//$('#category'+categoryId).html(result);
					if (result == 1) {
						$('#category'+categoryId).html(categoryName);
						$('#operation-area').hide();
					}
					else {
						$('#category'+categoryId).html(result);
						alert("\u0130\u015Flemde bir hata olu\u015Ftu.")	
					}
				},
				failure: function(list) {
					$('#ajaxLoading').hide();
					alert('Ajax Error');
				}
			});
}
function deleteCategory(categoryId)
{
	if (confirm('Bu kategoriyi ve e\u011Fer var ise alt\u0131ndaki tüm kategorilerle beraber bu kategorilerdeki yaz\u0131lar\u0131 silmek istedi\u011Finize emin misiniz?'))
	{
		$.ajax({
				type: 'POST',
				url: pageName,
				data:'ajaxReq=true&action=deleteCategory&categoryId='+categoryId,
				dataType: 'script',
				beforeSend: function(){ 
						$('#ajaxLoading').show(); 
						//location.href='#comments';
				},
				success: function(result){							
					$('#ajaxLoading').hide();					
					if (result == 0)
					{
						alert("\u0130\u015Flemde bir hata olu\u015Ftu.")
					}
					else
					{
						$('#blogCategories').html(result);
					}
					
				},
				failure: function(list) {
					$('#ajaxLoading').hide();
					alert('Ajax Error');
				}
			});
	}
}
function addCategory( ownerCategoryId )
{
	$('#operationButton').attr('value', 'Ekle');
	$('#categoryName').attr('value','');
	$('#categoryIdInput').attr('value', ownerCategoryId);
	$('#operationButton').unbind('click');
	$('#operationButton').bind('click', insertCategory);
	$('#operation-area').show();
}
function insertCategory()
{
	var ownerCategoryId = $('#categoryIdInput').attr('value');
	var categoryName = $('#categoryName').attr('value');
	$.ajax({
				type: 'POST',
				url: pageName,
				data:'ajaxReq=true&action=insertCategory&ownerCategoryId='+ownerCategoryId+'&categoryName='+categoryName ,
				dataType: 'script',
				beforeSend: function(){ 
						$('#ajaxLoading').show(); 
				},
				success: function(result){							
					$('#ajaxLoading').hide();					
					//$('#category'+categoryId).html(result);
					if (result == 0) {
						alert("\u0130\u015Flemde bir hata olu\u015Ftu.")							
					}
					else {
						$('#blogCategories').html(result);
						$('#operation-area').hide();						
					}
				},
				failure: function(list) {
					$('#ajaxLoading').hide();
					alert('Ajax Error');
				}
			});	
}


function getSubCats(categoryId, listType)
{
	if ($('#category'+categoryId+'block #listArrow').hasClass("openedCategory")) //attr('src') == downArrowURL
	{
		$('#category'+categoryId+'block .subCategories').hide();
		$('#category'+categoryId+'block #listArrow').addClass("closedCategory");   //attr('src',rightArrowURL);
		$('#category'+categoryId+'block #listArrow').removeClass("openedCategory");
		
		//$('#category'+categoryId+'block img').attr('src',rightArrowURL);	
	}
	else
	{
		if ($('#category'+categoryId+'block .subCategories').html() != null)
		{
			$('#category'+categoryId+'block .subCategories').show();
			$('#category'+categoryId+'block #listArrow').addClass("openedCategory"); 
			$('#category'+categoryId+'block #listArrow').removeClass("closedCategory");
			//$('#category'+categoryId+'block img').attr('src',downArrowURL);
		}
		else
		{
			$.ajax({
				type: 'POST',
				url: pageName,
				data:'ajaxReq=true&action=getSubCategories&ownerCategoryId='+categoryId+'&listType='+listType,
				dataType: 'script',
				beforeSend: function(){ 
						$('#ajaxLoading').show(); 
				},
				success: function(result){							
					$('#ajaxLoading').hide();					
					//$('#category'+categoryId).html(result);
					if (result == 0) {
						alert("\u0130\u015Flemde bir hata olu\u015Ftu.")							
					}
					else {
						$('#category'+categoryId+'block').append(result);
						$('#category'+categoryId+'block #listArrow').removeClass("closedCategory");
						$('#category'+categoryId+'block #listArrow').addClass("openedCategory"); 
						//$('#category'+categoryId+'block img').attr('src',downArrowURL);					
					}
				},
				failure: function(list) {
					$('#ajaxLoading').hide();
					alert('Ajax Error');
				}
			});	
		}
	}
}

function approveEssay(essayId)
{
	$.ajax({
				type: 'POST',
				url: pageName,
				data:'ajaxReq=true&action=approveEssay&essayId='+essayId,
				dataType: 'script',
				beforeSend: function(){ 
						$('#ajaxLoading').show(); 
						//location.href='#comments';
				},
				success: function(result){							
					$('#ajaxLoading').hide();					
					$('#essay'+essayId).html(result);
					$('#comment-area').html("");
				},
				failure: function(list) {
					$('#ajaxLoading').hide();
					alert('Ajax Error');
				}
			});	
}
function checkCommentForm()
{
		if (document.commentForm.comment.value.length < 3) {
			alert("Lütfen yorumunuzu yaz\u0131n\u0131z.");
			return false;
		}
		return true;		
}

function showPreview()
{
	var title = encodeURI($('#essayTitle').attr('value'));
	var essay = encodeURIComponent(tinyMCE.get('richTextArea').getContent({format : 'raw'}));
	

	$.ajax({
		type: 'POST',
		url: pageName,
		data:'ajaxReq=true&action=getEssayPreview&title='+title+'&essay='+essay,
		dataType: 'script',
		beforeSend: function(){ 
				$('#ajaxLoading').show(); 
				location.href='#';
		},
		success: function(result){							
			$('#ajaxLoading').hide();	
			$('#writeEssay #previewEssay').html(result);
			
			$('#writeEssay #previewEssay').slideDown();
			
		},
		failure: function(list) {
			$('#ajaxLoading').hide();
			alert('Ajax Error');
		}
	});	

}

