/**
 * iXBT mobile site
 * Copyright (c) 1997-2011 iXBT.com
 * Date: 20/10/2011
 * @author Ruslan Shashkov
 * @version 1.0
 *
 * http://m.ixbt.com
 */

var core = {
    /**
     * Class options and data
     */
    options: {
        template : null,
        content_urls: [
            {
                rel: 'important_news',
                url: '/_include/hot.html'
            },
            {
                rel: 'software_news',
                url: '/_include/short_soft_m2.html'
            },
            {
                rel: 'hardware_news',
                url: '/_include/short_hard_m2.html'
            },
            {
                rel: 'fresh_articles',
                url: '/_include/art.html'
            },
            {
                rel: 'other_articles',
                url: '/_include/art2.html'
            }

        ]
    },

    /**
     * Set loading bar into selected container (erase all content inside, before loading)
     * @param target {object} jQuery selected DOM object.
     * */
    setContentLoading: function(target){
        var html = '<div class="loading_bar">Загрузка</div>';
        target.html(html);
    },

    /**
     * Unset loading bar from selected container
     * @param target {object} jQuery selected DOM object.
     * */
    unSetContentLoading: function(target){
        target.find('div.loading_bar').remove();
    },

    /**
     * Get content url by related identifier
     * @param rel {string} identifier of content url
     * */
    getContentUrl: function(rel){
        for(var i = 0, l = this.options.content_urls.length; i < l; i++){
            if(this.options.content_urls[i].rel == rel){
                return this.options.content_urls[i].url;
            };
        };

        return false;
    },

    /**
     * Load related section content if is is not loaded, and show it if not showd
     * @param rel {string} related content item (.items_container).
     * */
    showSectionContent: function(rel){
        if(rel.length > 0){
            var target = $('div.items_container[rel="'+rel+'"]');
            
            if(target.attr('showed') == '0'){
                $('a.header[rel="'+rel+'"]').addClass('active');
                target.attr('showed', '1').show();

                if(target.attr('loaded') == '0'){
                    this.setContentLoading(target);

                    $.ajax({
                        url: this.getContentUrl(rel),
                        type: 'GET',
                        success: function(data){
                            core.unSetContentLoading(target);
                            target.html(data);
                            target.attr('loaded', '1');
                            core.strollTo('a.header[rel="'+rel+'"]');

                            /* Content items openers */
                            $('div#content div.items_container div.item').unbind('click').bind('click', function(){
															if($(this).hasClass('noajax')){
																location.href = $(this).find('h2.item_header>a').attr('href');
															}else{
                                core.showItemContent($(this));
															}
                            });
                        }
                    });
                }else{
                    core.strollTo('a.header[rel="'+rel+'"]');
                };
            }else{
                $('a.header[rel="'+rel+'"]').removeClass('active');
                target.attr('showed', '0').hide();
            };
        };
    },

    /**
     * Load related content item if is is not loaded, and show it if not showd
     * @param object {object} jQuery selected DOM object
     * */
    showItemContent: function(object){
        var url = object.find('h2.item_header>a').attr('href');
				var rel = object.find('h2.item_header>a').attr('rel');
        var short = object.find('.item_short_content');
        var full = object.find('.item_full_content');
        var addition_html = '<div class="content_item_right_link">' +
                                '<a href="javascript:void(0)" class="content_item_close"></a>' +
                            '</div>' +
                            '<div class="content_item_right_link">' +
                                '<a href="'+rel+'">Полная версия</a>' +
                            '</div>';

        if(object.attr('showed') != '1'){
            if(object.attr('loaded') != '1'){
                this.setContentLoading(full);

                $.ajax({
                    url: url,
                    type: 'GET',
                    success: function(data){
                        core.unSetContentLoading(full);
                        short.hide();
                        full.html(data);
                        full.show();
                        full.prepend(addition_html);
                        object.attr('showed', '1');
                        object.attr('loaded', '1');
                        core.strollTo(object);
                        object.unbind('click');
                    }
                });
            }else{
                short.hide();
                full.show();
                object.attr('showed', '1');
                core.strollTo(object);
                object.unbind('click');
            };
        };
    },

    /**
     * Close showed content item
     * @param object {object} jQuery selected DOM object
     * */
    closeContentItem: function(object){
        var carrier = object.parents('div.item:first');

        carrier.find('span.item_short_content').show();
        carrier.find('span.item_full_content').hide();
        carrier.attr('showed', '0');

        /* Content items openers */
        carrier.bind('click', function(){
            core.showItemContent($(this));
        });
    },
    
    /**
     * Scrolls page to the top of given selector
     * @param selector {string} jQuery selector
     * */
    strollTo: function(selector){
        var to = parseInt($(selector).offset().top);

        $('html, body').animate({
            scrollTop: to
        }, 500, "swing");
    },

    /**
     * Bind page actions
     * */
    bindControls: function(){
        if(this.options.template != null){
            switch(core.options.template){
                /* Mainpage news actions */
                case 'main_news' : {
                    /* Headers clicks */
                    $('div#content a.header').live('click', function(){
                        core.showSectionContent($(this).attr('rel'));
                    });

                    /* Content items openers */
                    $('div#content div.items_container div.item').bind('click', function(){
											if(!$(this).hasClass('noajax')){
                        core.showItemContent($(this));
											}else{
												location.href= $(this).find('h2.item_header>a').attr('href');
											}
                    });

                    /* Remove link follw from h2 */
                    $('div#content div.items_container div.item h2.item_header>a').live('click', function(){
                        return false;
                    });

                    /* Close opened item */
                    $('a.content_item_close').live('click', function(){
                        core.closeContentItem($(this));
                    });
                }; break;

                /* Mainpage articles actions */
                case 'main_articles' : {
                    /* Headers clicks */
                    $('div#content a.header').live('click', function(){
                        core.showSectionContent($(this).attr('rel'));
                    });
										
                    $('div#content div.items_container div.item').bind('click', function(){
												location.href = $(this).find('h2.item_header>a').attr('href');
                    });
										
                }; break;
            };
        };

        /* Common actions */
        /* Scroll top */
        $('div#footer a#to_the_top').live('click', function(){
            core.strollTo('body');
        });
    },

    /**
     * Strartup the core
     */
    init: function(template){
        this.options.template = template;
        this.bindControls();
    }
};
