﻿/// <reference path="jquery-1.3.2.min.js"/> 

var popWindow = "#popupmap";
var mapimage = "#mapimage";
var photo = "#photo";
var maplink = "#maplink";

$(document).ready(function() {
    var initialIndexMain = 0;
    var initialIndexChild = 0;
    var direction, daypart;
    var url = window.location.search;
    if (url.length > 0) {
        var params = url.slice(1).split("&");
        if (params != null) {
            var elements = params[0].split("=");
            direction = elements[0];
            daypart = elements[1];
            if (direction != null && direction.length > 0) {
                switch (direction) {
                    case "medway":
                        initialIndexMain = 0;
                        break;
                    case "vigo":
                        initialIndexMain = 1;
                        break;
                    case "newashgreen":
                        initialIndexMain = 2;
                        break;
                    default:
                        initialIndexMain = 0;
                        break;
                }
            }
            if (daypart != null && daypart.length > 0) {
                switch (daypart) {
                    case "pm":
                        initialIndexChild = 1;
                        break;
                    case "am":
                    default:
                        initialIndexChild = 0;
                        break;
                }
            }
        }
    }


    // activate main tabs

    $("ul.tabs").tabs("div.tabscontent > div", { event: 'click', onBeforeClick: function() { closePopup(); }, initialIndex: initialIndexMain, history: true });
    // add classes for tabs
    $("ul.tabs > li").each(function(i) { $(this).addClass("tab" + i) });
    // activate child tabs
    $("div.tabschildcontent").each(function(i)
    { $("#childtabs" + i + " > ul.tabschild").tabs("#childtabs" + i + " > div", { event: 'click', onBeforeClick: function() { closePopup(); }, initialIndex: initialIndexChild, history: true }); });
    // add classes to colorfy columns
    $("table.timetable tr").each(function() { $(this).children("td:even").not($(this).children("td:lt(2)")).addClass("alt"); });
    $("table.timetable tr").each(function() { $(this).children("th:even").not($(this).children("th:lt(2)")).addClass("alt"); });

    // activate popup
    $(".col_busstop span").click(function() {
        // copy text from the selected cell span to popup
        $(popWindow + ' .maptitle span').text($(this).text());

        // load busstopimage
        var imgGuid = $(this).attr('imageguid');
        if (imgGuid != '00000000-0000-0000-0000-000000000000') {
            $(photo).attr('src', 'GetPicture.aspx?Guid=' + imgGuid + '&height=120');
        }
        else {
            $(photo).attr('src', '/img/TimeTable/busstop_noimage.jpg');
        }
        $(photo).attr('alt', $(this).text());
        $(photo).attr('title', $(this).text());
        // load image with a google map        
        var lat = $(this).attr('latitude');
        var lg = $(this).attr('longitude');
        $(mapimage).attr("src", 'http://maps.google.com/staticmap?center=' + lat + ',' + lg + ',0&zoom=14&size=165x125&maptype=roadmap&markers=' + lat + ',' + lg + '&key=ABQIAAAAK8SWJ77_3K49MXIoaW-lXxTG4IjVI8dnJaRh15CNaCisbtyYzxRgXtrXjdrFhpL6t4Qo6nv3sxAtmQ');

        $(maplink).attr("href", 'http://maps.google.com/maps?q=' + lat + ',' + lg + '&hl=en');

        // calculate position of popup
        var element = $(this);
        var elementPosition = element.position();
        var newTop, newLeft;
        newTop = elementPosition.top - 4;
        newLeft = elementPosition.left - 2;
        // make popup
        $(popWindow).bgiframe();
        $(popWindow).css({
            "left": newLeft,
            "top": newTop,
            "position": "absolute",
            "z-index": "10"
        });

        $(popWindow).fadeTo("fast", "show");
        // hides iframe in IE6
        $(popWindow + ' iframe').hide();
    });

    $(".busstopclose").click(function() { closePopup(); });
    $(".maptitle").click(function() { closePopup(); });

});

// closes popup with effect
function closePopup() {
    $(popWindow).fadeTo("fast", "hide");
    //$(photo).attr('src', '/img/TimeTable/busstop_noimage.jpg');
}