var IZI = {}

IZI.toolbox = {
  hideSelects: function ()
  {
    var elements = document.getElements('select');
    elements.each( function ( el ) {
      if ( typeof(el) == 'object' )
        el.style.visibility = 'hidden';
    } );
  },
  showSelects: function ()
  {
    var elements = document.getElements('select');
    elements.each( function ( el ) {
      if ( typeof(el) == 'object' )
        el.style.visibility = '';
    } );
  },
  applyCufon: function ()
  {
    Cufon.set( 'fontFamily', 'headers' );
    Cufon.set( 'hover', true );
    Cufon.replace( $$('h1,h2,h3,h4') );
    Cufon.now();
  }
}

IZI.toolbox.URL = {
  initialized: false,
  listeners: new Array(),
  timer: null,
  previous: null,

  init: function ()
  {
    if ( this.initialized ) return;
    this.timer = setInterval( this.checkForChanges, 250 );
    this.initialized = true;
  },

  addListener: function ( callback )
  {
    if ( !this.initialized ) this.init();
    this.listeners.push( callback );
  },

  broadcastEvent: function ()
  {
    for( var i = 0 ; i < IZI.toolbox.URL.listeners.length ; i++ ) {
      IZI.toolbox.URL.listeners[i]( IZI.toolbox.URL.getVariables() );
    }
  },

  checkForChanges: function ()
  {
    if ( IZI.toolbox.URL.previous && IZI.toolbox.URL.previous != document.location.href ) {
      IZI.toolbox.URL.broadcastEvent();
    }
    IZI.toolbox.URL.previous = document.location.href
  },

  goTo: function ( href )
  {
    document.location.href = href
  },

  getVariables: function ()
  {
    var varString = new Array();
    var varParts = new Array();
    var keys = new Array();
    var values = new Array();
    var buffer = new Array();
    var query = document.location.href.split( '#' )[1];
    if ( !query ) return new Object();
    if ( query.length > 0 ) {
      varString = query.split( "&" );
      for ( var i = 0 ; i < varString.length ; i++ ) {
        varParts = varString[i].split( '=' );
        keys.push( varParts[0] );
        values.push( varParts[1] );
      }
      buffer = values.associate( keys );
      return buffer;
    }
    return new Object();
  },

  add: function ( variable, value )
  {
    var buffer = new Array();
    var variables = this.getVariables();
    variables[variable] = value;
    var url = document.location.href.split( '#' )[0];
    $each( variables, function( variable, key ) {
      buffer.push( key + '=' + escape( variable ) );
    } );
    this.goTo( url + '#' + buffer.join( '&' ) );
  },

  remove: function ( variable )
  {
    var buffer = new Array();
    var variables = this.getVariables();
    var url = document.location.href.split( '#' )[0];
    delete(variables[variable]);
    $each( variables, function( variable, key ) {
      buffer.push( key + '=' + variable );
    } );
    this.goTo( url + '#' + buffer.join( '&' ) );
  },

  goRoot: function ()
  {
    this.goTo( document.location.href.split( '#' )[0] );
  }
}

IZI.Ajax = {
  parseResponse: function ( response )
  {
    eval( "var value = " + response.text );
    return value;
  }
}

IZI.language = {
  setLanguage: function ( code )
  {
    var url = '/service/json/IZICore/setLanguage/'+ code +'.html';
    IZI.loader.show();
    var jSonRequest = new Request.JSON( {'url': url, onComplete: function( result ){
      if ( result.success ) {
        document.location.reload();
      }
    }}).send();
  }
}

IZI.admin = {}