﻿/// <reference path="jquery/jquery.intellisense.js" />

function scrollToElement(element) {
    $('html, body').animate({
        scrollTop: $(element).offset().top
    }, 500);
}

function setupImageGallery() {

    /* configures the image gallery to work with javascript rather than conventional postback/refresh*/

    if($('ul.thumbnails-collection').length){
        //simple thumbs gallery with no scrolling
        var thumbs = $('ul.thumbnails-collection li a.gallery-image')
    } else {
        var thumbs = $('#imagegallerynavigator ul.item-collection li a.gallery-image')
    }

    $(thumbs).click(function() {

        var thumbImageName = $(this).children('img').attr("src");
        var largeImageName = thumbImageName.replace('_thumb', '_large');

        //fade out the current image and in the callback pre-load and display the next one
        $('#imagegallerylargeimage').customFadeTo('fast', 0, function() {

            //have we preloaded the image already?
            if ($('#imagegallerylargeimagepreloader img[src=\'' + largeImageName + '\']').length) {
                //already preloaded
                $('#imagegallerylargeimage').html('<img src="' + largeImageName + '" alt="" />')
                $('#imagegallerylargeimage').customFadeTo('fast', 100);

            } else {
                //not preloaded
                var img = new Image();
                document.getElementById('imagegallerylargeimagepreloader').appendChild(img)

                img.onload = function() {
                    $('#imagegallerylargeimage').html('<img src="' + largeImageName + '" alt="" />')
                    $('#imagegallerylargeimage').customFadeTo('fast', 100);
                };

                img.src = largeImageName;
            }
        });

        return false;
    });

    $(thumbs).mouseover(function() { $(this).addClass('hover') });
    $(thumbs).mouseout(function() { $(this).removeClass('hover') });

}


function setupPageTabs(){

    $('#detailtabs').show();
    $('#detailtabs li:first').addClass('selected');

    //add a class to the tabcontent containers so we can apply a border
    $('.tabcontent').addClass('tabbed')

    $('div.product-detail-body h3.section-heading').addClass('screen-hidden');

    $('ul#detailtabs li a').click(function() {
        //work out the content container id from the tab id
        var tabId = $(this).attr('id');
        var containerId = tabId.substring(0, tabId.length - 3) + 'section';

        $('.tabcontent').addClass('screen-hidden');

        $('#detailtabs li').removeClass('selected');
        $(this).parent('li').addClass('selected');
        $('#' + containerId).removeClass('screen-hidden');
        $.get('/blank.html', 'ProductDetailTab=' + $(this).attr('href').substr(1, $(this).attr('href').length - 1));

        if (tabId == 'financetab') {
            //load the first tab content
            if ($('#financesectioncontent').find('.ajax-load-placeholder').length > 0) {
                $('#financesection ul:first li:first a').click();
            }
        }

        return false;
    });

    $('#detailtabs li:first a').click();

    if (window.location.hash.toLowerCase() == '#reviews') {
        $('#reviewstab').click();
    }
    if (window.location.hash.toLowerCase() == '#bundles') {
        $('#bundlestab').click();
    }
    if (window.location.hash.toLowerCase() == '#accessories') {
        $('#accessoriestab').click();
    }
    if (window.location.hash.toLowerCase() == '#usedstock') {
        $('#usedstocktab').click();
    }
    if (window.location.hash.toLowerCase() == '#features') {
        $('#featurestab').click();
    }
    if (window.location.hash.toLowerCase() == '#findsimilar') {
        $('#findsimilartab').click();
    }
    if (window.location.hash.toLowerCase() == '#specifications') {
        $('#specificationstab').click();
    }
    if (window.location.hash.toLowerCase() == '#resources') {
        $('#resourcestab').click();
    }
    if (window.location.hash.toLowerCase() == '#awards') {
        $('#awardstab').click();
    }    

    //hook up handlers for finance area
    $('#financesection ul:first li a').click(function() {

        $('#financesection ul:first li').removeClass('selected');
        $(this).parent('li').addClass('selected');

        var url = $(this).attr('href')
        var ajaxUrl = url + '&mode=ajax';

        $('#financesectioncontent').show();
        $('#financesectionerror').hide();

        $.ajax({
            url: ajaxUrl,
            success: function(data) { $('#financesectioncontent').html(data) },
            error: function(xhr, textStatus, errorThrown) { $('#financesectioncontent').hide(); $('#financesectionerror').show(); }
        });

        return false;
    });

    setupImageGallery()

}