﻿$(function ()
{
    MAINLIB.overlay();
});

MAINLIB =
{
    styles: function (width)
    {
        width = parseInt(width);
        //alert(width);
        if (width < 1200)
        {
            $("#sizeStyle").attr("href", "/Styles/960.css");
            $("#sizeStyle2").attr("href", "/Styles/Landscape.css");
        }
        else
        {
            $("#sizeStyle").attr("href", "/");
            $("#sizeStyle2").attr("href", "/");
        }
    },

    veryStyles: function (width)
    {
        width = parseInt(width);
        if (width < 1000)
        {
            $("#sizeStyle").attr("href", "/Styles/Very960.css");
            $("#sizeStyle2").attr("href", "/Styles/Landscape.css");
        }
        else
        {
            $("#sizeStyle").attr("href", "/");
            $("#sizeStyle2").attr("href", "/");
        }
    },

    overlay: function ()
    {
        $(".menuOverlay a[rel]").overlay({ top: 'center' });
        $('.scrollpaneOverlay').jScrollPane({ autoReinitialise: true });
    },

    scrollpane: function ()
    {
        var element = $('.scrollpane').jScrollPane({ autoReinitialise: true });
    }, //scroll

    boutique: function ()
    {
        var lib = this;
        lib.verticalGallery();
        $(window).resize(function ()
        {
            lib.verticalGallery()
        });

        lib.scrollpane();

    }, //boutique

    news: function ()
    {
        var lib = this;
        lib.verticalGallery();
        $(window).resize(function ()
        {
            lib.verticalGallery()
        });

        var $leftNav = $('.leftNav');
        var $linkActive = $("ul li a.active", $leftNav);
        var positionY = $linkActive.position().top - $leftNav.position().top;
        $('.leftDescription').jScrollPane({ autoReinitialise: true });
        //this.scrollpane();

        var scroll = $('.leftNav').jScrollPane({ autoReinitialise: true });
        var api = scroll.data('jsp');
        api.scrollToY(positionY, true);
    }, //news

    collections: function ()
    {
        this.thumbOver();
        this.scroller();
        $(".picture a").lightBox();
    }, //collections

    veryVhernier: function ()
    {
        this.thumbOver();
        this.scrollerVery();
        $(".pictureVery a").lightBox();
    }, //collections

    pressroom: function ()
    {
        this.thumbOver();
        this.scroller();
        this.validateFiels();
        this.scrollpane();
        $(".picturePress a").lightBox();
        $(".pressformHandle").overlay({ target: '#pressOverlay', top: 'center' });

    }, //pressroom

    contacts: function ()
    {
        this.scrollpane();
        this.validateFiels();
        $(".contactformHandle").overlay({ target: '#contactOverlay', top: 'center' });

    }, //pressroom

    scroller: function ()
    {
        $scrollable = $(".scrollable");
        $scrollable.scrollable({ mousewheel: true });
        var api = $scrollable.data("scrollable");

        var items = api.getItems();

        items.each(function (index, Element)
        {
            if ($(this).find('a.active').size() > 0)
            {
                api.seekTo(index);
                return;
            }
        }); //each
    }, //scroller

    scrollerVery: function ()
    {
        $scrollable = $(".scrollable");
        $scrollable.scrollable({ vertical: true, mousewheel: true });
        var api = $scrollable.data("scrollable");

        var items = api.getItems();

        items.each(function (index, Element)
        {
            if ($(this).find('a.active').size() > 0 && index > 0)
            {
                api.seekTo(index);
                return;
            }
        }); //each

        if (items.length == 1)
        {
            $('.next').addClass('disabled');
        }
        else
        {
            $('.next').removeClass('disabled');
        }

    }, //scroller

    thumbOver: function ()
    {
        $('#thumbContainer a:not(.active) img').css("opacity", '0.6').hover(
              function ()
              {
                  $(this).fadeTo('fast', 1);
              },
              function ()
              {
                  $(this).fadeTo('fast', 0.6);
              }
            );
    }, //thumbOver

    verticalGallery: function ()
    {
        var $container = $("#verticalGallery"),
            $item = $('.item', $container),
            spacer = 10;

        var spacerTotal = spacer * ($item.length - 1);
        var itemW = (($container.width() - spacerTotal) / $item.length);
        itemW = (Math.floor(itemW * 100)) / 100;

        $item.width(itemW)
        $item.css("margin-right", spacer + "px");
        $item.filter(":last").css("margin-right", "0px");

        //$item.filter(":last").width(itemW)

        $item.find('img').each(function ()
        {
            var imgLeft = (itemW - $(this).width()) / 2;
            $(this).css("margin-left", imgLeft);
            $(this).css("opacity", '0.5');

        }); //each

        $item.fadeIn('slow');

        $("img", $item).hover
        (
              function ()
              {
                  $(this).fadeTo('normal', 1);
              },
              function ()
              {
                  $(this).fadeTo('normal', 0.5);
              }
        );
        $("a", $item).lightBox();
    }, //verticalGallery

    validateFiels: function ()
    {
        $("#form1").validate({
            onfocusout: false,
            invalidHandler: function (form, validator)
            {
                var errors = validator.numberOfInvalids();
                if (errors)
                {
                    $('#messageBox').html(validator.errorList[0].message);
                    validator.errorList[0].element.focus();
                }
            },
            highlight: function (element, errorClass, validClass)
            {
                $(element).addClass(errorClass).removeClass(validClass);
                $(element).parent().addClass('liError');
            },
            unhighlight: function (element, errorClass, validClass)
            {
                $(element).removeClass(errorClass).addClass(validClass);
                $(element).parent().removeClass('liError');
            },
            errorPlacement: function (error, element)
            {
            }
        });
    },

    haitiPopUp: function ()
    {
        $("#haitiOverlay").overlay({ api: true, top: 'center' }).load();
    }

} // MAINLIB
