﻿/// <reference path="jQuery/jquery-1.4.1-vsdoc.js" />
$.ajaxSetup({ cache: false });

function renderScrollers() {
    var $productReel = $(".productReel");

    if ($productReel.length == 0)
        return;

	var $rightScroll = $(".rightScroll");
	var $leftScroll;

	if ($rightScroll.length == 0) {
	    var extension = $.browser.msie && $.browser.version < 7
            ? "gif"
            : "png";

		$rightScroll = $("<img src=\"/Content/images/rightScrollBtn."+extension+"\" class=\"multipleScroll rightScroll\" alt=\"Right scroll button\" />")
			.appendTo($productReel);

		$leftScroll = $("<img src=\"/Content/images/leftScrollBtn."+extension+"\" class=\"multipleScroll leftScroll\" alt=\"Left scroll button\" />")
			.appendTo($productReel);
	}
	else $leftScroll = $(".leftScroll");

	var $scroller = $(".product-reel-scroller");
	if ($scroller.length > 0) {
	    var scrollerPosition = $scroller.position();
	    if (scrollerPosition) {
	        var scrollerLeft = $scroller.position().left;

	        $leftScroll
        		.css({
        		    display: (scrollerLeft < 0) ? "block" : "none"
        		});

	        $rightScroll
        		.css({
        		    display: (($scroller.width() + scrollerLeft) > $productReel.width()) ? "block" : "none"
        		});
	    }
	}
};

$(".sub-category-container")
	.delegate(".sub-category-headings a", "click", function (evt) {
		var $this = $(this);
		var href = $this.attr("href");
		var matches = href.match(/\/Products\/([\w]*)\?selected=([\d]+)/);
		console.log(href, matches);

		if (matches.length < 3)
			return;

		evt.preventDefault();

		$(".sub-category-container")
		.load("/Products/SubCategories?code=" + matches[1] + "&selected=" + matches[2], function () {
			var $imagePointer = $("#productReel" + matches[2]);
			if ($imagePointer.length > 0) {
				$(".product-reel-scroller")
					.stop()
					.animate({
						left: -($imagePointer.position().left)
					}, 2000, renderScrollers);
			}
		});
	});

	$(".productReel")
	.delegate(".multipleScroll", "click", function () {
		var $productReel = $(".productReel");
		var $scroller = $(".product-reel-scroller");

		var productReelWidth = $productReel.width();
		var scrollerWidth = $scroller.width();

		if (scrollerWidth <= productReelWidth)
			return;

		var $this = $(this);

		var scrollerLeft = Math.abs($scroller.position().left);
		var targetLeft;

		if ($this.hasClass("rightScroll")) {
			var limit = scrollerWidth - productReelWidth;
			if (scrollerLeft >= limit)
				return;

			var nextSlide = scrollerLeft + productReelWidth;
			targetLeft = nextSlide < limit
				? nextSlide
				: limit;
		}
		else {
			if (scrollerLeft <= 0)
				return;

			var nextSlide = scrollerLeft - productReelWidth;
			targetLeft = nextSlide > 0
				? nextSlide
				: 0;
		}

		$scroller
			.stop()
			.animate({
				left: -(targetLeft)
			}, 1000, renderScrollers);
	});

	$(function () {
	    var $productReelScroller = $(".product-reel-scroller");
	    var $productReelProducts = $(".product-reel-scroller .product");

	    $productReelScroller.width($productReelProducts.length * $productReelProducts.outerWidth());

	    renderScrollers();

	    $('.numericinput').numeric({ allow: " " });
	    $('.alphainput').alpha({ allow: " .'" });

	    $("body")
            .delegate("a.modal", "click", function (evt) {
                evt.preventDefault();
                $.get($(this).attr("href"), function (content) {
                    $("body").append(content);

                    if (mapFunction && $("#map").length > 0) {
                        mapFunction();
                        $(".lightbulb").remove();
                    }
                    else $(".inner-content").jScrollPane();

                    $("body > #modal-back").addClass("js");
                });
            })
            .delegate(".productReel .product", "click", function (evt) {
                if ($(this).hasClass("no-modal"))
                    return false;

                var cssClass = $(this).attr("class").replace(/product /, "");
                if (cssClass && cssClass.length > 0)
                    $.get("/products/productmodal?code=" + cssClass, function (content) {
                        $("body").append(content);

                        $(".inner-content").jScrollPane();

                        $("body > #modal-back").addClass("js");
                    });
            })
            .delegate("#modal-back, .modal-close", "click", function (evt) {
                if (!$(evt.target).is("#modal-back, .modal-close"))
                    return;

                evt.preventDefault();
                $("body > #modal-back").remove();
            });

	    $(document).keydown(function (evt) {
	        if (evt.keyCode == 27)
	            $("body > #modal-back").remove();
	    });

	    $(".inner-content").jScrollPane();
	    $(".scrollable").jScrollPane();
	});

	
