$(function() {

    if($("#main_home_gallery").length == 1) { initHomeGallery(); }
    if($("article .location .map").length >= 1) { checkHash(); }

    $("input[type=text]").focus(function(){
        // Select field contents
        setTimeout(this.select(), 500);
    });

});


/**** HOME GALLERY ****/
var homeGalleryCount = 0;
var homeGalleryChangeTime = 6000;
var homeGalleryRunning = 0;
var homeGalleryTimer;
function initHomeGallery() {
    homeGalleryCount = $("#main_home_gallery .item").length;
    if(homeGalleryCount != 1) {
        $("#main_home_gallery").append("<div class='navigation'></div>");
        for(var i = 0; i < homeGalleryCount; i++) {
            $("#main_home_gallery .navigation").append("<div class='nitem'>&nbsp;</div>");
        }
        $("#main_home_gallery .navigation .nitem:eq(0)").addClass("selected");
        $("#main_home_gallery .navigation .nitem").click(function() {
            var index = $("#main_home_gallery .navigation .nitem").index(this);
            moveHomeGalleryTo(index);
            return false;
        });
        homeGalleryTimer = setInterval("homeGalleryTimerFunc()", homeGalleryChangeTime);
        $("#main_home_gallery").hover(
            function() { homeGalleryTimer = clearInterval(homeGalleryTimer); },
            function() { homeGalleryTimer = setInterval("homeGalleryTimerFunc()", homeGalleryChangeTime); }
        );
    }
}
function moveHomeGalleryTo(index) {
    if(1 == homeGalleryRunning) {
        return false;
    }
    homeGalleryRunning = 1;
    $("#main_home_gallery .item:not(#main_home_gallery .item:eq("+index+"))").fadeOut("1000", function() {
        $(this).removeClass("selected")
    });
    $("#main_home_gallery .item:eq("+index+")").fadeIn("1000", function() {
        $(this).addClass("selected");
        $("#main_home_gallery .navigation .nitem").removeClass("selected");
        $("#main_home_gallery .navigation .nitem:eq("+index+")").addClass("selected");
        homeGalleryRunning = 0;
    });
    return false;
}
function homeGalleryTimerFunc() {
    var index = $("#main_home_gallery .item.selected").index("#main_home_gallery .item");
    index++;
    if(index >= homeGalleryCount) {
        index = 0;
    }
    moveHomeGalleryTo(index);
}

/**** Contact Page Map Hash ****/
var hash = false;
function checkHash(){
    var newhash = window.location.hash.substring(1);
    if(!newhash) {
        newhash = "London";
    }
    if(newhash != hash) {
        hash = newhash;
        processHash(hash);
    } t = setTimeout("checkHash()",400);
}

function processHash(hash){
    var lowerhash = hash.toLowerCase();
    $('a[href^="#"]').removeClass("selected"); // remove selected class from all anchors
    $("article div.location").hide();

    $('a[href="#'+hash+'"]').addClass("selected"); // remove selected class from all anchors
    $('#location_'+lowerhash).show();

    var boxID = $('#location_map_'+lowerhash).attr("id");
    var coords = $('#location_'+lowerhash+' .ords').text().split(",");
    var point = new google.maps.LatLng(coords[0], coords[1]);
    var myMapOptions = {
        zoom: 15,
        center: point,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById(boxID),myMapOptions);
    var marker = new google.maps.Marker({
        map: map,
        position: point
    });
}
