/**
 * @author Wrzasq <wrzasq@gmail.com>
 * @copyright 2008 - 2010 (C) by Wrzasq
 * @package WrzasqCMF
 * @version 0.0.1
 */

// blog preview
var blog;

// switches list page
function getBlogPageSwitcher(page)
{
    return function(event) {
    	blog.pagination.page = page;

    	blog.reload();

        event.stop();
    };
}

// switches tag category
function getBlogTagSwitcher(tag)
{
	return function(event) {
		blog.pagination.page = 1;
		blog.tag = tag;
		
		blog.reload();
		
		event.stop();
	};
}

// initializes JavaScript interface
document.observe("dom:loaded", function() {
	blog = $("blog");

    // pagination
	$(blog.parentNode).getElementsBySelector(".pagination").each( function(element) {
		blog.pagination = element;
    } );

    // if not exists create new node
    if(!blog.pagination)
    {
    	blog.pagination = new Element("div", {
    		"class": "pagination"
    	} );
        $(blog.parentNode).insert(blog.pagination);
    }

    blog.pagination.page = 1;

    // checks current page
    blog.pagination.getElementsBySelector("span").each( function(element) {
    	$A(element.childNodes).each( function(child) {
    		if(child.nodeType == 3)
    		{
    			blog.pagination.page = child.nodeValue;
    		}
    	} );
    } );

    // pager
    blog.pagination.getElementsBySelector("a").each( function(element) {
    	var params = element.href.toQueryParams();
    	element.observe("click", getBlogPageSwitcher(params.page) );
    	
    	if(!blog.tag && params.tag)
    	{
    		blog.tag = params.tag;
    	}
    } );

    // tag links
    blog.getElementsBySelector("a[rel=tag]").each( function(element) {
    	element.observe("click", getBlogTagSwitcher( element.href.toQueryParams().tag ) );
    } );

    // fetches list
    blog.reload = function() {
    	var content = {
    		action: "Blog",
    		page: this.pagination.page
    	};
    	
    	// adds tag flter
    	if(this.tag)
    	{
    		content.tag = this.tag;
    	}
    	
		new Ajax.Request("/ajax.php", {
			parameters: content,
			onSuccess: function(transport) {
				var list = transport.responseJSON.Blog.data;
				
		        // cleans list
				this.update();

				var site;
				var element;
				var content;

		        // adds blog notes
		        for(var id in list.sites)
		        {
		    		site = list.sites[id];
		    		
		    		if( typeof(site) == "function")
		    		{
		    			continue;
		    		}
		    		
		    		content = new Element("p");
		    		
		    		// thumbnail
		    		if(site.image && site.image.length > 0)
		    		{
		    			content.update(  new Element("a", {
			    			href: "/index.php?action=DisplaySite&id=" + id,
			    			title: site.name
			    		} ).update( new Element("img", {
		    				src: site.image,
		    				alt: site.name
		    			} ) ) );
		    		}

		    		this.insert( new Element("h2").update( new Element("a", {
		    			href: "/index.php?action=DisplaySite&id=" + id,
		    			title: site.name
		    		} ).update(site.name) ) ).insert( new Element("h3").update(site.dateTime) ).insert( content.insert(site.content + "… ").insert( new Element("a", {
		    			href: "/index.php?action=DisplaySite&id=" + id,
		    			title: site.name
		    		} ).update("więcej.") ) ).insert( new Element("hr") );
		        }
		        
		        // clears old pager content
		        this.pagination.update();

		        // checks if paginaton should be added
		        if(list.pages.count > 1)
		        {
		            this.pagination.paginate( parseInt(list.pages.count), parseInt(list.pages.current), list.pages.link, getBlogPageSwitcher);
		        }
			}.bindAsEventListener(this)
		} );
    };
} );

