(function() {

var carousel = function() {
	
	var carousel_length = document.getElementById("thumbs").getElementsByTagName("LI").length;
	var offset = 480;
	var pos_current = 0;
	var num_visible = 4;
	var pos_max = parseInt((carousel_length) / num_visible);
	
	if (carousel_length % num_visible == 0) 
		pos_max -= 1;
	
	var prev = document.getElementById("prevBtn");
	var next = document.getElementById("nextBtn");	
	
	if(carousel_length<=num_visible) {
		$(prev).hide();
		$(next).hide();
	}
	
	prev.onclick = function() {
		if((pos_current-1) >= 0) {
			pos_current -= 1;
			move_carousel(pos_current);			
		}
		
		else {
			pos_current = pos_max;
			move_carousel(pos_current);
		}			
	};
	
	next.onclick = function() {
		if((pos_current+1) <= pos_max) {
			pos_current += 1;			
			move_carousel(pos_current);								
		}
		
		else {
			pos_current = 0;
			move_carousel(pos_current);
		}
	};
	
	function move_carousel(a) {			
		var newPos = (a*offset*-1)+'px';
		$('#thumbs').animate({ left:newPos }, 1500 );
	}
	
	
}; // carousel


function setSelected(a) {       
    $(a).removeClass('selected');
    $(this).addClass('selected');
} // setSelected


var sfHover = function(a) {
	$(a).each(function() {
	  $(this).hover(
	 
	    function() {
	    	$(this).addClass('sfhover');
	    },
	    function() {
	    	$(this).removeClass('sfhover');
	    }
	  );
		
	});
}; // sfHover
      
$(function() {
    $('#tabBlock').tabs();
    // NOTE: viewLarger may need to select the selected image
    $('a.sendToFriend').bind("click",function(e) {
		LANE.lbForm.launch(this.href,{width: '520', height: '540'});
		e.preventDefault();
    });
    $('a.viewLarger,a.viewSwatch').bind("click",function(e) {
        LANE.lbForm.launch(this.href,{width: '940', height: '500'});
        e.preventDefault();
    });
    
    /*
    $('a.viewSwatch').bind("click",function(e) {
        this.href = "partials/sendToFriend.html?format=nojs&img=" + this.parentNode.parentNode.parentNode.parentNode.firstChild.firstChild.src;
    });
    */
    
        $('a.fabricVisualization').bind("click",function(e) {
        LANE.lbForm.launch(this.href,{width: '870', height: '640', windowBGColor: '#fff'});
        e.preventDefault();
    });

    
//    $('a.fabricVisualization').openDOMWindow({ 
//            borderSize:'0',
//            windowBGColor: '#fff',
//            eventType: 'click',
//            width: 765,
//            height: 570,
//            windowSource: 'iframe',
//            functionCallOnOpen: function() {
//                $(this).attr("scollable","no");
//            }
//    });         
           
    $('.woodFinishes a.thumb').bind("click",function(e) {
         $('.woodFinishes a.thumb').removeClass('selected'); 
        $(this).addClass('selected');
        
        $('.finishName').html(this.firstChild.title);
        e.preventDefault();
    });
    
    $('a.selectFinish').bind("click",function(e) {
        $(this).parent().parent().parent().parent().children('.thumb').click();
        e.preventDefault();
    });
   
    $('#thumbs a').bind('click',function(e) {
        $('#thumbs a').removeClass('selected'); 
        $(this).addClass('selected');
        
        var prodImage = $('.prodImage');
        prodImage.removeClass('selected');
        prodImage.hide();
        
        $('a.viewLarger')[0].href = this.href;

        var hero = $("#"+this.id.split('Thumb')[0]);
        hero.fadeIn("slow");
        hero.addClass('selected');           
        e.preventDefault();
    });
    
    carousel();
    sfHover('#productLinks li');
    sfHover('.woodFinishes li');
    
    // Evil fix to fix evil caused by table wrapping page content
	if(!$.support.leadingWhitespace) {
		window.onresize = function() {
			$('body table td[colspan=2]')[0].innerHTML += ' ';
		}
	}	
});


})();