var node_count, nodeWidth, nodeBlock, nodeBMarginL = 0, flag = true, currentNode = 0, lastNode, speed = 500, channelOmniture,slideLen;

function SetChannel(channel) {
    channelOmniture = channel;
}

var nCount=0;
$(document).ready(function() {
    nodeBlock = $(".homeSlider");
	
    $(nodeBlock).each(function() {
        $("ul", this).css('width', ($("li", this).width()) * ($("li", this).length));
		slideLen = $("li", this).length;
    });
	
	if(slideLen < 4)
	$(".homeSlider .sliderNextBtn a").addClass("notActive");

    // Next Button Click Code 
    $(".sliderNextBtn a").click(function() {
        if (flag && slideLen>3) {
            flag = false;
            var thisB = $(this).parents(".homeSlider");
            var nodeBWidth = ($("ul", thisB).css("width"));
            nodeWidth = ($("li", thisB).width());
            nodeBMarginL = eval($("ul", thisB).css("marginLeft").substring(0, $("ul", thisB).css("marginLeft").length - 2));
            nodeBWidth = eval(nodeBWidth.substring(0, nodeBWidth.length - 2)) - eval(nodeWidth);
            nodeCount = ($("li", thisB).length - Math.round($(".homeSliderChild", thisB).width() / nodeWidth));
            if (nodeCount != nCount) {
                if (nodeBMarginL / nodeWidth)
                    currentNode = parseInt(Math.abs(nodeBMarginL / nodeWidth)) + 1;
                else
                    currentNode = 1;
                nodeBMarginL = -(Math.abs(nodeBMarginL) + nodeWidth);

                nCount++;
                if (channelOmniture.toLowerCase() == 'default') {
                    trackLink("Hmpg - Featured DIY Projects", 'Next', location.pathname.toLowerCase(), false);       
                }
                        
                animateMe(thisB, nodeBMarginL, nCount, nodeCount);   // function that causes the animation effect
            } else {
                flag = true;
            }
        }

    });

    // Previus Button Click Code
    $(".sliderPrvBtn a").click(function() {
        if (flag && slideLen>3) {
            flag = false;
            var thisB = $(this).parents(".homeSlider");
            var nodeBMarginL = $("ul", thisB).css("marginLeft");
            nodeBMarginL = eval(nodeBMarginL.substring(0, nodeBMarginL.length - 2));
            nodeWidth = ($("ul li", thisB).width());
            nodeCount = ($("li", thisB).length - Math.round($(".homeSliderChild", thisB).width() / nodeWidth));
            if (Math.abs(nodeBMarginL) > 0) {
                if (nodeBMarginL / nodeWidth)
                    currentNode = parseInt(Math.abs(nodeBMarginL / nodeWidth)) + 1;
                nodeBMarginL = -(Math.abs(nodeBMarginL) - nodeWidth);
                nCount--;
                if (channelOmniture.toLowerCase() == 'default') {
                    trackLink("Hmpg - Featured DIY Projects", 'Previous', location.pathname.toLowerCase(), false);
                }           
                animateMe(thisB, nodeBMarginL, nCount, nodeCount);   // function that causes the animation effect
            } else
                flag = true;
        }

    });

    // Main Animation Function
    function animateMe(thisB, nodeBMarginL, nCount, nodeCount) {
        if (nCount == 0)
            $(".sliderPrvBtn a", thisB).addClass("notActive");
        else
            $(" .sliderPrvBtn a", thisB).removeClass("notActive");
        if (nCount == nodeCount)
            $(" .sliderNextBtn a", thisB).addClass("notActive");
        else
            $(" .sliderNextBtn a", thisB).removeClass("notActive");

        if (speed == 0)
        //Large image show button so no animation 
            $("ul", thisB).css("marginLeft", nodeBMarginL);
        else
            $("ul", thisB).animate({
                marginLeft: nodeBMarginL
            }, speed, function() {
                flag = true;
            });
    }

});

