/**
 * gigs.js
 *
 * JavaScript for gigs page
 *
 * JavaScript
 *
 *  @category JavaScript
 *  @package  Noke.me.uk
 *  @author   Adam Phillips <adam@pfunc.co.uk>
 *  @license  http://www.pfunc.co.uk pfunc
 *  @link     http://www.pfunc.co.uk
 */

var scrollIncrement = 90;
var scrollOffset = 125;

var currentGig = 1;

var map;
var directions;

var localSearch;

/*
var gigs = [

    {
        "title": "Clarendon Jam",
        "description": "Every thrusday we host the clarendon jam",
        "date": "14th November 2009",
        "postcode": "GL54 4NJ",
        "picture": "klm.png"
    },

    {
        "title": "Clarendon Jam",
        "description": "Yep every thursday",
        "date": "21th November 2009",
        "postcode": "CV31 1NE",
        "picture": "boggle.png"
    },

    {
        "title": "Clarendon Jam",
        "description": "I said EVERY THURSDAY",
        "date": "28th November 2009",
        "postcode": "CV31 1LX",
        "picture": "grass.png"

    }

];

var numGigs = gigs.length;
*/

function initGigs()
{
    /*
    */
    //alert(gigs);

    /**
     * deals with wierd ie issue
     */

    numGigs = gigs.length;

    if ($("div#giglist ul").children().length == 0)
    {
        for (i=0; i<numGigs; i++)
        {
            $("div#giglist ul").append("\
                <li>\
                        <h3><a onclick=\"loadGig(" + (i + 1) + ");\">" + gigs[i].title + "</a></h3>\
                        <p>" + gigs[i].date + "</p>\
                </li>\
                ");
        }
    }

    currentGig = 1;

    $("p#prevgig a").click(function() {

            if (currentGig > 1)
            {
                loadGig(currentGig - 1);
            }

    });

    $("p#nextgig a").click(function() {

            if (currentGig < numGigs)
            {
                loadGig(currentGig + 1);
           }

    });

    $("p#openmap a").click(function() {

        showMap();

    })

    $("p#opendirections a").click(function() {

        showDirections();
        showMap();

    })

    $("span#closemap a").click(function() {

        hideMap();

    })

    $("span#toggledirections a").click(function() {

        if ($("#directions_container").css("display") == "block")
        {
            hideDirections();

            $(this).html("directions");
        }
        else
        {
            showDirections();

            $(this).html("hide directions");
        }

    })

    if (GBrowserIsCompatible())
    {
        map = new GMap2(document.getElementById("map_contents"));
        map.setUIToDefault();

        //hideDirections();
        //$("#map_outercontainer").hide();
        //hideMap()
    }

    loadGig(currentGig);

    //hideDirections();
    $("#map_outercontainer").hide();
    //hideMap();
}

function loadGig(index)
{
    $("#giglist ul").animate(
    {
        "marginTop": (scrollOffset - (scrollIncrement * index)) + "px"
    });


    $("div#gigdescription h4").hide();
    $("div#gigdescription p").hide();

    $("div#gigdescription h4").html(gigs[index - 1].title);
    $("div#gigdescription p").html(gigs[index - 1].description);

    $("div#gigdescription p#loadingMessage").show();

    $("div#gigdescription img").attr("src", gigs[index - 1].picture);

    $.get(gigs[index - 1].picture, function(data)
    {
        $("div#gigdescription img").fadeIn();
        $("div#gigdescription h4").fadeIn();
        $("div#gigdescription p").fadeIn();
        $("div#gigdescription p#loadingMessage").hide();

    });



    currentGig = index;

    setMap();
    loadDirections($("#fromlocation").val());

    if (currentGig == 1)
    {
        $("#prevgig  a").hide();
    }
    else
    {
        $("#prevgig  a").show();
    }

    if (currentGig == gigs.length)
    {
        $("#nextgig  a").hide();
    }
    else
    {
        $("#nextgig  a").show();
    }
}

function loadDirections(fromlocation)
{
    if (fromlocation)
    {
        var cnt = document.getElementById("directions_contents");

        if (directions == undefined) directions = new GDirections(map, cnt);

        $("#directions_contents").html("<p><img src=\"/images/gr-loading.png\" alt=\"Loading...\" /></p>");

        directions.clear();

        GEvent.addListener(directions, "load", function() {
            
            $("#directions_contents").html("");

            //alert("Loaded");
            
        });

        directions.load(fromlocation + ", UK to " + gigs[currentGig -1].postcode);
    }
}

function setMap() {

  var postcode = gigs[currentGig -1].postcode;

  var title = gigs[currentGig -1].title;
  var picturepath = picturesFolder + gigs[currentGig -1].picture;

  if (localSearch == undefined) localSearch = new GlocalSearch();

  if (GBrowserIsCompatible())
  {
     map = new GMap2(document.getElementById("map_contents"));
     map.setUIToDefault();
  }

  localSearch.setSearchCompleteCallback(null,

    function() {

      if (localSearch.results[0])
      {
        $("#map_contents").css("width", "400px");
        $("#map_contents").css("height", "400px");

        var resultLat = localSearch.results[0].lat;
        var resultLng = localSearch.results[0].lng;
        var point = new GLatLng(resultLat,resultLng);

        map.setCenter(point, 10);

        map.openInfoWindowHtml(point, "<div id=\"infowindow_container\"><h3>" + title + "</h3><img src=\"" + picturepath + "\" /></div>");
      }
      else
      {
        alert("Postcode not found!");
      }
    });

  localSearch.execute(postcode + ", UK");
}

function showMap()
{
 $("#directions_container").hide();

 $("#map_outercontainer").fadeIn(function() {
   setMap();
 });

    //showDirections();

}

function showDirections()
{
    $("#map_outercontainer").animate({

        "marginLeft": "-150px",
        "width": "800px"

    }, 500, null, function() {

       $("#directions_container").show();

    });
}

function hideMap()
{
    hideDirections();
    
    $("#map_outercontainer").fadeOut();
}

function hideDirections()
{
   $("#directions_container").hide();

    $("#map_outercontainer").animate({

        "marginLeft": "0px",
        "width": "500px"

    }, 500);

    
}


