jiyiri.register_namespace('jiyiri.page.promoshoppingcart');

jiyiri.require_once('jiyiri.page.promoshoppingcart.shoppingcartapi');

( 
function() 
{
	/**
	 * ShoppingCart 的工厂方法
	 */
	var ShoppingCart_Tiny_Factory = Class.create();
	ShoppingCart_Tiny_Factory._instance = null;
	ShoppingCart_Tiny_Factory.GetInstance = function() {
		if (null == ShoppingCart_Tiny_Factory._instance) {
			var is_ie = jiyiri.helper.browser.BrowserType.is('ie',6);
			if( is_ie ){
				ShoppingCart_Tiny_Factory._instance = new ShoppingCart_Tiny_IE();
			}else{
				ShoppingCart_Tiny_Factory._instance = new ShoppingCart_Tiny_W3c();
			}
		}
		return ShoppingCart_Tiny_Factory._instance;
	};
	
	/**
	 * 下方弹出购物车控件
	 * 使用在ie 6 版本得浏览器上
	 */
	var ShoppingCart_Tiny_IE = Class.create();
//	ShoppingCart_Tiny_IE._insatance = null;
//	ShoppingCart_Tiny_IE.GetInstance = function() {
//		if (null == ShoppingCart_Tiny_IE._instance) {
//			ShoppingCart_Tiny_IE._instance = new ShoppingCart_Tiny_IE();
//		}
//		return ShoppingCart_Tiny_IE._instance;
//	};
	
	
	ShoppingCart_Tiny_IE.prototype = {
		
		initialize:function(){
			
			this._Shop_Cart_Div_Height;
			
			this._Shop_Cart_Div;
			
			this._Shop_Cart_Is_Show;
			
			
			
			this._add_event();
			
			this._Shop_Cart_Top_Height;  /* 一直保留的高度  如果隐藏状态的话  也可以看见这高度的Div */
			
			this._Already_Move = 0;  /* Div 已经移动的量 用户判断是否可以停止移动 */
			this._Move_Rate = 1;	/* 每隔多少毫秒移动 */
			this._Move_Step = 4;	/* 每次移动的距离 */
			
			/**
			 * ShopCart 的UI Manager
			 */
			this._Shop_Cart_Ui;
		
		},
		config:function( Shop_Cart_Div , Shop_Cart_Div_Height , Shop_Cart_Top_Height ){
			this._Shop_Cart_Div = $(Shop_Cart_Div);
			this._Shop_Cart_Div.style.position = "absolute";
			/* 设置Div 的Left值 */
			var left = (document.documentElement.clientWidth - this._Shop_Cart_Div.getWidth()) / 2;
			this._Shop_Cart_Div.style.left = left + "px";
			
			this._Shop_Cart_Div_Height = Shop_Cart_Div_Height;
			this._Shop_Cart_Top_Height = Shop_Cart_Top_Height;
		},
		init:function(){
			this._Shop_Cart_Is_Show = false;
			this._Shop_Cart_Div.style.height = this._Shop_Cart_Top_Height + "px";		//设置为0  防止下滚动条可以无限下滑
			this._Shop_Cart_Div.style.top = document.documentElement.clientHeight + document.documentElement.scrollTop - this._Shop_Cart_Top_Height + "px" ;
			this._Shop_Cart_Div.style.display = "none";
//			var current_top = this._Shop_Cart_Div.offsetTop;
//			this._Shop_Cart_Div.style.top = current_top - 10 + "px";
//			this._Shop_Cart_Div.style.height = this._Shop_Cart_Div.getHeight() + 11 + "px";
		},
		reload:function(){
			this._Shop_Cart_Div.contentWindow.location.reload();
		},
		show:function( is_show ){
			this._Shop_Cart_Div.style.display = "block";
			//this._Shop_Cart_Div.style.height = this._Shop_Cart_Top_Height + "px";
				
			if( is_show ){
				this._show();
			}else{
				this._hidden();
			}
		},
		/**
		 * 把Cart 的UIManager 注册进来
		 * 用于显示隐藏时候的回调
		 */
		regist_ui:function( ShopCartUi ){
			this._Shop_Cart_Ui = ShopCartUi;
		},
		_add_event:function(){
			
			/* 给window移动滚动条和改变窗口大小加上事件 */
			window.onresize = jiyiri.helper.eventhelper.EventHelper.create_callback_function(this,'_on_window_resize');
			window.onscroll = jiyiri.helper.eventhelper.EventHelper.create_callback_function(this,'_on_window_resize');
			
		},
		_on_window_resize:function(){
			if( this._Shop_Cart_Is_Show ){
				this._Shop_Cart_Div.style.top = document.documentElement.clientHeight + document.documentElement.scrollTop - this._Shop_Cart_Div_Height + "px" ;
			}else{
				this._Shop_Cart_Div.style.top = document.documentElement.clientHeight + document.documentElement.scrollTop - this._Shop_Cart_Top_Height + "px" ;
			}
		},
		_show:function(){
			
			if( this._Shop_Cart_Is_Show ){
				return false;
			}
			this._move( 'up' , this._Move_Step );
		},
		_hidden:function(){
			
			if( this._Shop_Cart_Is_Show ){
				this._move( 'down' , this._Move_Step );
			}
			
		},
		_move:function( direction , permove ){
			var current_top = this._Shop_Cart_Div.offsetTop;
			
			/* up 向上移动 否则向下动 */
			/* 改变位置 同时再慢慢增加高度  
			 * 防止过多得下拉条出来  超过可视范围 */
			if( direction == 'up' ){
				this._Shop_Cart_Div.style.top = current_top - permove + "px";
				this._Shop_Cart_Div.style.height = this._Shop_Cart_Div.getHeight() + permove + "px";
			}else{
				this._Shop_Cart_Div.style.top = current_top + permove + "px";
				this._Shop_Cart_Div.style.height = this._Shop_Cart_Div.getHeight() - permove + "px";
			}
			
			this._Already_Move += permove;
			
			/* 如果移动完毕则停止移动 否则设定的毫秒后继续移动 */
			if( this._Already_Move + this._Shop_Cart_Top_Height < this._Shop_Cart_Div_Height  ){
				setTimeout( jiyiri.helper.eventhelper.EventHelper.create_event_function(this,'_move' , direction , permove) , 
							this._Move_Rate );
			}else{
				this._after_move(direction);
				
			}
		},
		_after_move:function( direction ){
			this._Already_Move = 0;
			
			if( direction == 'up' ){
				this._Shop_Cart_Is_Show = true;
				this._Shop_Cart_Ui.on_show_cart();
			}else{
				this._Shop_Cart_Is_Show = false;
				this._Shop_Cart_Div.style.height = this._Shop_Cart_Top_Height + "px";  //设置为0  防止下滚动条可以无限下滑
				this._Shop_Cart_Ui.on_hidden_cart();
				//alert(this._Shop_Cart_Div.offsetTop);
			}
		}
	};
	
	/**
	 * 在w3c标准下的shoppingcart
	 * 使用position:fixed;
	 */
	var ShoppingCart_Tiny_W3c = Class.create();
	
	ShoppingCart_Tiny_W3c.prototype = {
			
			initialize:function(){
				
				this._Shop_Cart_Div_Height;
				
				this._Shop_Cart_Div;
				
				this._Shop_Cart_Is_Show;
				
				
				this._Shop_Cart_Top_Height;  /* 一直保留的高度  如果隐藏状态的话  也可以看见这高度的Div */
				
				this._Already_Move = 0;  /* Div 已经移动的量 用户判断是否可以停止移动 */
				this._Move_Rate = 1;	/* 每隔多少毫秒移动 */
				this._Move_Step = 4;	/* 每次移动的距离 */
			
			},
			config:function( Shop_Cart_Div , Shop_Cart_Div_Height , Shop_Cart_Top_Height ){
				this._Shop_Cart_Div = $(Shop_Cart_Div);
				this._Shop_Cart_Div.style.position = "fixed";
				
				/* 设置Div 的Left值 */
				var left = (document.documentElement.clientWidth - this._Shop_Cart_Div.getWidth()) / 2;
				this._Shop_Cart_Div.style.left = left + "px";
				
				this._Shop_Cart_Div_Height = Shop_Cart_Div_Height;
				this._Shop_Cart_Top_Height = Shop_Cart_Top_Height;
			},
			reload:function(){
				this._Shop_Cart_Div.contentWindow.location.reload();
			},
			/**
			 * 把Cart 的UIManager 注册进来
			 * 用于显示隐藏时候的回调
			 */
			regist_ui:function( ShopCartUi ){
				this._Shop_Cart_Ui = ShopCartUi;
			},
			init:function(){
				this._Shop_Cart_Is_Show = false;
				//this._Shop_Cart_Div.style.height = this._Shop_Cart_Top_Height + "px";		//设置为0  防止下滚动条可以无限下滑
				this._Shop_Cart_Div.style.top = document.documentElement.clientHeight  - this._Shop_Cart_Top_Height + "px" ;
				this._Shop_Cart_Div.style.display = "none";
//				var current_top = this._Shop_Cart_Div.offsetTop;
//				this._Shop_Cart_Div.style.top = current_top - 10 + "px";
//				this._Shop_Cart_Div.style.height = this._Shop_Cart_Div.getHeight() + 11 + "px";
			},
			show:function( is_show ){
				this._Shop_Cart_Div.style.display = "block";
				//this._Shop_Cart_Div.style.height = this._Shop_Cart_Top_Height + "px";
					
				if( is_show ){
					this._show();
				}else{
					this._hidden();
				}
			},
			_show:function(){
				if( this._Shop_Cart_Is_Show ){
					return false;
				}
				this._move( 'up' , this._Move_Step );
			},
			_hidden:function(){
				
				if( this._Shop_Cart_Is_Show ){
					this._move( 'down' , this._Move_Step );
				}
				
			},
			_move:function( direction , permove ){
				var current_top = this._Shop_Cart_Div.offsetTop;
				
				/* up 向上移动 否则向下动 */
				/* 改变位置 同时再慢慢增加高度  
				 * 防止过多得下拉条出来  超过可视范围 */
				if( direction == 'up' ){
					this._Shop_Cart_Div.style.top = current_top - permove + "px";
					//this._Shop_Cart_Div.style.height = this._Shop_Cart_Div.getHeight() + permove + "px";
				}else{
					this._Shop_Cart_Div.style.top = current_top + permove + "px";
					//this._Shop_Cart_Div.style.height = this._Shop_Cart_Div.getHeight() - permove + "px";
				}
				
				this._Already_Move += permove;
				
				/* 如果移动完毕则停止移动 否则设定的毫秒后继续移动 */
				if( this._Already_Move + this._Shop_Cart_Top_Height < this._Shop_Cart_Div_Height  ){
					setTimeout( jiyiri.helper.eventhelper.EventHelper.create_event_function(this,'_move' , direction , permove) , 
								this._Move_Rate );
				}else{
					this._after_move(direction);
					
				}
			},
			_after_move:function( direction ){
				this._Already_Move = 0;
				
				if( direction == 'up' ){
					this._Shop_Cart_Is_Show = true;
					this._Shop_Cart_Ui.on_show_cart();
				}else{
					this._Shop_Cart_Is_Show = false;
					this._Shop_Cart_Ui.on_hidden_cart();
					//this._Shop_Cart_Div.style.height = this._Shop_Cart_Top_Height + "px";  //设置为0  防止下滚动条可以无限下滑
					//alert(this._Shop_Cart_Div.offsetTop);
				}
			}
		};
	
	
	
	
	/**
	 * 购物车页面UI管理类
	 */
	var ShoppingCart_Tiny_UI = Class.create();
	ShoppingCart_Tiny_UI._instance = null;
	ShoppingCart_Tiny_UI.GetInstance = function() {
		if (null == ShoppingCart_Tiny_UI._instance) {
			ShoppingCart_Tiny_UI._instance = new ShoppingCart_Tiny_UI();
		}
		return ShoppingCart_Tiny_UI._instance;
	};
	
	ShoppingCart_Tiny_UI.prototype = {
			
		initialize:function(){
			
			/**
			 *   购物车div
			 */
			this._Cart_Div;
			this._Cart_Li;   /* 切换按钮 */
			
			/**
			 *  促销优惠Div
			 */
			this._Promo_Div;
			this._Promo_Li;
			
			/**
			 *   配送div
			 */
			this._Dispatch_Div;
			this._Dispatch_Li
			
			/**
			 * 配送Div
			 */
			this._PayStyle_Div;
			this._PayStyle_Li;
			
			/**
			 * 售后服务Div
			 */
			this._Service_Div;
			this._Service_Li;
			
			/**
			 * 显示和隐藏的Button
			 */
			this._Show_Button;
			this._Hidden_Button;
			
			
			//todo 检测Parent  
			//必须是在Iframe 中被打开
			
			/**
			 * 注册到父级容器里边
			 */
			this._regist();
			
		},
		config:function( Cart_Li , Cart_Div , Promo_Li , Promo_Div , Dispatch_Li , Dispatch_Div , PayStyle_Li , PayStyle_Div , Service_Li , Service_Div ,
						 Show_Button , Hidden_Button){
			
			this._Cart_Div = $( Cart_Div );
			this._Cart_Li = $( Cart_Li );
			
			this._Promo_Li = $( Promo_Li );
			this._Promo_Div = $( Promo_Div );
			
			this._Dispatch_Div = $( Dispatch_Div );
			this._Dispatch_Li = $( Dispatch_Li );

			this._PayStyle_Div = $( PayStyle_Div );
			this._PayStyle_Li = $( PayStyle_Li );
			
			this._Service_Div = $( Service_Div );
			this._Service_Li = $( Service_Li );
			
			this._Show_Button = $(Show_Button);
			this._Hidden_Button = $( Hidden_Button );
			
			this._add_click_li_event();
			
		},
		/**
		 * 当显示产生的回调
		 */
		on_show_cart:function(){
			this._Show_Button.style.display = 'none';
			this._Hidden_Button.style.display = 'block';
			
		},
		on_hidden_cart:function(){
			this._Show_Button.style.display = 'block';
			this._Hidden_Button.style.display = 'none';
		},
		do_set_dispatch:function(){
			parent.window.location.href = __APP__ + "/Cart/SetDispatch";			
		},
		do_logined:function(){
			
			parent.jiyiri.page.promoshoppingcart.ShoppingCart_Tiny_Factory.GetInstance().show(false);
			
			parent.window.location.hash='top';
			var login_url =  __APP__ + "/User/Login?view_mode=tiny&finish_action=close";
		    parent.jiyiri.ui.Dialog.GetInstance().show(login_url ,
			{
				on_close : jiyiri.helper.eventhelper.EventHelper.create_callback_function(this, 'on_user_login_div_close' )
			});
		},
		on_user_login_div_close:function(rst){
			if( rst.is_logined == true )
			{
				parent.window.location.reload();
			}
		},
		remove_from_cart:function( product_type , product_id ){
			jiyiri.page.promoshoppingcart.ShoppingCartAPI.GetInstance().remove_product_from_cart( product_type , product_id , 
					jiyiri.helper.eventhelper.EventHelper.create_callback_function(this, '_reload' )	);
		},
		_reload:function(){
			location.reload();
		},
		/**
		 * 注册到父级容器里边去
		 */
		_regist:function(){
			parent.jiyiri.page.promoshoppingcart.ShoppingCart_Tiny_Factory.GetInstance().regist_ui( this );
		},
		/**
		 * 给点击Li 添加点击事件
		 */
		_add_click_li_event:function(){
			
			Event.observe( this._Cart_Li ,
				   	   	   'click',
				   	   	   jiyiri.helper.eventhelper.EventHelper.create_callback_function(this, '_on_click_li_event' )
				  	  	  );
			
			Event.observe( this._Promo_Li ,
			   	   	   'click',
			   	   	   jiyiri.helper.eventhelper.EventHelper.create_callback_function(this, '_on_click_li_event' )
			  	  	  );
			
			Event.observe( this._Dispatch_Li ,
			   	   	   'click',
			   	   	   jiyiri.helper.eventhelper.EventHelper.create_callback_function(this, '_on_click_li_event' )
			  	  	  );
			
			Event.observe( this._PayStyle_Li ,
			   	   	   'click',
			   	   	   jiyiri.helper.eventhelper.EventHelper.create_callback_function(this, '_on_click_li_event' )
			  	  	  );
			
			Event.observe( this._Service_Li ,
			   	   	   'click',
			   	   	   jiyiri.helper.eventhelper.EventHelper.create_callback_function(this, '_on_click_li_event' )
			  	  	  );
		
		},
		_on_click_li_event:function(){
			parent.jiyiri.page.promoshoppingcart.ShoppingCart_Tiny_Factory.GetInstance().show( true );
		},
		show:function( page ){
			
			this._show_close_button();
			
			if( page == 'cart' ){
				
				this._Cart_Div.style.display = 'block';
				this._Cart_Li.className = 'current';
				
				this._Promo_Div.style.display = 'none';
				this._Promo_Li.className = '';
				
				this._Dispatch_Div.style.display = 'none';
				this._Dispatch_Li.className = '';

				this._PayStyle_Div.style.display = 'none';
				this._PayStyle_Li.className = '';
				
				this._Service_Div.style.display = 'none';
				this._Service_Li.className = '';
				
				
			}else if( page == 'promo' ){
				
				this._Cart_Div.style.display = 'none';
				this._Cart_Li.className = '';
				
				this._Promo_Div.style.display = 'block';
				this._Promo_Li.className = 'current';
				
				this._Dispatch_Div.style.display = 'none';
				this._Dispatch_Li.className = '';

				this._PayStyle_Div.style.display = 'none';
				this._PayStyle_Li.className = '';
				
				this._Service_Div.style.display = 'none';
				this._Service_Li.className = '';
				
			}else if( page == 'dispatch' ){
				
				this._Cart_Div.style.display = 'none';
				this._Cart_Li.className = '';
				
				this._Promo_Div.style.display = 'none';
				this._Promo_Li.className = '';
				
				this._Dispatch_Div.style.display = 'block';
				this._Dispatch_Li.className = 'current';

				this._PayStyle_Div.style.display = 'none';
				this._PayStyle_Li.className = '';
				
				this._Service_Div.style.display = 'none';
				this._Service_Li.className = '';
				
			}else if( page == 'paystyle' ){
				
				this._Cart_Div.style.display = 'none';
				this._Cart_Li.className = '';
				
				this._Promo_Div.style.display = 'none';
				this._Promo_Li.className = '';
				
				this._Dispatch_Div.style.display = 'none';
				this._Dispatch_Li.className = '';

				this._PayStyle_Div.style.display = 'block';
				this._PayStyle_Li.className = 'current';
				
				this._Service_Div.style.display = 'none';
				this._Service_Li.className = '';
				
			}else if( page == 'service' ){
				
				this._Cart_Div.style.display = 'none';
				this._Cart_Li.className = '';
				
				this._Promo_Div.style.display = 'none';
				this._Promo_Li.className = '';
				
				this._Dispatch_Div.style.display = 'none';
				this._Dispatch_Li.className = '';

				this._PayStyle_Div.style.display = 'none';
				this._PayStyle_Li.className = '';
				
				this._Service_Div.style.display = 'block';
				this._Service_Li.className = 'current';
				
			}
		},
		_show_close_button:function(){
			
			/* 根据父级状态来显示button */
			if( parent.jiyiri.page.promoshoppingcart.ShoppingCart_Tiny_Factory.GetInstance()._Shop_Cart_Is_Show ){
				this.on_show_cart();
			}else{
				this.on_hidden_cart();
			}
			
		}
		
	}
	
	
	



	jiyiri.page.promoshoppingcart.ShoppingCart_Tiny_Factory = ShoppingCart_Tiny_Factory;
	jiyiri.page.promoshoppingcart.ShoppingCart_Tiny_UI = ShoppingCart_Tiny_UI;

}
)();
