// wanchaiferry.co.uk javascript
// by Jonathan Brain 
// http://www.jonathanbrain.com

var thisServer = "http://" + document.location.host;
var thisPage = getThisPage();

$(document).ready(function() {
    addPrintButton();
    //addEmailNote();
    addProductRotator();
});

function getThisPage() {
    var thisPath = window.location.pathname;
    return thisPath.substring(thisPath.lastIndexOf('/') + 1);
}


function addPrintButton() {
    $('.teachings-instructions').append('<a class="button-print" href="javascript: window.print();">Print this page</a>');
}


function addEmailNote() {
    var note = "Please enter correctly";

    //initialise
    if ($('.email-address input').val() == "" || $('.email-address input').val() == note) {
        $('.email-address input').addClass("email-note").val(note);
    }

    //focus event
    $('.email-address input').focus(function() {       
        if ($(this).val() == note) {
            $(this).removeClass("email-note").val("");
        }
    });

    //blur event
    $('.email-address input').blur(function() {
        if ($(this).val() == "") {
            $(this).addClass("email-note").val(note);
        }
    });
}

function addProductRotator() {
    //if (thisPage.toLowerCase() == "index.aspx") {
	if ( $('.section').hasClass('home-lower') ) {
        setTimeout(function() { showNextProduct(2); }, 5000);
    }
}

function showNextProduct(index) {
    //initiate ajax call
    $.ajax({
        url: "xml/rotator-home.xml",
        type: "GET",
        dataType: "xml",
        cache: false,
        success: function(xml) {
            var link, product_src, button_src;
            try {
                $(xml).find("item[id='" + index + "']").each(function() {
                    link = $(this).find('url').text();
                    product_src = $(this).find('product-src').text();
                    product_alt = $(this).find('product-alt').text();
                    button_src = $(this).find('button-src').text();
                    button_alt = $(this).find('button-alt').text();
                });

                //change images
                var fadeTime = 600;
                $('#button-product img').fadeOut(fadeTime, function() { $(this).attr({ src: product_src, alt: product_alt }).fadeIn(fadeTime); }).parent().attr({ href: link });
                $('#button-learn-more img').fadeOut(fadeTime, function() { $(this).attr({ src: button_src, alt: button_alt }).fadeIn(fadeTime); }).parent().attr({ href: link });


                index++;
                if (index > 3) {
                    index = 1;
                }
                setTimeout(function() { showNextProduct(index); }, 5000);

            }
            catch (e) {
            }
        },
        error: function(xml, textStatus, errorThrown) {
            //Use the following line to diagnose problems:
            //alert("Homepage rotator: " + textStatus + " | " + errorThrown);
        }
    });	
}    

