/**
 * 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 = {
	
	fetchMore: {},
	
	/**
	 * Constructor: init events
	 *
	 * @param	void
	 * @return	void
	 */
	
	init: function()
	{
		document.observe( 'dom:loaded', function()
		{
			if ( $( 'linksModForm' ) )
			{
				ipb.links.initMultiMod();
			}
			
			if ( $( 'links_load_more' ) )
			{
				$( 'links_load_more' ).observe( 'click', ipb.links.loadMore );
				$( 'more_links' ).show();
			}
			
			$$( '.boinkMe' ).each( function( elem )
			{
				$( elem ).stopObserving( 'click' ).observe( 'click', ipb.links.boinkMe );
			} );
			
			/* 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 = '';
						}
					} );
				} );
			}
			
			/* Member autocomplete */
			if ( $( 'newUser' ) )
			{
				var theUrl = ipb.vars['base_url'] + 'app=core&module=ajax&section=findnames&do=get-member-names&secure_key=' + ipb.vars['secure_hash'] + '&name=';
				var autoComplete = new ipb.Autocomplete( $('newUser' ), { multibox: false, url: theUrl, templates: { wrap: ipb.templates['autocomplete_wrap'], item: ipb.templates['autocomplete_item'] } } );
			}
		} );
	},
	
	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 );
		} );
	},
	
	loadMore: function( e )
	{
		Event.stop( e );		
		if( ! ipb.links.fetchMore ) { return; }
		
		if ( ipb.links.fetchMore['st'] > 0 )
		{
			ipb.links.fetchMore['st'] += ipb.links.fetchMore['max'];
		}
		else
		{
			ipb.links.fetchMore['st'] = parseInt( ipb.links.fetchMore['max'] );
		}
		
		
		var url = ipb.vars[ 'base_url' ] + "app=links&module=ajax&section=links&do=getLinks&md5check=" + ipb.vars[ 'secure_hash' ] + "&" + $H( ipb.links.fetchMore ).toQueryString();
		
		new Ajax.Request( url.replace( /&amp;/g, '&' ),
		{
			method: 'get',
			evalJSON: 'force',
			onSuccess: function( t )
			{
				if ( t.responseJSON[ 'links' ] == '' )
				{
					$( 'links_load_more' ).replace( "<span class='desc lighter'>" + ipb.lang[ 'no_more_links' ] + "</span>" );
					return;
				}
				
				$$( 'tbody.dynamic_update' ).invoke( "removeClassName", "dynamic_update" );
				var newtbody = new Element( "tbody", { 'class': 'dynamic_update' } );
				$( 'category_table' ).select( 'tbody:last' )[ 0 ].insert( { 'after': newtbody } );
				newtbody.update( t.responseJSON[ 'links' ] );
				$$('.pagination').invoke( "replace", t.responseJSON[ 'pages' ] );
			
				if ( t.responseJSON[ 'links' ] == '' || t.responseJSON[ 'hasMore' ] == false )
				{
					$( 'links_load_more' ).replace( "<span class='desc lighter'>" + ipb.lang[ 'no_more_links' ] + "</span>" );
					return;
				}								
			}
		});
	},
	
	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 );
		} );
	},
	
	boinkMe: function( e )
	{
		var linkElem = Event.element( e );
		var linkId = $( linkElem ).id.replace( 'link_', '' );
		var linkUrl = $( linkElem ).readAttribute( 'href' );
		var url =	ipb.vars[ 'base_url' ] + "&app=links&module=ajax&section=links&secure_key=" + ipb.vars[ 'secure_hash' ] + "&do=boinkMe&lid=" + linkId;
		
		new Ajax.Request( url, { method: 'post', onSuccess: function()
		{
			window.location.href = linkUrl;
		} } );
		
		Event.stop( e );
	}
}

ipb.links.init();
