var buttonHtml = '';

$(document).ready(
    function()
    {
        /**
         * activate news ticker
         */
        $("#topNav ul").newsTicker();
        
        /**
         * Replace chosen headings with falsh equivalents
         */
        if(typeof sIFR == "function") {
            sIFR.replaceElement("h1", named({sFlashSrc: "/templates/flash/punkkid.swf", sBgColor: "#000000", sColor: "#f5f5f5", sCase: "upper", sWmode: "opaque"}));
            sIFR.replaceElement("h2", named({sFlashSrc: "/templates/flash/punkkid.swf", sBgColor: "#000000", sColor: "#f5f5f5", sCase: "upper", sWmode: "opaque"}));
            sIFR.replaceElement("h3", named({sFlashSrc: "/templates/flash/punkkid.swf", sColor: "#333333", sCase: "upper", sWmode: "opaque"}));
            sIFR.replaceElement(".navLink", named({sFlashSrc: "/templates/flash/punkkid.swf", sBgColor: "#f5f5f5", sColor: "#333333", sCase: "upper", sWmode: "opaque"}));
        }
        
        /**
         *
         */
        if( $('#orderItem') )
        {
            //disable the button until after a successful stock check
            buttonHtml = $('#buttonArea').html();
            $('#buttonArea').html('');
        
            $('#quantity').change(
                function()
                {
                    checkStockLevels();
                }
            );
            
            $('#size').change(
                function()
                {
                    checkStockLevels();
                }
            );
            
            $('#colour').change(
                function()
                {
                    checkStockLevels();
                }
            );
        }
    }
);

/**
 * Performs an ajax request to check stock levels
 */
function checkStockLevels()
{
    var colour      = $('#colour option:selected').val();
    var productId   = $('#p').val();
    var quantity    = $('#quantity option:selected').val();
    var size        = $('#size option:selected').val();
       
    if(quantity > 0 && size > 0 && colour > 0)
    {
        $('#buttonArea').html( '<img src="/templates/images/loading.gif" width="16" height="16" alt="Processing stock check" />' );
        
        //here we need to do an ajax request to
        //confirm we have sufficient stock
        $.ajax(
            {
                type:       "POST",
                url:        "/ajax/currentStockLevel.php",
                data:       "p=" + productId + "&col=" + colour + "&qty=" + quantity + "&size=" + size,
                success:    processResponse
            }
        );
    }
}

/**
 * Processes the ajax response
 */
function processResponse(msg)
{
    var quantity   = parseInt( $('#quantity option:selected').val() );
    var stockLevel = parseInt( msg );
        
    if( stockLevel == 0)
    {
	    // build out of stock notice
	    var html = "<p>This item is out of stock. Please enter your email address to " +
	    		   "be alerted when this product becomes available:</p>" +
	    		   "<p><input type='text' name='alertEmail' id='alertEmail' value='' /></p>" +
	    		   "<p><input type='submit' name='submit' id='submit' value=' Send ' /></p>";
	    		   
		// change form location
		$('#orderItem').attr('action', '/products/outofstock.php');
		
        // change button area html
        $('#buttonArea').html( html );
    }
    else if( stockLevel < quantity )
    {
	    // change form location
		$('#orderItem').attr('action', '/products/process.php');
		
        //item in stock but insufficient quantity to fulfil this order
        $('#buttonArea').html( '<p class="cancel"><strong>Only ' + stockLevel + ' available</strong></p>' );
    }
    else
    {
	    // change form location
		$('#orderItem').attr('action', '/products/process.php');
		
        //item in stock
        $('#buttonArea').html( buttonHtml + '<p class="tick"><strong>In stock</strong></p>' );
    }
}
/*
function processResponse(msg)
{
    //'YES' response
    if( msg.indexOf('YES') != -1 )
    {
        alert( msg );
    }
    else
    {
        alert
    }
}
*/
