jiyiri.register_namespace('jiyiri.page.common');


( 
function() 
{

	var NavFlash = Class.create();
	
	
	NavFlash.prototype = {
			
			initialize:function()
			{
				/**
				 * flash 里边的pictures列表
				 * { id ,   	加入是内部自动生成 
				 * 	 picture ,  图片
				 *   url ,      点击时候进入的链接
				 *   text		显示的文本       
				 *  }
				 */
				this._pictures;
				
				/**
				 * 每隔多少秒到下一个间隔
				 */
				this._circle_rate = 4;
				
				/**
				 * 定时控制器
				 */
				this._timer;
				
				/**
				 * 是否进行定时显示
				 */
				this._is_circle = true;
				
				/**
				 * 导航li 的容器
				 */
				this._nav_container;
				
				/**
				 * 图片的显示区域
				 */
				this._image_ctl;
				
				/**
				 * 文字显示区域
				 */
				this._nav_container;
				
				/**
				 * 当前正在显示的picture
				 */
				this._current_id;
				
				/**
				 * 从第几个id 开始
				 */
				this._begin_id = 1;
				
				
				
				
			},
			config:function( nav_container , image_ctl , text_ctl , pictures ){
				this._nav_container = $( nav_container );
				this._image_ctl     = $( image_ctl );
				if(text_ctl){
					this._text_ctl		= $( text_ctl );
				}
				
				this._add_pictures(pictures);
			},
			run:function()
			{
				this._draw_nav_items();
				this.show( this._begin_id );
				if( this._is_circle ){
					this._begin_circle();
				}
			},
			show:function( id ){
				if( this._nav_container ){
					this._show_nav_item(id);
				}
				
				if( this._image_ctl  ){
					this._show_image( id );
				}
				
				if( this._text_ctl ){
					this._show_text( id );
				}
				
				this._current_id = id;
			},
			next:function(){
				var next_id = this._get_next_id( this._current_id );
				this.show( next_id );
			},
			before:function(){
				var before_id = this._get_before_id( this._current_id );
				this.show(before_id);
			},
			set_circle_rate:function( circle_rate ){
				this._circle_rate = circle_rate;
			},
			_get_before_id:function(id){
				var count = this._pictures.length;
				var before_id = id - 1;
				if( before_id <= 0 ){
					before_id = count;
				}
				return before_id;
			},
			_get_next_id:function( id ){
				var count = this._pictures.length;
				var next_id = id + 1;
				if( next_id > count ){
					next_id = 1;
				}
				return next_id;
			},
			_add_pictures:function( pictures ){
				var begin_id = 1;
				for( var i = 0 ; i < pictures.length ; i ++ ){
					pictures[i].id = begin_id;
					begin_id++;
				}
				this._pictures = pictures;
			},
			_show_nav_item:function(id){
				for( var i = 0 ; i < this._pictures.length ; i ++ ){
					if( this._pictures[i].id == id ){
						$( this._get_nav_dom_id(this._pictures[i].id) ).className = 'current';
					}else{
						$( this._get_nav_dom_id(this._pictures[i].id) ).className = '';
					}
				}
			},
			_show_image:function( id ){
				var picture = this._get_picture( id );
				this._image_ctl.src = picture.picture;
				
				/* 如果picture 设置的url 的话才加入 onclick事件 */
				if( picture.url ){
					this._image_ctl.onclick = function(){
						location.href = picture.url;
					}
				}
			},
			_show_text:function( id ){
				var picture = this._get_picture( id );
				this._text_ctl.innerHTML = picture.text;
			},
			_begin_circle:function(){
				this._timer = setInterval(jiyiri.helper.eventhelper.EventHelper.create_callback_function(this,'next'),this._circle_rate * 1000);
			},
			_end_circle:function(){
				clearInterval(this._timer);
			},
			_draw_nav_items:function(){
				for( var i=0 ; i <　this._pictures.length ; i++ ){
					var nav_li = this._draw_nav_item(this._pictures[i]);
					this._nav_container.appendChild(nav_li);
				}
			},
			_draw_nav_item:function( picture ){
				var li = document.createElement('li');
				var li_a = document.createElement( 'a' );
				li_a.innerHTML = picture.id;
				li_a.id		   = this._get_nav_dom_id( picture.id );
				li.appendChild(li_a);
				
				/* 加入事件 */
				li_a.onmouseover = jiyiri.helper.eventhelper.EventHelper.create_event_function(this,'_on_nav_item_mouseover',picture.id);
				li_a.onmouseout  = jiyiri.helper.eventhelper.EventHelper.create_event_function(this,'_on_nav_item_mouseout');
				
				return li;
			},
			_get_picture:function( id ){
				for( var i = 0 ; i < this._pictures.length ; i ++ ){
					if( this._pictures[i].id == id ){
						return this._pictures[i];
					}
				}
			},
			_get_nav_dom_id:function( id ){
				return 'nav_li_a_' + id;
			},
			_on_nav_item_mouseover:function(id)
			{
				this.show(id);
				
				if( this._is_circle ){
					this._end_circle();
				}
				
			},
			_on_nav_item_mouseout:function(id)
			{
				if( this._is_circle ){
					this._begin_circle();
				}
			}
			
	}


	/* Codes End Here */

	/* Register Start Here */
	jiyiri.page.common.NavFlash = NavFlash;
	/* Register End Here */
}
)
();
