jiyiri.register_namespace('jiyiri.page.common');
/* jiyiri.require_once('jiyiri.helper.eventhelper.eventhelper'); */

( function() {

	/* Codes Start Here */

	var CardFlash = Class.create();
	CardFlash._instance = null;
	CardFlash.GetInstance = function()
	{
		if(null==CardFlash._instance)
		{
			CardFlash._instance = new CardFlash();
		}
		return CardFlash._instance;
	}
	CardFlash.prototype = {
			_items:new Array(),
			_timer:null,
			_roll_time:8,
			_current_roll_id:0,
			_container:null,
			_image_ctl:null,
			initialize:function()
			{
			},
			_set_items:function()
			{
				var arr = new Array();
				arr.push({
					id:1,
					picture:'/Public/v3/img/cards-sample/card-index-flash-bg6.jpg',
					url:__APP__ + '/Card/CardList/category_id/7?at=card_index_banner',
					text:'元旦主题贺卡'
				});
				arr.push({
					id:2,
					picture:'/Public/v3/img/cards-sample/card-index-flash-bg2.jpg',
					url:__APP__ + '/Card/CardList/category_id/2?at=card_index_banner',
					text:'送爱人'
				});
				arr.push({
					id:3,
					picture:'/Public/v3/img/cards-sample/card-index-flash-bg3.jpg',
					url:__APP__ + '/Card/CardList/category_id/3?at=card_index_banner',
					text:'送父母'
				});
				arr.push({
					id:4,
					picture:'/Public/v3/img/cards-sample/card-index-flash-bg4.jpg',
					url:__APP__ + '/Card/CardList/category_id/4?at=card_index_banner',
					text:'送朋友/同事/同学'
				});

				this._items = arr;
			},
			show:function(container,image_ctl)
			{
				this._container = container;
				this._image_ctl = image_ctl;
				this._set_items();
				this._render();
				this._switch_to_item(1);
				this._begin_roll();
			},
			on_label_mouseover:function(id)
			{
				this._switch_to_item(id);
				this._end_roll();
			},
			on_label_mouseout:function(id)
			{
				this._begin_roll();
			},
			_render:function()
			{
				var html = '';
				var items = this._items;
				for(var i=0;i<items.length;i++)
				{
					var item = items[i];
					this._generate_item_html(item);
				}
			},
			_generate_item_html:function(item)
			{
				var id = item.id;
				var picture = item.picture;
				var url = item.url;
				var text = item.text;

				var li = document.createElement('li');
				li.id = 'card_flash_' + id;
				li.onmouseover = jiyiri.helper.eventhelper.EventHelper.create_event_function(this,'on_label_mouseover',id);
				li.onmouseout = jiyiri.helper.eventhelper.EventHelper.create_event_function(this,'on_label_mouseout',id);
				var a = document.createElement('a');
				a.href='javascript:void(0);return true;';
				a.innerHTML = text;
				li.appendChild(a);
				this._container.appendChild(li);
			},
			_switch_to_item:function(id)
			{
				var item = this._get_item(id);
				var olds = this._container.getElementsByClassName('current');
				for(var i=0;i<olds.length;i++)
				{
					Element.removeClassName(olds[i],'current');
				}
				Element.addClassName('card_flash_'+id,'current');
				this._image_ctl.src = item.picture;
				this._image_ctl.onclick = function()
				{
					location.href=item.url;
				}
			},
			_get_item:function(id)
			{
				var items = this._items;
				for(var i=0;i<items.length;i++)
				{
					if(items[i].id==id)
					{
						return items[i];
					}
				}
				return null;
			},
			_begin_roll:function()
			{
				this._timer = setInterval("jiyiri.page.common.CardFlash.GetInstance()._roll_proc()"
						,this._roll_time*1000);
			},
			_end_roll:function()
			{
				clearInterval(this._timer);
			},
			_roll_proc:function()
			{
				var next_id = this._current_roll_id+1;
				var max_id = this._items.length;
				if(next_id>max_id)
				{
					next_id = 1;
				}
				this._current_roll_id = next_id;
				this._switch_to_item(next_id);
			}
	}


	/* Codes End Here */

	/* Register Start Here */
	jiyiri.page.common.CardFlash = CardFlash;
	/* Register End Here */
})();
