/*
* Copyright Truly Wedding
*
*
*/

// Global Truly namespace.
window.Truly = {};

/************************************
* General Functions
************************************/
Truly.getScroll = function ()
{
    var scrOfX = 0, scrOfY = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return {left:scrOfX, top:scrOfY};
}

/************************************
* Category Grid Actions
************************************/
Truly.volumeFormAction = function ()
{
    $('form.volume').submit(function (event) {
        $('div#products div.outer').addClass('loading');
        var sep = this.action.indexOf('?') == -1 ? '?' : '&';
        $('div#products').load(this.action + sep + 'volume=' + $(this).parent().find('input.volume').val(), function () {
            Truly.volumeFormAction();
            Truly.paginationAction();
        });

        event.preventDefault();
    });
}
Truly.paginationAction = function ()
{
    $('div.pages a, a.view-all').click(function (event) {
        $('div#products div.outer').addClass('loading');
        $('div#products').load(this.href, function () {
            Truly.paginationAction();
            Truly.volumeFormAction();
        });

        event.preventDefault();

        if ($(this).parent().parent().get(0).className.indexOf('pagination') !==-1)
        {
            window.location.href = '#';
        }
    });
}

/************************************
* Tooltips
************************************/
Truly.cleanTooltip = function ()
{
    $('div#tooltip').removeClass('above').removeClass('below');
    $('div#tooltip a.close').unbind('click');
}
Truly.setupTooltips = function (parent)
{
    $parent = $(parent);

    // CLICK: Click to reveal static tooltip
    $(parent+' .tooltip-click').click(function (event) {
        
        event.preventDefault();

        // If already open...
        if ($('div#tooltip').css('display') !== 'none')
        {
            $('div#tooltip').fadeOut();
            return;
        }

        $body = $(this).find('.tooltip-body');

        // Remove old stuff.
        Truly.cleanTooltip();

        // Body.
        $('div#tooltip span.body').html($body.html());

        // Close event.
        $('div#tooltip a.close').click(function (event) {

            $('div#tooltip').fadeOut();
            event.preventDefault();
        });

        // Position.
        $('div#tooltip').fadeIn();
        $('div#tooltip').css({top:($(this).offset().top - $('div#tooltip').height())+'px', left:($(this).offset().left - ($('div#tooltip').width() / 2) + ($(this).width() / 2))+'px'});

        // Head/Foot.
        if ($body.attr('className').indexOf('below') !== -1)
        {
            $('div#tooltip').addClass('below');
        }
        else if ($body.attr('className').indexOf('above') !== -1)
        {
            $('div#tooltip').addClass('above');
        }
        else if ($body.attr('className').indexOf('left') !== -1)
        {
            $('div#tooltip').addClass('left');
        }
        else if ($body.attr('className').indexOf('right') !== -1)
        {
            $('div#tooltip').addClass('right');
        }
    });

    // NOTICE: Automatic notice with delay
    if (document.getElementById('tooltip'))
    {
        Truly.showNoticeTooltips(parent);
    }
    else
    {
        $(document).ready(function () {
            Truly.showNoticeTooltips(parent);
        });
    }

    // HOVER: Hover to reveal moving tooltip
    $(parent+' .tooltip-hover').mouseover(function () {

        $body = $(this).find('.tooltip-body');

        // Remove old stuff.
        Truly.cleanTooltip();

        // Body.
        $('div#tooltip span.body').html($body.html());

        // Position.
        $('div#tooltip').show();

        // Head/Foot.
        if ($body.attr('className').indexOf('below') !== -1)
        {
            $('div#tooltip').addClass('below');
            this.classIsBelow = true;
        }
        else if ($body.attr('className').indexOf('above') !== -1)
        {
            $('div#tooltip').addClass('above');
            this.classIsAbove = true;
        }
        else if ($body.attr('className').indexOf('left') !== -1)
        {
            $('div#tooltip').addClass('left');
            this.classIsLeft = true;
        }
        else if ($body.attr('className').indexOf('right') !== -1)
        {
            $('div#tooltip').addClass('right');
            this.classIsRight = true;
        }
    }).mousemove(function (event) {

        /*if (this.classIsBelow)
        {
            var top = event.clientY + Truly.getScroll().top + 15;
            var left = event.clientX + Truly.getScroll().left - ($('div#tooltip').width() / 2);
        }
        else if (this.classIsAbove)
        {
            var top = event.clientY + Truly.getScroll().top - $('div#tooltip').height() - 5;
            var left = event.clientX + Truly.getScroll().left - ($('div#tooltip').width() / 2);
        }
        else if (this.classIsLeft)
        {
            var top = event.clientY + Truly.getScroll().top - ($('div#tooltip').height()/2);
            var left = event.clientX + Truly.getScroll().left - $('div#tooltip').width() - 5;
        }
        else if (this.classIsRight)
        {
            var top = event.clientY + Truly.getScroll().top - ($('div#tooltip').height()/2);
            var left = event.clientX + Truly.getScroll().left + ($('div#tooltip').width()/2) + 5;
        }*/

        if (this.classIsBelow)
        {
            var top = $(this).offset().top + $(this).height() + 5;
            var left = $(this).offset().left - ($('div#tooltip').width() / 2) + ($(this).width() / 2);
        }
        else if (this.classIsAbove)
        {
            var top = $(this).offset().top - $('div#tooltip').height();
            var left = $(this).offset().left - ($('div#tooltip').width() / 2) + ($(this).width() / 2);
        }
        else if (this.classIsLeft)
        {
            var top = $(this).offset().top + ($(this).height()/2) - ($('div#tooltip').height()/2);
            var left = $(this).offset().left - $('div#tooltip').width() - 5;
        }
        else if (this.classIsRight)
        {
            var top = $(this).offset().top + ($(this).height()/2) - ($('div#tooltip').height()/2);
            var left = $(this).offset().left + 5;
        }

        // Position it to the mouse
        $('div#tooltip').css({top: top+'px', left: left+'px'});

    }).mouseout(function () {

        $('div#tooltip').hide();
    });    
}
Truly.showNoticeTooltips = function (parent)
{
    $(parent+' .tooltip-notice').each(function () {

        $body = $(this).find('.tooltip-body');

        // Remove old stuff.
        Truly.cleanTooltip();

        // Body.
        $('div#tooltip span.body').html($body.html());

        // Automatic close
        setTimeout("$('div#tooltip').fadeOut()", 5000);

        // Show.
        $('div#tooltip').fadeIn();

        // Head/Foot Position.
        if ($body.attr('className').indexOf('below') !== -1)
        {
            $('div#tooltip').addClass('below');
            var top = $(this).offset().top + $(this).height() + 5;
            var left = $(this).offset().left - ($('div#tooltip').width() / 2) + ($(this).width() / 2);
        }
        else if ($body.attr('className').indexOf('above') !== -1)
        {
            $('div#tooltip').addClass('above');
            var top = $(this).offset().top - $('div#tooltip').height();
            var left = $(this).offset().left - ($('div#tooltip').width() / 2) + ($(this).width() / 2);
        }
        else if ($body.attr('className').indexOf('left') !== -1)
        {
            $('div#tooltip').addClass('left');
            var top = $(this).offset().top + ($(this).height()/2) - ($('div#tooltip').height()/2);
            var left = $(this).offset().left - $('div#tooltip').width() - 5;
        }
        else if ($body.attr('className').indexOf('right') !== -1)
        {
            $('div#tooltip').addClass('right');
            var top = $(this).offset().top + ($(this).height()/2) - ($('div#tooltip').height()/2);
            var left = $(this).offset().left + 5;
        }
        $('div#tooltip').css({top: top+'px', left: left+'px'});
    });
}

/************************************
* General Behaviors
************************************/
$(document).ready(function () {
    $('a.bookmark').click(function (event) {
        try {
            // Firefox/Mozilla
            if (window.sidebar)
            {
                window.sidebar.addPanel('Truly Wedding Favors', this.href, '');
            }
            // Opera/Other
            else if (window.opera && window.print)
            {
                var e = document.createElement('a');
                e.setAttribute('href', this.href);
                e.setAttribute('title', 'Truly Wedding Favors');
                e.setAttribute('rel', 'sidebar');
                e.click();
            }
            // IE
            else if (document.all)
            {
                window.external.AddFavorite(this.href, 'Truly Wedding Favors');
            }
        } catch (e) {}

        event.preventDefault();
    });

    $('a.sign-up').click(function (event) {
        
        window.location.href = this.href;
        document.getElementById('eTrulySignupName').focus();
        event.preventDefault();
    });

    $('a.print').click(function (event) {
        
        if (window.print)
        {
            window.print();
        }
        else
        {
            alert('It appears your browser does not support this feature.');
        }

        event.preventDefault();
    });
});