/************************************************/
/* IPB3 Javascript								*/
/* -------------------------------------------- */
/* ipc.pagelayoutright.js -Page Layout code			*/
/************************************************/
var _idx = window.IPBoard;

_idx.prototype.pagelayout_right = {
	
	count_sbars: 0,
	count_hidden: 0,
	
	/*------------------------------*/
	/* Constructor 					*/
	init: function()
	{
		Debug.write("Initializing ipc.pagelayoutright.js");
		
		document.observe("dom:loaded", function(){
			
			ipb.pagelayout_right.initSidebarsRight();
			
			
			
						
		});
	},
	
	initSidebarsRight: function()
	{
		$('toggle_sidebar_right').on('click', ipb.pagelayout_right.toggleSidebarRight);
		
		if( $('ipc_right').visible() ){
			$('toggle_sidebar_right').update( $('toggle_sidebar_right').readAttribute("data-open") );
		} else {
			$('toggle_sidebar_right').update( $('toggle_sidebar_right').readAttribute("data-closed") );
		}
		
		ipb.pagelayout_right.setUpSideBarBlocksRight();
	},
	
	toggleSidebarRight: function(e){
		if( $('ipc_right').visible() )
		{
			ipb.Cookie.set('hide_sidebar_right', 1, 1);
			
			if( Prototype.Browser.WebKit ){
				// Chrome utterly fails to animate our ipsLayout styling. So we have to refresh the page.
				window.location.reload();
			}
			else
			{
				new Effect.Fade( $('ipc_right'), { duration: 0.4, afterFinish: function(){
					new Effect.Morph( $('ipc_mid'), { duration: 0.3, style:'no_sidebar_right', afterFinish: function(){
						$('ipc_mid').removeClassName('force_sidebar_right');
					} });
				} } );
			}
			
			$('toggle_sidebar_right').update("&laquo;");
		}
		else
		{
			ipb.Cookie.set('hide_sidebar_right', 0, 1);
			
			if( Prototype.Browser.WebKit ){
				window.location.reload();
			}
			else
			{
				new Effect.Morph( $('ipc_mid'), { duration: 0.3, style:'force_sidebar_right', afterFinish: function(){
					new Effect.Appear( $('ipc_right'), { duration: 0.4, afterFinish: function(){
						$('ipc_mid').removeClassName('no_sidebar_right');
					}});
				} } );
			}
			
			$('toggle_sidebar_right').update("&times;");
		}
		
		Event.stop(e);
	},
	
	/**
	 * Add in collapsable icons
	 */
	setUpSideBarBlocksRight: function()
	{
		if ( $('ipc_right').visible() )
		{
			$$('#ipc_right h3').each( function(h3)
			{
				$( h3 ).identify();
				
				ipb.pagelayout_right.count_sbars++;
				
				/* Set title - fugly 
				   Here we run through escape first to change foreign chars to %xx with xx being latin, and then we remove % after */
				title = escape( $( h3 ).innerHTML.replace( /<(\/)?([^>]+?)>/g, '' ) ).replace( /%/g, '' );
				title = title.replace( / /g, '' ).replace( /[^a-zA-Z0-9]/, '' ).toLowerCase();

				$( h3 ).up('div').addClassName( '__xX' + title );
				
				/* insert the image */
				$( h3 ).update( '<a href="#" class="ipsSidebar_trigger ipsType_smaller right desc mod_links">' + ipb.lang['hide'] + '</a>' + h3.innerHTML );
			});
			
			cookie = ipb.Cookie.get('toggleSBlocksRight');
		
			if( cookie )
			{
				var cookies = cookie.split( ',' );
				
				for( var abcdefg=0; abcdefg < cookies.length; abcdefg++ )
				{
					if( cookies[ abcdefg ] )
					{
						var top     = $('ipc_mid').down('.__xX' + cookies[ abcdefg ] );
						
						if ( top )
						{
							var wrapper	= top.down('._sbcollapsable');
							
							if ( ! wrapper )
							{
								wrapper = top.down('ul');
							}
							
							if ( ! wrapper )
							{
								wrapper = top.down('ol');
							}
							
							if ( ! wrapper )
							{
								wrapper = top.down('div');
							}
							
							if ( ! wrapper )
							{
								wrapper = top.down('table');
							}

							if ( wrapper )
							{
								if ( top.hasClassName('alt') )
								{
									top._isAlt = true;
									top.removeClassName('alt');
								}
								
								ipb.pagelayout_right.count_hidden++;
								top.down('.ipsSidebar_trigger').update( ipb.lang['unhide'] );
								wrapper.hide();
							}
						}
					}
				}
			}
		}
		
		ipb.delegate.register(".ipsSidebar_trigger", ipb.pagelayout_right.toggleSideBarBlockRight);
		
	},
	
	/**
	 * Toggle the block
	 */
	toggleSideBarBlockRight: function( e, elem )
	{
		Event.stop(e);
		elem.identify();
		
		var remove = $A();
		cookie = ipb.Cookie.get('toggleSBlocksRight');
		
		if( cookie == null )
		{
			cookie = $A();
		} 
		else 
		{
			cookie = cookie.split(',');
		}
		
		/* Test for known class name */
		var top   = elem.up('div');
		
		moo   = top.className.match('__xX([0-9A-Za-z]+)');
		topId = moo[1]; 
		
		block = top.down('._sbcollapsable');
		
		if ( ! $( block ) )
		{
			block = elem.up('div').down('ul');
		}
		
		if ( ! $( block ) )
		{
			block = elem.up('div').down('ol');
		}
		
		if ( ! $( block ) )
		{
			block = elem.up('div').down('div');
		}
		
		if ( ! $( block ) )
		{
			block = elem.up('div').down('table');
		}
		
		if ( $( block ) )
		{
			$( block ).identify();
			
			ipb.pagelayout_right.animating = true;
			
			if ( $( block ).visible() )
			{
				if ( $( top ).hasClassName('alt') )
				{
					$( top )._isAlt = true;
					$( top ).removeClassName('alt');
				}
				
				top.down('.ipsSidebar_trigger').update( ipb.lang['unhide'] );
				ipb.pagelayout_right.count_hidden--;
				Debug.write( "Adding " + topId );
				cookie.push( topId );
			}
			else
			{
				if ( $( top )._isAlt )
				{
					$( top ).addClassName('alt');
				}
				
				top.down('.ipsSidebar_trigger').update( ipb.lang['hide'] );
				
				ipb.pagelayout_right.count_hidden--;
				
				Debug.write( "Removing " + topId );
				remove.push( topId );
			}
		
			Effect.toggle( block, 'blind', {duration: 0.4, afterFinish: function(){ ipb.pagelayout_right.animating = false; } } );
		}
		
		cookie = "," + cookie.uniq().without( remove ).join(',') + ",";
		
		ipb.Cookie.set('toggleSBlocksRight', cookie, 1);
		
	}
	};

ipb.pagelayout_right.init();
