jiyiri.register_namespace('jiyiri.page.gift');

jiyiri.require_once('jiyiri.page.common.productview.list');
jiyiri.require_once('jiyiri.page.common.productview.view');
jiyiri.require_once('jiyiri.page.common.productview.zoom');
jiyiri.require_once('jiyiri.page.gift.userword');

(
function()
{
	var GiftView_UI = Class.create();
	GiftView_UI._insatance = null;
	GiftView_UI.GetInstance = function() {
		if (null == GiftView_UI._instance) {
			GiftView_UI._instance = new GiftView_UI();
		}
		return GiftView_UI._instance;
	};


	GiftView_UI.prototype = {

		initialize:function() {

			/**
			 * 产品类型
			 */
			this._ProductType;

			/**
			 * 产品Id
			 */
			this._ProductId;

			/**
			 * ProductView_List的图片转换器
			 * 将original图片转换成List所需要的图片
			 */
			this._ProductView_List_Translator;

			/**
			 * 图片列表显示区域控制
			 */
			this._ProductView_List;

			/**
			 * 图片放大显示控制
			 */
			this._ProductView_Zoom;

			/**
			 * 图片显示区域控制
			 */
			this._ProductView_View;

			/**
			 * 用户意见区域控制
			 */
			this._ProductComment;

			/**
			 * 用户咨询区域控制
			 */
			this._ProductConsultation;
			

			/**
			 * 分享到其他网站控件
			 */
			this._ShareIt;


			this._init();

		},
		show:function() {

			/* 显示图片显示区域  图片列表 查看大图 * */
			this._show_product_view();

			/**显示分享到哪的区域 */
			this._show_share_area();

			/**显示历史浏览区域*/
			this._show_history_view();

			/** 显示用户留言*/
			this._show_user_word();

			this.show_productdetail();
			
			/* 展示购物车区域 */
			this.show_cart_area();


		},
		/**
		 * @param ProductType		   当前显示的产品类型
		 * @param ProductId			   当前显示的产品ID
		 * @param ProductView_Pictures 图片显示的图片列表
		 * [ { sequence:1 , original_url:xxx } , { sequence:2 , original_url:xxx } ]
		 *
		 */
		config:function( ProductType , ProductId , ProductView_Pictures ){

			this._ProductType = ProductType;
			this._ProductId   = ProductId;

			/** 配置ProductView 区域信息 */
			this._config_product_view( ProductView_Pictures );

			this._config_user_word( ProductType , ProductId );

			this._config_share_area();



		},


		/**
		 * 用户意见接口
		 */
		user_comment:function(){
			var body	  = $F( 'comment_body' );
			
			if( !jiyiri.helper.validatehelper.DataValidater.CheckEmpty(body) ){
				alert( "请输入意见内容" );
				return;
			}
			
			
			
			this._ProductComment.user_comment( body ,
				jiyiri.helper.eventhelper.EventHelper.create_callback_function(this,'_on_user_comment_callback_function'));
		},


		/**
		 * 用户咨询接口
		 */
		user_consultation:function(){
			var user_name = $F( 'user_name' );
			var email	  =	$F( 'email' );
			var consultation_body	  = $F( 'consultation_body' );
			var consultation_code	  = $F( 'consultation_code' );
			
			if( !jiyiri.helper.validatehelper.DataValidater.CheckEmpty(user_name) ){
				alert( "请输入昵称" );
				return;
			}
			
			if( !jiyiri.helper.validatehelper.DataValidater.CheckEmpty(email) ){
				alert( "请输入Email地址" );
				return;
			}
			
			if( !jiyiri.helper.validatehelper.DataValidater.CheckEmpty(consultation_body) ){
				alert( "请输入咨询内容" );
				return;
			}
			
			if( !jiyiri.helper.validatehelper.DataValidater.CheckEmail(email) ){
				alert( "Email格式不正确" );
				return;
			}
			
			

			this._ProductConsultation.user_consultation( consultation_body , user_name , email , consultation_code ,
						jiyiri.helper.eventhelper.EventHelper.create_callback_function(this,'_on_user_consultation_callback_function'));
		},
		
		/* 商品列表图片左移右移 */
		product_view_list_move:function( direction ){
			
			if( direction == 'left' ){
				this._ProductView_List.move_left();
			}else if( direction == 'right' ){
				this._ProductView_List.move_right();
			}
			
		},

		show_shouhou:function(){
			$( 'shouhoufuwu_area' ).style.display = "block";
			$( 'shouhoufuwu_button' ).className = "current";
			$( 'productdetail_area' ).style.display = "none";
			$( 'productdetail_button' ).className = "";
		},

		show_productdetail:function(){
			$( 'shouhoufuwu_area' ).style.display = "none";
			$( 'shouhoufuwu_button' ).className = "";
			$( 'productdetail_area' ).style.display = "block";
			$( 'productdetail_button' ).className = "current";
		},
		
		/**
		 * 显示购物车数量区域
		 * 每次调用都通过ajax 获取后台最新数据
		 */
		show_cart_area:function(){
			jiyiri.page.promoshoppingcart.ShoppingCartAPI.GetInstance().get_product_cart_count(
			function(response){
				var rst = jiyiri.helper.net.Ajax.parse_tp_return(response.responseText);
				if (rst.status) {
					$( 'cart_count' ).innerHTML = rst.info;
				}
			}	
			);	
		},
		_init:function(){

			/* ProductView初始化 */
			this._init_product_view();

			this._init_user_word();

			/* 初始化分享区域 */
			this._init_share_area();



		},


		_init_product_view:function(){

			this._ProductView_List_Translator = new jiyiri.page.common.productview.ProductViewer_List_Translator();
			this._ProductView_List			  = new jiyiri.page.common.productview.ProductViewer_List();
			this._ProductView_Zoom			  = new jiyiri.page.common.productview.ProductViewer_Zoom();
			this._ProductView_View			  = new jiyiri.page.common.productview.ProductViewer_View();


		},

		_init_user_word:function(){

			this._ProductComment		=	new jiyiri.page.gift.ProductComment();
			this._ProductConsultation	=	new jiyiri.page.gift.ProductConsultation();

		},

		_init_share_area:function(){

			this._ShareIt = new jiyiri.widget.shareit.Shareit();
		},


		_config_product_view:function( ProductView_Pictures ){
			this._ProductView_List_Translator.config( jiyiri.page.common.PromoShop_Pic_Standard.ProductView_List );
			this._ProductView_Zoom.config( 'product_view' , 'product_view_img' , 'product_view_big_view',
											  322	,	242,
											  150 	, 	100,
											  2 ,
											  '');
			this._ProductView_List.config( this._ProductView_List_Translator.translate(ProductView_Pictures),
										  'product_view_list' , 52 , 5 ,
										   jiyiri.helper.eventhelper.EventHelper.create_callback_function(this,'_on_select_picture_in_product_view_list')
										  );
			this._ProductView_View.config( 'product_view_img' );
		},


		_config_user_word:function( ProductType , ProductId ){

			this._ProductComment.config( ProductType , ProductId , 'product_comment_list' , 'product_comment_list_count' );
			this._ProductConsultation.config( ProductType , ProductId , 'product_consultation_list' );

		},

		_config_share_area:function(){

			var options = {
					button_container:'product_share_area',
					textbox_container:'share_list_textbox_container',
					textbox_classname:'importInput'
			};
			this._ShareIt.config(options);

		},



		_show_product_view:function(){
			this._ProductView_Zoom.run();
			this._ProductView_List.run();
		},

		_show_user_word:function(){
			jiyiri.ui.ValidateImage.GetInstance().init_show($('consultation_code_img'), $('consultation_code_reload'));
			this._ProductComment.show();
			this._ProductConsultation.show();
		},


		_show_share_area:function(){

			this._ShareIt.render();

		},
		
		_show_history_view:function(){
			jiyiri.page.common.ProductViewHistoryManager.GetInstance().Show( 'product_history_area' );
		},


		/**
		 * 点击List图片后的回调
		 * 当选取List图片时先更换ProductView_View里的图片，然后再设置放大区域图片内容
		 *
		 * @param original_url
		 * 选取图片的源图片地址
		 */
		_on_select_picture_in_product_view_list:function( original_url ){
			var picView	= jiyiri.page.common.PromoShop_Pic_Getter.getPic(original_url , jiyiri.page.common.PromoShop_Pic_Standard.ProductView_View );
			var picZoom	= jiyiri.page.common.PromoShop_Pic_Getter.getPic(original_url , jiyiri.page.common.PromoShop_Pic_Standard.ProductView_Zoom );
			this._ProductView_View.show(picView);
			this._ProductView_Zoom.set_big_img_url(picZoom);
		},



		/**
		 * 用户点击意见后的回调
		 */
		_on_user_comment_callback_function:function( response ){


			var rst = jiyiri.helper.net.Ajax.parse_tp_return(response.responseText);
			if (rst.status) {
				alert( "发表成功,请等候管理员回复:)" );
				this._ProductComment.reload();
				$( 'comment_body' ).value='';
			} else {
				alert(rst.info);
			}

		},
		/**
		 * 用户点击咨询后的回调
		 */
		_on_user_consultation_callback_function:function( response ){


			var rst = jiyiri.helper.net.Ajax.parse_tp_return(response.responseText);
			if (rst.status) {
				alert( "发表成功,请等候管理员回复:)" );
				this._ProductConsultation.reload();
				jiyiri.ui.ValidateImage.GetInstance().reload();
				$( 'user_name' ).value='';
				$( 'email' ).value='';
				$( 'consultation_body' ).value='';
				$( 'consultation_code' ).value='';
				
			} else {
				alert(rst.info);
			}

		}

	};


	/* Codes End Here */

	/* Register Start Here */
	jiyiri.page.gift.GiftView_UI = GiftView_UI;
	/* Register End Here */
}
)
();

