/**
 * CKW.js
 *
 * Author:       Chris Knowles<chris.knowles@ckweb.com.au>
 * Date:         24/03/2006
 * Last Updated: 28/12/2006
 */

var CKW =
{
    flash_timer: null,

    id: function(id)
    {
        return document.getElementById(id);
    },

    tags: function(tagname, elm)
    {
        if (!elm) {
            elm = document;
        }
        return elm.getElementsByTagName(tagname);
    },
    
    classes: function(class_name, tag)
    {
        // 1. parse the doc and get all tags that have the given class    
        
        // 2. parse the doc and get all tags of type tag that have the given class 
        
    },

    mask: function(elm, mask, percentage)
    {
        var hidden_elm = CKW.id(elm);
        var mask_elm = CKW.id(mask);
        hidden_elm.style.zIndex = 0;
        mask_elm.style.zIndex = 1;
        CKW.setTransparency(mask, percentage);
    },

    unmask: function(elm, mask)
    {
        var hidden_elm = CKW.id(elm);
        var mask_elm = CKW.id(mask);
        hidden_elm.style.zIndex = 1;
        mask_elm.style.zIndex = -1;
        CKW.setTransparency(mask, 100);
    },

    getEvent: function(e)
    {
        var evt = (e) ? e : ((window.event) ? event : null);
        return evt;
    },

    getEventType: function(e)
    {
        var evt = CKW.getEvent(e);
        var event_type = (evt.button) ? evt.button : null;
        return event_type;
    },

    hideSelects: function(id)
    {
        var elm = CKW.id(id);
        elm.onmouseover = function()
        {
            var selects = CKW.tags("select");
            for (var i = 0; i < selects.length; i++) {
                selects[i].style.visibility = "hidden";
            }
        };
        elm.onmouseout = function()
        {
            var selects = CKW.tags('select');
            for (var i = 0; i < selects.length; i++) {
                selects[i].style.visibility = "visible";
            }
        }
    },

    hideFlash: function(id)
    {
        var elm = CKW.id(id);
        elm.onmouseover = function()
        {
            clearTimeout(CKW.flash_timer);
            var flashes = CKW.tags('div');
            for (var i = 0; i < flashes.length; i++) {
                if (flashes[i].className == 'flash') {
                    flashes[i].style.visibility = "hidden";
                }
            }
        };
        elm.onmouseout = function()
        {
            CKW.flash_timer = window.setTimeout('CKW.showFlash()', 600);
        };
    },

    showFlash: function()
    {
        var flashes = CKW.tags('div');
        for (var j = 0; j < flashes.length; j++) {
            if (flashes[j].className == 'flash') {
                flashes[j].style.visibility = "visible";
            }
        }
        clearTimeout(CKW.flash_timer);
    },

    includeFlash: function(id, swf_file, width, height, version, bg_color)
    {
        var elm = CKW.id(id);
        if (elm) {
            var swf = new SWFObject(swf_file, id + "_swf", width, height, version, bg_color);
            swf.write(id);
        }
    },

    toggleContent: function(show, hide, display_type)
    {
        if (!display_type) {
            display_type = 'block';
        }
        var hide_elm = CKW.id(hide);
        if (hide_elm) {
            hide_elm.style.display = 'none';
        }
        if (show) {
            var show_elm = CKW.id(show).style.display = display_type;
        }
    },

    ieDropNav: function(nav)
    {
        var nav_id = CKW.id(nav);
        var nav_items = CKW.tags('li', nav_id);
        for (var i = 0; i < nav_items.length; i++) {
            nav_items[i].onmouseover = function()
            {
                this.className += " show_nav";
            };
            nav_items[i].onmouseout = function()
            {
                this.className = this.className.replace(new RegExp(" show_nav\\b"), "");
            }
        }
    },

    toggleTextfieldMessage: function(id, message)
    {
        var field = CKW.id(id);
        field.onfocus = function()
        {
            if (this.value == message) {
                this.value = '';
                return;
            }
        };
        field.onblur = function()
        {
            if (this.value == '') {
                this.value = message;
                return;
            }
        }
    },

    /**
     * Sets the transparency of an element
     *
     * @var string id      - id of the element to make semi-transparent
     * @var int percentage - the percentage transparency
     */
    setTransparency: function(id, percentage)
    {
        var mask = CKW.id(id);
        mask.style.KHTMLOpacity = percentage / 100;
        mask.style.MozOpacity = percentage / 100;
        mask.style.opacity = percentage / 100;
        mask.style.filter = "alpha(opacity:" + percentage + ")";
    },

    /**
     * Redirects directly from a select control without clicking a submit button.
     * @var string id  - id of the select element
     *
     * NOTE: It's assumed that the values of the selects option fields are url's
     */
    selectRedirect: function(id, new_window)
    {
        var select = CKW.id(id);
        select.onchange = function()
        {
            var url = this.options[this.selectedIndex].value;
            if (url) {
                if (new_window) {
                    window.open(url, '_blank');
                } else {
                    window.location = url;
                }
            }
        }
    },

    blurAnchors: function(id)
    {
        if (!id) {
            id = document;
        } else {
            id = CKW.id(id);
        }
        var tags = CKW.tags("a");
        if (tags.length > 0) {
            for (var i = 0; i < tags.length; i++) {
                tags[i].onfocus = function(){this.blur()};
            }
        }
    },

    setNewWindowLinks: function(class_name)
    {
        if (!class_name) {
            class_name = 'ckw_new_win';
        }
        var tags = CKW.tags('a');
        var re = new RegExp(class_name);
        if (tags.length > 0) {
            for (var i = 0; i < tags.length; i++) {
                if (tags[i].className.search(re) != -1) {
                    tags[i].onclick = function()
                    {
                        window.open(this.href, '_blank');
                        return false;
                    }
                }
            }
        }
    },

    setBackLinks: function(class_name)
    {
        if (!class_name) {
            class_name = 'ckw_back';
        }
        var tags = CKW.tags('a');
        var re = new RegExp(class_name);
        if (tags.length > 0) {
            for (var i = 0; i < tags.length; i++) {
                if (tags[i].className.search(re) != -1) {
                    tags[i].onclick = function()
                    {
                        window.history.go(-1);
                        return false;
                    }
                }
            }
        }
    }

}


