/* 
 * Stoneworth JS functions
 * Namespace: SW.*
 */

SW = {
    
    faders: '.jcarousel-prev, .jcarousel-next, .products li, #pics li img, .pricing a',
    
    init: function () {
        
        $('body').addClass('js');
        
        if ($('.page-home').length) {
            SW.initHome();
        }
        
        if ($('.page-product').length) {
            SW.initProduct();
        }
        
        if (typeof(ie6) != undefined) {
            SW.initIE6Fixes();
        }
        
        SW.initFX();
    },
    
    initIE6Fixes: function () {        
        // Product nav height for IE6
        var h = $('#nav-products').height();
        var h2 = $('#main').height();
        if ( h < h2 ) $('#nav-products').height(h2);
    },
    
    initFX: function () {
        $(SW.faders).hover(SW.fxMouseOver, SW.fxMouseOut);
    },
    
    // Basic hover effects used by many elements
	fxMouseOver: function () {
       // console.log('hi');
        $(this).stop().fadeTo('fast', 0.875);
    },
    fxMouseOut: function () {
       // console.log('hi');
        $(this).stop().fadeTo('fast', 1.0);
    },
    
    initHome: function () {
        // Select products carousel
        $('#selected-products').jcarousel();

        // Fading promo banner
        /*$('#promo ul.promos').innerFade({
            timeout: 6000,
            speed: 2000,
            callback_index_update: function (i) {
                $('#promo-controls li.active').removeClass('active');
                $('#promo-controls li').eq(i).addClass('active');
            }
        });*/
        // Banner indicator
       /* $('#promo').append('<ul id="promo-controls"></ul>');
        
        for (var i = 0; i < $('#promo .promos li').length; i ++) {
            $('#promo-controls').append('<li><a href="#">'+i+'</a></li>');
        }
        $('#promo-controls li:first-child').addClass('active');
        $('#promo-controls a').click(function(e) {
            var n = $(this).text() - 1;
            /*
             * IE doesn't fade when we click, so this is disabled.
             $('#promo ul.promos').innerFadeTo(n);
        });*/
    },
    
    initProduct: function () {
        // Image swapper
        $('#pics img').click(function () {
/*
* Original code cause swapping so this was commented out and new code added by JOOLS
            var i = $(this);
            if (!i.parent().hasClass('main')) {
                i.parent().append($('#pics .main img'));
                $('#pics .main').append(i);
            }
            */
                $('#full_image').attr("src", $(this).attr("src"));
        });
    }
    
}

$(function () {
    SW.init();
});

$(document).ready(function() {
	$('.addToFav').click(function(){
		prod_id = $(this).attr('id');
		$.post('/favourites/ajax_add', { id: prod_id }, function(data){
			//$('#'+prod_id).hide();
			//alert(data);
			$('#favLink span').html(data);
			return false;
		});
		
		return false;
	})
});
