/**
 * [PHPFOX_HEADER]
 * 
 * @copyright		[PHPFOX_COPYRIGHT]
 * @author			Raymond Benc
 * @package 		Phpfox
 * @version 		$Id: main.js 1494 2010-03-05 14:23:08Z Raymond_Benc $
 */
var Phpfox = {};
var Behavior = {};

$.ajaxSetup({
	type: "POST",
  	url: getParam('sJsStatic') + "ajax.php",
  	dataType: "script"
});

$.fn.message = function(sMessage, sType) 
{
	switch(sType)
	{
		case 'valid':
			sClass = 'valid_message';
			break;
		case 'error':
			sClass = 'error_message';
			break;
		case 'public':
			sClass = 'public_message';
			break;
	}
	this.html(this.html() + '<div class="' + sClass + '">' + sMessage + '</\div>');
	
	return this;
}

$.getParams = function(sUrl)
{
	var aArgs = sUrl.split('#');
	var aArgsFinal = aArgs[1].split('?');	
	var aFinal = aArgsFinal[1].split('&');
	
	var aUrlParams = Array();
	if (aArgs[0].search(/inline/) == -1)
	{
		aUrlParams['main_url'] = aArgs[0];
	}
	for (i = 0; i < aFinal.length; i++)
	{
		var aArg = aFinal[i].split('=');	
		
		aUrlParams[aArg[0]] = aArg[1];
	}
	
	return aUrlParams;
}

$.ajaxProcess = function(sMessage, sSize)
{
	sMessage = (sMessage ? sMessage : getPhrase('core.processing'));
	
	if (empty(sSize))
	{
		sSize = 'small';
	}	
	
	return '<span style="margin-left:4px; margin-right:4px; font-size:9pt; font-weight:normal;"><img src="' + eval('oJsImages.ajax_' + sSize + '') + '" class="v_middle" /> ' + (sMessage === 'no_message' ? '' : sMessage + '...') + '</span>';
}

Behavior.imageHoverHolder = function()
{
	$('.image_hover_holder').hover(function()
	{		
		if (!empty($(this).find('.image_hover_menu:first').html()))
		{
			$(this).addClass('image_hover_holder_hover').find('.image_hover_menu:first').show();
		}
	},
	function()
	{
		$(this).removeClass('image_hover_holder_hover').find('.image_hover_menu:first').hide();
	});
}

Behavior.targetBlank = function()
{
	$('.targetBlank').click(function()
	{
		window.open($(this).get(0).href);
		return false;
	});
}

var bCacheIsHover = false;
Behavior.dropDown = function()
{	
	$('.sJsDropMenu').click(function()
	{
		$(this).blur();
		
		$('.dropContent').hide();
		
		if ($(this).hasClass('is_already_open'))
		{
			$(this).parent().find('.dropContent:first').hide();
			$(this).removeClass('is_already_open');
		}
		else
		{
			$(this).parent().find('.dropContent:first').show();	
			$(this).addClass('is_already_open');	
		}
			
		return false;
	});	
	
	
	$('.dropContent').hover(function()
	{
		bCacheIsHover = false;	
	},
	function()
	{
		bCacheIsHover = true;
		setTimeout("if (bCacheIsHover) { $('.dropContent').hide(); $('.sJsDropMenu').removeClass('is_already_open'); bCacheIsHover = false; }", 1000);
	});	
}

/**
 * Drop down auto jump
 */
Behavior.goJump = function()
{
	$('.goJump').change(function()
	{
		// Empty value, do nothing
		if ($(this).get(0).value == "")
		{
			return false;
		}		
		
		// Is this a delete link? If it is make sure they confirm they want to delete the item
		if ($(this).get(0).value.search(/delete/i) != -1 && !confirm(getPhrase('core.are_you_sure')))
		{
			return false;
		}		
		
		// All set lets send them to the new page
		window.location.href = $(this).get(0).value;
	});
}

Behavior.inlineCheckbox = function()
{
	$('.inlineCheckbox').click(function()
	{
		if ($(this).get(0).checked)
		{
			$(this).parent().parent().addClass('inlineMessage');
		}
		else		
		{
			$(this).parent().parent().removeClass('inlineMessage');
		}
	});
}

Behavior.inlinePopup = function()
{
	$('.inlinePopup').click(function()
	{
		var $aParams = $.getParams($(this).get(0).href);
		var sParams = '&tb=true';
		for (sVar in $aParams)
		{			
			sParams += '&' + sVar + '=' + $aParams[sVar] + '';
		}
		sParams = sParams.substr(1, sParams.length);		
		
		tb_show($(this).get(0).title, $.ajaxBox($aParams['call'], sParams));		
		
		return false;
	});
}

Behavior.hoverAction = function()
{
	$('.hover_action_div').hover(
		function () 
		{
			$(this).show();
			
			return false;
		},
		function ()
		{
			$(this).hide();
			
			return false;
		}
	);
	
    $(".hover_action").hover(
   		function () 
   		{
        	$(this).parents('div:first').find('.hover_action_div').show();
      	}, 
      	function () 
      	{
        	$(this).parents('div:first').find('.hover_action_div').hide();
      	}
    );
}

Behavior.blockClick = function()
{
	$('.block .menu ul li a').click(function()
	{
		$(this).parents('.block:first').find('li').removeClass('active');
		$(this).parent().addClass('active');
		
		if (this.href.match(/#/))
		{
			var aParts = explode('#', this.href);
			var aParams = explode('?', aParts[1]);
			var aParamParts = explode('&', aParams[1]);
			var aRequest = Array();
			for (i in aParamParts)
			{
				var aPart = explode('=', aParamParts[i]);
				
				aRequest[aPart[0]] = aPart[1];
			}			

			$('.js_block_click_lis_cache').remove();
			$(this).parents('.menu:first').find('ul').append('<li class="js_block_click_lis_cache" style="margin-top:2px;">' + $.ajaxProcess('no_message') + '</li>');
			$.ajaxCall(aParams[0], aParams[1] + '&js_block_click_lis_cache=true');			
		}		
		
		return false;
	});
}

Behavior.deleteLink = function()
{
	$('.delete_link').click(function()
	{
		if (confirm(getPhrase('core.are_you_sure')))
		{
			$aParams = $.getParams($(this).get(0).href);
			var sParams = '';
			for (sVar in $aParams)
			{			
				sParams += '&' + sVar + '=' + $aParams[sVar] + '';
			}
			sParams = sParams.substr(1, sParams.length);
				
			$.ajaxCall($aParams['call'], sParams);
		}
				
		return false;			
	});	
}

Phpfox.loadInit = function()
{
	debug('Behavior.init() Loaded');	
	
	$('.inlinePopup').unbind('click');
	
	$.each(Behavior, function() 
	{
		this(this);
	});
}

Phpfox.init = function()
{		
	debug('Phpfox.init() Loaded');
	
	$(document).ready(function()
	{		
		// Confirm before deleting an item
		$('.sJsConfirm').click(function()
		{
			if (confirm(getPhrase('core.are_you_sure')))
			{
				return true;
			}
			return false;
		});
			
		$.each(Behavior, function() 
		{
			this(this);
	    });	
	});	
	
	// Load the default editor
	if ((!getParam('bWysiwyg') || typeof(bForceDefaultEditor) != 'undefined') && typeof(Editor) == 'object')
	{
		Editor.getEditors();
	}
	
	if (empty($('#sidebar').html()))
	{
		$('#content').removeClass('split');
		$('#content').addClass('full');
	}
	
	$('#select_lang_pack').click(function()
	{
		tb_show(oTranslations['core.language_packages'], $.ajaxBox('language.select', 'height=300&amp;width=300'));
		
		return false;
	});
	
	$('.js_update_status').click(function()
	{		
		if (document.getElementById('js_current_user_status').style.display == '' 
			|| document.getElementById('js_current_user_status').style.display == 'inline'
			|| document.getElementById('js_current_user_status').style.display == 'block'
		)
		{
			$('#js_current_user_status').hide();
			$('#js_update_user_status').show();		
			$('#js_status_input').focus();	
		}
		else
		{
			$('#js_current_user_status').show();
			$('#js_update_user_status').hide();
		}
		
		return false;
	});	
	
	$('#js_footer_start_button').click(function()
	{		
		deleteCookie('im_last_open_window');
		
		if ($(this).hasClass('is_already_open'))
		{
			$('.js_footer_holder').hide();	
			$(this).removeClass('focus');	
			$(this).removeClass('is_already_open');	
		}
		else
		{		
			$('#footer_bar').find('a').removeClass('focus').removeClass('is_already_open');
			$(this).addClass('is_already_open');
			$('.js_footer_holder').hide();		
			$('#footer_bar a').removeClass('focus');
			$(this).addClass('focus');
			$('#footer_menu').show();
		}	
		
		return false;
	});
	
	$('#js_favorite_start_button').click(function()
	{		
		deleteCookie('im_last_open_window');
		
		if ($(this).hasClass('is_already_open'))
		{
			$('.js_footer_holder').hide();	
			$(this).removeClass('focus');	
			$(this).removeClass('is_already_open');	
		}
		else
		{		
			$('#footer_bar').find('a').removeClass('focus').removeClass('is_already_open');
			$(this).addClass('is_already_open');
			$('.js_footer_holder').hide();		
			$('#footer_bar a').removeClass('focus');
			$(this).addClass('focus');
			$('#footer_bar_favorite').show();
			if ($('#js_footer_bar_favorite_content').html().match(/EMPTY_FOOTER_BAR/))
			{
				$('#js_footer_bar_favorite_content').html($.ajaxProcess(oTranslations['language.loading'], 'large'));
				$.ajaxCall('favorite.getFooterBar');
			}
		}	
		
		return false;
	});	
	
	$('.js_notify_link').click(function()
	{	
		deleteCookie('im_last_open_window');
		
		if ($(this).hasClass('is_already_open'))
		{
			$('.js_footer_holder').hide();	
			$(this).removeClass('focus');	
			$(this).removeClass('is_already_open');	
		}
		else
		{			
			$('#footer_bar').find('a').removeClass('focus').removeClass('is_already_open');
			eleOffset = $(this).offset();
			$(this).addClass('is_already_open');
			$('.js_footer_holder').hide();
			$('#footer_bar a').removeClass('focus');
			$(this).addClass('focus');	
			$('#js_notification_popup').find('.js_footer_holder').show();		
			$('#footer_notification').remove();		
			$('body').prepend('<div id="footer_notification">' + $('#js_notification_popup').html() + '</div>');
			
			if ($('#js_notification_border').height() < 300)
			{
				$('#js_notification_over_flow').height($('#js_notification_border').height());
			}
			
			var sIsSeenIds = $('#js_notification_seen_id').html();			
			
			if (!empty(sIsSeenIds))
			{
				$('#js_notification_seen_id').html("");
				$('#js_notification_total_count').html("0");
				$('#js_notification_total_count_show').html("0");
				$('#js_notification_total_count_show').hide();
				$.ajaxCall('notification.updateSeen', 'id=' + sIsSeenIds);
			}		
		}
				
		return false;
	});
	
	/*
	if ($('#js_ad_space_2').length > 0)
	{			
		if ($('.header_bar_menu').length > 0)
		{			
			$('.header_bar_menu').after($('#js_ad_space_2').parent().html());
			$('#js_ad_space_2').parent().html('');
		}	
		if ($('.header_bar').length > 0)
		{			
			$('.header_bar').after($('#js_ad_space_2').parent().html());
			$('#js_ad_space_2').parent().html('');
		}	
	}	
	*/
	
	if (!oCore['core.is_admincp'])
	{
		if ($('#country_iso').length > 0 && !empty(oCore['core.country_iso']))
		{			
			if (empty($('#country_iso').val()))
			{
				$('#js_country_iso_option_' + oCore['core.country_iso']).attr('selected', true);			
			}	
		}
	}
	
    $('.js_item_active').click(function()
    {    	
    	$(this).parent().find('.js_item_active input').attr('checked', false);
    	if ($(this).hasClass('item_is_active'))
    	{
    		$(this).parent().find('.item_is_active input').attr('checked', true);
    	}
    	else
    	{
    		$(this).parent().find('.item_is_not_active input').attr('checked', true);
    	}
    });	
    
    $('.hover_action').hover(function()
    {
    	$(this).find('.row_action:first').show();
    },
    function()
    {
    	$(this).find('.row_action:first').hide();
    });
}

Phpfox.getObjectPosition = function(sId) 
{
	if ($('#' + sId).length <= 0)
	{
		return false;
	}
	
	var curleft = 0;
    var curtop = 0;
    var obj = document.getElementById(sId);
    if (obj.offsetParent) 
    {
    	do 
    	{
        	curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
    
	return {left: curleft, top: curtop};
}

Phpfox.getFriends = function(aParams)
{
	tb_show('', $.ajaxBox('friend.search', 'height=410&width=600&input=' + aParams['input'] + '&type=' + (isset(aParams['type']) ? aParams['type'] : '') + ''));
}

Phpfox.browseUsers = function(aParams)
{
	tb_show('', $.ajaxBox('user.browse', 'height=410&width=600&input=' + aParams['input'] + ''));
}

Phpfox.getParams = function(sHref)
{
	var aParams = new Array();
	var aUrlParts = explode('/', sHref);
	var iRequest = 0;
	for (i in aUrlParts)
	{
		if (empty(aUrlParts[i]))
		{
			continue;
		}
			
		aUrlParts[i] = aUrlParts[i].replace('#', '');
		if (aUrlParts[i].match(/_/i))
		{
			var aUrlParams = explode('_', aUrlParts[i]);
				
			aParams[aUrlParams[0]] = aUrlParams[1];
		}
		else
		{
			iRequest++;			

			aParams['req' + iRequest] = aUrlParts[i];		
		}	
	}	
	
	return aParams;	
}

Phpfox.getRequests = function(sHref, bReturnPath)
{
	var sParams = '';	
	var sUrlString = '';
	var sModuleName = getParam('core.section_module');	
	
	switch (oCore['core.url_rewrite'])
	{
		case '1':
			var oReq = new RegExp("" + getParam('sJsHome') + "(.*?)$","i");
			var aMatches = oReq.exec(sHref);
			var aParts = explode('/', aMatches[1]);
						
			// sUrlString = (aParts[0] == sModuleName ? '' : sModuleName + '/') + aMatches[1];		
			sUrlString = '/' + aMatches[1];			
					
			break;
		case '3':
			if (oCore['profile.is_user_profile'])
			{
				var aProfileMatches = sHref.match(/http:\/\/(.*?)\.(.*?)/i);
				sModuleName = aProfileMatches[1];
			}		
		
			var oReq = new RegExp("" + getParam('sJsHome') + "(.*?)$","i");
			var aMatches = oReq.exec(sHref);
			
			sUrlString = sModuleName + '/' + aMatches[1];		
			break;
		default:
			var oReq = new RegExp("(.*?)=\/(.*?)$","i");
			var aMatches = oReq.exec(sHref);			
			var aParts = explode('/', aMatches[2]);		
			
			sUrlString = aMatches[2];		
					
			break;
	}	
	
	if (bReturnPath === true)
	{
		return '/' + ltrim(sUrlString, '/');
	}	
	
	return Phpfox.parseUrlString(sUrlString);
}

Phpfox.parseUrlString = function(sUrlString)
{
	var sParams = '';
	var aUrlParts = explode('/', sUrlString);
	var iRequest = 0;
	var iLoadCount = 0;
	
	for (i in aUrlParts)
	{
		if (empty(aUrlParts[i]) || aUrlParts[i] == '#')
		{
			continue;
		}		
		
		iLoadCount++;
		
		if (iLoadCount != 1 && aUrlParts[i].match(/_/i))
		{
			var aUrlParams = explode('_', aUrlParts[i]);
				
			sParams += '&' + aUrlParams[0] + '=' + aUrlParams[1];	
		}
		else
		{
			iRequest++;
			
			sParams += '&req' + iRequest + '=' + aUrlParts[i];						
		}	
	}	
	
	return sParams;
}

Phpfox.reverseUrl = function(sForm, aSkip)
{	
	var aForms = explode('&', sForm);	
	var sUrlParam = '';	
	for (i in aForms)
	{			
		var aFormParts = aForms[i].match(/(.*?)=(.*?)$/i);
		if (aFormParts !== null)
		{
			if (aFormParts[1].match(/phpfox/i))
			{
				continue;
			}
			
			if (isset(aSkip))
			{				
				if (in_array(aFormParts[1], aSkip))
				{					
					continue;
				}
			}
				
			sUrlParam += aFormParts[1] + '_' + encodeURIComponent(aFormParts[2]) + '/';
		}
	}		
		
	return sUrlParam;
}

/**
 * Adds a hash to the URL string, which is used to emulate a AJAX page
 *
 * @param object oObject Is the anchor object (this)
 */
Phpfox.addUrlPager = function(oObject)
{	
	window.location = '#' + Phpfox.getRequests(oObject.href, true);
}

Phpfox.getHashParam = function(sHref)
{
	var sParams = '';
	var aParams = $.getParams(sHref);
	
	for (var sKey in aParams)
	{
		sParams += '&' + sKey + '=' + aParams[sKey];
	}
	sParams = ltrim(sParams, '&');
	
	return sParams;
}

Phpfox.popup = function(sUrl, aParams)
{
	oDate = new Date();
	iId = oDate.getTime();
	var sParams = '';
	var iCount = 0;
	var bCenter = false;
	for (i in aParams)
	{
		if (i == 'center')
		{
			bCenter = true;
			continue;
		}
		
		iCount++;
		if (iCount != 1)
		{
			sParams += ',';
		}	
		
		sParams += i + '=' + aParams[i];
	}
	
	if (bCenter === true)
	{
		sParams += ',left=' + (($(window).width() - aParams['width']) / 2) + ',top=' + (($(window).height() - aParams['height']) / 2) + '';
	}
	
	window.open(sUrl, iId, sParams);
}

Phpfox.ajaxMessage = function()
{
	$('#global_ajax_message').html('Saving...').animate({opacity: 0.4}).show();
}

window.alert = function(sPhrase)
{
	tb_show('', $.ajaxBox('core.javascriptAlert', 'height=200&width=300&phrase=' + encodeURIComponent(sPhrase)));
}
/*
window.confirm = function(sPhrase)
{
	tb_show('', $.ajaxBox('core.javascriptConfirm', 'height=200&width=300&phrase=' + encodeURIComponent(sPhrase)));
}
*/

/**
 * Used for the accordion effect on sections with many categories
 */
Phpfox.toggleCategory = function(sName, iId)
{
    $('.special_subcategory').each(function(){
	if ($(this).attr('class').indexOf(sName) == -1)
	{	    
	    $(this).hide();	    
	}
    });
    $('.category_show_more_less').each(function(){
	if ($(this).attr('id').indexOf('show_more_') >= 0)
	{
	    $(this).show();
	}
	
	if ($(this).attr('id').indexOf('show_less_') >= 0)
	{
	    $(this).hide();
	}
    });

    $('.' + sName).toggle();    
    $('#show_more_' + iId).toggle();
    $('#show_less_' + iId).toggle();
    
    
}