/**
 *  gslib.js  汎用クラス集
 *
 *  2006.05.05 KOBAYASHI Hajime++
 */


/**
 *  画像ロードクラス 
 *  Imageをロードし、ロードが完了した時点でコールバック関数を呼び出す
 *
 */
var GSImageLoader = Class.create();
GSImageLoader.prototype = 
{
  /**
   *  コンストラクタ
   *
   *  @param  url     ロードする画像のURL
   *  @param  onLoadEvent ロード完了時に呼び出すコールバック関数
   *  @param  eventArgs   コールバック関数の引数として渡される引数オブジェクト
   */
  initialize: function( url, onLoadEvent, eventArgs )
  {
    this.onLoadEvent = onLoadEvent;
    this.eventArgs = eventArgs ? eventArgs : null;

    this.image = new Image();
    this.image.src = url;
    if( this.image.complete )
    {
      this.onLoadImage();
    }
    else
    {
      this.checkLoading = function()
      {
        if( this.image.complete )
        {
          clearTimeout( this.timerId );
          this.onLoadImage();
          return;
        }
        this.timerId = setTimeout( this.checkLoading.bind( this ), 200 );
      }
      this.checkLoading();
    }
  },
  
  /**
   *  画像ロード完了時のイベント(コールバック関数を呼び出す)
   *
   */
  onLoadImage: function()
  {
    this.onLoadEvent( this.image, this.eventArgs );
  }
};

/**
 *  GSImageSizeScaler
 *  画像のサイズを縦横比を維持したまま、指定したサイズ内に収める
 *
 */
var GSImageSizeScaler = Class.create();
GSImageSizeScaler.prototype = 
{
  initialize: function( image, adjustWidth, adjustHeight )
  {

    if( ! image.complete )
    {
      return;
    }
    
    if( image.width <= adjustWidth && image.height <= adjustHeight )
    {
      this.width = image.width;
      this.height = image.height;
    }
    else
    {
      if( image.width > image.height && image.width > adjustWidth )
      {
        this.width = adjustWidth;
        this.height = parseInt( adjustWidth / image.width * image.height );
      }
      else
      {
        this.width = parseInt( adjustHeight / image.height * image.width );
        this.height = adjustHeight;
      }
    }
  }
};

/**
 *  GSTextList
 *  テキストファイルをロードし、各行をリスト(配列)の要素として保持する
 *
 */
GSTextList = Class.create();
GSTextList.prototype = 
{
  initialize: function( url, options )
  {
    this.url = url;
    this.items = new Array();
    this.start = options.start || 0;
    this.num = options.num || null;
    this.onLoad = options.onLoad || function(){};
    this.onFailure = options.onFailure || function(){};
    this.callbackArgs = options.callbackArgs || null;
    this.load();
  },
  
  addItem: function( text )
  {
    if( text != '' )
    {
      this.items.push( text );
    }
  },

  load: function()
  {
    this.complete = false;
    new Ajax.Request( this.url, { 
    	method: 'get', 
    	asynchronous: true, 
    	parameters: '', 
    	onSuccess: this.createList.bind( this ),
    	onFailure: this.onFailure,
    	onException: this.onFailure,
    	requestHeaders: ['If-Modified-Since','01 Jan 1970 00:00:00 GMT']
   } );
  },

  createList: function( req )
  {
     this.items.clear();
    var listStr = new String( req.responseText );
    var list = listStr.split("\r").join('').split("\n");
    for( var i = 0; i < list.length; i++ )
    {
      if( i >= this.start )
      {
        this.addItem( list[i] );
      }
      if( this.num != null && this.items.length >= this.num )
      {
        break;
      }
    }
    this.complete = true;
    this.onLoad( this.items, this.callbackArgs );
  }
};

var GSHashChecker = new Class.create();
GSHashChecker.prototype = {
  initialize: function( interval, onChange ){
    this.lastHash = "\n";
    this.interval = interval;
    this.onChange = onChange;
    this.intervalId = setInterval( this.checkHash.bind( this ), interval );
  },
  
  checkHash: function()
  {
    if( this.lastHash != "\n" )
    {
      var currentHash = this.getCurrentHashValue();
      if( this.lastHash != currentHash )
      {
        this.lastHash = currentHash;
        this.onChange( currentHash );
      }
    }
  },
  
  setHash: function( aValue )
  {
    clearInterval( this.intervalId );
    location.hash = '#' + aValue;
    this.lastHash = aValue;
    this.intervalId = setInterval( this.checkHash.bind( this ), this.interval );
  },
  
  getCurrentHashValue: function()
  {
    return location.hash.replace( '#', '' );
  }
};

var GSPeriodicalEvent = Class.create();
GSPeriodicalEvent.prototype = {
  initialize: function( interval, options )
  {
    this.interval = interval;
    this.timerId = null;

    this.onEvent = options.onEvent || function(){ return false; };
    if( options.bootEvent == true )
    {
      this.onTimer();
    }
    else if( options.start == true )
    {
      this.start();
    }
  },

  onTimer: function()
  {
    if( this.onEvent() == true )
    {
      start();
    }
  },

  start: function()
  {
    this.timerId = setTimeout( this.onTimer.bind( this ), this.interval );
  },

  stop: function()
  {
    clearTimeout( this.timerId );
  }
};

function getParameterObject(){
	var params = location.search;
	if( params.length < 2 ){
		return {};
	}
	else {
		var result = {};
		params.substring( 1 ).split('&').each( function( item ){
			var param = item.split('=');
			if( result[param[0]] !== undefined ){
				typeof(result[param[0]]) == 'string' ? result[param[0]] = new Array( result[param[0]], ( param[1] || '' ) ) : result[param[0]].push( param[1] || '' );
			}
			else {
				result[param[0]] = param[1] || '';
			}
		} );
		return result;
	}
}

function getStyleSize( aValue, aUnit ){
	aUnit = aUnit || 'px';
	return aValue != 0 ? aValue + aUnit : 0;
}

