
/**
 * Handles the rendering the correct pages via deep linking for the frames
 */
function renderDeepLinkedPage()
{
    // Grab address bar location and split it to get the correct url
    var uriLocation = top.location.href;
    var hashLocator = uriLocation.split("#");
    
    // Use location.replace so that the redirect does not get written to the history
    if (hashLocator.length > 1)
    {
        document.location.replace(hashLocator[1]);
    }
    else
    {
        document.location.replace("index.php");
    }
}

/**
 * Handles the changes of the deep linking hash. A common hack for this
 * type of situation where browser doesn't recognize a pasted deep linked
 * URL for a new location to render.
 */
var lastHash = '';
var doRunAddressPoll = true;
function pollDeepLinkedHash()
{
    // If hash has changed
    if(lastHash !== top.location.hash)
    {
        // Save the current hash
        lastHash = top.location.hash;
        // Run the deep link renderer again if this isn't the first loop after a refresh
        if (doRunAddressPoll === true)
        {
            renderDeepLinkedPage();
        }
        else
        {
            // Only skip first loop
            doRunAddressPoll = true;
        }
    }
}

// HANDLER FOR WEBKIT DEEP LINKING
function pageload(hash)
{
    if(hash)
    {
        top.document.location.replace(top.document.location.protocol + "//" +
                                      top.document.location.host + "#" +
                                      hash);
    }
}

function intervalTest()
{
    alert('voff');
}

function checkForFrameset()
{
    var uriLocation = parent.window.location.href;
    var hashLocator = uriLocation.split("#");

    // Use location.replace so that the redirect does not get written to the history
    if (hashLocator.length === 1 && parent.window.location.pathname.length > 1)
    {
        top.document.location.replace(top.document.location.protocol + '//' +
                                      top.document.location.host + '#' +
                                      top.document.location.pathname);
    }
}

$(document).ready(function()
{

    // Verify that the frameset is loaded
    checkForFrameset();

    // SET UP THE DEEP LINKING FOR NON-WEBKIT BROWSERS
    if (/webkit/.test( navigator.userAgent.toLowerCase()) == false)
    {
        if (window.parent.didFramesetLoad === false)
        {

            // Put the deep linking URI in the address bar
            top.document.location.replace(top.document.location.protocol + "//" +
                                          top.document.location.host + "#" +
                                          document.location.pathname);

            // Store the last location hash so it won't go into a loop
            lastHash = top.location.hash;
        }
        else
        {
            // Don't poll the address bar for the first loop
            // Hack for the double loading of the page on refresh in non webkit browsers
            doRunAddressPoll = false;
        }

        // Check for changes in the address bar
        setInterval(pollDeepLinkedHash, 500);
        
        // Fix for the refresh defaulting to /go/ problem for non webkit browsers
        window.parent.didFramesetLoad = false;
    }

    /*
    // SAFARI / CHROME / WEBKIT
    else
    {
        // Init history callback for deep linking
        $.history.init(pageload);

        // For every a href
        $('a').click(function()
        {
            // Skip tab links, update, play, playlist, buy and btn buttons as well as the logout button
            if ($(this).attr('href').charAt(0) != "#" &&
                $(this).attr('class') != "btn" &&
                $(this).attr('class') != "buy" &&
                $(this).attr('class') != "play" &&
                $(this).attr('class') != "playlist" &&
                $(this).attr('class') != "update" &&
                $(this).attr('id') != "navigation-logout" &&
                $(this).attr('id') != "navigation-cart-list-btn")
            {
                $.history.load($(this).attr("href"));
            }
        });
    }
    */
});