function Page(name)
{
    this.page       = null;
    this.action     = false;
    this.numPage	= 1;

    this.set_page(name);
}

Page.prototype.get_page = function(name, np)
{
    if (((name != this.page) || (np != this.numPage && name == this.page && np != undefined)) && name != 'undefined' && this.action == false)
    {
        var uri = '/ajax/' + name + '/',
            p = this;
        
        if (np)
        {
        	this.numPage = np;
        	uri = uri + this.numPage + '/';
        }
        
        this.action = true;
        //alert(uri);
        $.getJSON(uri, function(data) {
            //$('title').text(data.title);
            p.tp(data);
            p.menu(name);
        });
    }
}

Page.prototype.set_page = function(name)
{
    if (!name)
    {
        this.page   = 'home';
        return;
    }
    
    this.page   = name;
}

Page.prototype.tp = function(data)
{
    if (this.page == 'home')
        this.layoutHome(data);
    else
        this.layout(data);
}

Page.prototype.layoutHome = function(data)
{
    $('div.newsmodule').remove();
    $('div.leftmenu').animate({width:'235px'}, 500, 'linear', function(){
        $('body').removeClass().addClass(data.bodyClass);
        $('div.contentmodule').show().html(data.html).animate({opacity: '1'}, 500, 'linear');
    });
}

Page.prototype.layout = function(data)
{
    $('div.contentmodule').animate({opacity: '0'}, 500, 'linear', function(){
        $(this).text('').html(data.html).animate({opacity: '1'}, 500, 'linear');
        $('body').removeClass().addClass(data.bodyClass);
    });
}

Page.prototype.menu = function(name)
{
	if (name != this.page)
	{
		var oldId 	= '#id_menu_'+ this.page,
	    	newId 	= '#id_menu_'+ name,
	    	p 		= this;
	
	    if ($(oldId).length > 0)
	    	$(oldId).removeClass('active', 500);
	
		if ($(newId).length > 0)
	    	$(newId).addClass('active', 1000, function(){
		        p.action = false;
		    });
		else
			this.action = false;

	} else {this.action = false;}

    this.set_page(name);
}

Page.prototype.homeStart = function()
{
    $('div.leftmenu').animate({opacity: '1'}, 100, 'linear');
    $('div.intromovie').fadeOut(1000, function(){
        $('body').addClass('bg_05');
        $('div.headmodule').animate({opacity: '1'}, 1000, 'linear');
        $('div.topmenu').animate({opacity: '1'}, 1000, 'linear');
        $('div.leftmodules').animate({opacity: '1'}, 1000, 'linear');
        $('div.newsmodule').animate({opacity: '1'}, 1000, 'linear');
        $('div.footer').animate({opacity: '1'}, 1000, 'linear');
        $('.hide').removeClass('hide');
        $(this).remove();
    });
}

Page.prototype.toHome = function()
{
    var oldId = '#id_menu_'+ this.page;

    if(this.page == 'home')
        window.location = '/';

    if (this.page != 'home' && this.action == false)
    {
        this.action = true;
        var p = this;

        if ($(oldId).length > 0)
            $(oldId).removeClass('active', 500);

        $('div.contentmodule').animate({opacity: '0'}, 500, 'linear', function(){
            $('div.leftmenu').animate({width:'790px'}, 500, 'linear', function(){
                $.getJSON('/ajax/news/0/', function(data) {
                    //$('title').text('M-PROFI');
                    $('div.contentmodule').html('').hide();
                    $('body').removeClass().addClass(data.bodyClass);
                    $('div.content').append('<div class="newsmodule">'+data.html+'</div>');
                    $('div.contentmodule').animate({opacity: '1'}, 1000, 'linear', function(){
                        p.page = 'home';
                        p.action = false;
                    });
                });
            });
        });

        return false;
    }
}

jQuery.cookie = function (key, value, options) {
    if (arguments.length > 1 && (value === null || typeof value !== "object")) {
        options = jQuery.extend({}, options);

        if (value === null) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? String(value) : encodeURIComponent(String(value)),
            options.expires ? '; expires=' + options.expires.toUTCString() : '',
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '; domain=' + dm,
            options.secure ? '; secure' : ''
        ].join(''));
    }

    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};
