
function CacheHandler( block_id )
{
	this.block_id = block_id ? block_id : 1;
	this.data = new Array('');
}

CacheHandler.prototype.add = function ( ele )
{ 	
 	
	if( false == is_array( this.data[ this.block_id ] ) )
	{     	
		this.data[this.block_id] = new Array();
		this.data[this.block_id].push( ele );
	}	
	else
	{
		this.data[this.block_id].push( ele );
	}
};

CacheHandler.prototype.save =  function()
{  
	var zipdata = this._zip( this.data );
	
	var expDays = 30;
	var exp = new Date();
	exp.setTime(exp.getTime() + (expDays*24*60*60*1000));	
	
	document.cookie="select_cmm=" + zipdata +"; expires=" + exp.toGMTString();
	
};

CacheHandler.prototype.load = function()
{ 
	var ret = document.cookie.match(/select_cmm=(.*?)\; __/); 
	if( ret )
	{
		this.data = this._unzip( ret[1] ) ;
		return this.data;
	} 
};

CacheHandler.prototype._zip = function( data )
{	
	var result = escape( serialize( data ) );
	return result;
};

CacheHandler.prototype._unzip = function ( data )
{
	var result = unserialize( unescape(data) );
	return result;
}

CacheHandler.prototype.clear = function()
{
	this.data[ this.block_id] = false;	
	cookie_name = 'select_cmm';
  	cookie_date = new Date ( );  // current date & time
  	cookie_date.setTime ( cookie_date.getTime() - 1 );
  	document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

function CheckHandler( block_id )
{
	this.block_id = block_id;
	this.cacher = new CacheHandler( block_id );
	this._checktors = '.selected';
	this._submitBtn = '#submitBtn';	
	this._selectAllBtn = '#selectAllBtn';		
	this._unselectAllBtn = '#unselectAllBtn';
	this._cancelBtn = '#cancelBtn';	
	this.custom_callbacks = {};
}

CheckHandler.prototype.update = function()
{		
		cacher = this.cacher;
		cacher.clear();
		$j( this._checktors ).each( function()			
			{
				if( true == $j(this).attr('checked') )
				{
					if( $j(this).attr('id') ) 
					{ 
						cacher.add( $j(this).attr('id') ); 
					} 
				}
			} 				
		);
		
		cacher.save();	
};

CheckHandler.prototype.getResult =  function( unzip )
{
	this.update();
	datas = this.cacher.load()
	
	if(! datas ) return ;
	
	array_shift(  datas );
	datas = this.cacher.load()

	converted  = new Array();
	
	for( e in datas )
	{ 
		for( c in datas[e])
		{
		converted.push(datas[e][c]);
		}
	}
	
	if(! unzip )
	{
		converted = serialize( converted );
	}
	return converted;
}

CheckHandler.prototype.isSelectAll = function()
{
	var selectors = $j(this._checktors).get();
	var is_selectall = true;
	for( i in selectors )
	{
		if( false == $j( selectors[i] ).attr('checked') )
		{
			is_selectall = false;
			break;
		}
	}
	return is_selectall;
};

CheckHandler.prototype.getChecked =  function()
{
		return this.getCheckedOfBlock( this.block_id );
};

CheckHandler.prototype.getCheckedOfBlock =  function( num )
{	
		ret =  this.cacher.load();
		if( ret )
		{
			return ret[num];
		}	
};

CheckHandler.prototype.draw =  function()
{		this._bind_event();	
		oSelf = this;
		$j(this._checktors).each( function()
			{
				thisId = $j(this).attr('id');
				if( in_array( thisId, oSelf.getChecked() ) )
				{
						$j(this).attr('checked', true);
				}
			});	

		if( this.isSelectAll() )
		{
			$j( this._selectAllBtn ).attr('checked', true);
		}
		else
		{
			$j( this._selectAllBtn ).attr('checked', false);		
		}			
};

CheckHandler.prototype.selectAll = function()
{
		$j(this._checktors).each( function() 
			{
				$j(this).attr('checked', true);
			});
};

CheckHandler.prototype.unselectAll = function()
{
		$j(this._checktors).each( function() 
			{
				$j(this).attr('checked', false);
			});
};

CheckHandler.prototype._bind_event = function()
{
	oSelf = this;
	$j( this._submitBtn ).click(
			function() { 
				oSelf.submit();
			}
		);
		
	$j( this._selectAllBtn ).click(
			function() {  
				if( oSelf.isSelectAll() ) 
				{ oSelf.unselectAll(); 	$j(this).attr('chekced', false); }
				else
				{ oSelf.selectAll(); $j(this).attr('chekced', true); }				
			}
		);

	$j( this._cancelBtn ).click( function() { 
		oSelf.cacher.clear();
		oSelf.unselectAll(); 	
		$j(oSelf._selectBtn).attr('chekced', false);
		
		if( oSelf.custom_callbacks['cancel'] )
		{
			oSelf.custom_callbacks['cancel']();		
		}
	});	

};

I_PageLoader = function( settings )
{
	this.target_id = settings['target_id'];
	this.runners = settings['runners'];
	this.content_waraper_id = settings['content_waraper_id'] ? settings['content_waraper_id'] : '';
	this.default_url = settings['default_url'] ? settings['default_url'] : '';	
}

I_PageLoader.prototype.load = function( url )
{
	var hash = url.match(/#((\w|\d)+)/);  	
	if( hash )
	{
	  	hash = hash ? hash[0] : '';	
  	}	

	var waraper_id = this.content_waraper_id ? ' ' + this.content_waraper_id : '';			
	$j(this.target_id).load( this.get_ajax_url( url ) + waraper_id , 
		function(){ }
		);	
}

I_PageLoader.prototype.run = function()
{  
	var self = this;
	var waraper_id = this.content_waraper_id ? ' ' + this.content_waraper_id : '';
	for( index in this.runners )
	{	
		this_runner = this.runners[index]; 
		this_runner.click( function() {
			runner_self = $j(this);
			runner_self.attr('tmp', $j(this).attr('href') ).attr('href', 'javascript:;');
			$j(self.target_id).
				load( self.get_ajax_url( runner_self.attr('tmp') ) + waraper_id, 
				function() { runner_self.attr('href', runner_self.attr('tmp') );	});		
			});
	}
	
	if( this.default_url )
	{ 
		this.load( this.default_url );
	}
}

I_PageLoader.prototype.get_ajax_url = function( url )
{
	var prefix_get_param = 'ajax=y';
	var join_chr = '?';
	var hash = url.match(/#((\w|\d)+)/);  	

	if( hash )
	{
	  	hash = hash ? hash[0] : '';	
		url = url.replace( hash, '');  	
  	}	

	// the url has get param.
	if( url.split('/').pop().match(/(\w.*?\.\w+)?\?{1}/ ) )
	{
		join_chr = '&';
	}
	
	var prefix = url + join_chr + prefix_get_param;
	return prefix;
}

I_RadioBottomHandler = function( settings )
{
	this.wraper_id = settings['warap_id'];
	this.setBottoms( settings['bottoms'] );
}

I_RadioBottomHandler.prototype.setBottoms = function( bottoms )
{
	this.bottoms = bottoms;
}

I_RadioBottomHandler.prototype.draw = function()
{
	var self = this;

	for( index in this.bottoms )
	{	
		var this_bottom = this.bottoms[index];

		this_bottom.bind('click', this_bottom.setDown);
		this_bottom.bind('click', function(){
			self.selected_bottom_id = $j(this).attr('id');
			self.update();
		} );		
	}
}

I_RadioBottomHandler.prototype.update = function()
{
	for( index in this.bottoms )
	{
		this_bottom = this.bottoms[index];
		if( this.selected_bottom_id != this_bottom.attr('id') )
		{ this_bottom.setUp() }
	}
}

/*
	Bottom Maker
*/
I_BottomMaker = function ( settings )
{
	this.warap_id = settings['warap_id'];
	//this.img_root = settings['img_root'];
	this.img_extstr = {};
	custom_img_extstr = settings['img_extstr'] ? settings['img_extstr'] : {} ;
	this.img_extstr['default'] = custom_img_extstr['default'] ? custom_img_extstr['default'] : '';
	this.img_extstr['down'] = custom_img_extstr['down'] ? custom_img_extstr['down'] : '_select';
	this.img_extstr['focus'] = custom_img_extstr['focus'] ? custom_img_extstr['focus'] : '_over';

	this._default_press_callback = false; 
} 

I_BottomMaker.prototype.create = function( bottom_ids)
{
	var ret = {};
	var bottoms = $j( this.warap_id );
	var self = this;	
	
	bottoms.each( function() {
		bottom = self._bind_handlers( $j(this) );
		bottom_id = $j(this).attr('id')
		ret[bottom_id] = bottom;	 
		} );		
		
	return ret;		
}

I_BottomMaker.prototype._bind_handlers = function( this_bottom )
{		
		var self = this;
		this_bottom.attr('selected', 'n');

		// bind status handlers 
		this_bottom.extend({
			custom_callbacks:{},
			setUp:function() 
				{ 
					self.swap_bottom_img( $j(this), 'default' ); 
					$j(this).attr('selected', 'n');					
					this_bottom._excute_custom_callback('up');
				},		
			setDown:function() 
				{
					self.swap_bottom_img( $j(this), 'down' );
					$j(this).attr('selected', 'y');
					this_bottom._excute_custom_callback('down');					 
				},
			setFocus:function() 
				{ 
					if( this_bottom.attr('selected') != 'y') 
					{ 
						self.swap_bottom_img( $j(this), 'focus' );
						this_bottom._excute_custom_callback('focus');						
					}  
				},
			setUnFocus:function() 
				{
				 	if( this_bottom.attr('selected') != 'y') 
				 	{ 
				 		self.swap_bottom_img( $j(this), 'default' );
						this_bottom._excute_custom_callback('unFocus');				 		 
					} 
				},
			_excute_custom_callback:function( callback_name )
			{
				callback = this.custom_callbacks[callback_name];
				if( callback ) callback();
			}				
			});		

		// bind focus/unfocus callback
		this_bottom.
		mouseover( this_bottom.setFocus ).
		mouseout( this_bottom.setUnFocus );

		// bind press callback
		if( this.default_press_callback )
		{
			this_bottom.click( this.default_press_callback );
		}
		return this_bottom;	
}

I_BottomMaker.prototype.setHandlers = function ( callback )
{
	this.settings['callbacks'][callback_name] = callback;
}

I_BottomMaker.prototype.get_img = function( bottom_wraper )
{
	return bottom_wraper.find('img:first');
}

I_BottomMaker.prototype.swap_bottom_img = function ( bottom_wraper, img_type )
{	
	var img = this.get_img( bottom_wraper );	
	var splited = img.attr('src').split('/');
		img_exts = splited.pop().split('.');
		img_ext = img_exts.pop();
		
		img_root = splited.join('/');
	

	var img_extprifix = this.img_extstr[img_type];
	var img_name = img_exts.pop();
	for( i in this.img_extstr)
	img_name = img_name.replace( this.img_extstr[i] ,'');
	img.attr('src', img_root + '/' + img_name + img_extprifix + '.' + img_ext );

}

