window.topichover = Class.create({
	popupObj: $A(),
	appTopic: 0,
	vars: [],
	lang: [],
	templates: [],
	editors: $A(),
	initDone: false,
	
	initialize: function()
	{
		Debug.write("topichover.js is loading...");
		
		document.observe("dom:loaded", function(){
			
			// Show a little loading graphic
			Ajax.Responders.register({
			  onLoading: function() {
			    if( !$('ajax_loading') )
				{
					if( !ipb.templates['ajax_loading'] ){ return; }
					$('ipboard_body').insert( ipb.templates['ajax_loading'] );
				}
				
				var effect = new Effect.Appear( $('ajax_loading'), { duration: 0.2 } );
			  },
			  onComplete: function() {
			
				if( !$('ajax_loading') ){ return; }
			    var effect = new Effect.Fade( $('ajax_loading'), { duration: 0.2 } );
			  }
			});
			
		}.bind(this));
	},

});

ipb.th = new topichover;
var _topichover = window.topichover;
_topichover.prototype.hoverAjax = {
// Constructor
	init: function()
	{
		Debug.write("Initializing Topic Hover Preview AJAX");
		document.observe("dom:loaded",function(){
			//if( !$('ajax_loading') ){ return; }
			ids = $$('[class^="__topic__hover__preview"]');
			ids.each(function(item){
				leg = item.id.match('__hover__topic([0-9]+)__([0-9]+)')[1];
				count = item.id.match('__hover__topic([0-9]+)__([0-9]+)')[2];
				//alert(leg);
				name = '__hover__topic' + leg + '__' + count;
				$(name).observe( 'mouseover', ipb.th.hoverAjax.HoverPopup.bindAsEventListener(this,leg) );
				if(ipb.vars['th_mouse_close'] == 1) {
					$(name).observe( 'mouseout', ipb.th.hoverAjax.cancelForm.bindAsEventListener(this,leg) );
				}
			});
		});
	},
	// Handles all of our Popups
	HoverPopup: function(e)
	{
		Event.stop(e);
		var elem = e.element();
		var sourceid = elem.identify();
		// So we've told the popup class that we need some more info from it
		var args = $A(arguments);
		// Mainly the type arg...
		var topic = args[1];
		
		// Hide any popups that got left there
		ids = $$('.popup_hover_popup');
		//alert(ids);
		ids.each(function(item){
				// Remove any popups that have no content
				leg = item.id.match('popup_hover_([0-9]+)_popup_popup')[1];
				if(document.getElementById( 'popup_hover_' + leg + '_popup_inner' ).innerHTML == '') {
					item.remove();
					ipb.th.popupObj[topic] = undefined;
				} else {
					item.hide();
				}
			});
		delete ids;
		
		// To save on queries if the popup exists show it
		//if( $('popup_hover_'+topic+'_popup') )
		if(ipb.th.popupObj[topic] != undefined)
		{
			ipb.th.appTopic = topic;
			ipb.th.popupObj[ipb.th.appTopic].show();
		}
		// Otherwise we can't cheat :(
		else
		{
			ipb.th.appTopic = topic;
			if( ipb.th.appTopic == null || Object.isUndefined( ipb.th.appTopic ) ){ Debug.error("Error showing popup"); return; }
			var popid = 'popup_hover_' + ipb.th.appTopic + '_popup';
			var _url 		= ipb.vars['base_url'] + 'app=topichover&module=ajax&secure_key=' + ipb.vars['secure_hash'] + '&section=view&do=popup&topic=' + ipb.th.appTopic;
			// Make the popup so we can do funky stuff like hide it later
			if(ipb.vars['th_pane_popup'] == 1) {
				popup = new ipb.Popup( popid, {
											type: 'pane',
											modal: false,
											stem: true,
											//ajaxURL: _url,
											hideAtStart: true,
											classname: 'popup_hover_popup',
											attach: { target: elem, position: 'auto' },
											w: '600px',
											h: 'auto',
											//delay: {show: 300, hide: 0},
											close: 'a[rel="close"]',
											});
				ipb.th.popupObj[ipb.th.appTopic] = popup;
				
				// Lets get the form
				new Ajax.Request( _url,
								{
									method: 'post',
									evalJSON: 'force',
									onSuccess: function(t)
									{
										if( t.responseJSON['error'] )
										{
											if( t.responseJSON['error'] != '' )
											{
												alert( t.responseJSON['error'] );
											} else {	
												alert( ipb.lang['no_permission'] );
											}
											return;
										}
										else
										{
											ipb.th.popupObj[ipb.th.appTopic].update( t.responseJSON['success'] );
											//ipb.th.popupObj[ipb.th.appTopic].positionPane();
											ipb.th.popupObj[ipb.th.appTopic].show();
										}
									}
								}
							);
			} else {
				popup = new ipb.Popup( popid, {
											type: 'balloon',
											modal: false,
											stem: true,
											ajaxURL: _url,
											hideAtStart: false,
											classname: 'popup_hover_popup',
											attach: { target: elem, position: 'auto' },
											w: '600px',
											h: 'auto',
											delay: {show: 300, hide: 0},
											close: 'a[rel="close"]',
											});
				ipb.th.popupObj[ipb.th.appTopic] = popup;
			}
						
		}
		Event.stop(e);
	},
	cancelForm: function(e,type)
	{
		ipb.th.popupObj[ipb.th.appTopic].hide();
		
		Event.stop(e);
	}
}

ipb.th.hoverAjax.init();
