/**
* Start / Help Window
*/
function hideHelp() {
    $("#start-overlay").css("visibility", "hidden");
}

function showHelp() {
    $("#start-overlay").css("visibility", "visible");
}

/**
* Price Widget
*/
function hideWidget() {

    // Reset the frame source to prevent
    // rendering error on close    
    widget = document.getElementById("priceWidget");
    widgetSrc = widget.src;
    widget.src = "";

    $("#price-widget").css("visibility", "hidden");

    widget.src = widgetSrc;

}

function showWidget() {

    $("#price-widget").css("visibility", "visible");

}

/**
* Customize Your Rental Form
*/
function showStep1() {
    
    // Form    
    $("#rental-details").css("display", "block");
    $("#car-choices").css("display", "none");
    $("#rental-summary").css("display", "none");

    // Update the guide frame only if we are 
    // navigating from a different step
    guide = document.getElementById("guide-frame");
    
    if (guide.src.substring(guide.src.length - 16) != "guide-step1.html")
        updateGuide("guide-step1.html");
    
}

function showStep2() {

    // Form
    updateSummary();    
    $("#rental-details").css("display", "none");
    $("#car-choices").css("display", "block");
    $("#rental-summary").css("display", "none");

    // Update the guide frame only if we are 
    // navigating from a different step
    guide = document.getElementById("guide-frame");

    if (guide.src.substring(guide.src.length - 16) != "guide-step2.html")
        updateGuide("guide-step2.html");    
    
}

function showStep3() {

    // Form
    updateSummary();    
    $("#rental-details").css("display", "none");
    $("#car-choices").css("display", "none");
    $("#rental-summary").css("display", "block");

    // Update the guide frame only if we are 
    // navigating from a different step
    guide = document.getElementById("guide-frame");

    if (guide.src.substring(guide.src.length - 16) != "guide-step3.html")
        updateGuide("guide-step3.html");    
    
}

/**
* Listener for Code Examples
*/
window.attachEvent('onmessage', showCodeEvent);
function showCodeEvent(e) {
    if (e.origin == 'http://ie8demosite.cloudapp.net') {
        
        var code = document.getElementById("code_" + e.data);
        code.style.visibility = "visible";

        var hash = document.location.hash;

        if (hash && hash.length > 0) {
            
            // Get the hash parameter after the # symbol
            var params = hash.substring(1, hash.length);

            // Split the hash to see if a code example needs to be
            // displayed
            params = params.split("&");

            location.hash = "#" + params[0] + "&" + e.data;
            return false;
        }
    }
}

function showCode(code) {
    var div = document.getElementById("code_" + code);
    
    if (div != null)
        div.style.visibility = "visible";
}

/**
* Copy to clipboard
*/
function copyToClipboard(obj) {
    var code;

    code = $(obj).next().text();

    if (window.clipboardData && clipboardData.setData) {
        clipboardData.setData("text", code.replace("&lt;", "<").replace("&gt;", ">"));
    }
   
    return false;
}

/**
* Write iFrame into document
* Neccessary to avoid IE caching the src change, adding an entry
* to back/foward browser navigation
*/
function updateGuide(src) {
    // Not using jQuery b/c permission errors occurred randomly
    iFrame = document.getElementById("guide-content");
    iFrame.innerHTML = '<iframe src="' + src + '" id="guide-frame" class="frame" frameborder="0" marginheight="0" marginwidth="0"><p>Your browser does not support iframes.</p></iframe>';
}

function resetHashToStep() {
    
    // Update hash to remove code box
    // Get the hash parameter after the # symbol
    var hash = document.location.hash;
    var params = hash.substring(1, hash.length);

    // Split the hash to extract only the step
    params = params.split("&");

    document.location.hash = params[0];

    return false;
    
}
