

var Site = new Class({

    Implements: [Options, Events],
    options : {
        },

    num_slides: 13,
    slides: false,
    slideBin: false,
    current: 0,
    periodID: null,
    load_threshold: 3,

    initialize: function (options) {
        this.setOptions(options);
    },

    setup: function() {
        //this.RelayData();

        if ($('displayImage')) {
            this.slideshow();
        }


    },

    slideshow: function () {
        this.slideBin = $('displayImage');
        this.loadSlides();
    },

    loadSlides: function () {
        var files = new Array();
        for (var i=1;i<this.num_slides+1;i++) {
            if (i<10) {
                var add = '0';
            } else var add = '';
            var file = "images/gallery/boso"+add+i+'.jpg';
            files.include(file);
        }
        this.slides = new Asset.images(files,{
                        properties:{"alt":"slideshow images of boso store and products"},
                        onProgress: function (counter,i) {
                                if (counter == this.load_threshold) {
                                    this.startSlideshow();

                                }
                            }.bind(this),
                        onComplete: function () {
                                //this.startSlideshow();
                            }.bind(this)});
    },

    startSlideshow: function () {
        this.current = 0;
        this.periodID = this.nextSlide.periodical(3000,this);
        this.slideBin.addEvent('click',function () {
                if (this.periodID != null) {
                    this.periodID = $clear(this.periodID);
                } else {
                    this.nextSlide.periodical(3000,this);
                }
            }.bind(this));
    },

    nextSlide: function () {
        if (this.current == this.num_slides-1) {
            this.current = 0;
        } else this.current += 1;

        var next = this.slides[this.current];
        var slide = this.slideBin.getFirst();
        slide.ghost(0,500);
        next.set('opacity',0);
        next.replaces.delay(600,next,slide);
        next.ghost(1,2000);
    },

    RelayData: function () {

        var data = {'host':window.location.hostname,
                    'path':window.location.pathname,
                    'file':window.location.getFilename()};

        if (data.file == "") {
            data.file = "index.html";
        }
        //this.SendData(data);
    },

    SendData: function (data) {
        var ajax = new Request({'url':'http://localhost/snowbound/client_services/utils/pagehit.php',
                               'method':'post',
                               'data':data,
                               'onSuccess': function () {
                                    this.SendData = $empty;
                               }.bind(this)});
        ajax.send();
    }

});


Element.implement({
    overlay: function (target) {
        var coords = target.getCoordinates();
        this.setStyles({
            position: 'absolute',
            zIndex: (target.getStyle('z-index').toInt() + 10),
            top: coords.top,
            left: coords.top,
            width: coords.width,
            height: coords.height
        });
    },

    matchDimensions: function (target) {
        var coords = target.getCoordinates();
        this.setStyle({
            width: coords.width,
            height: coords.height
        });
    },

    getSelectedOption: function () {
        if (this.get('tag') == 'select') {
            var opts = $$(('#'+this.get('id')+' option'));
            var selected = null;
            opts.each(function (opt,i) {
                if (opt.get('selected')) {
                    selected = opt;
                }
            });
            return selected;
        } else return false;
    },

    ghost: function (op, animDuration) {
        if (!animDuration) {
            animDuration = 1000;
        }
        this.set('tween',{wait: false,duration: animDuration});
        if (!op) {
            if (this.get('opacity') > 0) {
                this.tween('opacity',0);
            } else {
                this.tween('opacity',1);
            }
        } else {
                this.tween('opacity',op);
        }

    },

/*
    fold assumes the element has been hidden -either by setting the opacity = 0
    or display: none
*/
    fold: function (state, animDuration) {
        if (!animDuration) {
            animDuration = 1000;
        }
        this.set('morph',{wait: false, duration: animDuration});
        if ( this.get('opacity') == 0
            || this.getStyle('display') == 'none'
            || state ){
                state = true;
        } else if (this.get('opacity') > 0 || this.getStyle('display') != none || !state){
            state = false;
        }
        if (this.retrieve('foldFlag',false)) {
            var overflow = this.retrieve('overflow', 'auto');
        } else {
            this.store('overflow', this.getStyle('overflow'));
            this.store('xVal', this.getStyle('width'));
            this.store('yVal', this.getStyle('height'));
            this.store('foldFlag', 1);
            var overflow = this.getStyle('overflow');
        }
        this.setStyle('overflow','hidden');
        if (state) { // reveals the element
            var xWide = this.retrieve('xVal');
            var xHigh = this.retrieve('yVal');
            this.setStyles({width: 0, height: 0});
            if (this.get('opacity') == 0) {
                this.set('opacity', 1);
            }
            if (this.getStyle('display') == 'none') {
                this.setStyle('display','block');
            }
            this.morph({
                width: [0,xWide],
                height: [0,xHigh]
                });
            this.setStyle.delay(animDuration,this,['overflow',overflow]);
        } else {
            var width = this.getStyle('width');
            var height = this.getStyle('height');
            this.setStyle('overflow','hidden');
            this.morph({
                width: [width,0],
                height: [height,0]
            });
            this.set.delay(1000,this,['opacity',0]);
            this.setStyle.delay(animDuration,this,['overflow',overflow]);
        }
    },

    getAncestorByTag: function (tag) {
        var par = this.getParent();
        if (par.get('tag') == tag) {
            return par;
        } else {
            return par.getAncestorByTag(tag);
        }
    },

    getCoords: function () {
        var coords = this.getCoordinates();
        coords.xRad = coords.width / 2;
        coords.xCenter = coords.left + coords.xRad;
        coords.yRad = coords.height / 2;
        coords.yCenter = coords.top + coords.yRad;
        return coords;
    }

});

window.location.getFilename = function () {
    var path = this.pathname;
    var arr = path.split('/');
    return arr.getLast();
}



var site = new Site();

window.addEvent('domready', function () {

    });

window.addEvent('load', function () {
        site.setup();
    });
