﻿var ok = new function () {
    return {
        interval: 0,
        submenuTimer: 0,
        subActionTimer: 0,
        scrollPosition: 0,
        offpix: 0,
        pics: 0,
        picObj: new Array(),
        galleryWidth: 0,
        fixSquareBg: false,
        fixMouseEvents: false,
        contentWidth: 740,
        init: function () {
            ok.galleryWidth = 0;
            $('li.list-block').each(function () { ok.galleryWidth += parseInt($(this).find("img").attr("width"), 10) + 29; $(this).css("display", "block").css("float", "left") });
            $('ul#content-list').width(ok.galleryWidth + 300);

            $("div#gallery-content").css("overflow", "hidden");

            //scroll links

            if (ok.galleryWidth > 700) {
                $("div#gallery-right").css("visibility", "visible");
            }
            $("div.scr-links").hover(ok.gOver, ok.gOut)
            $("div.scr-links").click(ok.stopClick);



            $("img.picture-img").css("padding", "0").css("border", "0"); //.hover(ok.picOver,ok.picOut);
            $("img.picture-img").bind('mouseover', ok.picOver);
            $("img.picture-img").bind('mouseout', ok.picOut);

            $("img.picture-img").parent().click(ok.imgClick);

            $("div.block-picture").css("padding", "5px").css("border", "2px solid #ccc").css("display", "block");
            ok.pics = new Array();
            $("img.picture-img").each(
                function () {
                    ok.pics[$(this).attr('src').substring($(this).attr('src').length - 20)] = { width: $(this).attr('width'),
                        height: $(this).attr('height'), bigW: Math.ceil($(this).attr('width') * 1.5),
                        bigH: Math.ceil($(this).attr('height') * 1.5), submenu: $(this).parents("li").find("ul.block-links")
                    };
                });

            $("ul.block-links").hover(ok.subOver, ok.subOut).css("opacity", "0").css("top", "245px").css("display", "none");
            ok.offpix = 16;

            if (!$.browser.msie) {
                $("ul.block-links").css("top", "255px").css("position", "absolute");
                ok.contentWidth -= ($("img.picture-img").length * 5);
            } else {
                ok.contentWidth += ($("img.picture-img").length * 2);
            }

            $('div#gallery-info-close').pngfix();
            $('div#gallery-info-close').click(ok.galleryCloseClick);

            $('div#gallery-info').css("opacity", "0");

            if ($.browser.msie && $.browser.version < 7) {
                $('.g-info').css('width', '610px');
            }

            $('div.square').hover(ok.squareOver, ok.squareOut);
            $('div.square').click(ok.squareClick);

            $('div#gallery-content').bind("mousewheel", function (event, delta) {
                if (delta < 0) {
                    var obj = $("div#gallery-content");
                    if (obj.scrollLeft() > ok.galleryWidth - ok.contentWidth) return;
                    obj.scrollLeft(obj.scrollLeft() + 20);
                    ok.testScroll();
                }
                else {
                    var obj = $("div#gallery-content");
                    //if (obj.scrollLeft() > ok.galleryWidth - ok.contentWidth) return;
                    obj.scrollLeft(obj.scrollLeft() - 20);
                    ok.testScroll();
                }
                event.stopPropagation();
                event.preventDefault();
            });

        },
        squareOver: function (obj) {
            ok.fixSquareBg = false;
            if ($(this).attr('id') == "black") {
                $('div#gallery-info-text p').css('color', '#fff');
            } else {
                $('div#gallery-info-text p').css('color', '#000');
            }
            $('div#gallery-info-inner').css('background-color', $(this).css('background-color'));
        },
        squareOut: function (obj) {
            if (!ok.fixSquareBg) {
                $('div#gallery-info-inner').css('background-color', 'transparent');
            }

            if (ok.fixSquareBg && $(this).attr('id') == "black") {
                $('div#gallery-info-text p').css('color', '#fff');
            } else {
                $('div#gallery-info-text p').css('color', '#000');
            }
        },
        squareClick: function (obj) {
            ok.fixSquareBg = !ok.fixSquareBg;
        },
        imgClick: function (obj) {
            if ($(this).parents("li").find("div.cid").html() == 0) {
                obj.preventDefault();

                var iid = $(this).parents("li").find("div.iid").html();

                $('h3#ginfo-title').html(pic.getPicInfo(iid).title);
                $('img#gallery-info-picture').attr('src', pic.getPicInfo(iid).lImgUrl);
                $('img#gallery-info-picture').attr('width', pic.getPicInfo(iid).lImgWidth);
                $('img#gallery-info-picture').attr('height', pic.getPicInfo(iid).lImgHeight);
                $('b#ginfo-year').html(pic.getPicInfo(iid).pYear);
                $('b#ginfo-size').html(pic.getPicInfo(iid).pSize);
                $('b#ginfo-material').html(pic.getPicInfo(iid).pMaterial);
                $('#pDesc p').html(pic.getPicInfo(iid).Desc);

                if (pic.getPicInfo(iid).pStatus == '0') {
                    $('p#gallery-buy-link').show();
                    $('p#gallery-buy-bought').hide();
                } else {
                    $('p#gallery-buy-link').hide();
                    $('p#gallery-buy-bought').show();
                }

                if ($("div#gallery-info:visible").length == 0) {
                    $('.dynamic').css('background-color', '#fff');


                    $('div#gallery-info-inner').css('background-color', 'transparent');
                    $('div#gallery-info-text p').css('color', '#000');

                    $('div#gallery-info').show().animate({ opacity: 0.7 }, "fast");
                    $('div#gallery-info-inner').fadeIn("fast",
                        function () {
                            $('.dynamic').css('background-color', 'transparent');
                        });

                    //$("img.picture-img").unbind('mouseover',ok.picOver);
                    ok.fixMouseEvents = true;
                    ok.picOut(obj);
                }
                else {
                    ok.galleryCloseClick();
                }
            }

        },
        galleryCloseClick: function (obj) {
            $('.dynamic').css('background-color', $('div#gallery-info-inner').css('background-color') == 'transparent' ? '#fff' : $('div#gallery-info-inner').css('background-color'));

            $('div#gallery-info').animate({ opacity: 0 }, "fast",
                function () {
                    $('div#gallery-info').hide();
                    $('.dynamic').css('background-color', 'transparent');
                    $('img#gallery-info-picture').attr('src', "");
                });
            $('div#gallery-info-inner').fadeOut("fast");

            ok.fixMouseEvents = false;
        },
        picOver: function (obj) {
            if (ok.fixMouseEvents) return;

            clearTimeout(ok.subActionTimer);
            var p = ok.pics[$(this).attr('src').substring($(this).attr('src').length - 20)];
            $(this).stop().animate({ width: p.bigW, height: p.bigH }, "slow",
               function () {
                   var leftOffset = $(this).offset().left - $("div#gallery-content").offset().left;
                   if (leftOffset + p.bigW > 740)
                       $("div#gallery-content").stop().animate({ scrollLeft: $("div#gallery-content").scrollLeft() - (740 - leftOffset - p.bigW - ok.offpix) }, "slow");
                   ok.testScroll();
               }
            );
            if (p.submenu != null) {
                $(p.submenu).stop().css("display", "block").animate({ opacity: 1 }, "slow");
            }

            var leftOffset = $(this).offset().left - $("div#gallery-content").offset().left;
            if (leftOffset + p.bigW > 740) {
                $("div#gallery-content").stop().animate({ scrollLeft: $("div#gallery-content").scrollLeft() - (740 - leftOffset - p.bigW - ok.offpix) }, "slow");
            }
            else if (leftOffset < 0) {
                $("div#gallery-content").stop().animate({ scrollLeft: $("div#gallery-content").scrollLeft() + leftOffset - ok.offpix }, "slow");
            }
            ok.testScroll();
        },
        picOut: function (obj) {
            ok.picObj.push(obj.target != null ? obj.target : obj);
            timedOut = function () {
                while (ok.picObj.length != 0) {
                    var curObj = ok.picObj.pop();
                    var p = ok.pics[$(curObj).attr('src').substring($(curObj).attr('src').length - 20)];
                    $(curObj).stop().animate({ width: p.width, height: p.height }, "slow");
                    if (p.submenu != null) {
                        $(p.submenu).stop().animate({ opacity: 0 }, "slow", function () { $(this).css("display", "none"); });
                    }

                    ok.checkFreeSpace();
                }
            }
            ok.testScroll();
            ok.submenuTimer = setTimeout("timedOut()", 300);
        },
        checkFreeSpace: function () {
            var v = $('ul#content-list').width() - 740 - $("div#gallery-content").scrollLeft();
            if (v < 300) {
                $("div#gallery-content").stop().animate({ scrollLeft: $('ul#content-list').width() - 1040 }, "slow");
            }
        },
        subOver: function (obj) {
            clearTimeout(ok.submenuTimer);
            clearTimeout(ok.subActionTimer);
        },
        subOut: function (obj) {
            timedOut = function () {
                ok.picOut(ok.picObj);
            }
            ok.subActionTimer = setTimeout("timedOut()", 300);
        },
        gOver: function (obj) {
            way = obj.target.id.indexOf("right") != -1 ? 5 : -5
            ok.scrollOver(way);
        },
        gOut: function (obj) {
            clearTimeout(ok.interval);
        },
        scrollOver: function (way) {
            clearTimeout(ok.interval);
            var obj = $("div#gallery-content");
            var clip = $("div#gallery-content");
            if (way > 0) {
                $("div#gallery-left").css("visibility", "visible");
                if (obj.scrollLeft() > ok.galleryWidth - ok.contentWidth) {
                    $("div#gallery-right").css("visibility", "hidden");
                    return;
                }
            } else {
                $("div#gallery-right").css("visibility", "visible");
                if (obj.scrollLeft() == 0) {
                    $("div#gallery-left").css("visibility", "hidden");
                    return;
                }
            }

            obj.scrollLeft(obj.scrollLeft() + way);
            ok.interval = setTimeout("ok.scrollOver(" + way + ")", 5);
        },
        testScroll: function () {
            var obj = $("div#gallery-content");

            if (obj.scrollLeft() == 0) {
                $("div#gallery-left").css("visibility", "hidden");
            }
            else {
                $("div#gallery-left").css("visibility", "visible");
            }

            if (obj.scrollLeft() > ok.galleryWidth - ok.contentWidth) {
                $("div#gallery-right").css("visibility", "hidden");
            }
            else {
                $("div#gallery-right").css("visibility", "visible");
            }
        },
        stopClick: function (obj) {
            obj.preventDefault();
        }
    };
} ();

$(document).ready(ok.init);