﻿$(document).ready(function() {

    var $openPlan = $("#openPlan"),                                 // Hide/Show Hole Plan link
        $prev = $("#controls a.prev"),                              // Show next hole
        $next = $("#controls a.next"),                              // Show previous hole
        $holePlan = $("#holePlan"),                                 // Hole plan 'wrapper' element - use this to animate inner image
        showSpeed = 1000,                                           // Speed to reveal Hole Plan
        hideSpeed = 1000,                                           // Speed to remove Hole Plan
        baseUrl = "/Content/Uploads/Images/CourseGuide/",           // Base content url
        holes = [                                                   // Define the three active images in an array
            { img: $("#bgImage img"), src: "imgHoleXX.jpg" },       // Background image
            { img: $("#holePlan img"), src: "planHoleXX.png" },     // Hole plan image
            { img: $("#description img"), src: "textHoleXX.png" },  // Description/text image
        ],
        noItems = 18,                                               // Total no items
        index = 1;                                                  // Current index e.g. 3 of 18



    /*
    **  Methods
    */
    function showPlan() {
        $holePlan.show(showSpeed);
        $openPlan.text("hide hole plan >")
    }
    function hidePlan() {
        $holePlan.hide(hideSpeed);
        $openPlan.text("< view hole plan")
    }

    function loadContent() {
        // Load the content for each active image
        for (var i = 0; i < holes.length; i++) {
            holes[i].img.attr("src", baseUrl + holes[i].src.replace(/XX/i, index));
            //alert(index);
            if (index == 19 | index == 20)
            {
					$openPlan.hide();
            } 
            else
            {
					$openPlan.show();
            }
        }
    }


    /*
    ** Event Management
    */
    $openPlan.click(function(evt) {
        evt.preventDefault();
        if ($holePlan.is(":visible")) {
            hidePlan();
        } else {
            showPlan();
        }
    });

    $next.click(function() {
        index++;
        if (index > noItems) { index = 1; }
        loadContent();
    });

    $prev.click(function() {
        index--;
        if (index < 1) { index = noItems; }
        loadContent();
    });


    /*
    ** Initialise
    */
    hidePlan();


    /*
    ** Clean up on exit
    */
    $(window).unload(function() {
        $openPlan.unbind();
        $next.unbind();
        $prev.unbind();
    });

}); 