/**
 * Links Directory by Brandon Davie
 * JS Library
 *
 * ID: $Id: links.js 20 2009-12-29 03:42:57Z brandond $
 * Last Updated: $Date: 2009-12-28 22:42:57 -0500 (Mon, 28 Dec 2009) $
 *
 * @author			$Author: brandond $
 * @link				http://brandondavie.com
 * @version			$Revision: 20 $
 */


var _links = window.IPBoard;

_links.prototype.links = {
	
	/**
	 * Constructor: init events
	 *
	 * @param	void
	 * @return	void
	 */
	
	init: function()
	{
		document.observe( 'dom:loaded', function()
		{
			if ( $( 'linksModForm' ) )
			{
				ipb.links.initMultiMod();
			}
			
			/* 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 );
			} );
			
			/* Delete reasons */
			if ( $( 'frmRejections' ) )
			{
				$$( '.deleteReason' ).each( function( elem )
				{
					$( elem.id ).observe( 'focus', function( e )
					{
						elem = Event.element( e );
						if ( $F( elem.id ).match( 'Tip: Click here to enter a reason for the rejection of' ) )
						{
							elem.value = '';
						}
					} );
				} );
			}
		} );
	},
	
	initMultiMod: function()
	{
		/* Already selected some links? */
		links = $F( 'selectedlids' ).split( ',' );

		if ( links )
		{
			links.each( function( check )
			{
				if ( check != '' )
				{
					if ( $( 'lmod_' + check ) )
					{
						$( 'lmod_' + check ).checked = true;
					}
				}
			} );
		}
		
		/* Check all box? */
		checkAll = true;
		$$( '.link_mod' ).each( function( check )
		{
			if ( check.checked == false )
			{
				checkAll = false;
			}
		} );
		
		if ( checkAll == true )
		{
			$( 'lmod_all' ).checked = true;
		}		
		
		$( 'lmod_all' ).observe( 'click', function( e )
		{
			check = Event.findElement( e, 'input' );
			toCheck = $F(check);
			toRemove = new Array();
			selectedLinks = $F( 'selectedlids' ).split( ',' ).compact();
			
			
			$$( '.link_mod' ).each( function( check )
			{
				if ( toCheck != null )
				{
					check.checked = true;
					selectedLinks.push( check.id.replace( 'lmod_', '' ) );
				}
				else
				{
					toRemove.push( check.id.replace( 'lmod_', '' ) );
					check.checked = false;
				}
			} );

			selectedLinks = selectedLinks.uniq();

			if ( toRemove.length >= 1 )
			{
				for ( i = 0; i < toRemove.length; i++ )
				{					
					selectedLinks = selectedLinks.without( parseInt( toRemove[ i ] ) );
				}
			}

			selectedLinks = selectedLinks.join( ',' );
			ipb.Cookie.set( 'lmodlinklids', selectedLinks, 0 );

			$( 'selectedlids' ).value = selectedLinks;		
		} );
		
		$$( '.link_mod' ).each( function( elem )
		{
			$( elem ).observe( 'click', ipb.links.checkLink );
		} );
	},
	
	checkLink: function( e )
	{
		remove = new Array();
		check = Event.findElement( e, 'input' );
		selectedLinks = $F( 'selectedlids' ).split( ',' ).compact();

		if ( check.checked == true )
		{
			selectedLinks.push( check.id.replace( 'lmod_', '' ) );
		}
		else
		{
			remove.push( check.id.replace( 'lmod_', '' ) );
		}

		selectedLinks = selectedLinks.uniq().without( remove ).join( ',' );		
		ipb.Cookie.set( 'lmodlinklids', selectedLinks, 0 );
		
		/* Check all box? */
		checkAll = true;
		$$( '.link_mod' ).each( function( check )
		{
			if ( check.checked == false )
			{
				checkAll = false;
			}
		} );
		
		if ( checkAll == true )
		{
			$( 'lmod_all' ).checked = true;
		}
		else
		{
			$( 'lmod_all' ).checked = false;
		}

		$( 'selectedlids' ).value = selectedLinks;
	},
	
	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();
