var topWindow = window.parent ;
var topDocument = topWindow.document ;
var s = navigator.userAgent.toLowerCase() ;

topDocument.name="first";
var sbBrowserInfo =
{
	IsIE		: /*@cc_on!@*/false,
	IsIE7		: /*@cc_on!@*/false && ( parseInt( s.match( /msie (\d+)/ )[1], 10 ) >= 7 ),
	IsIE6		: /*@cc_on!@*/false && ( parseInt( s.match( /msie (\d+)/ )[1], 10 ) >= 6 ),
	//IsSafari	: s.Contains(' applewebkit/'),		// Read "IsWebKit"
	IsOpera		: !!window.opera
	//IsAIR		: s.Contains(' adobeair/'),
	//IsMac		: s.Contains('macintosh')
} ;

function GetViewPaneSize( win )
{	
	
	return { Width : (isNaN(win.innerWidth))?1024:win.innerWidth, Height : (isNaN(win.innerHeight))?585:win.innerHeight } ;
}

function GetScrollPosition ( relativeWindow )
{
	return { X : (isNaN(relativeWindow.pageXOffset))?0:relativeWindow.pageXOffset, Y : (isNaN(relativeWindow.pageYOffset))?0:relativeWindow.pageYOffset } ;
}

function sbopenpopup(pg,width,height)
{	
		DisplayMainCover();
			var topDialog ;
			// Calculate the dialog position, centering it on the screen.
			var viewSize = GetViewPaneSize( topWindow ) ;
			var scrollPosition = { 'X' : 0, 'Y' : 0 } ;
			var useAbsolutePosition = sbBrowserInfo.IsIE && ( !sbBrowserInfo.IsIE7  ) ;
			if ( useAbsolutePosition )
				scrollPosition = GetScrollPosition( topWindow ) ;
			var iTop  = Math.max( scrollPosition.Y + ( viewSize.Height - height - 20 ) / 2, 0 ) ;
			var iLeft = Math.max( scrollPosition.X + ( viewSize.Width - width - 20 )  / 2, 0 ) ;

			// Setup the IFRAME that will hold the dialog.
			var dialog = topDocument.createElement( 'iframe' ) ;
			dialog.src = pg;
			
			//alert(useAbsolutePosition);
////////////////
			dialog.frameBorder = 0 ;
			dialog.allowTransparency = true ;
			SetElementStyles( dialog,
					{
						'position'	:  ( useAbsolutePosition ) ? 'absolute':'fixed',
						'top'		: iTop + 'px',
						'left'		: iLeft + 'px',
						'width'		: width + 'px',
						'height'	: height + 'px',
						'zIndex'	: 2
					} ) ;

			// Append the IFRAME to the target document.
			topDocument.body.appendChild( dialog ) ;

			// Keep record of the dialog's parent/child relationships.
			dialog._ParentDialog = topDialog ;
			topDialog = dialog ;
			return false;
		
}

	function DisplayMainCover()
		{
			
			// Setup the DIV that will be used to cover.
			cover = topDocument.createElement( 'div' ) ;
			//ResetStyles( cover ) ;
			cover.className="cover";
			cover.id="cover";

			SetOpacity( cover, .50 ) ;

			// For IE6-, we need to fill the cover with a transparent IFRAME,
			// to properly block <se lect> fields.
			if ( sbBrowserInfo.IsIE && !sbBrowserInfo.IsIE7 )
			{
				var iframe = topDocument.createElement( 'iframe' ) ;
				iframe.hideFocus = true ;
				iframe.frameBorder = 0 ;
				iframe.name="frame1";

				SetElementStyles( iframe,
					{
						'width' : '100%',
						'height' : '100%',
						'position' : 'absolute',
						'left' : '0px',
						'top' : '0px',
						'filter' : 'progid:DXImageTransform.Microsoft.Alpha(opacity=.50)'
					} ) ;
					
				cover.appendChild( iframe ) ;
			}

			// We need to manually adjust the cover size on resize.
			AddEventListener( topWindow, 'resize', resizeHandler ) ;
			resizeHandler() ;

			topDocument.body.appendChild( cover ) ;

		}
	function resizeHandler()
	{
		if ( !cover )
			return ;
		var relElement = topDocument.body ;
		SetElementStyles( cover,
			{
				'width' : Math.max( relElement.scrollWidth,
					relElement.clientWidth,
					topDocument.scrollWidth || 0 ) + 14 + 'px',
				'height' : Math.max( relElement.scrollHeight,
					relElement.clientHeight,
					topDocument.scrollHeight || 0 ) + 14 + 'px'
			} ) ;
	}
	function SetElementStyles( element, styleDict )
	{
		var style = element.style ;
		for ( var styleName in styleDict )
			style[ styleName ] = styleDict[ styleName ] ;
			
	}
	function SetOpacity( element, opacity )
	{
		if ( sbBrowserInfo.IsIE )
		{
			//opacity = Math.round( opacity * 100 ) ;
			//element.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=0.50)';
			element.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=.50)';
		}
		else
		element.style.opacity = opacity ;
	}
function AddEventListener( sourceObject, eventName, listener )
{
	eventName='on' + eventName;
	sourceObject.AttachEvent (eventName, listener );
}

function AttachEvent ( eventName, functionPointer )
{
	//this.eventName=functionPointer;
	this.onresize=functionPointer;
}

	function Cancel(  )
	{
		OnDialogClose( window ) ;
	} 

	function RemoveNode ( node )
	{
		return node.parentNode.removeChild( node ) ;
	}
	function OnDialogClose( dialogWindow )
	{
		var dialog = dialogWindow.frameElement ;
		cover=parent.topDocument.getElementById('cover');
		page=new String(parent.location);
		sbfound=page.match(/cart_items/)
		if(sbfound != null)
		parent.location.reload();
		RemoveNode(cover);
		RemoveNode( dialog);
	}
