/*! Copyright (c) 2009 Brandon Aaron (http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
 * Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
 *
 * Version: 3.0.2
 * 
 * Requires: 1.2.2+
 */
(function ($) {
    var types = ['DOMMouseScroll', 'mousewheel'];
    $.event.special.mousewheel = {
        setup: function () {
            if (this.addEventListener) for (var i = types.length; i;) this.addEventListener(types[--i], handler, false);
            else this.onmousewheel = handler;
        },
        teardown: function () {
            if (this.removeEventListener) for (var i = types.length; i;) this.removeEventListener(types[--i], handler, false);
            else this.onmousewheel = null;
        }
    };
    $.fn.extend({
        mousewheel: function (fn) {
            return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
        },
        unmousewheel: function (fn) {
            return this.unbind("mousewheel", fn);
        }
    });
    function handler(event) {
        var args = [].slice.call(arguments, 1),
        delta = 0,
        returnValue = true;
        event = $.event.fix(event || window.event);
        event.type = "mousewheel";
        if (event.wheelDelta) delta = event.wheelDelta / 120;
        if (event.detail) delta = -event.detail / 3; // Add events and delta to the front of the arguments
        args.unshift(event, delta);
        return $.event.handle.apply(this, args);
    }
})(jQuery);
/*Copyright Digital Century, 2009*/
var scrollLock = false;
function daScroll(scrollBlock, leftArrow, rightArrow, scrollSpeed, visibleCount) {
    visibleCount = scrollSpeed * visibleCount;
    document.onkeydown = NavigateThrough;
    function NavigateThrough(event) {
        if (!document.getElementById) return;
        if (window.event) event = window.event;
        if (event.ctrlKey) {
            var link = null;
            var href = null;
            switch (event.keyCode ? event.keyCode : event.which ? event.which : null) {
            case 0x25:
                el = $(scrollBlock);
                dal = el.css("left");
                if (dal == "0px") {
                    $(leftArrow).hide();
                } else {
                    $(rightArrow).show();
                    if (!scrollLock) {
                        scrollLock = true;
                        $(scrollBlock).animate({
                            "left": "+=" + scrollSpeed + "px"
                        },
                        "slow", function () {
                            scrollLock = false;
                            el = $(scrollBlock);
                            dal = el.css("left");
                            if (dal == "0px") {
                                $(leftArrow).hide();
                            }
                        });
                    }
                }
                break;
            case 0x27:
                $(leftArrow).show();
                el = $(scrollBlock);
                daw = el.width() - visibleCount;
                dah = el.css("left");
                daw = "-" + daw + "px"; //var ll = dah.length - 2;
                //var dah = dah.substring(1, ll);
                if (daw == dah) { //alert(daw+" : "+dah);
                    $(rightArrow).hide();
                } else { if (!scrollLock) {
                        scrollLock = true;
                        $(scrollBlock).animate({
                            "left": "-=" + scrollSpeed + "px"
                        },
                        "slow", function () {
                            scrollLock = false;
                            el = $(scrollBlock);
                            daw = el.width() - visibleCount;
                            dah = el.css("left");
                            daw = "-" + daw + "px"; //var ll = dah.length - 2;
                            //var dah = dah.substring(1, ll);
                            if (daw == dah) { //alert(daw+" : "+dah);
                                $(rightArrow).hide();
                            }
                        });
                    }
                }
                break;
            }
        }
    }
    $(scrollBlock).bind('mousewheel', function (event, delta) {
        if (delta > 0) {
            el = $(scrollBlock);
            dal = el.css("left");
            if (dal == "0px") {
                $(leftArrow).hide();
            } else {
                $(rightArrow).show();
                if (!scrollLock) {
                    scrollLock = true;
                    $(scrollBlock).animate({
                        "left": "+=" + scrollSpeed + "px"
                    },
                    "slow", function () {
                        scrollLock = false;
                        el = $(scrollBlock);
                        dal = el.css("left");
                        if (dal == "0px") {
                            $(leftArrow).hide();
                        }
                    });
                }
            }
        } else {
            $(leftArrow).show();
            el = $(scrollBlock);
            daw = el.width() - visibleCount;
            dah = el.css("left");
            daw = "-" + daw + "px"; //var ll = dah.length - 2;
            //var dah = dah.substring(1, ll);
            if (daw == dah) { //alert(daw+" : "+dah);
                $(rightArrow).hide();
            } else { if (!scrollLock) {
                    scrollLock = true;
                    $(scrollBlock).animate({
                        "left": "-=" + scrollSpeed + "px"
                    },
                    "slow", function () {
                        scrollLock = false;
                        el = $(scrollBlock);
                        daw = el.width() - visibleCount;
                        dah = el.css("left");
                        daw = "-" + daw + "px"; //var ll = dah.length - 2;
                        //var dah = dah.substring(1, ll);
                        if (daw == dah) { //alert(daw+" : "+dah);
                            $(rightArrow).hide();
                        }
                    });
                }
            }
        }
        return false;
    });

	$(leftArrow).hide();
	
	//Ширина показываемого на экране
	var widthDiv = parseFloat($("#galleryCont").css("width"));
	//Насколько мы будем ездить туда-сюда
	var step = 151;
	
	//Узнаём ширину первого изображения, если оно больше div-а, зануляем у него margin
	var firstImg = $("#gallery img:first");
	if (parseFloat(firstImg.css("width")) > widthDiv) firstImg.css("margin", "0px");
	$("#gallery img:last").css("margin-right", "0px");
	
	//Длина всей таблицы с изображениями
	var widthTable = parseFloat($("#gallery table").css("width"));
	
	var el = $("#gallery");
	
	$(leftArrow).click(function () {
		if (parseFloat(el.css("left")) < 0) {
			$(rightArrow).show();
			if (!scrollLock) {
				scrollLock = true;
				//Вычисляем шаг
				var steppy = -parseFloat(el.css("left")) > step ? step : -parseFloat(el.css("left"));
				el.animate({"left": "+=" + steppy + "px"}, "slow", function () {
					scrollLock = false;
					if (parseFloat(el.css("left")) == 0) $(leftArrow).hide();
				});
			}
		}
		return false;
    });
	
	$(rightArrow).click(function () {
		//Ширина скрытого слева сейчас
		var cc = -parseFloat(el.css("left"));
		
		//Пока справа ещё хоть что-то скрыто, прокручиваем
		if ((cc + widthDiv) < widthTable) {
			$(leftArrow).show();
			if (!scrollLock) {
				scrollLock = true;
				//Вычисляем шаг
				var steppy = (widthTable - (cc + widthDiv)) > step ? step : (widthTable - (cc + widthDiv));
				el.animate({"left": "-=" + steppy + "px"}, "slow", function () {
					scrollLock = false;
					if ((-parseFloat(el.css("left")) + widthDiv) >= widthTable) $(rightArrow).hide();
				});
			}
		}
		return false;
	});
}
