//Modal window methods
var win = null;
var params = 
{
	overlay:80,
	overlayCss: {backgroundColor:"#000"},
	containerCss: {'background-color':'#fff', 'padding':'5px', 'border':'2px solid black'},
	minHeight:50,
	onShow:function (dialog) {
		$("body").css({zoom: ''});
		$("body").css({zoom: '1'});
		$("#inventorytop").css({zoom: ''});
		$("#inventorytop").css({zoom: '1'});
		$("#inventorybottom").css({zoom: ''});
		$("#inventorybottom").css({zoom: '1'});
		}
}

//Open popup window.  If Navigator, set event handlers
// THIS IS THE OLD WINDOWING FUNCTION.
// NOTE NOT HAVING THIS AS A URL IS CAUSING SOME MAC BROWSEERS
// TO NOT BE ABLE TO OPEN THE WINDOW.  THE NEW WINDOWING
// FUNCTION DOES NOT TAKE THE URL AS A PARAMETER, THEREFORE
// WILL OPEN THE WINDOW
function showFixedWindow(url,winHeight,winWidth,showScrollbars)
{
    var w = 480;
    var h = 340;
    var topPos;
    var leftPos;
    var scrollbars;
    if (document.all || document.layers) 
    {
      w = screen.availWidth;
      h = screen.availHeight;
    }
    // handle calls not done via the taglib
    if (showScrollbars ==null)
    {
    	scrollbars=0;
    }
    else
    {
    	scrollbars=showScrollbars;
    }

    leftPos = (w-winWidth)/2;
    topPos = (h-winHeight)/2;

    if (win != null)
    {
    	
    	try
    	{
    		win.close();
    	}
    	catch (err)
    	{}
    }
    try
    {
    	win = window.open(url, "popup", "toolbar=0,scrollbars="+scrollbars+",location=0,statusbar=0,menubar=0,resizable=1,width="+winWidth+",height="+winHeight+",top="+topPos+",left="+leftPos);
    	win.focus();
    }
    catch (err)
    {
    	window.open(url, "popup", "toolbar=0,scrollbars="+scrollbars+",location=0,statusbar=0,menubar=0,resizable=1,width="+winWidth+",height="+winHeight+",top="+topPos+",left="+leftPos);
    }
}


// function called on the onUnload event of the window to remove any open child window
function closeChild()
{
    if (win && win.open && (win.closed==false))
    {
        win.close();
    }
}

//Open popup window.  If Navigator, set event handlers.
// This is currently used by the JSP Taglibs.  This windowing function
// should be the one used by all new code.
/*
    This needs to be embedded within a Hyperlink and should be used in conjunction
    with either the Hyperlink or PopupHyperlink JSP taglibs.
	Passing no value for showScrollbars will not render the bars
	Passing -1 for width or height will render that attribute full screen
*/	
function showFixedWindow2(winWidth,winHeight,showScrollbars)
{
    var w = 480;
    var h = 340;
    var topPos;
    var leftPos;
    var scrollbars;

    if (document.all || document.layers) 
    {
      w = screen.availWidth;
      h = screen.availHeight;
    }
    
    // See if the user specified a value, or the attribute should
    // be full screen.
    if (winWidth == -1)
    {
    	winWidth = w;
    }
    if (winHeight == -1)
    {
    	winHeight = h;
    }
    
    // handle calls not done via the taglib
    if (showScrollbars ==null)
    {
    	scrollbars=0;
    }
    else
    {
    	scrollbars=showScrollbars;
    }

    leftPos = (w-winWidth)/2;
    topPos = (h-winHeight)/2;

    if (win != null)
    {
    	win.close();
    }
    win = window.open('', "popup", "toolbar=0,scrollbars="+scrollbars+",location=0,statusbar=0,menubar=0,resizable=1,width="+winWidth+",height="+winHeight+",top="+topPos+",left="+leftPos);
 
    //setParentHandlers();
   
    win.focus();
}

function openModal(page, queryParameters, div, modalParameters)
{
	if (queryParameters != null)
	{
		page = page +'?'+ queryParameters;
	}
	
	// merge defaults and user options
	var windowParams = $.extend({}, params, modalParameters);
	$("#dialog").load(page, {}, function()
    		{
    			$("#dialog").modal(windowParams);
    		}			
    	);
}
function handleAjaxResponse(data)
{
	// Generic AJAX response code here
	
	// Clear previous errors by removing any div with the class of errortext
	$('div.errortext').remove();

	if (data.indexOf("/* {") == 0) 
	{
        var rlength = data.split('*/',1)[0].length;
        var json = eval("( " + data.substring(2, rlength) + " )");

        if (json.controlErrors)
        {
	        for (controlName in json.controlErrors)
	        {
		        var errorText = "<div class='errortext'>"+json.controlErrors[controlName]+"</div>";
		        $('#'+controlName).parent().prepend(errorText);

	        }
	        // resize the container if it got pushed off
	        $.modal.impl.resizeContainer();

        }
        else if (json.location)
        {
        	location.href=json.location;
            if ($.browser.safari)
	        {
                document.open();
                document.write('<html><head><meta http-equiv="REFRESH" content="0;url='+json.location+'"></head></html>');
                document.close();
            }
        }
        else if (json.modal)
        {
	        // display a modal window

        }
        
/*
        } else if (!errorCount && json.div){

            //$('#'+json.div).html(response.substring(rlength+2));

            //_ajax_unblockUI();

        } else {

            _ajax_unblockUI();

            return json;

        }
*/
	}
	else
	{
		// HTML content was returned, so replace the content
		$.modal.close();
		$("#dialog").html(data);
		$("#dialog").modal(params);

	}        
}

// Common inventory logic here to improve rendering times
function showCoin(htmlKey)
{
   	if ( ($(window).height() <= 718) || ($(window).width() <= 1024))
   	{
   		var URL = "/popupcontainer.jsp?include=/popup/viewimage.jsp&itemkey="+htmlKey;
   		showFixedWindow(URL,550,625,1);
   	}
   	else
   	{
   	$("#dialog").load("/popup/viewimage.jsp?itemkey="+htmlKey, {}, function()
   		{
   			$("#dialog").modal({
   				overlay:80,
   				overlayCss: {backgroundColor:"#000"},
   				containerCss: {'background-color':'#fff', 'padding':'5px', 'border':'2px solid black'},
   				onShow:function (dialog) {
   					$("body").css({zoom: ''});
       				$("body").css({zoom: '1'});
       				$("#inventorytop").css({zoom: ''});
       				$("#inventorytop").css({zoom: '1'});
       				$("#inventorybottom").css({zoom: ''});
       				$("#inventorybottom").css({zoom: '1'});
       				}
   			});
   		}			
   	);
   	}
}
function negotiate(htmlKey)
{
	openModal('/popup/makeAnOffer.jsp','itemkey='+htmlKey);
	//openModal('/servlet/InitialOffer_DisplayServlet','itemkey='+htmlKey);
}
function showImageRequest(htmlKey) 
{
   	openModal('/popup/requestImage.jsp','itemkey='+htmlKey);
}


