﻿window.addEvent('domready', loadMenu);
var objMenu = null;

function loadMenu()
{
    buildMenu();
    //position the menu
    bodyWidth = parseInt($$('body')[0].getStyle('width').replace('px', ''));
    leftPos = 0;
    if (bodyWidth > 1000)
    {
        leftPos = (bodyWidth - 1000) / 2;
    }
    leftPos += jsLeftOffset;
    $('theMenu').setStyle('left', leftPos.toString() + 'px');
    topPos = 241;
    topPos += jsTopOffset;
    $('theMenu').setStyle('top', topPos.toString() + 'px');
    //add the event to the honeymoons menu item
    $('mnuHoneymoon').addEvent('click', function()
    {
        document.location = 'packageViewer.aspx?categoryID=16';
    });
    $('mnuCruises').addEvent('click', function()
    {
        document.location = 'packageViewer.aspx?categoryID=34';
    });
    $('addThisButton').setStyle('left', (leftPos + 830).toString() + 'px');
    $('addThisButton').setStyle('top', (topPos + 367).toString() + 'px');
}

var menuBuilt = false;

var regiondata = packageRegions;

var safariData = [
    { 'description': 'Australia and New Zealand', 'link': 'quickspecials_nzaus.aspx' },
    { 'description': 'Africa', 'link': 'quickspecials_africa.aspx' },
    { 'description': 'Cruising', 'link': 'quickspecials_cruising.aspx' },
    { 'description': 'Indian Ocean Islands', 'link': 'quickspecials_ioi.aspx' },
    { 'description': 'Mediterranean', 'link': 'quickspecials_mediterranean.aspx' },
    { 'description': 'Middle East', 'link': 'quickspecials_middleeast.aspx' },
    { 'description': 'North America', 'link': 'quickspecials_northamerica.aspx' },
    { 'description': 'South America', 'link': 'quickspecials_southamerica.aspx' },
    { 'description': 'Sporting Events', 'link': 'quickspecials_sportingevents.aspx' } ];

function hasCountries(region)
{
    for (var k = 0; k < destinationdata.length; k++)
    {
        if (destinationdata[k].region == region)
            return true;
    }
    return false;
}

function buildMenu()
{
    //build the destinations menus and submenus
    mnu = $('mnuDestinationsSub');
    for (var i = 1; i < regiondata.length; i++)
    {
        //only add it if it has 'child locations'
        if (hasCountries(regiondata[i]))
        {
            //create the region submenu
            mnuSub = new Element('SPAN');
            mnuSub.addClass('subMenuItemD');
            mnuSub.innerHTML = regiondata[i];
            mnuSub.addEvent('mouseover', function()
            {
                buildSubMenuRegion(this);
            });
            mnuSub.addEvent('mouseout', function()
            {
                setTimeout('subMenuDestroyer()', 250);
            });
            mnu.appendChild(mnuSub);
        }
    }
    //add the event to hide and show the sub-menu
    $('mnuDestinations').addEvent('mouseover', function()
    {
        for (var i = 0; i < $('mnuDestinationsSub').getChildren().length; i++)
            $('mnuDestinationsSub').getChildren()[i].setStyle('display', 'block');
    });
    $('mnuDestinations').addEvent('mouseout', function()
    {
        for (var i = 0; i < $('mnuDestinationsSub').getChildren().length; i++)
            $('mnuDestinationsSub').getChildren()[i].setStyle('display', 'none');
        setTimeout('subMenuDestroyer()', 250);
    });
    //build the safaris menu
    mnu = $('mnuSafarisSub');
    for (var i = 0; i < safariData.length; i++)
    {
        //create the region submenu
        mnuSub = new Element('SPAN');
        mnuSub.addClass('subMenuItemS');
        mnuSub.innerHTML = safariData[i].description;
        mnuSub.link = safariData[i].link;
        mnuSub.addEvent('click', function()
        {
            document.location = this.link;
        });
        mnu.appendChild(mnuSub);
    }
    //add the event to hide and show the sub-menu
    $('mnuSafaris').addEvent('mouseover', function()
    {
        for (var i = 0; i < $('mnuSafarisSub').getChildren().length; i++)
            $('mnuSafarisSub').getChildren()[i].setStyle('display', 'block');
    });
    $('mnuSafaris').addEvent('mouseout', function()
    {
        for (var i = 0; i < $('mnuSafarisSub').getChildren().length; i++)
            $('mnuSafarisSub').getChildren()[i].setStyle('display', 'none');
    });
    menuBuilt = true;
}

var theMnu = null;

function subMenuDestroyer()
{
    if ($('mnuDestinationsSub').getChildren()[0].getStyle('display') == 'none')
    {
        if ($('regionDestinations') != null)
            $('regionDestinations').dispose();
    }
    //setTimeout('subMenuDestroyer()', 250);
}

function buildSubMenuRegion(thisObj)
{
    if ($('regionDestinations') != null)
        $('regionDestinations').dispose();
    mnuSubG = new Element('SPAN');
    mnuSubG.addClass('subSubMenuItemGroupD');
    mnuSubG.id = 'regionDestinations';
    mnuSubG.setStyle('left', '1px');
    ppp = 0;
    currentRegion = destinationdata[0].region;
    for (var i = 0; i < destinationdata.length; i++)
    {
        if (currentRegion != destinationdata[i].region)
            ppp++;
        if (thisObj.innerHTML == currentRegion)
            break;
        currentRegion = destinationdata[i].region;
    }
    //alert(currentRegion + ':' + ppp);
    ppp--;
    ppp--;
    if (currentRegion == destinationdata[destinationdata.length - 1].region)
        ppp++;
    mnuSubG.setStyle('top', (ppp * 21 + 65).toString() + 'px');
    mnuSubG.setStyle('left', '230px');
    //add subitems
    for (var j = 0; j < destinationdata.length; j++)
    {
        if (destinationdata[j].region == thisObj.innerHTML)
        {
            found = true;
            mnuSubSub = new Element('SPAN');
            mnuSubSub.innerHTML = destinationdata[j].country;
            mnuSubSub.addClass('subSubMenuItemD');
            mnuSubSub.link = destinationdata[j].link;
            //mnuSubSub.id = 'region_' + obj.newsdata[j].id;
            mnuSubSub.addEvent('click', function()
            {
                document.location = this.link;
                $('regionDestinations').dispose();
                for (var k = 0; k < $('mnuDestinationsSub').getChildren().length; k++)
                    $('mnuDestinationsSub').getChildren()[k].setStyle('display', 'none');
            });
            mnuSubG.appendChild(mnuSubSub);
        }
    }
    //function to keep the regions submenu visible when looking @ the articles
    mnuSubG.addEvent('mouseover', function()
    {
        for (var i = 0; i < $('mnuDestinationsSub').getChildren().length; i++)
            $('mnuDestinationsSub').getChildren()[i].setStyle('display', 'block');
    }
    );
    mnuSubG.addEvent('mouseout', function()
    {
        for (var i = 0; i < $('mnuDestinationsSub').getChildren().length; i++)
            $('mnuDestinationsSub').getChildren()[i].setStyle('display', 'none');
        setTimeout('subMenuDestroyer()', 250);
    }
    );

    $('theMenu').appendChild(mnuSubG);
}
