var ie6 = navigator.userAgent.toLowerCase().indexOf(': msie 6') != -1;

//Flash Message
Flash = {
    show:function(dur)
    {
        $('flashmsg').visualEffect('appear');
        if(dur)
        {
            this.timer = setTimeout('Flash.hide()',dur*1000);
        }
        else
        {
            this.timer = setTimeout('Flash.hide()',3000);
        }
    },
    hide:function()
    {
        $('flashmsg').visualEffect('fade');
        clearTimeout(this.timer);
        return false;
    }
};

//Loading animation
Loading = {
    show:function()
    {
        $('loading').visualEffect('appear');
    },
    hide:function()
    {
        $('loading').visualEffect('fade');
        return false;
    }
};

Preloader = {
    callbacks: [],
    images: [],
    loadedImages: [],
    imagesLoaded: 0,

    add: function(image)
    {
        if (typeof image == 'string') this.images.push(image);
        if (typeof image == 'array' || typeof image == 'object')
        {
            for (var i=0; i< image.length; i++)
            {
                this.images.push(image[i]);
            }
        }
    },

    load: function()
    {
        for(var i=0; i<this.images.length; i++)
        {
            this.loadedImages[i] = new Image();
            //this.loadedImages[i].onload = function(){ Preloader.checkFinished.apply(Preloader) }
            this.loadedImages[i].src = this.images[i];
        }
    },

    checkFinished: function()
    {
        this.imagesLoaded++;
        if (this.imagesLoaded == this.images.length) this.fireFinish();
    },

    fireFinish: function()
    {
        for (var i=0; i<this.callbacks.length; i++)
        {
            this.callbacks[i]();
        }

        this.images = [];
        this.loadedImages = [];
        this.imagesLoaded = 0;
        this.callbacks = [];
    }
}


//Menu state
Menu = {
    //dur:0.2,
	applyState:function()
    {
		var menuItems = document.getElementsByClassName('menu_off');

        for(i=0; i<menuItems.length;i++)
        {
            var e = $(menuItems[i]);

            Event.observe(e, 'mouseover', function()
            {
                Menu.applyOverState(this);
            });

            Event.observe(e, 'mouseout', function()
            {
                Menu.applyOffState(this);
            });
        }
    },

    applyOverState:function(el)
    {
        var collection = el.childElements();
        el.removeClassName('menu_off').removeClassName('menu_on').addClassName('menu_over');

        if(ie6)
        {
            if(el.id.indexOf('IE6on') > 0)
            {
                var eid = el.id;
                el.id = eid.replace('IE6on','IE6over');
            }
            else
            {
                el.id = el.id+'IE6over';
            }
        }

        if(collection[1]) collection[1].show();
    },

    applyOffState:function(el)
    {
        var collection = el.childElements();
        if(getTopLevel(el))
        {
            el.removeClassName('menu_over').addClassName('menu_on');
            if(ie6)
            {
                var eid = el.id;
                el.id = eid.replace('IE6over','IE6on');
            }
        }
        else
        {
            el.removeClassName('menu_over').addClassName('menu_off');
            if(ie6)
            {
                var eid = el.id;
                el.id = eid.replace('IE6over','');
            }
        }

        if(collection[1]) collection[1].hide();
    },

    applyOnState:function()
    {
		var menuItems = document.getElementsByClassName('menu_off');

        for(i=0; i<menuItems.length;i++)
        {
            var e = $(menuItems[i]);
            if(getTopLevel(e))
            {
                e.removeClassName('menu_off').addClassName('menu_on');

                if(ie6)
                {
                      e.id = e.id+'IE6on';
                      e.className = '';
                }
            }
        }
    }
};

function getTopLevel(el)
{
    var host = location.protocol + '//' + location.host;
    var url = el.down().href.substring(host.length)
    var path = '/' + location.pathname.split('/')[1];

    //alert((url == path) ? 'true' : 'false');

    return (url == path) ? true : false;
}

function swapClass(e,off,on)
{
    e.removeClassName(off).addClassName(on);
}

function submitform()
{
    new Ajax.Updater($('FormType').value,'/formhandler', {
            asynchronous:false,
            parameters:Form.serialize('BlockFormhandlerForm'),
            requestHeaders:['X-Update', '/formhandler'],
            evalScripts:false
            });
}

Event.observe(window, 'load', function()
    {
    	Menu.applyState();
        Menu.applyOnState();
        Preloader.add(new Array('/img/nav_contacts_over.png','/img/nav_corporate_over.png','/img/nav_gallery_over.png','/img/nav_solutions_over.png'));
        Preloader.load();
    }
);






