/**
* Links System v3.1.1
*
* Original component ( < v3.1 ) by Brandon Farber
*
* Porting to IPB3 ( v3.1 ) by Brandon Davie
* brandon@brandondavie.com
*
* (c) Copyright Brandon Farber 2006-2009
*/

var _links = window.IPBoard;

_links.prototype.links = {
	
	/**
	* Constructor: init events
	*
	* @param	void
	* @return	void
	*/
	
	init: function()
	{
		document.observe( 'dom:loaded', function()
		{
			/* Mod form submission */
			if ( $( 'modform' ) )
			{
				$( 'modform' ).observe( 'submit', ipb.forums.submitModForm );
			}
			
			/* Link listing filters */
			if ( $( 'filter_form' ) )
			{
				$( 'filter_form' ).hide();
				$( 'show_filters' ).show();
			}
			
			/* Statistic Links */
			$$( '.link_slink' ).each( function( elem )
			{
				ipb.links.initStatPopups( elem );				
			} );
			
			/* Resizing images? */
			if ( $( 'theComments' ) )
			{
				ipb.global.findImgs( 'theComments' );
			}
			
			/* AJAX mark category as read */
			$$( '.linksMarkCat' ).each( function( elem )
			{
				ipb.links.initCatMarker( elem );
			} );
		} );
	},
	
	initStatPopups: function( elem )
	{
		var statType	= elem.id.replace( 'slink_', '' );
		var url				= ipb.vars[ 'base_url' ] + "&app=links&module=ajax&secure_key=" + ipb.vars[ 'secure_hash' ] + "&do=" + statType;
		
		$( elem ).observe( 'click', function( e )
		{
			Event.stop( e );			
			popup = new ipb.Popup( 'statsPane', { type: 'pane', modal: false, w: '500px', h: '400px', ajaxURL: url, hideAtStart: false, close: 'a[rel="close"]' } );
		} );
	},
	
	initCatMarker: function( elem )
	{
		var catId		= elem.id.replace( 'links_cat_img_', '' );
		var url			=	ipb.vars[ 'base_url' ] + "&app=links&module=ajax&section=markread&secure_key=" + ipb.vars[ 'secure_hash' ] + "&do=markread&marktype=cat&cid=" + catId;
		var classes	= $w( elem.className );
		
		$( elem ).observe( 'click', function( e )
		{
			new Ajax.Request( url, { method: 'post', onSuccess: function( t )
			{
				$( elem ).replace( ipb.global.boardMarkers[ classes[ classes.length - 1 ] ] );
			} } );
			
			Event.stop( e );
		} );
	}
}

ipb.links.init();
