/*
 * Based on jQuery JavaScript Library v1.3.2
 *
 * Custom
 * Copyright (c) 2009 aXcessTen Ltd
 *
*/

/*
 * Homepage Feature product
 */
var ProductRotator = function(){
    var self = this;
    self.v = {ani:{duration:600}};
    self.el = {};
    self.products = [];
    //
    // Product
    self.Product = function(name, url, img, thumb, img_alt, thumb_alt){
        var product = this;
        //
        // Init
        product.init = function(name, url, img, thumb, img_alt, thumb_alt){
            product.name = name;
            product.url = url;
            product.img = img;
            product.thumb = thumb;
            product.img_alt = img_alt;
            product.thumb_alt = thumb_alt;
            return product;
        };
        return product.init(name, url, img, thumb, img_alt, thumb_alt);
    };
    self.addProduct = function(name, url, img, thumb, img_alt, thumb_alt) {
        var product = new self.Product(name, url, img, thumb, img_alt, thumb_alt);
        self.products.push(product);
        return self;
    };
    self.getNext = function(i) {
        if (++i >= self.products.length) {
            return 0;
        } else {
            return i
        };
    };
    self.getPrev = function(i) {
        if (--i < 0) {
            return self.products.length - 1;
        } else {
            return i
        };
    };
    self.setFeature = function(i) {
        var product = self.products[i];
        var link = $('<a/>')
            .attr('href', product.url)
            .appendTo(self.el.featureContainer);
        var productEl = $('<img/>')
            .attr('src', product.img)
            .attr('alt', product.img_alt)
            .appendTo(link);
        return productEl;
    };

    self.setNav = function(i) {
        var product = self.products[i];
        var productEl = $('<img/>')
            .attr('src', product.thumb)
            .appendTo(self.el.navContainer)
            .click(function(){ self.goTo(i, 1) });
        return productEl;
    };
    self.goTo = function(i, dir) {
        // Feature
        self.el.featureContainer.find('img')
            .animate({
                'opacity': 0
            }, {
                complete: function(){
                    $(this).remove();
                    self.setFeature(i).hide().fadeIn('slow');
                }
            });

        // Nav
        self.el.navContainer.find('img')
            .animate({
                //'right': 150*dir,
				'top': 150*dir,
                'opacity': 0
            }, {
                duration: self.v.ani.duration,
                complete: function(){ $(this).remove(); }
            });
        self.setNav(self.getNext(i))
            .css({
                //'right': -150*dir,
				'top': -150*dir,
                'opacity': 0
            })
            .animate({
                //'right': 0,
				'top': 0,
                'opacity': 1
            }, {
                duration: self.v.ani.duration
            });
    };
    //
    // Start
    self.start = function(){
        self.setFeature(0).hide().fadeIn('slow');
        self.setNav(self.getNext(0)).hide().fadeIn('slow');
        return self;
    };
    //
    // Init
    self.init = function(){
        self.el.navContainer = $('#navContainer');
        self.el.featureContainer = $('#featureContainer');
        return self;
    };
    return self.init();
};
$(document).ready(function(){
    pr = new ProductRotator();
    
        pr.addProduct(
            'Prowash',
            '/products/prowash/',
            '/images/home/product-prowash-large.jpg',
            '/images/home/product-prowash-small.jpg',
            'Prowash',
            'Prowash'
        );
        
        pr.addProduct(
            'Hygienius',
            '/products/hygienius/',
            '/images/home/product-hygienius-large.jpg',
            '/images/home/product-hygienius-small.jpg',
            'Hygienius',
            'Hygienius'
        );

        pr.addProduct(
            'Super Stallette',
            '/products/super-stallette/',
            '/images/home/product-super-stallette-large.jpg',
            '/images/home/product-super-stallette-small.jpg',
            'Super Stallette',
            'Super Stallette'
        );		

        pr.addProduct(
            'Big Sink',
            '/products/big-sink/',
            '/images/home/product-big-sink-large.jpg',
            '/images/home/product-big-sink-small.jpg',
            'The Big Sink',
            'The Big Sink'
        );

        pr.addProduct(
            'Patient Wash',
            '/products/patient-wash/',
            '/images/home/product-patient-wash-large.jpg',
            '/images/home/product-patient-wash-small.jpg',
            'Patient Wash',
            'Patient Wash'
        );
		
	    pr.addProduct(
            'General Washing',
            '#',
            '/images/home/product-general-info-large.jpg',
            '/images/home/product-general-info-small.jpg',
            'Portable hot water hand washing',
            'Portable hot water hand washing'
        );
    pr.start();
});
