/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 *
 * @copyright  Copyright (c) 2010 Ecommerce Developers (http://www.ecomdev.org)
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 * @author     Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
 */
 
if (!window.Curves) {
    window.Curves = {}
}

var CurvesSlider;

Curves.Slider = Class.create({
    initialize: function (container, switcher) {
        this.container = $(container);
        this.switcher = $(switcher);
        this.items = this.container.select('.hp-product');
        this.totalItems = this.items.length;
        this.currentPage = Math.floor(Math.random() * this.totalItems + 1);
        // If no items to slide, we hiding the switcher
        if (this.totalItems == 1) {
            this.switcher.hide();
            return;
        }
        
        this.switcher.childElements().each(this.removeSwitcher);
        this.switcher.lastElement = this.switcher.childElements().last();
        this.switcher.firstElement = this.switcher.childElements().first();
        this.pages = [];
        for (var i=1; i <= this.totalItems; i ++) {
            var liItem = new Element('li');
            var aHref = new Element('a', {href:'#'});
            aHref.update(i);
            aHref.page = i;
            liItem.page = i;
            liItem.insert(aHref);
            aHref.observe('click', this.handleClick.bind(this));
            this.switcher.lastElement.insert({before:liItem});
            this.pages.push(liItem);
            if (this.items[i-1].down('.banner a')) {                
                this.items[i-1].observe('click', function() { 
					setLocation(this.down('.banner a').href);
				});
            }
        }
        this.switcher.firstElement.observe('click', this.prev.bind(this));
        this.switcher.lastElement.observe('click', this.next.bind(this));
        this.markSwitcher();
        this.nextTimer(true);
    },
    removeSwitcher: function (element) {
        if (element.down('.a-prev') || element.down('.a-next')) {
            return;
        }
        
        element.remove();
    },
    handleClick: function (evt) {		
        this.go(Event.findElement(evt, 'a').page);
        Event.stop(evt);
        this.nextTimer(true);
    },
    go: function (page) {
        this.currentPage = page;
        this.markSwitcher();
    },
    markSwitcher: function () {
        for (var i = 0, l = this.pages.length; i < l; i++) {
            if (this.pages[i].active && this.pages[i].page != this.currentPage) {
                this.pages[i].down('a').removeClassName('current');
                this.pages[i].active = false;
                this.items[i].style.zIndex = 210;
                new Effect.Opacity(this.items[i], {from: 1, to:0, duration: 0.5});
            } else if (this.pages[i].page == this.currentPage && !this.pages[i].active) {
                this.pages[i].down('a').addClassName('current');
                this.pages[i].active = true;
                this.items[i].style.zIndex = 220;
                
                if (this.nextTime) {
                    new Effect.Opacity(this.items[i], {from: 0, to:1, duration: 0.5});
                } else {
                    this.nextTime = true;
                }
            } else if (this.pages[i].page != this.currentPage) {
                this.items[i].style.zIndex = 200;
                this.items[i].setStyle({'opacity':0});
            }
        }
    },
    prev: function (evt) {
        if (this.currentPage - 1 == 0) {
            this.go(this.totalItems);
        } else {
            this.go(this.currentPage - 1);
        }
        Event.stop(evt);
    },
    next: function (evt) {
        if (this.currentPage + 1 > this.totalItems) {
            this.go(1);
        } else {
            this.go(this.currentPage + 1);
        }
        if (evt) {
            Event.stop(evt);
        }
    },
    nextTimer: function(wait) {
        if (this.timer) {
            clearTimeout(this.timer);
        }
        if (!wait) {
            this.next();
        }
        this.timer = setTimeout("CurvesSlider.nextTimer()", 6000);
    }
});

document.observe('dom:loaded', function () {
    var container = $$('.hp-slide').first();
    var switcher = $$('.hp-switcher').first();
    if (container && switcher) {
        CurvesSlider = new Curves.Slider(container, switcher);
    }
    
    $$('.nospam').each(function(elm) {
        elm.textContent = elm.textContent.replace('*', '@');
        elm.href = 'mailto:' + elm.textContent;
    });
});
