/*******************************************************************
 *  (C) Copyright Rubus.com 2002                                   *
 *  All Rights Reserved.  No use, copying or distribution of this  *
 *  work may be made except in accordance with a valid licence     *
 *  granted through rubus.com. This notice must be                 *
 *  included on all copies, modifications and derivatives of this  *
 *  work.                                                          *
 *                                                                 *
 *  Please email copyright@rubus.com for details.                  *
 *******************************************************************/

/**************************************************************
 * 3UTILS.JS
 *
 * This file holds utility functions for use across the site.
 * Page specific functions / initialisation should placed in
 * separate files.
 *
 *************************************************************/

/**
 * ---------------------------------------------------
 * Hashmap
 * This has the following methods:
 *     set(name, value) - sets a name / value pair
 *     get(name)        - gets the value for a name
 */
function HashMap() {
    this.i = 0;
    this.names = new Array();
    this.values = new Array();

    this.set = _hashmap_set;
    this.get = _hashmap_get;
}

function _hashmap_set(name, value) {
    // Reset existing first
    for(var j=0;j<this.i;j++) {
        if(this.names[j] == name) {
            this.values[j] = value;
            return;
        }
    }
    // Doesn't exist, so add new entry
    this.names[this.i] = name;
    this.values[this.i] = value;
    this.i++;
}

function _hashmap_get(name) {
    for(var j=0;j<this.i;j++) {
        if(this.names[j] == name) {
            return this.values[j];
        }
    }
    return null;
}
// --------------------------------------------------------

/**
 * --------------------------------------------------------
 * ImageHandler
 * This has the following methods:
 *     preload(ref, image_path)         - Preload an image
 *     load(target, ref, image_path)    - Load an image and set a target to it
 *     swap(image_name, ref)            - Swap the target image for a preloaded one
 *     fade(image_name, ref)            - Fade transition the target image for a preloaded one (IE5.5 up only)
 *
 *     setFade(fade_time)               - Set the Fade time in seconds
 *
 * Notes: If it can't fade, it will swap. if it can't swap, it won't do anything!
 */
function ImageHandler() {
    // Attributes
    this.map = new HashMap();
    this.agt = navigator.userAgent.toLowerCase();
    this.is_ie5 = (this.agt.indexOf("msie 5.0")!=-1);
    this.is_ie5_5 = (this.agt.indexOf("msie 5.5") !=-1);
    this.is_ie6 = (this.agt.indexOf("msie 6.")!=-1);
    this.fade_time = 0.7;

    this.preload = _imagehandler_preload;
    this.load = _imagehandler_load;
    this.swap = _imagehandler_swap;
    this.fade = _imagehandler_fade;
    this.setFade = _imagehandler_set_fade;
}

function _imagehandler_preload(name, src) {
    img = new Image();
    img.src = src;
    this.map.set(name, img);
}

function _imagehandler_load(target, to, src) {
    if(!this.map.get(to)) {
        this.preload(to, src);
    }
    this.swap(target, to);
}

function _imagehandler_swap(target, to) {
    if(document.images) {
        img = document.images[target];
        ref = this.map.get(to);
        if(img && ref) {
            img.src = ref.src;
        }
    }
}

function _imagehandler_fade(target, to) {
    if(document.images) {
        img = document.images[target];
        ref = this.map.get(to);
        if(img && ref) {
            if((this.is_ie5_5 || this.is_ie6) && img.style) {
                img.style.filter = "progid:DXImageTransform.Microsoft.Fade(overlap=1.00,duration="+this.fade_time+")";
                img.filters[0].apply();
                img.src = this.map.get(to).src;
                img.filters[0].play();
            }
            else {
                img.src = ref.src;
            }
        }
    }
}

function _imagehandler_set_fade(t) {
    this.fade_time = t;
}

//function used to control rollover background colors on services detail page 22.01.03
function changeBg(id,style){
	var prefix1="bgIcon";
	var prefix2="bgMenuItem";
	id1 = prefix1+id;
	id2 = prefix2+id; 
	document.getElementById(id1).className = style;
	document.getElementById(id2).className = style;
	}
//-------------------------------------------

