function swapImages(){
 	var $active = $('#flash .active');
	var $next = ($('#flash .active').next().length > 0) ? $('#flash .active').next() : $('#flash img:first');

	$active.fadeOut(function(){
		$active.removeClass('active');
		$next.fadeIn().addClass('active');
	});
}

$(document).ready(function(){
	var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
	if (badBrowser) {
		$("#menu li img").pngfix();
		$("#fading-left").pngfix();
		$("#fading-right").pngfix();
		$(".left_fader .a").pngfix();
		$(".right_fader .a").pngfix();
		//$("#cite").fadeTo(0, 0.5, function(){});
	}

	// Run our swapImages() function every 5secs
	setInterval('swapImages()', 4000);

	$("#jcarousel").jMyCarousel({  
		visible: '100%',
		auto: true,
		circular: true,
		speed: 2000 
	});
});

//Slider
var sliderInfo = {
    path: false
};

var picSlider = {
    WIDTH: 12,
    IMGWIDTH: 148,
    IMGHEIGHT: 221,
    IMGHEIGHTCALC: 237,
    STARTPOS: [1, 0.87, 0.74, 0.6],
    STARTPOS_delta: [3, 3, 3],
    deltaListFader: 0,
    ImgBorder: 2,
    EM: 16,
    init: function (a) {
        this.isResize = false;
        if (a) {
            this.isResize = a;
        }
        var b = this;
        this.picList = $("#date_navigation .pic_list");
        this.picListWidth = $("#date_navigation .pic_list").width();
        this.lineLeft = $("#date_navigation .left_fader");
        this.lineLeftMove = this.lineLeft.width();
        this.dateController = $("#date_navigation .date_controller");
        this.lineDelta = this.lineLeftMove;
        this.dateController.css({
            "margin-left": round(this.lineDelta / 2),
            "width": this.picListWidth - round(this.lineDelta)
        });
        this.dateControllerWidth = this.dateController.width();
        this.dateControllerOffset = this.dateController.offset().left;
        this.decorate = $("#decorate");
        this.slider = $("#date_slider");
        this.container = $("#date_navigation");
        this.containerWidth = this.container.width();
        this.containerOffset = this.container.offset().left;
        this.dateList = $("#date_list_ul");
        this.lis = $(b.dateList).find("li.picture");
        $(this.lis[0]).addClass("selected");
        this.selectedItem = this.dateList.find("li.selected");
        this.imgList = $("img", this.dateList);
        this.imgWidths = [];
        this.imgList.each(function (c) {
            b.imgWidths[c] = $(this).attr("width");
        });
        this.currImg = $(this.imgList[0]);
        this.currImgFirst = true;
        this.picDescriptions = $("#pictures_description p");
        this.currentDescriptionNum = 0;
        this.hoverDescriptionNum = 0;
        this.imgPoints = [];
        this.imgPointsDelta = [];
        this.imgList.css("position", "absolute");
        this.recalculateHeights(1);
        this.test = 0;
        this.imgList.each(function () {
            b.test += $(this).width();
        });
        this.test += 40;
        this.measurerWidth = $("#inner_measurer").width();
        this.sliderOffset = 0;
        this.sliderMouseOffset = 0;
        this.dateListWidth = this.calculateWidth();
        this.rateDelta = 0;
        this.rate = ((this.dateListWidth - this.containerWidth) / this.containerWidth) + this.rateDelta;
        this.dateList.css("left", 0);
        this.slider.css("left", 0);
        this.clicked = false;
        if (this.isResize) {
            this.decorate.css({
                width: this.currImg.width()
            });
        }
        this.attachEvents();
        this.recalculateParams();
        this.iTimeFinish = 0;
        this.sliderInfo = "sliderInfo";
        this.bFirstPlay = true;
        if (sliderInfo.path !== false) {
            this.bFirstPlay = getCookieCopy(this.sliderInfo);
        }
        this.iPlayTime = 2;
        if (this.bFirstPlay === "true") {} else {
            //this.firstRun(1);
            if (sliderInfo.path !== false) {
                setCookieCopy(this.sliderInfo, "true", sliderInfo.path, 0);
            }
        }
        common.Measurer.setFunc(function () {
            b.recalculateParams();
        });
    },
    calculateWidth: function () {
        var a = this;
        return (a.test);
    },
    recalculateParams: function () {
        this.picListWidth = this.picList.width();
        this.lineLeftMove = this.lineLeft.width();
        this.lineDelta = this.lineLeftMove;
        this.dateController.css({
            "margin-left": round(this.lineDelta / 2),
            "width": this.picListWidth - this.lineDelta
        });
        this.dateControllerWidth = this.dateController.width();
        this.dateControllerOffset = this.dateController.offset().left;
        this.EM = $("#measurer").height();
        var b = parseInt(this.containerWidth);
        this.dateListWidth = this.calculateWidth();
        this.containerWidth = this.container.width();
        this.containerOffset = this.container.offset().left;
        this.rate = ((this.dateListWidth - this.containerWidth) / this.containerWidth) + this.rateDelta;
        this.sliderOffset = parseInt(this.slider.css("left"));
        var c = this.sliderOffset * (this.containerWidth / b);
        this.sliderOffset = c;
        var a = this.sliderOffset / this.containerWidth;
        this.slider.css({
            left: c
        });
        this.dateList.css({
            left: -((this.sliderOffset + this.WIDTH * a) * this.rate)
        });
        this.recalculateHeights(c, -((c + (this.WIDTH * a)) * this.rate));
    },
    recalculateHeights: function (g, a) {
        var d = 0;
        var l, c, f, j, m;
        var k = 0;
        if (this.imgPoints[0]) {
            for (var b = 0; b < this.imgList.length; b++) {
                c = $(this.imgList[b]);
                l = this.STARTPOS[0];
                if (!this.isResize) {
                    l = this.STARTPOS[3];
                    if (Math.abs(g - this.imgPoints[b]) < 350) {
                        l = -0.001 * (Math.abs(g - this.imgPoints[b])) + 1;
                    }
                }
                j = round(this.imgWidths[b] * l);
                f = this.IMGHEIGHT * l;
                m = round(this.IMGHEIGHTCALC - f - this.ImgBorder * 2 + 1);
                c.css({
                    width: j,
                    height: round(f),
                    top: m,
                    left: d
                });
                d += round(this.imgWidths[b] * l + this.ImgBorder * 2);
            }
        } else {
            for (b = 0; b < this.imgList.length; b++) {
                c = $(this.imgList[b]);
                l = this.STARTPOS[3];
                if (b < 3) {
                    l = this.STARTPOS[b];
                    k = this.STARTPOS_delta[b];
                }
                j = round(this.imgWidths[b] * l);
                f = this.IMGHEIGHT * l;
                m = round(this.IMGHEIGHTCALC - f - this.ImgBorder * 2 + 1);
                c.css({
                    width: j,
                    height: round(f),
                    top: m,
                    left: d
                });
                d += round(this.imgWidths[b] * l + this.ImgBorder * 2);
            }
        }
        this.recalculatePoints(a, this.STARTPOS[3]);
    },
    recalculatePoints: function (k, h) {
        var g = this;
        var j = this.IMGWIDTH / this.imgList.length;
        var f, c, i;
        this.imgList.each(function (a) {
            f = parseInt($(g.imgList[a]).css("left"));
            c = j * h * a;
            i = f + c + k;
            g.imgPoints[a] = i;
        });
    },
    firstRun: function (d) {
        var b = d;
        var h = this;
        var i = 0;
        var g = h.containerWidth - h.WIDTH;
        if (b === -1) {
            i = g;
            g = 0;
        }
        var j = round(600 / h.imgList.length) * b;
        var a;

        function c() {
            var l = h.sliderOffset / h.containerWidth;
            h.sliderOffset = i;
            h.slider.css({
                left: i
            });
            var o = -((i + (h.WIDTH * l)) * h.rate);
            h.dateList.css({
                left: o
            });
            for (var m = 0; m < h.imgList.length; m++) {
                var k = parseInt($(h.imgList[m]).css("left")) + o;
                var n = $(h.imgList[m]).width();
                if (i > k && i < (k + n) && m != h.currentDescriptionNum) {
                    if (h.isResize) {
                        h.currImgFirst = false;
                        h.currImg = $(h.imgList[m]);
                        h.decorate.css({
                            width: h.imgList[m].width - 6
                        });
                    }
                    $(h.picDescriptions[h.currentDescriptionNum]).removeClass("selected");
                    $(h.picDescriptions[m]).addClass("selected");
                    h.currentDescriptionNum = m;
                }
            }
            h.recalculateHeights(i, o);
            i += j;
            if (g === 0 && i > 0 && i < Math.abs(j)) {
                i = Math.abs(j);
            }
            if (i * b > g * b) {
                h.iTimeFinish++;
                clearInterval(a);
                if (h.iPlayTime > h.iTimeFinish) {
                    b *= -1;
                    h.firstRun(b);
                }
            }
            return true;
        }
        var f = 0;
        if (h.iTimeFinish === 0) {
            f = 1000;
        }
        setTimeout(function () {
            a = setInterval(function () {
                c();
            }, 50);
        }, f);
    },
    attachEvents: function () {
        var a = this;
        this.slider.mousedown(function (b) {
            b.preventDefault();
            a.sliderMouseOffset = b.pageX - a.dateControllerOffset - parseInt(a.slider.css("left"));
            a.clicked = true;
        });
        $(document).mousemove(function (h) {
            if (a.clicked) {
                h.preventDefault();
                a.sliderOffset = parseInt(a.slider.css("left"));
                var j = parseInt(h.pageX - a.dateControllerOffset - a.sliderMouseOffset);
                if (j >= 0 && j <= (a.dateControllerWidth - a.WIDTH)) {
                    var g = 0;
                    if (a.imgList.length > 12) {
                        g = a.imgList.length;
                    }
                    a.rate = (a.dateListWidth - (a.dateControllerWidth - g * 2)) / a.dateControllerWidth;
                    a.sliderOffset = j;
                    a.slider.css({
                        left: round(j)
                    });
                    var f = -j * a.rate;
                    a.dateList.css({
                        left: round(f)
                    });
                    for (var c = 0; c < a.imgList.length; c++) {
                        var b = parseInt($(a.imgList[c]).css("left")) + f;
                        var d = $(a.imgList[c]).width();
                        if (b < j + a.lineDelta / 2 && j + a.lineDelta / 2 < (b + d + a.WIDTH) && c != a.currentDescriptionNum) {
                            a.lis.removeClass("selected");
                            $(a.lis[c]).addClass("selected");
                            $(a.picDescriptions).removeClass("selected");
                            $(a.picDescriptions[c]).addClass("selected");
                            a.currentDescriptionNum = c;
                        }
                    }
                    if (!a.currImgFirst) {
                        a.decorate.css({
                            left: parseInt(a.currImg.css("left"))
                        });
                    }
                    if (!a.isResize) {
                        a.recalculateHeights(j, f);
                    }
                }
            } else {
                $(a.picDescriptions).removeClass("selected");
                $(a.picDescriptions[a.currentDescriptionNum]).addClass("selected");
            }
        });
        $(document).mouseup(function () {
            a.clicked = false;
        });
        this.imgList.click(function () {
            if (!a.intervalId && !$(this).parent().hasClass("selected")) {
                var i = $(this);
                var h = i.parent().prevAll();
                a.lis.removeClass("selected");
                i.parent().addClass("selected");
                a.selectedItem = i.parent();
                var g = a.sliderOffset;
                var d = parseInt(i.css("left"));
                var c = a.sliderOffset / a.containerWidth;
                var f = -((g + (a.WIDTH * c)) * a.rate);
                var k = 10;
                if (g > d + f) {
                    k *= -1;
                }
                function b() {
                    d = parseInt(i.css("left"));
                    g += k;
                    a.sliderOffset = g;
                    c = a.sliderOffset / a.containerWidth;
                    f = -((g + (a.WIDTH * c)) * a.rate);
                    var l = 0;
                    if (g >= 0) {
                        l = g;
                    }
                    a.slider.css({
                        left: l
                    });
                    a.dateList.css({
                        left: f
                    });
                    $(a.picDescriptions).removeClass("selected");
                    $(a.picDescriptions[h.length]).addClass("selected");
                    a.currentDescriptionNum = h.length;
                    a.recalculateHeights(g, f);
                    if (k > 0 && g > a.imgPoints[h.length]) {
                        clearInterval(a.intervalId);
                        a.intervalId = false;
                    }
                    if (k < 0 && g < a.imgPoints[h.length]) {
                        clearInterval(a.intervalId);
                        a.intervalId = false;
                    }
                }
                a.intervalId = setInterval(function () {
                    b();
                }, 5);
            } else {
                var j = $(a.picDescriptions[a.currentDescriptionNum]).find("a");
                if (j.length > 0) {
                    window.location = $(j[0]).attr("href");
                }
            }
        });
        this.imgList.mousemove(function (g) {
            if (!a.clicked) {
                g.stopPropagation();
                var d = g.pageX - a.containerOffset;
                var h = parseInt(a.dateList.css("left"));
                for(var c = 0; c < a.imgList.length; c++) {
                    var b = parseInt($(a.imgList[c]).css("left")) + h;
                    var f = $(a.imgList[c]).width();
                    if (d > b && d < (b + f)) {
                        if (a.isResize) {
                            a.currImgFirst = false;
                            a.currImg = $(a.imgList[c]);
                            a.decorate.css({
                                width: a.imgList[c].width - 6
                            });
                        }
                        $(a.picDescriptions).removeClass("selected");
                        $(a.picDescriptions[c]).addClass("selected");
                        a.hoverDescriptionNum = c;
                    }
                }
            }
        });
    },
    getSliderInfo: function () {
        var a = this;
        return sliderInfo;
    }
};

//Slider 2 part
function setWidthSlider() {
    var a = $("#date_navigation");
    var b = 0;
    $("#date_list_ul img").each(function () {
        b += 93;
    });
    b += 133;
    if (a.width() > b) {
        $("#date_navigation").css("cssText", "width: " + b + "px !important");
    }
    picSlider.init();
}

// Slider functions
function round(a) {
    return Math.round(a);
}

if (undefined === common) {
    var common = {};
}
common.Measurer = (function () {
    var b = {};
    var d = 500;
    var i = 1;
    var h;
    var c;
    var g = false;
    var j = false;
    $(function () {
        j = true;
        g && f();
    });

    function f() {
        c = $("#measurer");
        h = c.height();
        setInterval(function () {
            a();
        }, d);
        $(window).resize(k);
    }
    function a() {
        var l = c.height();
        if (l != h) {
            h = l;
            k();
        }
    }
    function k() {
        for (var l in b) {
            b[l]();
        }
    }
    return {
        setFunc: function (l, m) {
            if (!$.isFunction(l) && !$.isFunction(m)) {
                b[l] && (delete b[l]);
                return;
            }
            g = true;
            j && f();
            if ($.isFunction(l)) {
                b[i.toString()] = l;
                i++;
            } else {
                b[l] = m;
            }
        }
    };
})();
/////
var dateSlider = {
    WIDTH: 12,
    EM: 16,
    init: function () {
        var b = this;
        this.slider = $("#date_slider");
        this.sliderLine = $(".date_controller");
        this.line = $(".line");
        this.disabled = false;
        this.container = $("#date_navigation:not(.pic_slider_full, .pic_slider)");
        this.dateList = $("#date_list_ul");
        this.measurerWidth = $("#inner_measurer").width();
        this.containerWidth = this.container.width();
        this.containerOffset = this.container.offset().left;
        this.sliderOffset = parseInt(this.slider.css("left"));
        this.sliderMouseOffset = 0;
        this.dateListWidth = this.calculateWidth();
        this.rate = (this.dateListWidth - this.containerWidth) / this.containerWidth;
        this.selectedItem = this.dateList.find("li.selected");
        var c = this.dateListWidth - this.containerWidth;
        var a = this.selectedItem.offset().left - this.containerOffset;
        a += -(this.containerWidth / 2) + (this.selectedItem.width() / 2) + 10;
        if (a > c) {
            this.dateList.css({
                left: -(this.dateListWidth - this.containerWidth)
            });
            this.slider.css({
                left: this.containerWidth - this.WIDTH
            });
        } else {
            if (a > 0) {
                this.dateList.css({
                    left: -(a)
                });
            }
            if (a / this.rate < 0) {
                this.slider.css({
                    left: 0
                });
            } else {
                if (a / this.rate > this.dateListWidth) {
                    this.slider.css({
                        left: this.dateListWidth - this.WIDTH
                    });
                } else {
                    this.slider.css({
                        left: a / this.rate
                    });
                }
            }
        }
        if (this.dateListWidth < this.containerWidth) {
            this.slider.addClass("disabled");
            this.slider.css({
                left: 0
            });
            this.dateList.css({
                left: 0
            });
            this.line.addClass("disabled_line");
            this.disabled = true;
        }
        this.clicked = false;
        this.attachEvents();
        common.Measurer.setFunc(function () {
            b.recalculateParams();
        });
    },
    calculateWidth: function () {
        var c = this;
        var d = $("li:last", this.container);
        var b = 0;
        var a = $("li", this.container);
            a.each(function () {
            b += $(this).width() + 13;
            if ($(this).hasClass("year")) {
                b += 1;
            }
        });
        return (b);
    },
    recalculateParams: function () {
        this.EM = $("#measurer").height();
        var a = this.containerWidth;
        this.dateListWidth = this.calculateWidth();
        this.containerWidth = this.container.width();
        this.containerOffset = this.container.offset().left;
        if (this.dateListWidth > this.containerWidth) {
            if (this.disabled) {
                this.slider.removeClass("disabled");
                this.line.removeClass("disabled_line");
                this.disabled = false;
            }
            this.rate = (this.dateListWidth - this.containerWidth) / this.containerWidth;
            this.sliderOffset = parseInt(this.slider.css("left"));
            var c = this.sliderOffset * (this.containerWidth / a);
            if (c > this.containerWidth - this.WIDTH) {
                c = this.containerWidth - this.WIDTH;
            }
            this.sliderOffset = c;
            var b = this.sliderOffset / this.containerWidth;
            this.slider.css({
                left: c
            });
            this.dateList.css({
                left: -((this.sliderOffset + this.WIDTH * b) * this.rate)
            });
        } else {
            this.slider.addClass("disabled");
            this.line.addClass("disabled_line");
            this.disabled = true;
        }
    },
    attachEvents: function () {
        var a = this;
        this.slider.mousedown(function (b) {
            b.preventDefault();
            b.stopPropagation();
            a.sliderMouseOffset = b.pageX - a.containerOffset - parseInt(a.slider.css("left"));
            a.clicked = true;
        });
        $(document).mousemove(function (d) {
            if (a.clicked && !a.disabled) {
                d.preventDefault();
                a.sliderOffset = parseInt(a.slider.css("left"));
                var b = a.sliderOffset / a.containerWidth;
                var f = parseInt(d.pageX - a.containerOffset - a.sliderMouseOffset);
                if (f > 0 && f < (a.containerWidth - a.WIDTH)) {
                    a.sliderOffset = f;
                    a.slider.css({
                        left: f
                    });
                    var c = -((f + (a.WIDTH * b)) * a.rate);
                    a.dateList.css({
                        left: c
                    });
                }
            }
        });
        $(document).mouseup(function () {
            a.clicked = false;
        });
        this.sliderLine.mousedown(function (d) {
            if (!a.disabled) {
                a.sliderMouseOffset = a.WIDTH / 2;
                var b = a.sliderOffset / a.containerWidth;
                var f = parseInt(d.pageX - a.containerOffset - 6);
                if (f > 0 && f < (a.containerWidth - a.WIDTH)) {
                    a.sliderOffset = f;
                    a.slider.css({
                        left: f
                    });
                    var c = -((f + (a.WIDTH * b)) * a.rate);
                    a.dateList.css({
                        left: c
                    });
                }
            }
        });
    }
};
