/* 
 * jQuery BackGround Manager plug-in
 */

(function($)
{

	$.fn.resizeBG = function(options)
    {
        var o = $.extend({}, $.fn.resizeBG.defaults, options);

		var winWidth = $(window).width();
        var winHeight = $(window).height();

        var nleft = 0;
        var ntop = 0;
        var nwidth = 0;
        var nheight = 0;

        function percent(value, whole)
        {
          return (100 * (value / whole));
        }

        function unpercent(percent, whole)
        {
          return ((percent * whole) / 100);
        }

        switch (o.mode)
        {
        case 'proportion':
            //if the window is a rectangle
            if (winWidth != winHeight)
            {
                if (percent(winWidth, o.bgWidth) < percent(winHeight, o.bgHeight))
                {
                    nwidth = winWidth;
                    nheight = unpercent(percent(nwidth, o.bgWidth), o.bgHeight);

                    ntop = (winHeight - nheight) / 2;
                }
                else if (percent(winWidth, o.bgWidth) > percent(winHeight, o.bgHeight))
                {
                    nheight = winHeight;
                    nwidth = unpercent(percent(nheight, o.bgHeight), o.bgWidth);

                    nleft = (winWidth - nwidth) / 2;
                }
                else
                {
                    nheight = winHeight;
                    nwidth = winWidth;
                }

            }
            else //if the window is a square
            {
                if (o.bgWidth > o.bgHeight)
                {
                    nwidth = winWidth;
                    nheight = unpercent(percent(nwidth, o.bgWidth), o.bgHeight);

                    ntop = (winHeight - nheight) / 2;
                }
                else if (o.bgHeight > o.bgWidth)
                {
                    nheight = winHeight;
                    nwidth = unpercent(percent(nheight, o.bgHeight), o.bgWidth);

                    nleft = (winWidth - nwidth) / 2;
                }
                else
                {
                    nheight = winHeight;
                    nwidth = winWidth;
                }
            }
            break;

        case 'stretch':
            nwidth = winWidth;
            nheight = winHeight;
            break;

        case 'crop':
            //if the window is a rectangle
            if (winWidth != winHeight)
            {
                if (percent(winWidth, o.bgWidth) > percent(winHeight, o.bgHeight))
                {
                    nwidth = winWidth;
                    nheight = unpercent(percent(nwidth, o.bgWidth), o.bgHeight);

                    ntop = (winHeight - nheight) / 2;
                }
                else if (percent(winWidth, o.bgWidth) < percent(winHeight, o.bgHeight))
                {
                    nheight = winHeight;
                    nwidth = unpercent(percent(nheight, o.bgHeight), o.bgWidth);

                    nleft = (winWidth - nwidth) / 2;
                }
                else
                {
                    nheight = winHeight;
                    nwidth = winWidth;
                }

            }
            else //if the window is a square
            {
                if (o.bgWidth < o.bgHeight)
                {
                    nwidth = winWidth;
                    nheight = unpercent(percent(nwidth, o.bgWidth), o.bgHeight);

                    ntop = (winHeight - nheight) / 2;
                }
                else if (o.bgHeight < o.bgWidth)
                {
                    nheight = winHeight;
                    nwidth = unpercent(percent(nheight, o.bgHeight), o.bgWidth);

                    nleft = (winWidth - nwidth) / 2;
                }
                else
                {
                    nheight = winHeight;
                    nwidth = winWidth;
                }

            }
            break;
        }

        $(this)
            .css('left', nleft+'px')
            .css('top', ntop+'px')
            .width(nwidth)
            .height(nheight);
		
	};

	// resizeGB plugin defaults
	$.fn.resizeBG.defaults = {
		bgWidth: 1920,  //[integer] the width of the background image
		bgHeight: 1080, //[integer] the height of the background image
        mode: 'crop'    //[string] 'proportion': The image is shrunken, but keeps window proportions
                        //         'stretch': The image is resized to window height and width
                        //         'crop': proportioned to the less side, then crop to fit the new window height and width
	};

    $.fn.centerVertically = function()
    {
        var $this = $(this);

        var oheight = $this.height(); //object height
        var wheight = $(window).height(); //window height
        var pos = '0px';

        if (wheight > oheight)
        {
            pos = ((wheight - oheight) / 2)+'px';
        }

        $this.css('position', 'absolute').css('top', pos);
    };

    $.fn.centerHorizontally = function()
    {
        var $this = $(this);

        var owidth = $this.width(); //object width
        var wwidth = $(window).width(); //window width
        var pos = '0px';

        if (wwidth > owidth)
        {
            pos = ((wwidth - owidth) / 2)+'px';
        }

        $this.css('position', 'absolute').css('left', pos);
    };

    $.fn.centerAll = function()
    {
        //&&&& $(this).centerVertically().centerHorizontally();
    };
    
    $.fn.chekForScroll = function(options)
    {
        var o = $.extend({}, $.fn.chekForScroll.defaults, options);

		var winWidth = $(window).width();
        var winHeight = $(window).height();

        var $container = $(this);
        var $bg = $('img.bg');

        var overflow = 'hidden';

        if (o.showScrollAtMin)
        {
            if ((winWidth <= o.scrollWidthDimension)
                || (winHeight <= o.scrollHeightDimension))
            {
                overflow = 'auto';

                var bgsrc = $bg.attr('src');
                $container.css('background-image', 'url('+bgsrc+')');
                $bg.hide();

                $('#band')
                    .css('left', '-'+$('#band-back').offset().left+'px')
                    .width(($(document).width() + 0)+'px');
            }
            else
            {
                $bg.show();
            }
        }

        $container.css('overflow', overflow);
    }

    // chekForScroll plugin defaults
	$.fn.chekForScroll.defaults = {
        showScrollAtMin: true, //[boolean] Show or not the scroll of the browser when gets min dimensions
        scrollWidthDimension: 1000, //[integer] the width at which the browser show scrolls
        scrollHeightDimension: 750  //[integer] the height at which the browser show scrolls
	};

})(jQuery);

