var blog;
$j(document).ready(function(){
	blog = new Blog();
});
function Blog()
{
	this._ajaxUrl = 'handleAjax.php';
	this._module = 'blog';
	
	this._container = $j('#divContainer');
	this._msgBox = $j('#divErrorSuccess');
	
	this._timeOut = null;
	
	this._loading = '<img src="images/loading-thickbox.gif" alt="Loading" width="208" height="13" />';
	
	$j(this._loading)
		.prependTo('body')
		.hide();
}

Blog.prototype.preview = function()
{
	var obj = this;
	var tit = $j('#blogTitle').val();
	var oEditor = FCKeditorAPI.GetInstance('blogMessage');
    var desc = oEditor.GetXHTML(true);
	var im = $j('#ruimg').attr('src');
	
	var imStr;
	
	if(im == '' || im == 'undefined' || !im)
	{
		imStr = '';
	}
	else
	{
		imStr='<img src="'+im+'" width="100" height="100" border="" align="left" style="padding:0 5px 5px 0" />';
	}
	
	var mbody = '';
	
	var cssDefault = {
		border: 'none',
		background: '#FFFFFF',
		//width:	'450px',
		//height:	'450px',
		cursor: 'default',
		top: '50px'
	};
	
	$j.unblockUI();
	$j.blockUI({
		message:'<table border="0" bgcolor="#F3F3F4" style="text-align:left; border: #9a2d1b 2px solid; color:#000; padding:10px"><tr><td><strong>Blog Title :</strong> '+tit+'</td></tr><tr><td valign="top" width="10%"><strong>Image :</strong></td></tr><tr><td>'+imStr +' '+ desc+'</td></tr><tr ><td><input style="float:right;text-align:right;"  type="button" class="alert_cancel_button" value="Close" onclick="JavaScript: blog.closeBox();" /></td></tr></table>',
		css: cssDefault,
		overlayCSS: {
			'background-color': '#282828',
			opacity: 0.7
		}
	});
}

Blog.prototype.setTextbox = function(elem, defaultText, ev, e, pageNo)
{
	var obj = this;
	
	if (ev == 'focus')
	{
		if ($j(elem).val() == defaultText)
		{
			$j(elem).val('');
		}
	}
	else if (ev == 'blur')
	{
		if ($j(elem).isEmpty())
		{
			$j(elem).val(defaultText);
		}
	}
	else if (ev == 'keypress')
	{
		if ($j(elem).keyCode(e) == 13)
		{
			obj.searchItem(elem, defaultText, pageNo);
		}
	}
}
Blog.prototype.searchItem = function(elem, defaultText, pageNo)
{
	var obj = this;
	var tabs = $j('#ulTabs').find('.active');
	var span = $j(tabs[0]).children();
	var menu = $j(span[0]).text();
	var flag;
	if (menu.indexOf('Recent') != -1)
	{
		flag = 'Recent';
	}
	else if (menu.indexOf('Others') != -1)
	{
		flag = 'Other';
	}
	else if (menu.indexOf('My Blogs') != -1)
	{
		flag = 'My Blog';
	}
	obj._msgBox
		.text('')
		.hide();
	var data = {
		module:		obj._module,
		action:		'listBlogs4JS',
		pageNo:		0,
		srchKey:	$j(elem).val(),
		flag:		flag
	};
	$j.ajax({
		url: obj._ajaxUrl,
		data: data,
		type: 'POST',
		dataType: 'json',
		success: function(jData){
			obj._container
				.html(jData.html);
			obj.closeBox();
		},
		timeout: 60000,
		error: function(XMLHttpRequest, textStatus, errorThrown){
			obj._msgBox
				.text('Error: A critical error occured.')
				.show();
			obj.closeBox();
		}
	});
}
Blog.prototype.blogPagination = function(pageNo, totalPages, addlData)
{
	var obj = this;
	
	obj._msgBox
		.text('')
		.hide();
	
	obj.loading();
	
	$j('#btnAddNew').attr('val', pageNo);
	 
	var data = {
		module:		obj._module,
		action:		'blogPagination',
		pageNo:		pageNo,
		srchKey:	addlData.srchKey,
		flag:		addlData.flag
	};
	$j.ajax({
		url: obj._ajaxUrl,
		data: data,
		type: 'POST',
		dataType: 'json',
		success: function(jData){
			obj._container
				.html(jData.html);
			obj.closeBox();
		},
		timeout: 60000,
		error: function(XMLHttpRequest, textStatus, errorThrown){
			obj._msgBox
				.text('Error: A critical error occured.')
				.show();
			obj.closeBox();
		}
	});
}
Blog.prototype.toogleDetails = function(elem)
{
	var obj = this;
	
	$j(elem).next().toggle();
}
Blog.prototype.createBlog = function(elem)
{
	var obj = this;
	var pageNo = $j(elem).attr('val');
	
	obj._msgBox
		.text('')
		.hide();
	
	obj.loading();
	var data = {
		module:		obj._module,
		action:		'createBlog',
		pageNo:		pageNo
	};
	$j.ajax({
		url: obj._ajaxUrl,
		data: data,
		type: 'POST',
		dataType: 'json',
		success: function(jData){
			obj._container
				.html(jData.html);
			obj.closeBox();
		},
		timeout: 60000,
		error: function(XMLHttpRequest, textStatus, errorThrown){
			obj._msgBox
				.text('Error: A critical error occured.')
				.show();
			obj.closeBox();
		}
	});
}
Blog.prototype.createPost = function(elem, pageNo)
{
	var obj = this;
	
	obj._msgBox
		.text('')
		.hide();
	
	var oEditor = FCKeditorAPI.GetInstance('blogMessage') ;
	var message = oEditor.GetXHTML(true);
	
	var error = new Array();
	if ($j('#blogTitle').isEmpty())
	{
		error.push('Please enter blog title.');
	}
	if ($j.isEmpty(message))
	{
		error.push('Please enter blog content.');
	}
	if (error.length == 0)
	{
		obj.loading();
		var data = {
			module:		obj._module,
			action:		'createPost',
			pageNo:		pageNo,
			title:		$j('#blogTitle').val(),
			message:	message
		};
		var imgContainer = $j('#flBlogImage').next();
		var images = imgContainer.find('img');
		var src = '';
		for (i = 0; i < images.length; i++)
		{
			src = $j(images[i]).attr('src');
			data['photos[' + i + ']'] = src.substr(src.lastIndexOf('/') + 1);
		}
		$j.ajax({
			url: obj._ajaxUrl,
			data: data,
			type: 'POST',
			dataType: 'json',
			success: function(jData){
				obj._container
					.html(jData.html);
				obj.closeBox();
			},
			timeout: 60000,
			error: function(XMLHttpRequest, textStatus, errorThrown){
				obj._msgBox
					.text('Error: A critical error occured.')
					.show();
				obj.closeBox();
			}
		});
	}
	else
	{
		obj._msgBox
			.html(error.join('<br />'))
			.show();
	}
}
Blog.prototype.closeReplyBlog = function(elem)
{
	var obj = this;
	
	var tag = $j(elem)
				.parent()
					.parent()
						.parent();
	tag.next().show();
	tag.html('').hide();
}
Blog.prototype.replyBlogPost = function(elem, id, rootId, pageNo)
{
	var obj = this;
	
	obj._msgBox
		.text('')
		.hide();
	
	var error = new Array();
	if ($j('#blogReply').isEmpty())
	{
		error.push('Please enter your reply.');
	}
	if (error.length == 0)
	{
		obj.loading();
		var data = {
			module:		obj._module,
			action:		'replyBlogPost',
			pageNo:		pageNo,
			id:			id,
			rootId:		rootId,
			title:		$j('#blogReply').parent().prev().text(),
			message:	$j('#blogReply').val()
		};
		$j.ajax({
			url: obj._ajaxUrl,
			data: data,
			type: 'POST',
			dataType: 'json',
			success: function(jData){
				obj._container
					.html(jData.html);
				obj.closeBox();
			},
			timeout: 60000,
			error: function(XMLHttpRequest, textStatus, errorThrown){
				obj._msgBox
					.text('Error: A critical error occured.')
					.show();
				obj.closeBox();
			}
		});
	}
	else
	{
		obj._msgBox
			.html(error.join('<br />'))
			.show();
	}
}
Blog.prototype.postCommentonBlogForm = function(id, title)
{
	var obj = this;
	obj._msgBox
		.text('')
		.hide();
		
		title=title.replace(/\\/g,'\\\\');
		title=title.replace(/\'/g,'\\\'');
		title=title.replace(/\0/g,'\\0');

	var onclick = '"JavaScript: blog.postCommentonBlog(' + id + ', \'' + title + '\'); return false;"';
	
	var html = '<div class="postcomment_right" style="width:400px;"><p class="gray_txt"><strong>Leave your comment</strong></p><textarea name="txtBlogMessage" id="txtBlogMessage" class="comment_txtfield"></textarea><p>You can use some HTML tags, such as  &lt;b&gt;, &lt;i&gt;, &lt;a&gt; </p><p>&nbsp;</p><p class="gen_left" style="margin:0 0 0 100px"><a href="#" onclick=' + onclick + ' class="commentbttn"><span>+&nbsp;Post&nbsp;Comment</span></a></p><p class="gen_left" style="margin:0 0 0 0"><a href="#" class="commentbttn" onclick="JavaScript: blog.closeBox(); return false;"><span>+&nbsp;Cancel</span></a></p><div class="spacer"></div><br /></div>';
	
	var vleft = parseInt($j(window).width()/2, 10) - 200;
	vleft = vleft.toString() + 'px';
	
	$j.blockUI({
		message: html,
		css: {
			top: '50px',
			left: vleft,
			border:	'none'
		},
		overlayCSS: {
			'background-color': '#282828',
			opacity: 0.7
		}
	});
}
Blog.prototype.postCommentonBlog = function(id, title)
{
	var obj = this;
	obj._msgBox
		.text('')
		.hide();
	title = 'Re: ' + title;
	if ($j('#txtBlogMessage').isEmpty())
	{
		obj._msgBox
			.text("Empty Post !!")
			.show();
		alert("Empty Post !!");
		return;
	}
	obj.loading();
	$j.ajax({
		data		:{
			action:		'replyBlogPost',
			module:		obj._module,
			title:		title,
			message:	$j('#txtBlogMessage').val(),
			id:			id
		},
		url:		obj._ajaxUrl,
		type:		'post',
		dataType:	'json',
		success:	function(jData){
			//obj.listBlogs(0, 'Recent');
			//document.getElementById("txtBlogMessage").value = "";
			alert(jData.html);
			$j('#BlogMsg').html(jData.html).show();;
			obj.closeBox();
		}
	});
}
Blog.prototype.replyBlog = function(elem, id, pageNo, rootId)
{
	var obj = this;
	
	obj._msgBox
		.text('')
		.hide();
	
	var el = $j('#blogReply').parent().parent().parent();
	el.next().show();
	el.html('').hide();
	
	obj.loading();
	var data = {
		module:		obj._module,
		action:		'replyBlog',
		pageNo:		pageNo,
		id:			id,
		rootId:		rootId
	};
	$j.ajax({
		url: obj._ajaxUrl,
		data: data,
		type: 'POST',
		dataType: 'json',
		success: function(jData){
			$j(elem)
				.parent()
				.hide()
				.prev()
				.html(jData.html)
				.show();
			obj.closeBox();
		},
		timeout: 60000,
		error: function(XMLHttpRequest, textStatus, errorThrown){
			obj._msgBox
				.text('Error: A critical error occured.')
				.show();
			obj.closeBox();
		}
	});
}
Blog.prototype.setTabs = function(flag)
{
	var tabs = $j('#ulTabs').find('a');
	$j(tabs).removeClass('active');
	switch (flag)
	{
		case 'My Blog':
			$j(tabs[1]).addClass('active');
			break;
		case 'Other':
			$j(tabs[0]).addClass('active');
			break;
		case 'Recent':
			$j(tabs[2]).addClass('active');
			break;
		default:
			$j(tabs[0]).addClass('active');
			break;
	}
}
Blog.prototype.listBlogs = function(pageNo, flag,elem)
{
	var obj = this;
	var changeTabText 
	
		changeTabText = $j(elem).attr('myattr');		
	///alert(changeTabText);
	//alert($j('#ulTabs').parent().parent().parent());
	obj._msgBox
		.text('')
		.hide();
	obj.setTabs(flag);
	obj.loading();
	var data = {
		module:		obj._module,
		action:		'listBlogs4JS',
		pageNo:		pageNo,
		flag:		flag
	};
	$j.ajax({
		url: obj._ajaxUrl,
		data: data,
		type: 'POST',
		dataType: 'json',
		success: function(jData){
			$j('#changeTabText').html(changeTabText);
			obj._container
				.html(jData.html);
			obj.closeBox();
		},
		timeout: 60000,
		error: function(XMLHttpRequest, textStatus, errorThrown){
			obj._msgBox
				.text('Error: A critical error occured.')
				.show();
			obj.closeBox();
		}
	});
}
Blog.prototype.viewBlog = function(id, pageNo, flag)
{
	var obj = this;
	
	obj._msgBox
		.text('')
		.hide();

	obj.loading();
	var data = {
		module:		obj._module,
		action:		'viewindexBlog',
		pageNo:		pageNo,
		id:			id,
		flag:		flag
	};
	$j.ajax({
		url: obj._ajaxUrl,
		data: data,
		type: 'POST',
		dataType: 'json',
		success: function(jData){
			obj._container.html(jData.html);
			obj.closeBox();
		},
		timeout: 60000,
		error: function(XMLHttpRequest, textStatus, errorThrown){
			obj._msgBox
				.text('Error: A critical error occured.')
				.show();
			obj.closeBox();
		}
	});
}
Blog.prototype.uploadImage = function(elem, id)
{
	var obj = this;
	var imgContainer = $j(elem).next();
	var fieldID = $j(elem).attr('id');
	obj.loading();
	$j.ajaxFileUpload({
			url: obj._ajaxUrl + '?module=' + obj._module + '&action=uploadBlogImage&uploader=' + fieldID + '&id=' + id,
			secureuri: false,
			fileElementId: fieldID,
			dataType: 'json',
			success: function (jData, status){
				if (jData.flag)
				{
					$j('#' + fieldID).val('');
					imgContainer.html(imgContainer.html() + '<img id="ruimg" src="uploaded/blogImage/temp/' + jData.photo + '" width="100" height="100"/><a href="#" onclick="JavaScript: blog.removeBlogImage(this, \'' + jData.photo + '\'); return false;">remove</a>');
				}
				else
				{
					obj._msgBox
						.html(jData.message)
						.show();
				}
				obj.closeBox();
			},
			error: function (data, status, e){
				obj._msgBox
					.text('Error: A critical error occured.')
					.show();
				obj.closeBox();
				clearTimeout(obj._timeOut);
				obj._timeOut = setTimeout(function(){
					obj._msgBox.fadeOut(500);
				}, 3000);
			}
		});
}
Blog.prototype.paginationImage = function(pageNo, totPages, addlData)
{
	var obj = this;
	
	obj._msgBox
		.text('')
		.hide();
	
	obj.loading();
	
	var data = {
		module:		obj._module,
		action:		'showImages',
		pageNo:		pageNo,
		id:			addlData.id
	};
	$j.ajax({
		url: obj._ajaxUrl,
		data: data,
		type: 'POST',
		success: function(hData){
			obj._container
				.html(hData);
			obj.closeBox();
		},
		timeout: 60000,
		error: function(XMLHttpRequest, textStatus, errorThrown){
			obj._msgBox
				.text('Error: A critical error occured.')
				.show();
			obj.closeBox();
		}
	});
}
Blog.prototype.removeBlogImage = function(elem, fileName)
{
	var obj = this;
	obj.loading();
	$j.ajax({
		data:		{
			module:		obj._module,
			action:		'removeBlogImage',
			file:		fileName
		},
		type:		'post',
		dataType:	'json',
		url:		obj._ajaxUrl,
		success:	function(response){
			obj._msgBox
				.text(response.message)
				.show();
			if (response.flag)
			{
				obj._msgBox.fadeOut("slow");
				$j(elem).prev().remove();
				$j(elem).remove();
			}
			obj.closeBox();
		},
		error: function (data, status, e){
			obj._msgBox
				.text('Error: A critical error occured.')
				.show();
			obj.closeBox();
			clearTimeout(obj._timeOut);
			obj._timeOut = setTimeout(function(){
				obj._msgBox.fadeOut(500);
			}, 3000);
		}
	});
}
Blog.prototype.loading = function()
{
	var obj = this;
	
	obj.closeBox();
	$j.blockUI({
		message: obj._loading,
		css: {
			border: 'none',
			background: 'transparent'
		},
		overlayCSS: {
			'background-color': '#282828',
			opacity: 0.7
		}
	});
}
Blog.prototype.modalBody = function(title, modalbody)
{
	var obj = this;
	return '<div class="alert_box"><p class="alert_box_title">' + title + '</p><p class="alert_txt">' + modalbody + '</p>	</div>';
}
Blog.prototype.flagBox = function(title, modalbody, config)
{
	var cssDefault = {
		border: 'none',
		background: 'transparent',
		width:	'450px',
		height:	'450px',
		cursor: 'default'
	};
	
	config = $j.extend(cssDefault, config);
	$j.unblockUI();
	$j.blockUI({
		message: this.modalBody(title, modalbody),
		css: config,
		overlayCSS: {
			'background-color': '#282828',
			opacity: 0.7
		}
	});
}
Blog.prototype.closeBox = function()
{
	$j.unblockUI();
}
Blog.prototype.deleteBlog = function(id)
{
	var obj = this;
	var title, modBody;
	title = 'Remove Blog?';

	modBody = '<table style="color:#000;"><tr><td>Are you sure you want to remove this Blog?</td><tr><tr><td align="center"><input type="button" value="Remove Blog" class="alert_other_button" style="width:110px;" onclick="JavaScript: blog.blogDelete('+id+');" /><input type="button" value="Cancel" class="alert_cancel_button" onclick="JavaScript: blog.closeBox();" style="width:90px;" /></td><tr></table>';
	obj.flagBox(title, modBody);
}
Blog.prototype.blogDelete = function(id)
{
	var obj = this;
	var data = {
		module:		obj._module,
		action:		'blogDelete',
		id:			id
	};
	$j.ajax({
		url: obj._ajaxUrl,
		data: data,
		type: 'POST',
		dataType: 'json',
		success: function(hData){
			if(hData.flag  == 1)
				$j('#blog_'+id).remove();
			else
			{
				obj._msgBox
					.text('Error: A critical error occured.')
					.show();
				obj.closeBox();
			}
		},
		timeout: 60000,
		error: function(XMLHttpRequest, textStatus, errorThrown){
			obj._msgBox
				.text('Error: A critical error occured.')
				.show();
			obj.closeBox();
		}
	});
	
	$j.unblockUI();
}
Blog.prototype.deletePosts = function(id)
{
	var obj = this;
	var title, modBody;
	title = 'Remove Post?';

	modBody = '<table style="color:#000;"><tr><td>Are you sure you want to remove this Post?</td><tr><tr><td align="center"><input type="button" value="Remove Post" class="alert_other_button" style="width:110px;" onclick="JavaScript: blog.postDelete('+id+');" /><input type="button" value="Cancel" class="alert_cancel_button" onclick="JavaScript: blog.closeBox();" style="width:90px;" /></td><tr></table>';
	obj.flagBox(title, modBody);
	
}
Blog.prototype.postDelete = function(id)
{
	var obj = this;
	var data = {
		module:		obj._module,
		action:		'postDelete',
		id:			id
	};
	$j.ajax({
		url: obj._ajaxUrl,
		data: data,
		type: 'POST',
		dataType: 'json',
		success: function(hData){
			if(hData.flag  == 1)
				$j('#post_'+id).remove();
			else
			{
				obj._msgBox
					.text('Error: A critical error occured.')
					.show();
				obj.closeBox();
			}
		},
		timeout: 60000,
		error: function(XMLHttpRequest, textStatus, errorThrown){
			obj._msgBox
				.text('Error: A critical error occured.')
				.show();
			obj.closeBox();
		}
	});
	
	$j.unblockUI();
}
