/*
    Product compare functions for the compare pop-up window.
    Copyright (C) 2000-2008 Kohler Company.    All Rights Reserved.
    Author: Dennis Spaag
    Date Created: 01/29/08
 */

function getProductPosition(jObj) {
    var parentId = jObj.parents('div.compare-column.scroll-me').attr('id');
    var result = 0;
    $('#compare-scroll').children('div.compare-column.scroll-me').each(function(idx) {
        var thisId = $(this).attr('id');
        if (parentId == thisId) { result = idx; }
    });
    return result;
}

function getProductPositionId(jObj) {
    return jObj.parents('div.compare-column.scroll-me').attr('id').split("-product")[1];
}

function updateCompareSize(section) {
    var compareArLen = getCompareArray(section).length;
    compare_size = 150 * compareArLen;
    if (compareArLen > 5) {
        $('.compare-scroll').css("width", compare_size + "px");
    } else {
        $('.compare-scroll').css("width", "754px");
    }

    // remove scroll arrows if 3 items left
    if (compareArLen <= 5) {
        $('.scroll-left').hide();
        $('.scroll-right').hide();
    }

    // now update the number of compare items on the page
    $('#numCompared').text(compareArLen);
}

function removeProductFromCompare(section, jObj) {
    var position = parseInt(getProductPosition(jObj));
    var result = [];
    var compareArray = getCompareArray(section);
    for (var i=0; i<compareArray.length; i++) {
        if (i!=position) {
            result[result.length] = compareArray[i];
        }
    }
    setCompareCookie(section, result);
    var productPosition = getProductPositionId(jObj);
    $('#compare-product' + productPosition).remove();
    $('#compare-colorFinish' + productPosition).remove();
    updateCompareSize(section);
    if (result.length == 0) {
        addNoProductsNote();
    }
}

$(function() {
	$('.removeProduct a').click(function() {
        removeProductFromCompare(compareSection, $(this));
    });
	
	$('.compare').serialScroll({
	    items:'div.scroll-me', //selector to the items
	    prev:'img.scroll-left',//selector to the 'prev' button
	    next:'img.scroll-right',//selector to the 'next' button
	    axis:'x', //the default is 'y'
	    cycle: false,
	    duration:700 // ,
	    // onBefore: digit
	});
	
	updateCompareSize(compareSection);
});	


