var Oddballs = {
	lightboxWrapper : function(jElement) {
		$(jElement).wrap('<a class="lightBox" href="' + $(jElement).attr('src') + '" title="' + $(jElement).attr('alt') + '"></a>');
	}
}
var imgSwapper = {
    changeImage : function(attribId) {
        var attribIdArr = attribId.split("-");
        var imageId = attribIdArr[1] + "-" + attribIdArr[2];
        var target = "#attribImages div.attribImg-" + imageId;
        if ($("#attribImages div.attribImg-" + imageId).length > 0) {
            var selector = "#attribImages [class^=attribImg attribImg-" + attribIdArr[1] + "]";
            $(selector).each(function(){
                if ($(this).attr('class') !== $(target).attr('class')) {
                    if ($(this).css('display') == 'block') {
                        $(this).fadeOut('fast');
                    }
                }
            });
            if ($(target).css('display') == 'none')
                $(target).fadeIn('fast');
        }
    },
    selectOption : function(attribId) {
        var attribIdArr = attribId.split("-");
        var imageId = attribIdArr[1] + "-" + attribIdArr[2];
        var target = "#attrib-" + imageId;
        if ($(target).attr('disabled') == false)
            $(target).attr('checked','checked');
    },
    init : function() {
        $("#attribImages .attribImg").css({'display':'none'});
        $("#attribImages .attribImg:nth-child(1)").css({'display':'block'});
        $(".wrapperAttribsOptions input").hover(function(){
            imgSwapper.changeImage($(this).attr("id"));
        }, null);
        $(".wrapperAttribsOptions label").hover(function(){
            imgSwapper.changeImage($(this).attr("for"));
        }, null);
        $("#attribImageThumbs .attribImg, .attribSelector .attribImg").hover(function(){
            var id = $(this).attr("class").split(" ");
            imgSwapper.changeImage(id[1]);
        }, null)
        .click(function(){
            var id = $(this).attr("class").split(" ");
            imgSwapper.selectOption(id[1]);
        });
    }
}

$(document).ready(function() {
	$("img.zoom").each(function(){
		Oddballs.lightboxWrapper($(this));
	}).attr("title", "Click to zoom");
	$("#attribImages img").each(function(){
		Oddballs.lightboxWrapper($(this));
	}).attr("title", "Click to zoom");
	$("a.lightBox").lightBox();
	$.localScroll({
		lazy : true,
		onAfter : function(anchor, settings) {
			var originalColor = $(anchor).css('color');
			//alert(originalColor);
			$(anchor).animate({
				color : '#fffc00'
			}, 300, function(){
				$(anchor).animate({
					color : originalColor
				}, 700);
			});
		}
	});
	$("#indexCategories .scrollContent h3").prepend(
		'<a href="' + document.location.pathname + '#indexCategoriesHeading" class="backtotop">Back to the top</a>'
	);
	imgSwapper.init();
	//special deals
	$("#ts a.add-col").click(function(){
		var aId = $(this).attr("id");
		var splitId = aId.split("-");
		var optionNameId = "44";
		var optionValueId = splitId[1];
		var ahref = $(this).attr("href");
		var optionName = ahref.substring(1);
		$("#ts-div").html(
			'<' + 'span' + '>You have choosen:' + '<' + '/span' + '>' +
			'&nbsp;' + '<' + 'div class="ts-fb"' + '>'+ optionName + '<' + '/div' + '>' + '<' + 'input type="image" title=" Add to Cart " alt="Add to Cart" src="includes/templates/oddballs_template/buttons/english/button_in_cart.png"/' + '>' +
			'<' + 'input class="none" type="hidden" value="' + 
			optionValueId + '" name="id[' + 
			optionNameId + ']" id="attrib-' +
			optionNameId + "-" + optionValueId + '"/>'
		);
		return false;
	});
	$("#ts a.add-name").click(function(){
		$("#ts-div").html(
			'<' + 'span class="ts-fb"' + '>' + 'Please choose size and colour' + '<' + '/span' + '>'
		);
		return false;
	});
	$("#ts a.add-size").click(function(){
		$("#ts-div").html(
			'<' + 'span class="ts-fb"' + '>' + 'Please choose colour' + '<' + '/span' + '>'
		);
		return false;
	});
	$('#ts li').hover(
		function(){
			$(this).addClass('sfhover')},
		function(){
			$(this).removeClass('sfhover')}
	);
	//$.localScroll.hash();
	var _gaq = _gaq || [];
	  _gaq.push(['_setAccount', 'UA-1027247-1']);
	  _gaq.push(['_trackPageview']);
	  (function() {
	    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
	  })();
})
