﻿//************************************************************************************* 
// File     :   arbu_functions.js
// Version  :   1.0
// Requires :   mf_domLibrary_0.1.js, prototype.js (1.6 or higher)
// Author   :   Kyle Weems (ksw)
// Origin   :   mindfly.com
// Created  :   July 22, 2008
// Modified :   August 14, 2009
// Purpose  :   A library of functions specific to ARB USA.
//*************************************************************************************

jQuery(document).ready(function() {

    dealerListMaps();
});

function dealerListMaps() {
    var list = jQuery('#dealerList .vcard');
    e = 5;
    if (list.length < e) { e = list.length; }
    for (i = 0; i < e; i++) {
        BrainGnat.Google.map.loadFromHCard('#dealerList .vcard .vcardWrapper:eq(' + i + ')', '#dealerList .googleMap:eq(' + i + ')', 13, false, i);
    }
    
    
    /*for (i = 0; i < list.length; i++) {
        if (i < 5) {
        } else {
            jQuery('#dealerList .googleMap:eq(' + i + ')').css('display', 'none');
        }
    }*/
}







// Old Prototype Stuff Below Here //////////////////

// GLOBAL VARIABLES //
var imageDir = '/Controls/ARBU/Rotator/imageList.aspx';     // the file that will list the images.
var imageList = new Array();                                // the array that will hold the image names.
var maxImage = 0;                                           // the variable that will hold the number of images found.
var currentImage = 0;                                       // the variable that will hold the current image number.
var loopTime = 6000;                                        // the delay between each loop, in milliseconds.

// Called when the page loads. Checks if the body tag has a class name applied to it. If it dos, then
// it finds any list items that match the class name, and determines whether any items in the submenu need
// to be displayed.
function openCurrentSubMenu() {
    // Get the body element.
    var body = $$('body')[0];
    // If the body element has a class name...
    if (body.className) {
        // ... then see if any of the list items have a title that matches the class name.
        var matches = $$('li[title="' + body.className + '"]');
        // If there's a match...
        if (matches.length > 0) {
            // ... then get the match (there should be only one).
            var menuItem = matches[0];
            // Get a list of the match's ancestors.
            var itemAncestors = menuItem.ancestors();
            // If the match's great-great grandfather is a list item...
            if (itemAncestors[3].match('li')) {
                // Then it's the root menu item, and should be marked as the current menu.
                itemAncestors[3].addClassName('currentMenu');
            }
            // If that's not the case, but the match's grandfather is a list item...
            else if (itemAncestors[1].match('li')) {
                // Then that is the root menu, and should be marked as the current menu.
                itemAncestors[1].addClassName('currentMenu');
            }
            // If neither is the case...
            else {
                // then the match is the root menu and should be marked as the current menu.
                menuItem.addClassName('currentMenu');
            } // end if
        }
    }
} // end of function openCurrentSubMenu()


// Called when the page loads. Using AJAX, it gets a list of images and stuffs them in an array, then starts
// the loop to rotate the banner's current image.
function loadImageList() {
    rotateImage();
} // end of function imageRotate()

function rotateImage() {
    BrainGnat.slideshow.ajaxLoadCrossfade('/Controls/ARBU/imageList/imageList.aspx?dir=/uploads/imageRotator/', '#branding', 'img', 5000, 1000);
}

// Called after the page loads. Checkfs if there's an element tagged '.olAccessories', and if so adds an
// observer to all its immediate li children to run openAccessoryList() when the li is clicked.
function observeAccessoryList() {
    if ($$('.olAccessories').length) {
        var AccessorySubList = $$('.olAccessories > li');
        var i = 0;
        for (i = 0; i < AccessorySubList.length; i++) {
            AccessorySubList[i].observe('click', function(event) {
                openAccessoryList(Event.element(event));
            });
        }
    }
} // end of function observeAccessoryList()

// Called by event observers set in observeAccessoryList().
function openAccessoryList(element) {
    if (element.tagName == 'LI' && element.ancestors()[0].hasClassName('olAccessories')) {
        hideOpenList();
        element.addClassName('displayChild');
    } else if (element.tagName == 'A' && element.ancestors()[1].hasClassName('olAccessories')) {
        hideOpenList();
        element.ancestors()[0].addClassName('displayChild');
    }
} // end of function openAccessoryList(element)


function hideOpenList() {
    var listDisplayChild = $$('.olAccessories li.displayChild');
    var i = 0;
    for (i = 0; i < listDisplayChild.length; i++) {
        listDisplayChild[i].removeClassName('displayChild');
    }
} // end of function hideOpenList()



// Events to be called after the page loads.
addLoadEvent(loadImageList);
addLoadEvent(openCurrentSubMenu);
addLoadEvent(observeAccessoryList);