/*
    Filename:   common.js
    Author:     Steve Smith stephen.smith@akqa.com
    Description:    Common functions for Ferrari f149 site
                    Has dependancies on MooTools library
                    Please ensure any updates maintain document's validity
                    Test validity at http://jslint.com/lint.html
*/

//Using mootools string.extend to
//provide method of extracting querystring
//key value by key name
//NB: Uses mootools for string.extend
String.extend({
    parseQuery: function() {
        var query = this.split(/[&;]/);
        var keyValue = {};
        if (query.length) {
            query.each(function(val) {
                var keys = val.split('=');
                if (keys.length && keys.length == 2) {
                    keyValue[encodeURIComponent(keys[0])] = encodeURIComponent(keys[1]);
                }
            });
        }
        return keyValue;
    }
});

//Generic popup function
//called from event listener attached
//in findPopUpAnchors()
function launchPopUp(url, width, height, title) {
    var params = "scrollbars=1,location=0,toolbar=0,menubar=0,titlebar=0,resizable=0,status=0,height=" + height + ",width=" + width;
    var newWindow = window.open(url, "",params);
    if(newWindow) {
        newWindow.focus();
        newWindow = null;
    }        
}

//Gets collection of anchors by class
//and attaches pop up event
//NB: Uses mootools $$ for getElementsByClass
function findPopUpAnchors() {
    var popUpAnchors = $$("a.popUp");
    var dimensions = [];
    for(var i = 0; popUpAnchors.length > i; i++) {
        popUpAnchors[i].onclick = function() {
            dimensions = this.rel.split(",");
            launchPopUp(this.href, dimensions[0], dimensions[1], dimensions[2]);
            return false;
        };
    }
}


//Gets collection of anchors by class
//and attaches new window
//NB: Uses mootools $$ for getElementsByClass
function findNewWindowAnchors() {
    var popUpAnchors = $$("a.newWindow");
    var dimensions = [];
    for(var i = 0; popUpAnchors.length > i; i++) {
        popUpAnchors[i].addEvent("click", function(){ 
            window.open(this.href);
            return false;
        });
    }
}

function hideSubItemsInItemContainer(itemContainer) {
    var subItems;
    if(itemContainer.className.indexOf("itemContainer") > -1) {
        subItems =  $ES(".subItem", itemContainer);
        for(var i = 0; i < subItems.length; i++) {
            subItems[i].setStyle('display', 'none');
        }
    }
}

//Toggles the display value of an element with
//a transition effect
//NB: depends on MooTools for setStyles and FX.Style
function displayElementWithTransition(elementToToggle, styleType, defaultState, transitionDuration) {
    var elementDisplayType = $(elementToToggle).style.display;
    var elementVisibilityValue = $(elementToToggle).style.visibility;
    var element;
    var showAttribute;
    var hideAttribute;
    var transition;
    
    if(styleType == "visibility") {
        if(elementVisibilityValue == 'hidden' || (elementVisibilityValue === '' && defaultState == 'none')) {
            element = $(elementToToggle).setStyle('visibility', 'visible');
        }
        else {
            element = $(elementToToggle).setStyle('visibility', 'hidden');
        }
    }
    else {
        if(elementDisplayType == 'none' || (elementDisplayType === '' && defaultState == 'none')) {
            element = $(elementToToggle).setStyle('display', 'block');
        }
        else {
            element = $(elementToToggle).setStyle('display', 'none');
        }
    }
    
}

//Toggles the display value of collection of elements with
//a transition effect
//NB: depends on displayElementWithTransition()
function displayElementsWithTransition(elementsToToggle, styleType, defaultState, transitionDuration) {
    for(var i = 0; i < elementsToToggle.length; i++) {
        displayElementWithTransition(elementsToToggle[i], styleType, defaultState, transitionDuration);
    }
}

function configureItemContainers() {
    var itemContainers = $$(".itemContainer");
    var hasSubItemsElements;
    var subItems;
    for(var i = 0; i < itemContainers.length; i++) {
        //attach displayElementWithTransition event to anchors
        anchors = $ES("a", itemContainers[i]);
        for(var n = 0; n < anchors.length; n++) {
            if(anchors[n].hasClass("hasSubItem")) {
                anchors[n].addEvent("mouseover", function() { 
                    hideSubItemsInItemContainer(this.getParent().getParent());
                    displayElementWithTransition(this.getNext(), "display", "none", 800);
                });
            }
            else {
                anchors[n].addEvent("mouseover", function() { 
                    hideSubItemsInItemContainer(this.getParent().getParent());
                });                
            }
        }
    }
}

//Attaches functions onpageload
//NB: Used specifically for reference to DOM elements
//below swfObject
window.addEvent('domready', function() {
    findPopUpAnchors();
    findNewWindowAnchors();
    configureItemContainers();
});

