/*
 * The base class/namespace for all modal_base shared javascript functions
 */

var modal_base = function() {

};

modal_base.prototype = {

	/*
	 * this function creates a modal and populates the input fields
	 */
	init : function(_type, _title, _elem, _actionTitle, _onclick, _args, _xtras) {

	
	
		/* create the modal container */
		$.facebox($('#modalType_' + _type).html());

		/* inject the title and content */
		$('#facebox').find('#title').html(_title); 
		$('#facebox').find('#content').html($(_elem).html());

		/* inject the action and cancel buttons */
		if(_xtras == "no action")
			$('#facebox').find('#ffooter').hide();
		else
			$('#facebox').find('#ffooter').html($('#modal_actions').html());

/*
 * Take a look at the xtras argument to see if there is need for an additional
 * (register button) - this could be extended in future to accomodate more
 * actions well say we keep using the good 'ol fb
 */
 		
		if(_xtras == 1){
			if(!$('#ffooter').find('#reg_button_fb'))
				$('#ffooter').append('<a id="reg_button_fb" href=\'/join?next='+escape(location.href)+'\' class="modal_action">Register</a>');
		}	
		if(_xtras == 2){ 
			$('#regular_close_button').hide();
			$('#old_close_button').show();  
		}	

		if(_xtras == 3){ 
			$('#regular_close_button').hide();
			$('#old_close_button').show();  
			$('#ffooter').hide();			
		}
	
	$('#facebox').find('#actionTitle').html(_actionTitle);
	if (_onclick != undefined) {
		$('#facebox').find('#actionOnclick').click( function() {
			$.facebox.close();

			_onclick(_args);
			});
		}
	}
	
};



/* instantiate a singleton of this class for use */
var modal_base = new modal_base();

$(document).bind('reveal.facebox', function() {
	/* hide adverts */
	hideAdvert();
});



$(document).bind('close.facebox', function() {
	/* hide adverts */
     $('#fb_invite').css('display', 'none'); 
});

$(document).bind('close.facebox', function() {
	/* show adverts */
	setTimeout(showAdvert, 500);
});

/* qTip stuff */
/* constants */
var QTIP_EXPLORE  		= 1<<0;
var QTIP_INVITE			= 1<<1;
var QTIP_MATCHCENTRE	= 1<<2;
var QTIP_SELECTTEAMS	= 1<<3;
var QTIP_ADDTEAM		= 1<<4;

/* functions */
function addQTip(_element, _content, _target, _tip, _type) {

	$(function() {
		/* set the onclick and content */
		var _clone = $('#qtip_base').clone();
		_clone.find('#qtip_close').click( function () { 
			$('#'+_element).qtip('destroy');
			updateUserQtip(_type);
		});
		_clone.find('#content').html(_content);
		_clone.css('display', 'block');

		
		var tip = $('#'+_element).qtip( {
			show : {
				ready :true
			},
			hide : {
				fixed: false
			},
			content : _clone.html(),
			position : {
				corner : {
					target :_target,
					tooltip :_tip
				}
			},
			style : {
				tip :_tip,
				border : {
					width :3,
					radius :8,
					color :'#3D414E'
				},
				background :'#3D414E',
				color :'#fff'
			}
		});
		/* access the qtip API and append the cloned element */
		var api = $("#"+_element).qtip("api");
		if (api){
			api.elements.content.html("");
			api.elements.content.append(_clone);
		}
	});
}

function updateUserQtip(_type){
	$.ajax({
		type: "POST",
		url: "/scripts.php?script=update_qtip",
		data: 	"type="	+ _type,
		success: function(html){
		// alert(html);
		}
	});
}
