var contact = {
	message: null,
	open: function (dialog) {
		// add padding to the buttons in firefox/mozilla
		if ($.browser.mozilla) {
			$('#contact-container .contact-button').css({
				'padding-bottom': '2px'
			});
		}
		// input field font size
		if ($.browser.safari) {
			$('#contact-container .contact-input').css({
				'font-size': '.9em'
			});
		}
        
        $('#contact-container form').find("img").hide();
        
		var title = $('#contact-container .contact-title').html();
		$('#contact-container .contact-title').html('数据加载中...');
		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
				dialog.data.fadeIn(200, function () {
					$('#contact-container .contact-content').animate({
						height: 530
					}, function () {
						$('#contact-container .contact-title').html(title);
						
						$('#contact-container form').fadeIn(200, function () {
							$('#contact-container #contact-name').focus();
                            
				            $('#contact-container form').find("img").show();
				            
							// fix png's for IE 6
							if ($.browser.msie && $.browser.version < 7) {
								$('#contact-container .contact-button').each(function () {
									if ($(this).css('backgroundImage').match(/^url[("']+(.*\.png)[)"']+$/i)) {
										var src = RegExp.$1;
										$(this).css({
											backgroundImage: 'none',
											filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +  src + '", sizingMethod="crop")'
										});
									}
								});
							}
						});
					});
				});
			});
		});
	},
	show: function (dialog) {
	    $('#contact-container #UpdateShopCart').click(function (e) {
            e.preventDefault();
			// validate form
			if (contact.validate()) {
				$('#contact-container .contact-message').fadeOut(function () {
					$('#contact-container .contact-message').removeClass('contact-error').empty();
				});
				$('#contact-container .contact-title').html('数据传输中...');
				$('#contact-container form').fadeOut(200);
				$('#contact-container .contact-content').animate({
					height: '80px'
				}, function () {
					$('#contact-container .contact-loading').fadeIn(200, function () {
					    var MyGoodsDetail = new GoodsDetail();
						$.ajax({
							url: '/GeneralProcedures/EditShoppingCart.ashx',
							data: "OldGoodsItemId="+escape(MyGoodsColor.GetInitGoodsItemId())+"&OldSizeName="+escape(MyGoodsSize.GetInitSizeName())+"&OldQuantity="+GoodsInfoPartOldQuantity+"&GoodsItemId="+escape(SelectGoodsInfos.ItemId)+"&GoodsName="+escape(SelectGoodsInfos.Name)+"&GoodsImage="+escape(MyGoodsImg.GetFirstThumbnails())+"&Color="+escape(MyGoodsColor.GetColorName())+"&SizeName="+escape(MyGoodsSize.GetSizeName())+"&Quantity="+$("#contact-container #BuyObjNum").val()+"&ListPrice="+escape(SelectGoodsInfos.ListPrice)+"&CurrPrice="+escape(SelectGoodsInfos.CurrPrice)+"&PKID="+PKID,
							type: 'post',
							cache: false,
							dataType: 'html',
							complete: function (data) {
								$('#contact-container .contact-loading').fadeOut(200, function () {
									//$('#contact-container .contact-title').html('商品已放入购物车');
									$('#contact-container .contact-message').html(data.responseText).fadeIn(100);
									setTimeout(function(){ $.modal.close();},500);
									
									GetMyShoppingCart();
								});
							},
							error: contact.error
						});
					});
				});
			}
			else {
				contact.showMessage();				
			}
	    });
		$('#contact-container #SubmitShopCart').click(function (e) {
			e.preventDefault();
			// validate form
			if (contact.validate()) {
				$('#contact-container .contact-message').fadeOut(function () {
					$('#contact-container .contact-message').removeClass('contact-error').empty();
				});
				$('#contact-container .contact-title').html('数据传输中...');
				$('#contact-container form').fadeOut(200);
				$('#contact-container .contact-content').animate({
					height: '80px'
				}, function () {
					$('#contact-container .contact-loading').fadeIn(200, function () {
					    var MyGoodsDetail = new GoodsDetail();
					    
						$.ajax({
							url: '/GeneralProcedures/AddToShoppingCart.ashx',
							data: "GoodsItemId="+SelectGoodsInfos.ItemId+"&GoodsName="+escape(SelectGoodsInfos.Name)+"&GoodsImage="+escape(MyGoodsImg.GetFirstThumbnails())+"&Color="+escape(MyGoodsColor.GetColorName())+"&SizeName="+escape(MyGoodsSize.GetSizeName())+"&Quantity="+$("#contact-container #BuyObjNum").val()+"&ListPrice="+escape(SelectGoodsInfos.ListPrice)+"&CurrPrice="+escape(SelectGoodsInfos.CurrPrice),
							type: 'post',
							cache: false,
							dataType: 'html',
							complete: function (data) {
								$('#contact-container .contact-loading').fadeOut(200, function () {
									//$('#contact-container .contact-title').html('商品已放入购物车');
									$('#contact-container .contact-message').html(data.responseText).fadeIn(100);
									setTimeout(function(){  $.modal.close();},500);
								});
							},
							error: contact.error
						});
					});
				});
			}
			else {
                contact.showMessage();			
			}
		});
        $('#contact-container #SubmitPackage').click(function (e) {
			e.preventDefault();
			// validate form
			if (contact.validate()) {
			    var returnValue = MyPackageGoodsView.AddToPackage({MaxQuantity:GoodsInfoPartMaxQuantity,GoodsItemId:SelectGoodsInfos.ItemId,BuyNums:parseInt($("#contact-container #BuyObjNum").val()),CurrPrice:escape(SelectGoodsInfos.CurrPrice),ListPrice:escape(SelectGoodsInfos.ListPrice),GoodsName:escape(SelectGoodsInfos.Name),GoodsSize:escape(MyGoodsSize.GetSizeName()),GoodsImage:escape(MyGoodsImg.GetFirstThumbnails())});
                if(returnValue!=true){
                    var array = returnValue.split("_");
                    if(array[0]=="EXIST")
                    {
                        if(array[1]>0)
                            contact.message += "当前服装最多还能选择"+array[1]+"件";
                        else 
                            contact.message += "不能再选择当前服装";   
                        contact.showMessage();          
                        return;
                    }
                    else if(array[0]=="EMPTY")
                    {
                        if(array[1]>0)
                            contact.message += "当前礼包最多还能选择"+array[1]+"件衣物";
                        else
                            contact.message += "礼包已满!"
                        contact.showMessage();
                        return;
                    }
                }
				$('#contact-container .contact-message').fadeOut(function () {
					$('#contact-container .contact-message').removeClass('contact-error').empty();
				});
				$('#contact-container .contact-title').html('数据传输中...');
				$('#contact-container form').fadeOut(200);
				$('#contact-container .contact-content').animate({
					height: '80px'
				}, function () {
					$('#contact-container .contact-loading').fadeIn(200, function () {
					    $('#contact-container .contact-loading').fadeOut(200, function () {
					        $('#contact-container .contact-message').html('商品已放入礼包').fadeIn(100);
					        setTimeout(function(){$.modal.close();},500);
						});

					});
				});
			}
			else {
				contact.showMessage();
			}
		});
	},
	close: function (dialog) {	    
		$('#contact-container .contact-message').fadeOut();
		$('#contact-container .contact-title').html('关闭...');
		$('#contact-container form').fadeOut(200);
		$('#contact-container .contact-content').animate({
			height: 40 
		}, function () {
		    $(".modalClose img").css("display","none");
			dialog.data.fadeOut(200, function () {
				dialog.container.fadeOut(200, function () {
				    
					dialog.overlay.fadeOut(200, function () {
						$.modal.close();
						
						$.ajax({     
                            url: "/Data/GetMyShoppingCart.aspx",
                            cache:false,
                            success: function(msg){
                                $("#content").html(msg);
                           }
                           });
					});
				});
			});
		});
	},
	error: function (xhr) {
	    dialog.info(xhr.statusText);
//		alert(xhr.statusText);
		//alert('t');
	},
	validate: function () {
	
	var MyGoodsDetail = new GoodsDetail();
		contact.message = '';
		if (MyGoodsSize.GetSizeName()==null) {
			contact.message += '请选择尺寸 ';
			return false;
		}
		if(parseInt( MyGoodsSize.GetSizeName())==0)
		{
		    contact.message +='商品已售完';
		    return false;
		}
		if(parseInt( MyGoodsSize.GetSizeStock())<parseInt($("#contact-container #BuyObjNum").val()))
		{
		    contact.message+="库存不足";
		    return false;
		}
		if (contact.message.length > 0) {
			return false;
		}
		else {
			return true;
		}
	},
	showError: function () {
		$('#contact-container .contact-message')
			.html($('<div class="contact-error">').append(contact.message))
			.fadeIn(200);
	},
	showMessage:function(){
        if ($('#contact-container .contact-message:visible').length > 0) {
	        var msg = $('#contact-container .contact-message div');
	        msg.fadeOut(200, function () {
		        msg.empty();
		        contact.showError();
		        msg.fadeIn(200);
	        });
        }
        else {
	        $('#contact-container .contact-message').animate({
		        height: '30px'
	        }, contact.showError);
        } 
	}
};