﻿// JScript File
/*
var aryImage = new Array('front_header_background_1.jpg', 'front_header_background_2.jpg', 'front_header_background_3.jpg', 'front_header_background_4.jpg');
var iImageCount = 0;

function funcSetFrontBackground()
{
    // Preload the images
    var imgPreload_1 = new Image();
    imgPreload_1.src = 'images/layout/front_header_background_1.jpg'

    var imgPreload_2 = new Image();
    imgPreload_2.src = 'images/layout/front_header_background_2.jpg'

    var imgPreload_3 = new Image();
    imgPreload_3.src = 'images/layout/front_header_background_3.jpg'

    var imgPreload_4 = new Image();
    imgPreload_4.src = 'images/layout/front_header_background_4.jpg'

    // Set the initial background
//    window.document.getElementById('divFrontPageHeader').style.background = 'url(images/layout/' + aryImage[iImageCount] + ') top right no-repeat';
    iImageCount++;

    // Set the background rotator going, and rotate images every 10 seconds
    setInterval('funcFrontBackground();', 10000);
}

function funcFrontBackground()
{
    if (iImageCount == 4) iImageCount = 0;

    window.document.getElementById('divFrontPageHeader').style.background = 'url(images/layout/' + aryImage[iImageCount] + ') top right no-repeat';
    iImageCount++;
}
*/


// Load prototype & script.aculo.us
google.load("prototype", "1.6");
google.load("scriptaculous", "1.8.1");

var imageIndex = 1;

loadSlide = function() {

    if ($('imgHeader_Bottom') != null) {

        new Effect.Fade('imgHeader_Bottom', { 
            duration : 3.0, 
            fps : 100,
            afterFinish: function() {
                showNewSlide();
            }
        });
    }

}

showNewSlide = function() {

    imageIndex++;

    var imgHeader_Bottom = $('imgHeader_Bottom');

    if (imgHeader_Bottom != null) {

        new Ajax.Request('checkheaderimageexists.ashx?imagename=header_' + imageIndex + '.jpg', {
            method: 'get',
            onSuccess: function(transport) {

                var response = transport.responseText.evalJSON();
                
                if (response.status != null) {
                    if (response.status == 'complete') {
                        // Image found, set the src of the image element
                        imgHeader_Bottom.writeAttribute('src', 'images/layout/header/' + response.imagename);    
                    }
                    else {
                        // Image found, set the src of the image element
                        imageIndex = 1;
                        imgHeader_Bottom.writeAttribute('src', 'images/layout/header/header_1.jpg');    
                    }
                }
            },
            onFailure: function(transport) {

                var response = transport.responseText.evalJSON();

                // Image not found, so reset the src of the image element back to the first image
                imageIndex = 1;
                imgHeader_Bottom.writeAttribute('src', 'images/layout/header/header_1.jpg');
            }
        });
    }

    new Effect.Appear('imgHeader_Bottom', {
        duration : 3.0, 
        fps : 100 
    });
}

frontHelpInit = function() {

    var frontHelpColl = $$('.helpBox');

    frontHelpColl.each(function(frontHelpItem) {
        if (!Prototype.Browser.IE) {
            Event.observe(frontHelpItem, 'mouseover', frontHelp_Hover.bindAsEventListener(this, frontHelpItem), false);
            Event.observe(frontHelpItem, 'mouseout', frontHelp_Leave.bindAsEventListener(this, frontHelpItem), false);
        }
        else {
            Event.observe(frontHelpItem, 'mouseenter', frontHelp_Hover.bindAsEventListener(this, frontHelpItem), false);
            Event.observe(frontHelpItem, 'mouseleave', frontHelp_Leave.bindAsEventListener(this, frontHelpItem), false);
        }
    });
}

frontHelp_Hover = function(e, item) {

    frontHelp_Leave(e);

    // Create a new popup
    var helpContainer = new Element('div', { 'id' : 'divFrontHelp_Details' });
    var helpArrow = new Element('div', { 'id' : 'divFrontHelp_Details_Arrow' });
    var helpInnerContainer = new Element('div', { 'id' : 'divFrontHelp_Details_Inner' });
    var helpTitle = new Element('h2');
    var helpDetail = new Element('p');
    var extraTop = 0;
    
    var formFieldPrefix = '';

    switch (item.readAttribute('id')) {

        case formFieldPrefix + 'imgLocationHelp':
            helpTitle.update('Location (up to 3)');
            helpDetail.update('You can do multiple county selects by first of all selecting one of the counties.<br/><br/>Then hold down the control CRTL button (bottom left on keyboard) and scroll through the list (whilst the CTRL button is depressed) and selecting other counties.');
            break;

        case formFieldPrefix + 'imgKeywordHelp':
            helpTitle.update('Keyword search');
            helpDetail.update('If you are looking for walking, rambling etc, enter only the keywords, separated by commas, e.g. walking, rambling<br/><br/>We recommend that you use a maximum of two keywords for each search.<br/><br/>Vegetarians are advised to use the keyword \'veg\' as members use different terms to describe themselves, and this will identify all alternatives');
            break;

        default:
            return;

    }

    helpArrow.setStyle({ 'left' : '' + ($(item.readAttribute('class').replace('helpBox ', '')).positionedOffset().left + ($(item.readAttribute('class').replace('helpBox ', '')).getWidth() / 2) - 20)  + 'px', 'top': '' + ($(item.readAttribute('class').replace('helpBox ', '')).positionedOffset().top + $(item.readAttribute('class').replace('helpBox ', '')).getHeight()) + 'px'});
    helpContainer.setStyle({ 'left' : '' + (($(item.readAttribute('class').replace('helpBox ', '')).positionedOffset().left - ($(item.readAttribute('class').replace('helpBox ', '')).getWidth() / 2)) + 20) + 'px', 'top': '' + (($(item.readAttribute('class').replace('helpBox ', '')).positionedOffset().top + $(item.readAttribute('class').replace('helpBox ', '')).getHeight()) + 19) + 'px'});

    helpInnerContainer.insert(helpTitle);
    helpInnerContainer.insert(helpDetail);
    helpContainer.insert(helpInnerContainer);
    $('divSearchPanel').insert(helpArrow);
    $('divSearchPanel').insert(helpContainer);

    var boxPos = ((helpInnerContainer.getHeight() - 40) / 2)
        
    $$('.divAffiliation img').each(function(item) {
        $(item).hide();
    });
}

frontHelp_Leave = function (e) {

    // Remove any existing popups
    if ($('divFrontHelp_Details') != null)
        $('divFrontHelp_Details').remove();

    if ($('divFrontHelp_Details_Arrow') != null)
        $('divFrontHelp_Details_Arrow').remove();

    $$('.divAffiliation img').each(function(item) {
        $(item).show();
    });

}

google.setOnLoadCallback(function() {
    new PeriodicalExecuter(function(){ loadSlide(); }, 10);
});

google.setOnLoadCallback(frontHelpInit);