function PageManager() {
    
    function initialize() {
        window.onload = function() { pageManager.onLoad(); };
        window.onresize = function() { pageManager.onResize(); };
    } 
    
    function onLoad() {
        this.adjustScrolls(1);
        
        for(var i=0; i<this.pageScrolls.length; i++) {
            /*if( this.pageScrolls[i].autoScrollInterval > 0 ) {
                setInterval('pageManager.autoScroll(' + i + ')', this.pageScrolls[i].autoScrollInterval);
            }*/
		setInterval('pageManager.autoScroll(' + i + ')', 5000);
        }
    }
    function onResize() {
        this.adjustScrolls(2);
    }
    
    function registerScroll(scrollId, rollerId, btnPrevId, btnNextId, itemWidth, autoScrollInterval) {         
        var scroll = document.getElementById(scrollId);
        var roller = document.getElementById(rollerId);
        var btnPrev = document.getElementById(btnPrevId);
        var btnNext = document.getElementById(btnNextId);
        if( scroll && roller && btnPrev && btnNext) {

            var id = this.pageScrolls.push({ scrollId: scrollId, scroll: scroll, rollerId: rollerId, roller: roller, btnPrevId: btnPrevId, btnNextId: btnNextId, itemWidth: itemWidth, autoScrollInterval: autoScrollInterval }) - 1;
            scroll.parentNode.onmouseover = new Function("e", "pageManager.mouseOverScroll(" + id + ",e)");
            scroll.parentNode.onmouseout = new Function("e", "pageManager.mouseOutOfScroll(" + id + ",e)");
            btnPrev.onclick = new Function("pageManager.moveScroll(" + id + ",'-')");
            btnNext.onclick = new Function("pageManager.moveScroll(" + id + ",'+')");
        }
    }
    
    function adjustScrolls() {
        for(var i=0; i<this.pageScrolls.length; i++) {
            var itemWidth = this.pageScrolls[i].itemWidth;
            var columnWidth = this.pageScrolls[i].scroll.parentNode.offsetWidth;

            this.pageScrolls[i].allItemsWidth = this.pageScrolls[i].roller.getElementsByTagName('li').length * itemWidth;
            this.pageScrolls[i].rollerWidth = columnWidth - columnWidth % itemWidth;
            this.pageScrolls[i].rollerWidth = this.pageScrolls[i].rollerWidth > this.pageScrolls[i].allItemsWidth ? this.pageScrolls[i].allItemsWidth : this.pageScrolls[i].rollerWidth;
            this.pageScrolls[i].roller.style.width = this.pageScrolls[i].allItemsWidth + 'px';         
            this.pageScrolls[i].scroll.style.width = this.pageScrolls[i].rollerWidth + 'px';         
            this.pageScrolls[i].requiresScrilling = (columnWidth >= this.pageScrolls[i].allItemsWidth ? false : true);
        }
    }
    
    function moveScroll(scrollId,dir) {
        //this.pageScrolls[scrollId].rollerWidth
	$('#' + this.pageScrolls[scrollId].scrollId ).scrollTo(dir + '=' + 620 + 'px', { axis: 'x', duration: '4700' });
    }
    
    function autoScroll(scrollId) {
        var dir = this.pageScrolls[scrollId].autoScrollDirection;
        if( dir != "+" && dir != "-") {
            dir = this.pageScrolls[scrollId].autoScrollDirection = "+";
        }   
	//this.pageScrolls[scrollId].rollerWidth     
        $('#' + this.pageScrolls[scrollId].scrollId ).scrollTo(dir + '=' + 620 + 'px', { axis: 'x', duration: '4700', onAfter: new Function('pageManager.autoScrollUpdate(' + scrollId + ')') });
    }
    
    function autoScrollUpdate(scrollId) {
        //alert('Ala ma kota');
        var dir = this.pageScrolls[scrollId].autoScrollDirection;
        var scrollOffset = this.pageScrolls[scrollId].scroll.scrollLeft;
        var scrollMax = this.pageScrolls[scrollId].allItemsWidth - this.pageScrolls[scrollId].rollerWidth;
        if( scrollOffset >= scrollMax && dir == '+')
            this.pageScrolls[scrollId].autoScrollDirection = '-';
        if( scrollOffset == 0 && dir == '-')
            this.pageScrolls[scrollId].autoScrollDirection = '+';  
    }

    function mouseOverScroll(scrollId, e) {
        //Sprawdzam czy kursor nie pochodzi do potomka 
        if (!e) var e = window.event;
        var relTarg = e.relatedTarget || e.fromElement;

        while (relTarg != null) {
            if (relTarg == this.pageScrolls[scrollId].scroll.parentNode)
                return;
            relTarg = relTarg.parentNode;
        }

        //Wykonuje kod zdarzenia
        if( this.pageScrolls[scrollId].requiresScrilling == true)
        {
            $('#' + this.pageScrolls[scrollId].btnPrevId).fadeIn("slow");
            $('#' + this.pageScrolls[scrollId].btnNextId).fadeIn("slow");
        }
    }

    function mouseOutOfScroll(scrollId,e) {
        //Sprawdzam czy kursor pognał do potomka 
        if (!e) var e = window.event;
        var relTarg = e.relatedTarget || e.toElement;
        
        while (relTarg != null) {
            if (relTarg == this.pageScrolls[scrollId].scroll.parentNode)
                return;
            relTarg = relTarg.parentNode;
        }

        //Wykonuje kod zdarzenia
        if( this.pageScrolls[scrollId].requiresScrilling == true)
        {
            $('#' + this.pageScrolls[scrollId].btnNextId).fadeOut("slow");
            $('#' + this.pageScrolls[scrollId].btnPrevId).fadeOut("slow");
        }
    }
    
    ///
    /// Tabs
    ///
    function registerTab(tabs, panes, selected) {
        if( !selected )
            selected = ":first";
    
        //Nadaje unikale nazwy
	    var uniqueIdA = pageManager.tabsUniqueId;
	    var uniqueIdB = pageManager.tabsUniqueId;
		$(panes).each(function(i) { this.id = "tabDiv" + uniqueIdA; uniqueIdA++; });       
        $(tabs + ' li').each(function(i) { this.id = "tabLi" + uniqueIdB; uniqueIdB++; });            	
        pageManager.tabsUniqueId = uniqueIdA;
        	    
		$(panes).hide().filter(selected).show();		
	    $(tabs + ' li').click(new Function("pageManager.tabsSelectionHandler(this,'" + tabs + "','" + panes + "')"));
	    $(tabs + ' li').filter(selected).click();
    }
    
    function tabsSelectionHandler(source, tabs, panes) {
        var tabContent = "#tabDiv" + source.id.substring(5);
        $(panes).hide();
        $(tabContent).show();
        
        $(tabs + ' li').removeClass('selected');
        $("#" + source.id).addClass('selected');

        this.adjustScrolls();
    }
        
    this.onLoad = onLoad;
    this.onResize = onResize;
    
    this.registerScroll = registerScroll;
    this.adjustScrolls = adjustScrolls;
    this.moveScroll = moveScroll;
    this.autoScroll = autoScroll;
    this.autoScrollUpdate = autoScrollUpdate;
    this.mouseOverScroll = mouseOverScroll;
    this.mouseOutOfScroll = mouseOutOfScroll;
    
    this.registerTab = registerTab;
    this.tabsSelectionHandler = tabsSelectionHandler;
    this.tabsUniqueId = 1;
    
    this.pageScrolls = new Array();
    this.pageTabs = new Array();
    initialize();
}
var pageManager = new PageManager();
