/**
 * Site
 * 
 * Az site object 
 *
 * @package    mivilagunk
 * @subpackage Site
 * @author     Szijártó Tamás ( szicsu ) <szicsu@jquery.hu>
 * @version    SVN: $Id: site.js 11 2010-04-26 10:14:05Z rokad $
 */

/**
 * Site object inicailázása
 */
var Site = {};


/**
 * Site.Init
 *
 * Azok a methodusok kerülnek ide, amik minden oldalbetöltésnél lefutnak.
 * Külső fájlből is bővíthető
 *
 */
Site.Init = new function(){
	var self = this;

	/**
     * ajaxSetup
     *
     * beállítja az alapértelemzett ajax paramétereket
	 * 
	 * @see http://docs.jquery.com/Ajax/jQuery.ajax#options
	 */
    this.ajaxSetup = function(){
        $.ajaxSetup({
            type: "GET",
            async: false,
            cache: true,
            dataType: 'html',
            timeout: 15000,
            complete: function(){

            },
            error:function (XMLHttpRequest, textStatus, errorThrown){
				str = "AjaxError("+ textStatus +")\n";
				alert( str );
            }
        });
    }

	/**
	 *
	 */
	this.cacheIgnoreInit = function(){
			
		if( Site.Helper.checkSfIgnoreCache() ){
			
			$('a').each(function(){
				this.href = Site.Helper.addSfIgnoreCacheString( this.href );
			});
		}	
	}
	
	/**
	 * style Init
	 */
	this.siteStyleInit = function(){
		Site.Style.init();
	}
	
	/**
	 * i18nInit
	 */
	this.i18nInit = function(){
		window.__= Site.I18N.get;
	}
	
	/**
	 * eventsInit
     * 
     * Body class alapján inicializáljuk az egyes eventeket
     */
    this.eventsInit = function(){
        events = $('body').attr('class').split( /\s/g );

        for( var i=0; i < events.length; i++ ){
            Site.Events.run( events[i] );
        }
    }

    /**
     * Oldal betöltéskor lefutó kód, ami lefutatj az egyes methodokat
     */
    $(document).ready(function(){
        for( var i in self){
            if( typeof self[i] == 'function' ){
                self[i]();
            }
        }
    });
}  

/**
 * Site.Events object
 *
 * Itt kapnak helyet az egyes evenetek, amiket futatni lehet az egész siten
 * Küldő fájlből is bővíthető az object
 */
Site.Events = new function(){

	
	
	
	
	/**
    * run function
    *
    * ezzel tudjunk lefutatni az az egyes methodusokat
    **/
    this.run = function( events ){
        for( var i in this){
            if( i == events && typeof this[i]  == 'function'  ){
                this[i]();
                break;
            }
        }
    }
}

/**
 * Site.Component object
 *
 * Itt kapnak helyet az componentek actiopjai amiket dinamikusan töltönk
 * Küldő fájlből is bővíthető az object
 */
Site.Component = new function(){
	var self = this;
	this.componentPath = 'site/component/';
	
	/**
	 * betöti a komponetst és be is röffenti :)
	 */
	this.run = function( methodName, args ){
		
		if( args === undefined ){
			args = new Array();
		}
		else if( !args instanceof Array  ){
			args = new Array( args );
		}
		
		
		$.require( this.componentPath + methodName + '.js' );
		try{
			if( this[methodName].require instanceof Object && this[methodName].require != null ){
				this.runInclude( this[methodName].require );
			}
			this[methodName].run.apply( {}, args );
		}
		catch( error ){
			alert( error.toSource() );
			//alert( error.toString() );
			//throw error;
		}

	}
	

	/**
     * Betölti a componetshez szükséges fájlokat
	 */	
	this.runInclude = function( data ){
	
		if( data.js && data.js instanceof Array && data.js.length > 0 ){
			$.require( data.js );
		}
		
		if( data.css && data.css instanceof Array && data.css.length > 0){
			$.requireCss( data.css );
		}
	
	} 

} 


/**
 *  Site.I18N
 *
 * Itt kapnak helyet a a js localizációja
 * @see /js/site/i18n/%lang%/message.js
 */
 Site.I18N = new function(){
	var self = this;
	
	this.data = {};
	
	
	this.get = function( key ){
		
		if( self.data[ key ] ){
			return self.data[ key ] ;
		}
		else{
			return key;
		}
	}
	
	this.set = function( key, val ){
		this.data[ key ] = val;
	}
	
}
