/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @copyright  Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
 * @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
function toggleMenu(el, over)
{
    if (over) {
        Element.addClassName(el, 'over');
    }
    else {
        Element.removeClassName(el, 'over');
    }
}

function showMenu(){
    if ($('catmenu_container')){
        if ($('catmenu_container').getStyle('display') == 'none')
            $('catmenu_container').style.display = 'block';
       // if ($('main_menu').hasClassName('art_cat_btn'))
       //     $('main_menu').removeClassName('art_cat_btn').addClassName('art_cat_btn_over');
    }
}

function hideMenu(){
    if ($('catmenu_container')){
        if ($('catmenu_container').getStyle('display') == 'block')
            $('catmenu_container').style.display = 'none';
       // if ($('main_menu').hasClassName('art_cat_btn_over'))
       //    $('main_menu').removeClassName('art_cat_btn_over').addClassName('art_cat_btn');
    }
}

function flyout(element){
    element = $(element);
    var sub_list = element.descendants();
    if(sub_list.size()>1){
        if ('ul' == sub_list[1].tagName.toLowerCase()){
            sub_list[1].style.display = 'block';
            sub_list[0].addClassName('flyout');
        }
    }

}

function flyin(element){
    element = $(element);
    var sub_list = element.descendants();
    if(sub_list.size()>1){
        if ('ul' == sub_list[1].tagName.toLowerCase()){
            sub_list[1].style.display = 'none';
            sub_list[0].removeClassName('flyout');
            var sub_sub_list = sub_list[1].descendants();
            if(sub_sub_list.size()>1){
                if ('ul' == sub_sub_list[1].tagName.toLowerCase()){
                    sub_sub_list[1].style.display = 'none';
                    sub_sub_list[0].removeClassName('flyout');
                }
            }

        }
    }

    sub_list = element.ancestors();
    if(sub_list.size()>1){
        if ('ul' == sub_list[1].tagName.toLowerCase()){
            sub_list[1].style.display = 'none';
            sub_list[0].removeClassName('flyout');
            var sub_sub_list = sub_list[1].ancestors();
            if(sub_sub_list.size()>1){
                if ('ul' == sub_sub_list[1].tagName.toLowerCase()){
                    sub_sub_list[1].style.display = 'none';
                    sub_sub_list[0].removeClassName('flyout');
                }
            }
        }
    }

}

//clicking on elemnt 1 coasues elemnt 2 to toglle and toggles given class
// for element 1
function toggle_element(el1, el2,class_name){
    el1 = $(el1);
    el2 = $(el2);
    if(el1.hasClassName(class_name))
        el1.removeClassName(class_name);
    else
        el1.addClassName(class_name);
    el2.toggle();
}

ImagePreloader = Class.create();
ImagePreloader.prototype = {
    initialize: function(images, call_back){
        // store the call-back
        this.callback = call_back;

        // initialize internal state.
        this.nLoaded = 0;
        this.nProcessed = 0;
        this.aImages = new Array;

        // record the number of images.
        this.nImages = images.length;

        // for each image, call preload()
        for ( var i = 0; i < images.length; i++ )
          this.preload(images[i]);
    },

    preload: function(image){
        // create new Image object and add to array
        var oImage = new Image;
        this.aImages.push(oImage);

        // set up event handlers for the Image object
        oImage.onload = ImagePreloader.prototype.onload;
        oImage.onerror = ImagePreloader.prototype.onerror;
        oImage.onabort = ImagePreloader.prototype.onabort;

        // assign pointer back to this.
        oImage.oImagePreloader = this;
        oImage.bLoaded = false;

        // assign the .src property of the Image object
        oImage.src = image;
    },

    onComplete: function(){
       this.nProcessed++;
       if ( this.nProcessed == this.nImages ){
         // this.call_back(this.aImages, this.nLoaded);
       }
    },

    onload: function(){
       this.bLoaded = true;
       this.oImagePreloader.nLoaded++;
       this.oImagePreloader.onComplete();
    },

    onerror: function(){
       this.bError = true;
       this.oImagePreloader.onComplete();
    },

    onabort: function(){
        this.bAbort = true;
        this.oImagePreloader.onComplete();
    }
}

// banner rotation
Rotator = Class.create();
Rotator.prototype = {
    initialize: function(conf){
        this.active = true;
        this.timerID = [];
        this.conf = conf;
        this.preloadImages();
    },
    preloadImages: function(){
        var images = new Array();

        var i;
        for(i=0 ; i<this.conf.all.length ; i++){
            images.push("/skin/frontend/default/artgeist/texts/"+this.conf.lang+"/"+this.conf.all[i]+".jpg");
        }
        this.imagePreloader = new ImagePreloader(images);
    },

    rotate:  function(){
        this.stop();
        if (this.active == false)
            return;
        var items = $$('#catmenu_container li.home_mid a');
        var i = 1;
        var that = this;
        items.each(function(el){
            if(!$(el.parentNode).hasClassName('hidden')){
                that.timerID[i-1] = setTimeout(function(){that.step(el)},2500*i);
                i++;
            }
        });
        this.timerID[i] = setTimeout("rotator.rotate()", 2500 * (i+1));
    },
    step: function(el){
        var items = $$('#catmenu_container li.home_mid a');
        items.each(function(el){el.removeClassName('hover');});
        if (this.active == false){
            this.stop();
            return;
        }
        el.onmouseover();
        el.addClassName('hover');
    },
    stop: function(){
        for(key in this.timerID)
            window.clearTimeout(this.timerID[key]);
        var items = $$('#catmenu_container li.home_mid a');
        items.each(function(el){el.removeClassName('hover');});
    }
}
