//Prototipish Class
var Class = {
	create : function() {
		return function() {
			this.initialize.apply(this, arguments);
		};
	}
};

//Carat namespace
var Carat = new Object();

//Carat module loading
Carat.loadModules = function(moduleList){
	$(document).ready(function(){
		jQuery.each(moduleList, function() {
			try {
				this();
			}
			catch(err) {
				this;
			}	
		})
	});
}

//Carat News pager class
Carat.newsPager = Class.create();
Carat.newsPager.prototype = {
    initialize: function() {
        this.itemsPerPage = 8;
        this.items = $('ul.news_list li');
        this.prevButton = $('div.pager a.prevPage');
        this.nextButton = $('div.pager a.nextPage');
        this.pagerTitle = $('div.pager a.pagerTitle');
        this.actualPage = 1;
        this.pageNum = Math.ceil(this.items.length / this.itemsPerPage);
        this.setEvents();
        this.buildPager();
    },
    setEvents: function() {
        objectReference = this;
        this.prevButton.bind('click', function(e) {
            objectReference.loadPage(false);
            e.preventDefault();
        });
        this.nextButton.bind('click', function(e) {
            objectReference.loadPage(true);
            e.preventDefault();
        });
        this.pagerTitle.bind('click', function(e) {
            objectReference.loadPage(true);
            e.preventDefault();
        });
    },
    buildPager: function() {
        if (this.pageNum > 1) {
            this.prevButton.show();
            this.nextButton.show();
            this.pagerTitle.show();
        }
    },
    turnButtons: function() {
        if (this.actualPage == this.pageNum) {
            this.nextButton.hide();
        }
        else {
            this.nextButton.show();
        } 
        
        if (this.actualPage > 1) {
            this.prevButton.show();
        }
        else {
            this.prevButton.hide();                
        }        
    },
    hideActive: function() {
        this.items.filter(function() {
            return $(this).css('display') != 'none';
        }).hide();
    },
    loadPage: function(direction) {
        if (direction) {
            this.hideActive();
            if (this.actualPage == this.pageNum - 1) {
                
                for(var i = this.actualPage * this.itemsPerPage + 1; i <= this.items.length; i++) {
                    $(this.items[i - 1]).show();
                }
            }
            else {
                if (this.actualPage == this.pageNum) {
                  this.actualPage=0;
                
                }
    
                for (var i = this.actualPage * this.itemsPerPage + 1; i <= (this.actualPage + 1) * this.itemsPerPage; i++) {
                    $(this.items[i - 1]).show();                
                }                
            }
            this.actualPage++;
            //this.turnButtons();
        }
        else {
            this.hideActive();
            if (this.actualPage == 1) {
                this.actualPage=this.pageNum+1;
                for(var i = this.items.length; i >= (this.pageNum) * this.itemsPerPage - this.itemsPerPage + 1; i--) {
                    $(this.items[i - 1]).show();
                }
            }
            else {
                for (var i = (this.actualPage - 1) * this.itemsPerPage; i >= (this.actualPage - 2) * this.itemsPerPage + 1; i--) {
      
                    $(this.items[i - 1]).show();                
                }                
            }
            this.actualPage--;
            //this.turnButtons();
        }
    }
}

//Carat News pager instantiation
Carat.newsPagerObject = function() {
    var pager = new Carat.newsPager();
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date(); //get the actual date
		date.setTime(date.getTime()+(days*24*60*60*1000)); // calculates the date when cookie should expire
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}


$(document).ready(function(){
    $('.topMenu> li').mouseover(function(){
        $(this).find('ul').removeClass("hidden");
    });
    $('.topMenu> li').mouseout(function(){
        $(this).find('ul').addClass("hidden");
    });
	
	if (!readCookie('display')) {
		createCookie('display',true,30);
		var popup = document.getElementById('popupContainer');
		popup.style.display = "block";
		var closeButton = document.getElementById('close');
		closeButton.onclick = function() {
			popup.style.display = "none";
		}
	}
	
});
