// JavaScript Document
String.prototype.trim = function(){
  return this.replace( /^\s*/, "" ).replace( /\s*$/, "" );
};

var jCore = jQuery.extend( {
  debug : true
}, {
  debug : false,
  defaultApplications : 'api',
  progress : 0,
  startProgress : function( quiet ){
    if (quiet)
      return;
    if (jCore.progress++ === 0){
      jQuery.event.trigger( "jCR" );
    }
  },
  endProgress : function( quiet ){
    if (quiet)
      return;
    if ( --jCore.progress === 0){
      jQuery.event.trigger( "jCF" );
    }
  },
  query : function( func, params ){
    if (func.match( /^(?!\.)[a-zA-Z_.:]+[a-zA-Z_]$/ )){
      var callback = func.split( '.' );
      switch (callback.length) {
        case 2:
          var call = {
            apps : jCore.defaultApplications,
            act : callback[0],
            func : callback[1]
          };
          break;
        case 3:
          var call = {
            apps : callback[0],
            act : callback[1],
            func : callback[2]
          };
          break;
        default:
          jCore.trigger( func );
          return;
      }
    }
    if (params instanceof jQuery){
      params = params.serialize();
    }
    return ( typeof call === 'undefined' ? func.trim() : '/index.php?' + jQuery.param( call )) + (params ? '&' + ( typeof (params) === 'string' ? params : jQuery.param( params )) : '');
  },
  start : function( func, params, cb, options ){
    params = params || {
      params : ''
    }
    if (params instanceof jQuery){
      params = params.serialize();
    }
    var extraParam = $( 'body[origin]' ).length ? ("&jCOT=" + $( 'body[origin]' ).attr( 'origin' ).split( '/' ).pop().trim()) : '';

    if ( !(query = jCore.query( func ))){
    
      jCore.trigger( func );
      return;
    }
    if (jQuery.isFunction( cb )){
      options = $.extend( {
        quiet : false
      }, options || {} );
      jCore.startProgress( options.quiet );
      if (options && options.fileElement && options.fileElement.length){
        var upload = function(){
          jQuery.ajaxFileUpload( jQuery.extend( {
            dataType : 'json',
            secureuri : false,
            type : "POST",
            url : query,
            data : ( typeof (params) === 'string' ? params : jQuery.param( params )) + extraParam,
            success : function( response ){
              jCore.endProgress( options.quiet );
              cb( response );
            },
            error : function( a, b, c ){
              jCore.endProgress( options.quiet );
            
              jCore.trigger( a.responseText );
            }
          }, options || {} ) );
        }
        if (jQuery.ajaxFileUpload && jQuery.isFunction( jQuery.ajaxFileUpload )){
          upload();
        }
        else{
          jQuery.getScript( '/shared/jquery/jquery.ajaxfileupload.js', upload );
        }
        return;
      }
      jQuery.ajax( jQuery.extend( {
        dataType : 'json',
        type : "POST",
        url : query,
        data : ( typeof (params) === 'string' ? params : jQuery.param( params )) + extraParam,
        success : function( response ){
          jCore.endProgress( options.quiet );
          cb( response );
        },
        error : function( a, b, c ){
          jCore.endProgress( options.quiet );
         
          jCore.trigger( a.responseText );
        }
      }, arguments[3] || {} ) );
    }
    else{
      return jCore.parse( jQuery.ajax( jQuery.extend( {
        type : "POST",
        url : query,
        data : typeof (params) === 'string' ? params : jQuery.param( params ),
        async : false,
        error : function( a ){
    	
          jCore.trigger( a.responseText );
        }
      }, arguments[2] || {} ) ).responseText );
    }
  },
  redirect : function( func, params ){
    var argsnum = arguments.length;
    window.setTimeout( function(){
      if (argsnum){
      if ( typeof func == 'number'){
        window.history.go( func );
        return;
      }
      if (func.match( /^[0-9a-z.:]{3,}$/i )){
        location.href = jCore.query( func, params );
      }
      else{
        location.href = func;
      }
    }
    else{
      location.reload();
    }
    }, 0 );
  },
  trigger : function( error ){
	
    if (jCore.debug){
      if ($( 'body' ).length){
        $( 'body' ).append( $( '<li></li>' ).text( error ) );
      }
      else{
        alert( error );
      }
    }
  },
  parse : function( responseText ){
    if ( typeof JSON === 'undefined'){
      jCore.trigger( 'not import JSON' );
      return false;
    }
    else{
      return JSON.parse( responseText );
    }
  }
} );