YAHOO.namespace( "tapeshop.common" );

/***** AJAX calls *****/
YAHOO.tapeshop.common.AJAXNavigator = {
	load: function( form, action, placeHolder, customCallback, evalScript, isUpload )
	{
		// remove messages/errors div
		//YAHOO.tapeshop.common.Util.clearMessages();

		/* set default for evalScript to TRUE */
		if ( evalScript == null || evalScript == "" )
		{
			evalScript = true;
		}
	
		/* set default for isUpload to FALSE */
		if ( isUpload == null || isUpload == "" )
		{
			isUpload = false;
		}
	
		method = 'GET';
		if ( form != null )
		{
			YAHOO.util.Connect.setForm( form, isUpload ); 
			method = 'POST';
		}
		
		var callback = {
			success: function( o )
			{
				var placeHolder = o.argument[0];
				var customCallback = o.argument[1];
				var evalScript = o.argument[2];
				
				var container = document.getElementById( placeHolder );
				container.innerHTML = o.responseText;
				window.scrollTo( 0, 0 );
		
				if ( evalScript )
				{
					var scripts = container.getElementsByTagName( 'script' );
					for ( var i = 0; i < scripts.length; i++ )
					{
						eval( scripts[i].text );
					}
				}
		
				if ( customCallback != null )
				{
					eval( customCallback );
				}
			}, 
			
			failure: function( o )
			{
				/*failure handler code*/
				/* do some cleaning */
				//alert( o.statusText );
			}, 
			
			upload: function( o )
			{
				/*upload handler code*/
				this.success( o );
			}, 
			
			argument: [placeHolder, customCallback, evalScript] 
		};
		
		// POST transaction. 
		YAHOO.util.Connect.asyncRequest( method, location.protocol + '//' + location.host + '/ajax/' + action, callback );
	},
	
	navigate: function( state, url )
	{
		YAHOO.util.History.navigate( 'content', state, url );
	},
	
	loadScript: function( id, scriptSource, customCallback )
	{
		var head = document.getElementsByTagName( 'head' )[0];
		
		if ( document.getElementById( id ))
		{
			// if id ends with _refresh will be loaded every time
			if ( id.indexOf( '_refresh' ) == -1 )
			{
				return;
			}
			
			head.removeChild( document.getElementById( id ));
		}

		var script 	= document.createElement( 'script' );
		script.id 	= id;
		script.type	= 'text/javascript';
		script.src 	= scriptSource;
		
		head.appendChild( script );
	
		if ( customCallback != null )
		{
			script.customCallback = customCallback;
			script.onreadystatechange = function ()
			{
		        if ( script.readyState == 'loaded' )
		        {
		            eval( this.customCallback );
		        }
		    }
	
		    script.onload = function ()
		    {
		        eval( this.customCallback );
		    }		
		}
	},
	
	loadStyle: function( id, styleSource )
	{
		var head = document.getElementsByTagName( 'head' )[0];

		if ( document.getElementById( id ))
		{
			// if id ends with _refresh will be loaded every time
			if ( id.indexOf( '_refresh' ) == -1 )
			{
				return;
			}
	
			head.removeChild( document.getElementById( id ));
		}
		
		var style 	= document.createElement( 'link' );
		style.id 	= id;
		style.rel 	= 'stylesheet';
		style.type	= 'text/css';
		style.href 	= styleSource;
		
		head.appendChild( style );
	}
	
};
/***** end AJAX calls *****/

/***** Util *****/
YAHOO.tapeshop.common.Util = {
	clearMessages: function()
	{
		var messagesDiv = document.getElementById("messagesDiv");
		if ( messagesDiv && messagesDiv != 'undefined' )
		{
			var parentNode = messagesDiv.parentNode;
			if ( parentNode && parentNode != 'undefined' )
			{
				parentNode.removeChild( messagesDiv );
			}
		}
	}
};
/***** end Util *****/

/***** PANEL *****/
YAHOO.tapeshop.common.Panel = {
	
	panel: null,
	
	init: function( container, hideCloseButton, modal )
	{
		if ( !hideCloseButton )
		{
			hideCloseButton = false;
		}
	
		this.panel = new YAHOO.widget.Panel( container, {	width:					"auto",
															height:					"auto",
															fixedcenter: 			true,  
															constraintoviewport: 	true,  
															underlay:				"shadow",  
															close:					!hideCloseButton,  
															visible:				false,  
															draggable:				false,
															modal:					modal
												   		});
		this.panel.render();
	},
	
	show: function( title )
	{
		if ( title )
		{
			this.panel.setHeader( title );
		}
		
		this.panel.cfg.setProperty( "visible", true );
		this.refresh();
	},
	
	close: function()
	{
		this.panel.cfg.setProperty( "visible", false );
		this.panel.body.innerHTML = '';
	},
	
	refresh: function()
	{
		this.panel.center();
	}
}
/***** end PANEL *****/
