function HeadCarousel()
{
    this.counter = 1;
    this.max = 5;

    var _this = this;

    this.roll = function()
    {
    	$('#head-carousel').find('.img:nth-child(' + this.counter + ')').fadeOut('slow');
    	if (this.counter == this.max) {
    		this.counter = 1;
    	} else {
    		this.counter++;
    	}
    	$('#head-carousel').find('.img:nth-child(' + this.counter + ')').fadeIn('slow');
        setTimeout(function() { _this.roll(); }, 3000);
    };

    setTimeout(function() { _this.roll(); }, 3000);
}

var Mask = {

    show: function()
    {
        $('#mask').css({'width': $(window).width(), 'height': $(document).height()});
        $('#mask').fadeTo('slow', 0.6);
    },
    hide: function()
    {
    	$('#mask').fadeOut('fast'); 
    }
};

var Utils = {

    getPageScroll: function ()
    {
        var xScroll, yScroll;
        if (self.pageYOffset)
        {   // Some browser
            yScroll = self.pageYOffset;
            xScroll = self.pageXOffset;
        } else if (document.documentElement && document.documentElement.scrollTop)
        {   // Explorer 6 Strict
            yScroll = document.documentElement.scrollTop;
            xScroll = document.documentElement.scrollLeft;
        } else if (document.body)
        {   // All other browsers
            yScroll = document.body.scrollTop;
            xScroll = document.body.scrollLeft; 
        }
        var arrayPageScroll = {'xScroll':xScroll,'yScroll':yScroll};
        return arrayPageScroll;
    }
};

var Dialog =
{
    init: function()
    {
        if (!$('.ModalDialog').length) {
            return;
        }
        Mask.show();
        $('.ModalDialog').each(function(in_index, in_element) {
            in_element = $(in_element);
            in_element.appendTo($('body'));
            var sc = Utils.getPageScroll();
            in_element.css('top', sc.yScroll + $(window).height() / 2 - in_element.height() / 2);
            in_element.css('left', sc.xScroll + $(window).width() / 2 - in_element.width() / 2);
            in_element.fadeIn('slow');
        });
    },
    close: function(in_child)
    {
        $('body').find('.ModalDialog').fadeOut('slow', function () {
        	$('body').find('.ModalDialog').remove();
        });
        Mask.hide();
    }
}

$(document).ready(function()
{
    Dialog.init();
	new HeadCarousel();
});
