/**
 * version 1.6.1
 * package AWDwallPRO-Joomla
 * author   AWDsolution.com
 * link http://www.AWDsolution.com
 * license GNU/GPL http://www.gnu.org/copyleft/gpl.html
 * copyright Copyright (C) 2009 AWDsolution.com. All rights reserved.
*/

// post msg with ajax
function aPostMsg(url, url1)
{
	if(document.frm_message.awd_message.value != ''){
		document.getElementById("msg_loader").innerHTML = '<img src="components/com_awdwall/images/ajax-loader.gif" />';
		jQuery.post(url, jQuery("#frm_message").serialize(), 
		function(data){
			document.frm_message.awd_message.value = '';
			jQuery('#awd_message').trigger('blur');
			document.getElementById("msg_loader").innerHTML = "";
			jQuery("#msg_content").prepend(data);
			if(url1 != ''){
				getLatestPostByUserId(url1);
			}
		}
		, "html");
	}
}

function getLatestPostByUserId(url)
{
	jQuery.get(url, {}, 
			function(data){
				if(data != '')
					document.getElementById("awd_profile_status").innerHTML = data;			
			}, "html");
}

function showCommentBox(url, cid, commenter_id, wall_id)
{
	var str = document.getElementById("c_" + cid).innerHTML;
	
	if(str.toLowerCase().indexOf("textarea") == -1){		
		document.getElementById("c_loader_" + cid).style.display = 'inline-block';	
		jQuery.get(url + '&cid=' + cid + '&commenter_id=' + commenter_id + '&wall_id=' + wall_id, {}, 
		function(data){
			document.getElementById("c_loader_" + cid).style.display = 'none';
			jQuery("#c_" + cid).append(data);			
		//	document.getElementById("c_" + cid).append(data);
		}, "html");
	}else{ 
		document.getElementById("c_loader_" + cid).style.display = 'inline-block';
		var t = setTimeout("document.getElementById('c_loader_" + cid + "').style.display = 'none'", 100);
		jQuery('#frm_comment_' + cid).remove();	
	}
}

function closeCommentBox(cid)
{
	document.getElementById("c_loader_" + cid).style.display = 'inline-block';
	var t = setTimeout("document.getElementById('c_loader_" + cid + "').style.display = 'none'", 100);
	jQuery('#frm_comment_' + cid).remove();
}

function aPostComment(url, cid)
{	
	document.getElementById("c_loader_" + cid).style.display = 'inline-block';
	if(document.getElementById("awd_comment_" + cid).value != "")
		jQuery.post(url, jQuery("#frm_comment_" + cid).serialize(), 
		function(data){
			jQuery('#frm_comment_' + cid).remove();	
			document.getElementById("c_loader_" + cid).style.display = 'none';
			jQuery("#c_content_" + cid).prepend(data);		
		}
		, "html");
}

function openMsgDeleteBox(url, block_id)
{	
	document.getElementById("msg_delete_url").value = url;
	document.getElementById("msg_delete_block_id").value = block_id;	
	jQuery('#dialog_msg_delete_box').dialog().dialog('open');
}

jQuery(document).ready(function(){	
	jQuery('#dialog_msg_delete_box').dialog({ autoOpen: false, width: 400, resizable: false,
	buttons:{
				Cancel: function(){
					jQuery(this).dialog('close');
				},
				'Ok': function(){
					document.getElementById("msg_delete_loader").innerHTML = '<img src="components/com_awdwall/images/ajax-loader.gif" />';
					jQuery.post(document.getElementById("msg_delete_url").value, {}, function(data){});
					document.getElementById("msg_delete_loader").innerHTML = '';
					jQuery('#msg_block_' + document.getElementById("msg_delete_block_id").value).remove();					
					jQuery(this).dialog('close');
				}
		}
	});
	// comment
	jQuery('#dialog_c_delete_box').dialog({ autoOpen: false, width: 400, resizable: false,
	buttons:{
				Cancel: function(){
					jQuery(this).dialog('close');
				},
				'Ok': function(){
					document.getElementById("c_delete_loader").innerHTML = '<img src="components/com_awdwall/images/ajax-loader.gif" />';
					jQuery.post(document.getElementById("c_delete_url").value, {}, function(data){});
					document.getElementById("c_delete_loader").innerHTML = '&nbsp;';
					jQuery('#c_block_' + document.getElementById("c_delete_block_id").value).remove();					
					jQuery(this).dialog('close');
				}
		}
	});
	// like
	jQuery('#dialog_like_box').dialog({ autoOpen: false, width: 400, resizable: false,
	buttons:{
				Cancel: function(){
					jQuery(this).dialog('close');
				},
				'Ok': function(){
					document.getElementById("like_loader").innerHTML = '<img src="components/com_awdwall/images/ajax-loader.gif" />';
					jQuery.post(document.getElementById("like_url").value, {}, function(data){});
					document.getElementById("like_loader").innerHTML = '&nbsp;';					
					jQuery(this).dialog('close');
					// get who likes it data too
					jQuery.get(document.getElementById("who_like_url").value, {}, 
						function(data){	
							document.getElementById("wholike_box_" + document.getElementById("who_like_wid").value).innerHTML = data;
						}, "html");
				}
		}
	});
	// pm
	jQuery('#dialog_pm_delete_box').dialog({ autoOpen: false, width: 400, resizable: false,
	buttons:{
				Cancel: function(){
					jQuery(this).dialog('close');
				},
				'Ok': function(){
					document.getElementById("pm_delete_loader").innerHTML = '<img src="components/com_awdwall/images/ajax-loader.gif" />';
					jQuery.post(document.getElementById("pm_delete_url").value, {}, function(data){});
					document.getElementById("pm_delete_loader").innerHTML = '&nbsp;';
					jQuery('#c_block_' + document.getElementById("pm_delete_block_id").value).remove();					
					jQuery(this).dialog('close');
				}
		}
	});
	
	// video
	jQuery('#dialog_video').dialog({ autoOpen: false, width: 450, height: 300, resizable: false});
	// image
	jQuery('#dialog_image').dialog({ autoOpen: false, width: 430, height: 250, resizable: false});
	jQuery("a[rel^='prettyPhoto']").prettyPhoto();
	// search
	jQuery("#search_user").autocomplete("index.php?option=com_awdwall&view=awdwall&task=auto&tmpl=component", { minChars:1, matchSubset:1, matchContains:1, cacheLength:10, onItemSelect:selectItem, formatItem:formatItem, selectOnly:1 });
	// control height of textarea
	jQuery('#awd_message').css('width', '98%');
				// Google Chrome doesn't return correct outerWidth() else things would be nicer.
				// css('width', width()*2 - outerWidth(true));
	jQuery('#awd_message').css('width', jQuery('#awd_message').width() - parseInt(jQuery('#awd_message').css('borderLeftWidth'))
				                     - parseInt(jQuery('#awd_message').css('borderRightWidth'))
				                     - parseInt(jQuery('#awd_message').css('padding-left'))
				                     - parseInt(jQuery('#awd_message').css('padding-right')));
	
	var	options = {};
	options.lineHeight = 0;
	options.minHeight = 50;
	options.maxHeight = options.maxHeight || 300;
	jQuery('#awd_message').autogrow(options);
});

function openCommentDeleteBox(url, block_id)
{
	document.getElementById("c_delete_url").value = url;
	document.getElementById("c_delete_block_id").value = block_id;	
	jQuery('#dialog_c_delete_box').dialog().dialog('open');
}

function openLikeMsgBox(url, url1, wid)
{
	document.getElementById("like_url").value = url;
	document.getElementById("who_like_url").value = url1;
	document.getElementById("who_like_wid").value = wid;
	jQuery('#dialog_like_box').dialog().dialog('open');
}

function getWhoLikeMsg(url, wid)
{	
	var str = document.getElementById("like_" + wid).innerHTML;
	
	if(str.toLowerCase().indexOf("whitebox") == -1){
		document.getElementById("like_" + wid).innerHTML = '<img src="components/com_awdwall/images/ajax-loader.gif" />';
		jQuery.get(url, {}, 
		function(data){	
			document.getElementById("like_" + wid).innerHTML = data;		
		}, "html");
	}else{
		document.getElementById("like_" + wid).innerHTML = '<img src="components/com_awdwall/images/ajax-loader.gif" />';
		document.getElementById("like_" + wid).innerHTML = '';
	}
}

function showPMBox(url, cid, commenter_id, wall_id)
{
	var str = document.getElementById("pm_" + cid).innerHTML;
	
	if(str.toLowerCase().indexOf("textarea") == -1){
		document.getElementById("pm_loader_" + cid).style.display = 'inline-block';	
		jQuery.get(url + '&cid=' + cid + '&commenter_id=' + commenter_id + '&wall_id=' + wall_id, {}, 
		function(data){
			document.getElementById("pm_loader_" + cid).style.display = 'none';
			jQuery("#pm_" + cid).append(data);
		//	document.getElementById("pm_" + cid).innerHTML = data;			
		}, "html");
	}else{ 		
		document.getElementById("pm_loader_" + cid).style.display = 'inline-block';
		var t = setTimeout("document.getElementById('pm_loader_" + cid + "').style.display = 'none'", 100);
		jQuery('#frm_pm_' + cid).remove();	
	}
}

function closePMBox(cid)
{
	document.getElementById("pm_loader_" + cid).style.display = 'inline-block';
	var t = setTimeout("document.getElementById('pm_loader_" + cid + "').style.display = 'none'", 100);
	jQuery('#frm_pm_' + cid).remove();
}

function aPostPM(url, cid)
{	
	document.getElementById("pm_loader_" + cid).style.display = 'inline-block';
	if(document.getElementById("awd_pm_" + cid).value != "")
		jQuery.post(url, jQuery("#frm_pm_" + cid).serialize(), 
		function(data){
			jQuery('#frm_pm_' + cid).remove();	
			document.getElementById("pm_loader_" + cid).style.display = 'none';
			jQuery("#c_content_" + cid).prepend(data);		
		}
		, "html");
}

function openPMDeleteBox(url, block_id)
{
	document.getElementById("pm_delete_url").value = url;
	document.getElementById("pm_delete_block_id").value = block_id;	
	jQuery('#dialog_pm_delete_box').dialog().dialog('open');
}

function openVideoBox()
{	
	jQuery('#dialog_video').dialog().dialog('open');
}

function openImageBox()
{	
	jQuery('#dialog_image').dialog().dialog('open');
}

function checkImageForm(){
	var frm = document.getElementById('frmUpuloadImage');	
	var flag = true;
	if(trim(frm.image_name.value) == ''){
		el = $('image_name');
		var labels = $$('label');
		labels.each(function(label){
			if (label.getProperty('for') == el.getProperty('id')) {
				label.addClass('invalid');
			}
		});	
		flag = false;
	}
	
	if(trim(frm.main_image.value) == ''){
		el = $('main_image');
		var labels = $$('label');
		labels.each(function(label){
			if (label.getProperty('for') == el.getProperty('id')) {
				label.addClass('invalid');
			}
		});	
		flag = false;
	}
	
	if(trim(frm.image_description.value) == ''){
		el = $('image_description');
		var labels = $$('label');
		labels.each(function(label){
			if (label.getProperty('for') == el.getProperty('id')) {
				label.addClass('invalid');
			}
		});	
		flag = false;
	}
	return flag;
}

function trim(str) {
	return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function isUrl(s){
	var regexp = /^(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/
	return regexp.test(s);
}

function isUrl1(s){
	var regexp = 
/^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/
	return regexp.test(s);
}

// auto complete
function selectItem(li) {
	if (li.extra) {
		alert("That's '" + li.extra + "' you picked.")
	}
}
function formatItem(row) {
	//return row[0] + "<br><i>" + row[1] + "</i>";
	return row[0];
}

function getOlderPosts(url)
{
	var page = document.getElementById("awd_page").value;
	var task = document.getElementById("task").value;
	document.getElementById("older_posts_loader").style.display = 'inline-block';
	jQuery.get(url + '&awd_page=' + page + '&type=' + task, {}, 
	function(data){
		document.getElementById("older_posts_loader").style.display = 'none';
		page = parseInt(page)+1;
		document.getElementById("awd_page").value = page;		
		jQuery("#msg_content").append(data);
	}, "html");
}

function getOlderComments(url, cid)
{
	var page = document.getElementById("awd_c_page_" + cid).value;
	document.getElementById("older_comments_loader_" + cid).style.display = 'inline-block';
	jQuery.get(url + '&awd_c_page=' + page  + '&cid=' + cid, {}, 
	function(data){
		document.getElementById("older_comments_loader_" + cid).style.display = 'none';
		page = parseInt(page)+1;
		document.getElementById("awd_c_page_" + cid).value = page;		
		jQuery("#c_content_" + cid).append(data);
	}, "html");
}

function fbs_click(u, t) 
{	
	var width = 626;
    var height = 436;
    var left = parseInt((screen.availWidth/2) - (width/2));
    var top = parseInt((screen.availHeight/2) - (height/2));
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),' sharer', 'toolbar=0, status=0, width=' + width + ",height=" + height + ",status,resizable,left=" + left + ",top=" + top + "screenX=" + left + ",screenY=" + top); 
	return false; 
}

/* 
 * Auto Expanding Text Area (1.2.2)
 * by Chrys Bader (www.chrysbader.com)
 * chrysb@gmail.com
 *
 * Special thanks to:
 * Jake Chapa - jake@hybridstudio.com
 * John Resig - jeresig@gmail.com
 *
 * Copyright (c) 2008 Chrys Bader (www.chrysbader.com)
 * Licensed under the GPL (GPL-LICENSE.txt) license. 
 *
 *
 * NOTE: This script requires jQuery to work.  Download jQuery at www.jquery.com
 *
 */
 
(function(jQuery) {
		  
	var self = null;
 
	jQuery.fn.autogrow = function(o)
	{	
		return this.each(function() {
			new jQuery.autogrow(this, o);
		});
	};
	

    /**
     * The autogrow object.
     *
     * @constructor
     * @name jQuery.autogrow
     * @param Object e The textarea to create the autogrow for.
     * @param Hash o A set of key/value pairs to set as configuration properties.
     * @cat Plugins/autogrow
     */
	
	jQuery.autogrow = function (e, o)
	{
		this.options		  	= o || {};
		this.dummy			  	= null;
		this.interval	 	  	= null;
		this.line_height	  	= 20;
		this.min_height		  	= this.options.minHeight || parseInt(jQuery(e).css('min-height'));
		this.max_height		  	= this.options.maxHeight || parseInt(jQuery(e).css('max-height'));;
		this.textarea		  	= jQuery(e);
		
		if(this.line_height == NaN)
		  this.line_height = 0;
		
		// Only one textarea activated at a time, the one being used
		this.init();
	};
	
	jQuery.autogrow.fn = jQuery.autogrow.prototype = {
    autogrow: '1.2.2'
  };
	
 	jQuery.autogrow.fn.extend = jQuery.autogrow.extend = jQuery.extend;
	
	jQuery.autogrow.fn.extend({
						 
		init: function() {			
			var self = this;			
			this.textarea.css({overflow: 'hidden', display: 'block'});
			this.textarea.bind('focus', function() { self.startExpand() } ).bind('blur', function() {self.stopExpand();self.checkExpand();});
			this.checkExpand();	
		},
						 
		startExpand: function() {				
		  var self = this;
			this.interval = window.setInterval(function() {self.checkExpand()}, 400);
		},
		
		stopExpand: function() {
			clearInterval(this.interval);	
		},
		
		checkExpand: function() {
			
			if (this.dummy == null)
			{
				this.dummy = jQuery('<div></div>');
				this.dummy.css({
												'font-size'  : this.textarea.css('font-size'),
												'font-family': this.textarea.css('font-family'),
												'width'      : this.textarea.css('width'),
												'padding'    : this.textarea.css('padding'),
												'line-height': '20px',
												'overflow-x' : 'hidden',
												'position'   : 'absolute',
												'top'        : 0,
												'left'		 : -9999
												}).appendTo('body');
			}
			
			// Strip HTML tags
			var html = this.textarea.val().replace(/(<|>)/g, '');
			
			// IE is different, as per usual
			if (jQuery.browser.msie)
			{
				html = html.replace(/\n/g, '<BR>new');
			}
			else
			{
				html = html.replace(/\n/g, '<br>new');
			}
			
			if (this.dummy.html() != html)
			{
				this.dummy.html(html);	
				
				if (this.max_height > 0 && (this.dummy.height() + this.line_height > this.max_height))
				{
					this.textarea.css('overflow-y', 'auto');	
				}
				else
				{
					this.textarea.css('overflow-y', 'hidden');
			
					if ((this.textarea.height() < this.dummy.height() + this.line_height || (this.dummy.height() < this.textarea.height())) && this.dummy.height() > this.min_height)
					{	
						this.textarea.animate({height: (this.dummy.height() + this.line_height) + 'px'}, 100);	
					}else{
						
						this.textarea.animate({height: (this.min_height) + 'px'}, 100);	
					}
				}
			}
		}
						 
	 });
	 
})(jQuery);