﻿/*
    File Created By: Gaurang
    File Created Dt: 12/17/2010
    File Description: Used to create Slider.
*/

var MOSlider = Class.create({
    initialize: function(element, duration, n) {
        var children, l, dimensions, w = 0, h = 0, wrap, nav, slider;
        element = $(element);
        slider = element;
        children = element.childElements();
        l = children.length;

        if (n == undefined || isNaN(n)) {
            n = 0
        }
        else if (n < 0) {
            n = l + n;
            if (n < 0) {
                n = 0
            }
        }
        else if (n >= l) {
            n = 0
        }

        children.each(function(child) {
            dimensions = child.getDimensions();
            if (dimensions.width > w) {
                w = dimensions.width
            }
            if (dimensions.height > h) {
                h = dimensions.height
            }
        });

        wrap = new Element('div');

        wrap.setStyle({
            marginLeft: n * -w + 'px',
            width: children.length * w + 'px',
            height: h + 'px'
        });

        children.each(function(child) {
            child.setStyle({
                cssFloat: 'left',
                width: w + 'px'
            });
            wrap.appendChild(child)
        });

        element.setStyle({
            width: w + 'px',
            overflow: 'hidden'
        });

        element.appendChild(wrap);

        this.duration = (duration == undefined ? 0.5 : duration);
        this.wrap = wrap;
        this.w = w; // width of each slide
        this.h = h; // height of each slide
        this.l = l; // number of slides in slide show
        this.n = n; // number of the active slide


    },

    isFirst: function() {
        return (this.n == 0)
    },

    hasNextBtn: function() {
        return (this.n == this.l - 1)
        //return (this.n == this.l)
    },

    isLast: function() {
        return (this.n == this.l - 1)
       // return (this.n == this.l)
    },

    slide: function() {
        new Effect.Morph(this.wrap, {
            style: 'margin-left: -' + this.n * this.w + 'px;',
            duration: this.duration
        })
    },

    prev: function(e) {
        this.n--;
        this.slide();

    },

    next: function(e) {
        this.n++;
        this.slide();

    }

});
