/*
 * @package		plg_system_jpopmessages
 * @copyright	Copyright (C) 2012 Girolamo Tomaselli All rights reserved.
 * @email		girotomaselli@gmail.com
 * @website		http://bygiro.com
 * @license		GNU General Public License version 2 or later
 */

function jpopmessages(){
	var condition,
		sys_mes_con = jQuery('#system-message-container'),
		sys_mes = jQuery('#system-message');

	// added support for &%$£@#§ yootheme templates!!!!
	var alertsYT = jQuery('.uk-alert,[data-uk-alert]');
	if(alertsYT.length > 0){
		if(jQuery('#jPopMessagesContainer').length > 0){
			sys_mes_con = jQuery('#jPopMessagesContainer');
			sys_mes_con.append(alertsYT);
		} else {
			sys_mes_con = jQuery('<div id="jPopMessagesContainer"></div>');
			sys_mes_con.append(alertsYT).appendTo('body');
		}
	}
	
	if(sys_mes_con.length <= 0){
		return;
	} else if(sys_mes.length <= 0){
		condition = (sys_mes_con.html().trim() != '');
	} else {
		condition = (sys_mes.html().trim() != '');
	}

	if(condition){
		// check we already have the jPopModal		
		if(jQuery('#jPopModal').length <= 0){
		// create bootstrap modal HTML
		var html = '<div id="jPopModal" class="modal hide fade '+ jpm_cust_class +'" tabindex="-1" role="dialog" aria-hidden="true">';
			html += '<span class="close" data-dismiss="modal" aria-hidden="true" style="font-size: 30px; color: #000; margin: 5px 5px 0 0; opacity: 0.9;">&times;</span>';
			html += '<div style="clear: both;"></div><div class="modal-body"></div>';
			html += '</div>';
			
			jQuery('body').append(html);
		}
		
		// add the modal content
		jQuery('#jPopModal .modal-body').append(sys_mes_con.html());
		sys_mes_con.html('');
		
		if(jQuery('#jPopModal').is(':hidden')){
			jQuery('#jPopModal').modal('show');
		}

		if(typeof window['jPopMessagesInitialized'] == 'undefined'){
			jQuery('#jPopModal').on('hidden', function(){
				jQuery('#jPopModal .modal-body').html('');
			});
			
			window['jPopMessagesInitialized'] = true;
		}
	}
}
 
jQuery(document).ready(function(){
	jpopmessages();
});