jQuery(function($) {
    
    if($("#top_gallery").length == 1) { initHomeGallery(); }

    $("#gallery_pop a.gallery").lightBox({fixedNavigation:true});

    $("#used_stock select[name='orderby']").change(function(){
        $(this).parent().submit();
    });

    if($("#googlemap").length == 1) {
        initGoogleMap();
    }

    /**** Product Page Accordion****/
    $( "#productrange .accordion" ).accordion({
        autoHeight: false,
        collapsible: true,
        active: false
    });

});

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

function initGoogleMap() {
    var point = new google.maps.LatLng(53.926232, -0.962943);
    var myMapOptions = {
        zoom: 11,
        center: point,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("googlemap"),myMapOptions);
    var marker = new google.maps.Marker({
        map: map,
        position: point
    });
}
