﻿/// <reference path="jquery-1.4.1-vsdoc.js" />

// JavaScript functions for ploughmains main page

var currentProduct = 0;
var totalProducts = 0;
var currentlyAnimatingProduct = -1;
var productCopySelector = ".ProductCopy .Item_{0}";
var productMenuSelector = ".ProductMenu .Item_{0} img";
var intervalId = 0;
    
// ----------- Product rotation code ----------- //

function displayProduct(productIndex, isShow) {
    var productElement = $(String.format(productCopySelector, productIndex)).stop();
    var menuElement = $(String.format(productMenuSelector, productIndex)).stop();
    if (isShow) {
        productElement.customShow();
        menuElement.customShow();
    }
    else {
        productElement.hide();
        menuElement.hide();
    }
}

function rotateProduct() {
    scrollProduct((currentProduct + 1) % totalProducts);
}

function startRotator() {
    stopRotator();
    intervalId = setInterval("rotateProduct()", 7000);
}

function stopRotator() {
    if (intervalId != 0)
        clearInterval(intervalId);
    intervalId = 0;
}

function fadeProduct(productIndex, isFadeIn, callback) {
    var productElement = $(String.format(productCopySelector, productIndex)).stop();
    var menuElement = $(String.format(productMenuSelector, productIndex)).stop();

    if (isFadeIn) {
        productElement.customFadeIn(200, callback);
        menuElement.customFadeIn(200);
    }
    else {
        productElement.customFadeOut(200, callback);
        menuElement.customFadeOut(200);
    }
}

// hide current product, fade in product at targetProduct
function scrollProduct(targetProduct) 
{
    // don't interfere with animation unless changed mind
    if (targetProduct == currentlyAnimatingProduct)
        return;
    if (targetProduct == currentProduct)
        return; // cannot go 'back' to a product while animating away from it


    if (currentlyAnimatingProduct >= 0) {
        // if already mid-animation, jump ahead to the end of the last animation to begin this
        displayProduct(currentProduct, false);
        displayProduct(currentlyAnimatingProduct, true);
        currentProduct = currentlyAnimatingProduct;
    }
    
    currentlyAnimatingProduct = targetProduct;
    var target = String.format(productCopySelector, targetProduct);
    var current = String.format(productCopySelector, currentProduct);

    // let's do this animation
    fadeProduct(currentProduct, false, function() {
        fadeProduct(targetProduct, true, function() {
            displayProduct(currentProduct, false);
            currentProduct = currentlyAnimatingProduct;
            currentlyAnimatingProduct = -1;
        });
    });

}

// ----------- Lightbox code ----------- //

function reinitialiseScrollPane() {
    $('.jScrollFrame').jScrollPane({ scrollbarWidth: 22 });
}

function showLightbox(url, size, title) {
    // hide frame, change style
    $(".jLightBox")
        .show()
        .removeClass("LightBox600 LightBox700 LightBoxHidden")
        .addClass("LightBox" + size + " LightBoxVisible")

    // change background images
    $(".FrameBackground").hide();
    $(".FrameBackground" + size).show();
    
    // clean up any rubbish HTML left behind from the last time this was called
    var cleanBox = "<div class='jScrollFrame Frame'></div>";
    $('.jScrollFrame').replaceWith(cleanBox);
    $(".jScrollPaneContainer").replaceWith(cleanBox);

    // setup page title
    $(".jLightBox .Header h1").html(title);
    RefreshCufon();
    
    // setup content of shown box
    reinitialiseScrollPane();
    $('.jScrollFrame').load(url, '', function() {
        reinitialiseScrollPane();
        // rebind lightbox triggers which could be in the new content area
        bindLightboxTriggers();
    });

    return false;
}


// ----------- Initialisation code ----------- //

// this function binds anything inside the lightbox content, and will be
// recalled after every ajax content load
function bindLightboxTriggers() {
    // setup the lightbox triggers
    // open lightbox
    $(".jLightBoxTarget").click(function() {
        stopRotator(); // pause rotation
        var url = $(this).attr("href") + "?display=frame";
        var size = $(this).hasClass("jLightBoxTarget700") ? 700 : 600;
        var title = $(this).attr("title");
        return showLightbox(url, size, title);
    });
    // setup news item triggers inside lightbox
    $(".jNewsOlder").click(function() {
        return navigateNews(-1);
    });
    $(".jNewsNewer").click(function() {
        return navigateNews(1);
    });
}

$(function() {
    // setup the product rotator, or do nothing if not given
    $(".jSelectedProduct:first").each(function() {
        currentProduct = parseInt($(this).val());
        totalProducts = $(".jProductDetails .Item").size();
        if (totalProducts == 0)
            return;
        // start automatic rotator IF there is no visible lightbox (ie, if the lightbox is hidden)
        $(".LightBoxHidden:first").each(function() {
            startRotator();
        });

        // setup function for left-right buttons
        $(".jLeftProduct").click(function() {
            startRotator();
            scrollProduct((currentProduct + totalProducts - 1) % totalProducts);
        });
        $(".jRightProduct").click(function() {
            startRotator();
            scrollProduct((currentProduct + 1) % totalProducts);
        });
    });

    // setup the right hand menu
    $(".jProductMenu li").each(function(index) {
        $("a", $(this)).click(function() {
            startRotator();
            scrollProduct(index);
            return false;
        });
    });

    // setup any pre-rendered lightbox content, if given
    $(".LightBoxVisible:first").each(function() {
        reinitialiseScrollPane();
    });

    bindLightboxTriggers();
    // close lightbox
    $(".jCloseButton").click(function() {
        $(".jLightBox").hide();
        startRotator(); // resume rotation
        return false;
    });

    // hack external links,
    // we cannot have target="_blank" in xhtml strict, so ensure all external links here open in a new window
    $('a[rel=external]').click(function() {
        window.open(this.href);
        return false;
    });
});


