var oBasketScroller = null;

// JavaScript Document
var Desiary = {

    fadeMenu : {

        oActive : null,
        init : function()
        {
            $('#MetaNav > li').hover(function()
            {
                if (Desiary.fadeMenu.oActive)
                {
                    Desiary.fadeMenu.showHideMenu(0);
                    Desiary.fadeMenu.oActive = null;
                }

                Desiary.fadeMenu.showHideMenu(1,this);
            }, function()
            {
                Desiary.fadeMenu.oActive = $(this);
                window.setTimeout( function(){ Desiary.fadeMenu.checkMenu(); }, 250);
            });
        },

        showHideMenu : function(bShow,oObj)
        {
            if (bShow == 1 && oObj)
            {
                $(oObj).find('ul').fadeIn();
            }
            else
            {
                Desiary.fadeMenu.oActive.find('ul').fadeOut('fast');
            }
        },

        checkMenu : function()
        {
            if (Desiary.fadeMenu.oActive)
            {
                Desiary.fadeMenu.showHideMenu(0);
            }
        }
    },

    catNavHome : function(blFade)
    {
        $('#HomeCatNav li[id*="HomeCatNav"]').hover(function()
        {
            $(this).addClass('hover');
            if (blFade)
            {
                $(this).find('ul').fadeIn('slow');
            }
            else
            {
                $(this).find('ul').slideDown('normal');
            }
        },function()
        {
            $(this).removeClass('hover');
            if (blFade)
            {
                $(this).find('ul').fadeOut('fast');
            }
            else
            {
                $(this).find('ul').slideUp('fast');
            }
        });
    },

    detailImgs : {

        init : function()
        {
            var aZoomElems = ['Product','Designer','Producer'];
            var aZoomImg = [];

            for (var i = 0; i < aZoomElems.length; i++)
            {
                var changeImg = function(sObj, oImg)
                {
                    $('#' + sObj + 'Slider li a').click(function()
                    {
                        oImg.attr('src',$(this).attr('href'));
                        var sNow = new Date();
                        var sTimestamp = 'TS' + sNow.getTime();
                        var oZoomImg = new Object();

                        MojoMagnify.makeMagnifiable(
                            document.getElementById( oImg.attr('id') ),
                            $(this).attr('rel')
                        );

                        var self = this;
                        oZoomImg[sTimestamp] = new Image();
                        oZoomImg[sTimestamp].src = $(self).attr('rel');
                        oZoomImg[sTimestamp].onload = function() {
                            MojoMagnify.makeMagnifiable(
                                document.getElementById( oImg.attr('id') ),
                                $(self).attr('rel')
                            );
                        };

                        $('#ZoomButton').attr('href',$(this).attr('rel'));
                        $('#ZoomButton').lightBox();

                        return false;
                    });
                };

                var oZoomImg = $('#' + aZoomElems[i] + 'Zoom');
                changeImg(aZoomElems[i], oZoomImg);

                if (document.getElementById( aZoomElems[i] + "Zoom" ))
                {
                    aZoomImg[i] = new Image();
                    aZoomImg[i].src = $( "#" + aZoomElems[i] + "Zoom").attr('longdesc');
                    aZoomImg[i].name = aZoomElems[i] + "Zoom";
                    aZoomImg[i].onload = function()
                    {
                        MojoMagnify.makeMagnifiable(
                            document.getElementById( this.name ),
                            $('#' + this.name).attr('longdesc')
                        );
                    }
                }
            }
        }
    },

    tabs : {

        carouselInit : {
            'ProductBox' : true,
            'DesignerBox' : false,
            'ProducerBox' : false
        },

        bDesignerBoxActivated : false,

        bProducerBoxActivated : false,

        init : function()
        {
            //$('#DesignerBox, #ProducerBox').css({'height':'1px','overflow':'hidden'});

            $('.tabs li').click(function()
            {
                var sHref = $(this).find('a').attr('href');
                var aHref = sHref.split('#');
                var sLink = aHref[1];
                Desiary.tabs.change(sLink,this);
                return false;
            });
        },

        change : function(box,tab)
        {
            $('.tab-box').hide();

            var oBoxToShow = $('#' + box);
            oBoxToShow.show();

            $('.tabs li').removeClass('active');
            $(tab).addClass('active');

            if (this.carouselInit[box] == false)
            {
                var iSize;
                if (box == 'DesignerBox') {
                    iSize = iDesiaryDesignerArticlesCnt;
                } else if (box == 'ProducerBox') {
                    iSize = iDesiaryManufacturerArticlesCnt;
                } else {
                    iSize = iDesiaryCrossSellCnt;
                }

                oBoxToShow.find('.carousel-v').jcarousel({vertical: "true",size: 6,scroll: 3 });
                oBoxToShow.find('.carousel-h').jcarousel({size: iSize,scroll: 3  });
                this.carouselInit[box] = true;
            }
            if (box == 'DesignerBox' && this.bDesignerBoxActivated == false) {
                $('#DesignerDescription').jScrollPane({dragMaxHeight: "40", scrollbarWidth: "5"});
                this.bDesignerBoxActivated = true;
            }
            if (box == 'ProducerBox' && this.bProducerBoxActivated == false) {
                $('#ProducerDescription').jScrollPane({dragMaxHeight: "40", scrollbarWidth: "5"});
                this.bProducerBoxActivated = true;
            }

        },

        hideOnLoad : function()
        {
            $('#DesignerBox, #ProducerBox').hide();
            $('#DesignerBox, #ProducerBox').css({'height':'auto','overflow':'visible'});
        }
    },

    carousel : {

        init : function(iCountV1,iCountV2,iCountV3,iCountH1,iCountH2,iCountH3)
        {
            $('#ContentArea #mycarousel').jcarousel({vertical: "true",size: iCountV1,scroll: 3 });
            $('#ContentArea #mycarousel3').jcarousel({vertical: "true",size: iCountV2,scroll: 3 });
            $('#ContentArea #mycarousel5').jcarousel({vertical: "true",size: iCountV3,scroll: 3 });

            if (iCountH1)
                $('#ContentArea #mycarousel2').jcarousel({size: iCountH1, scroll: 1 });
            if (iCountH2)
                $('#ContentArea #mycarousel4').jcarousel({size: iCountH2, scroll: 3 });
            if (iCountH3)
                $('#ContentArea #mycarousel6').jcarousel({size: iCountH3, scroll: 3 });
        }
    },

    miniBasket : {

        active : false,
        check : null,

        init : function()
        {
            $('#ServiceNav03').mouseover(function()
            {
                var oLiObj = $(this);

                $('#MiniBasket').slideDown('normal', function(){
                    if (oBasketScroller && !oBasketScroller.scrollInit)
                    {
                        oBasketScroller.TMScrollInit();
                    }
                });

                oLiObj.addClass('hover');

                Desiary.miniBasket.active = true;
                Desiary.miniBasket.check = window.setInterval(function(){ Desiary.miniBasket.close(); },250);

                return false;
            });
            $('#ServiceNav03').mouseout(function(e)
            {
                Desiary.miniBasket.active = false;
            });
            $('#ServiceNav03').mousemove(function(e)
            {
                Desiary.miniBasket.active = true;
            });
        },

        showOnToBasket : function()
        {
            var oLiObj = $('#ServiceNav03');

            $('#MiniBasket').slideDown('normal');
            oLiObj.addClass('active');

            window.setTimeout(function()
            {
                $('#MiniBasket').slideUp('normal');
                oLiObj.removeClass('active');
            },3500);
        },

        close : function()
        {
            if (Desiary.miniBasket.active == false)
            {
                var oLiObj = $('#ServiceNav03');

                $('#MiniBasket').slideUp('normal');
                oLiObj.removeClass('hover');

                window.clearInterval(Desiary.miniBasket.check);
            }
        }

    },

    miniInfo : {

        active : false,
        check : null,

        init : function()
        {
            $('#ServiceNav02').mouseover(function()
            {
                var oLiObj = $(this);

                $('#MiniInfo').slideDown('normal');
                oLiObj.addClass('hover');

                Desiary.miniInfo.active = true;
                Desiary.miniInfo.check = window.setInterval(function(){ Desiary.miniInfo.close(); },250);

                return false;
            });
            $('#ServiceNav02').mouseout(function(e)
            {
                Desiary.miniInfo.active = false;
            });
            $('#ServiceNav02').mousemove(function(e)
            {
                Desiary.miniInfo.active = true;
            });
        },

        close : function()
        {
            if (Desiary.miniInfo.active == false)
            {
                var oLiObj = $('#ServiceNav02');

                $('#MiniInfo').slideUp('normal');
                oLiObj.removeClass('hover');

                window.clearInterval(Desiary.miniInfo.check);
            }
        }

    },

    miniAccount : {

        active : false,
        check : null,

        init : function()
        {
            $('#ServiceNav01').mouseover(function()
            {
                var oLiObj = $(this);

                $('#MiniAccount').slideDown('normal');
                oLiObj.addClass('hover');

                Desiary.miniAccount.active = true;
                Desiary.miniAccount.check = window.setInterval(function(){ Desiary.miniAccount.close(); },250);

                return false;
            });
            $('#ServiceNav01').mouseout(function(e)
            {
                Desiary.miniAccount.active = false;
            });
            $('#ServiceNav01').mousemove(function(e)
            {
                Desiary.miniAccount.active = true;
            });
        },

        close : function()
        {
            if (Desiary.miniAccount.active == false)
            {
                var oLiObj = $('#ServiceNav01');

                $('#MiniAccount').slideUp('normal');
                oLiObj.removeClass('hover');

                window.clearInterval(Desiary.miniAccount.check);
            }
        }

    },

    Stylefinder : {

        _sColorString : '',

        init : function()
        {
            // Color Select
            $('#colorSelect li').each(function(i,e) {
                $(this).click(function() {
                    if ( ! $(this).hasClass('checked')) {
                        $(this).addClass('checked');
                    }
                    else {
                        $(this).removeClass('checked');
                    }
                    Desiary.Stylefinder.collectColors();
                });

            });
            // Color Select all
            $('#ColorCheckAll').change(function() {
                if ($(this).val() == 'on') {
                    $('#colorSelect li').not('.checked').addClass('checked');
                    Desiary.Stylefinder.collectColors();
                }
            });

            // Color Select none
            $('#ColorUnCheckAll').change(function() {
                if ($(this).val() == 'on') {
                    $('#colorSelect li.checked').removeClass('checked');
                    Desiary.Stylefinder.collectColors();
                }
            });

            // Category Check All
            $('#CategoryCheckAll').change(function() {
                $('#rootCategorySelect input[type="checkbox"]').attr('checked', true);
            });

            // Category Check None
            $('#CategoryUnCheckAll').change(function() {
                $('#rootCategorySelect input[type="checkbox"]').attr('checked', false);
            });

            // Category summary status
            $('#rootCategorySelect input[type="checkbox"]').change(function() {
                if ( ! $(this).attr('checked')) {
                    $('#CategoryCheckAll').attr('checked',false);
                }
            });

            // Material Check All
            $('#MaterialCheckAll').change(function() {
                if($(this).attr('checked',true)) {
                    $('#materialSelect input[type="checkbox"]').attr('checked',true);
                }
            });

            // Material Check None
            $('#MaterialUnCheckAll').change(function() {
                if($(this).attr('checked',true)) {
                    $('#materialSelect input[type="checkbox"]').attr('checked',false);
                }
            });

            $('.sf-search-wrapper h4 a').click(function() {
                _oSelf = $(this);

                $('.sf-search-wrapper h4 a').each(function(i,e) {

                    if (_oSelf.attr('href') != $(this).attr('href')) {
                        $(this).removeClass('active');
                        $( $(this).attr('href') ).slideUp();
                    }

                });


                $($(this).attr('href')).slideToggle(function(){
                    if (_oSelf.hasClass('active'))
                        _oSelf.removeClass('active');
                    else
                        _oSelf.addClass('active');
                });
                return false;
            });
        },

        collectColors : function() {

            Desiary.Stylefinder._sColorString = '';

            $('#colorSelect li.checked').each(function(i,e) {

                Desiary.Stylefinder._sColorString += $(this).children('img').attr('rel') + ',';

            });

            $('#frmColors').val(Desiary.Stylefinder._sColorString);

            // RadioButtons update
            if($('#colorSelect li').not('.checked').length < 1) {
                $('#ColorCheckAll').attr('checked',true);
                $('#ColorUnCheckAll').attr('checked',false);
            }
            else {
                $('#ColorCheckAll').attr('checked',false);
            }
            //alert(this._sColorString);

        }
    }
}
