/**
 * 
 */

		var moveFlag = false;
		var nowPositionInt = 0;
		
		var productList = {};
		
		var selectedDropBox;
		
		$(document).ready(function() {
			$('#shipfd_btn').bind('click', function() {
				divHidden('#shipfd_btn');
				divHidden('#payment_icon');
				$('#bill-table').fadeOut(300, function() {
					divShow('#bill-table-edit');										
					divShow('#ship_button');										
				});
			});
			$('#ship_to_fd_btn_nothanks_ro').bind('click', function() {
				updateShippingInfo(false);												 
				divHidden('#bill-table-edit');
				divHidden('#shipfd_logo_no');
				divHidden('#shipfd_error');
				divHidden('#ship_nameT_ro');
				divHidden('#ship_addT_ro');
				divHidden('#ship_cityT_ro');
				divHidden('#ship_zipT_ro');
				divHidden('#ship_stateT_ro');
				divHidden('#ship_phoneT_ro');
				divHidden('#ship_emailT_ro');
				$('#ship_button').fadeOut(300, function() {
					divShow('#shipfd_btn');										
					divShow('#payment_icon');										
					divShow('#bill-table');	
					$('#ship_to_fd_btn_nothanks_ro').css('display', 'none');
				});
			});
			$('#ship_to_fd_btn_confirm').bind('click', function() {
				updateShippingInfo(true);
			});
			
			//Size Buttom Event
			$('div#sizeBtn').bind('mouseenter', function() {
				$('#sizeBtnOn').fadeIn(300);
				sizeBtnClickHandler(false);
			});
			$('div#sizeBtn').bind('mouseleave', function() {
				$('#sizeBtnOn').fadeOut(300);										 
				sizeBtnClickHandler(true);
			});
			//Color Buttom Event
			$('div#colorBtn').bind('mouseenter', function() {
				$('#colorBtnOn').fadeIn(300);
				colorBtnClickHandler(false);
			});
			$('div#colorBtn').bind('mouseleave', function() {
				$('#colorBtnOn').fadeOut(300);
				colorBtnClickHandler(true);
			});
			//Brand Buttom Event
			$('div#brandBtn').bind('mouseenter', function() {
				$('#brandBtnOn').fadeIn(300);
				brandBtnClickHandler(false);
			});
			$('div#brandBtn').bind('mouseleave', function() {
				$('#brandBtnOn').fadeOut(300);
				brandBtnClickHandler(true);
			});
			$('div#brandBtn span.radio').bind('click', function() {
				getProduct(true);
			});
			//Type Buttom Event
			$('div#typeBtn').bind('mouseenter', function() {
				$('#top_btn_ro').fadeIn(300);
				typeBtnClickHandler(false);
			});
			$('div#typeBtn').bind('mouseleave', function() {
				$('#top_btn_ro').fadeOut(300);
				typeBtnClickHandler(true);
			});
			$('div#typeBtn span.radio').bind('click', function() {
				getProduct(true);
			});
			
			$('#div_deliveryMethod_ship').bind('click', function() {
				update_shippingMethod('shipping');
			});
			
			$('#div_deliveryMethod_other').bind('click', function() {
				
				update_shippingMethod('other_request');
			});
			
			$('div#payment_credit_card').bind('click', function() {
				update_paymentMethod('credit_card');
			});
			
			$('div#payment_union_pay').bind('click', function() {
				update_paymentMethod('union_pay');
			});
			
			$('div#payment_alipay').bind('click', function() {
				update_paymentMethod('alipay');
			});
			
			$('#confirm_to_checkout').bind('click', function() {
				if (validate_checkout()) {
					$.getJSON('./json/confirm_checkout.json.php', {action:'confirmToCheckout'}, function(data) { 
						   if (data.success) {
							   changeBtnClickHandler('loading');
						   } else {
							  $('span#span_errMsg').html(data.errMsg);
							  divShow('#errMsg-container');
							  divShow('#errMsg-Box');
						   }
					});
				}
			}) 
			
			$('#errMsg-ok-btn').bind('click', function() {
				divHidden('#errMsg-container');
				divHidden('#errMsg-Box');
			});
			
			getProduct(true);
		});
		
		function validate_checkout() {
			
			var blnSuccess = true;
			
			if ($('input[name="deliveryMethod"]:checked').val() === undefined && $('input[name="paymentMethod"]:checked').val() === undefined) {
				$('#span_errMsg').html('<img src="img/store/methods_hints.png" />');
				blnSuccess = false;
			} else {
			
				if ($('input[name="deliveryMethod"]:checked').val() === undefined) {
					$('#span_errMsg').html('<img src="img/store/shipping_hint.png" />');
					blnSuccess = false;
				}
				
				if ($('input[name="paymentMethod"]:checked').val() === undefined) {
					$('#span_errMsg').html('<img src="img/store/payment_hint.png" />');
					blnSuccess = false;
				}
			
			}
			
			if (!blnSuccess) {
				divShow('#errMsg-container');
				divShow('#errMsg-Box');
			}
			return blnSuccess;
			
		}
		
		function update_shippingMethod(method) {
			
			openLoadingBar();
			
			$.getJSON("./json/update_shippingMethod.json.php", {action:"updateMethod", deliveryMethod: $('input[name="deliveryMethod"]:checked').val()}, 
				function(data) {
					
					$('#shipping_hints').fadeOut(300);
					
					if ($('input[name="deliveryMethod"]:checked').val() == 'special request') {
						$('#shipping_hints').fadeIn(300);
					}
					
				  if (data.shop_mode == "store") {
					calStoreTotalPrice();
					//getShippingFee();
					//getTotal();
					closeLoadingBar();
				  } else {
					if (data.shop_mode == "outlet") {
						calOutletTotalPrice();
						//getOutletShippingFee();
						//getOutletTotal();
						closeLoadingBar();
					}
				  }
			});
		}
		
		function update_paymentMethod(method) {
			
			openLoadingBar();
			
			$.getJSON("./json/update_paymentMethod.json.php",
							  {action:"updateMethod", paymentMethod: $('input[name="paymentMethod"]:checked').val()},
							  function(data) {
								  
								  closeLoadingBar();
					  				
							  });
		}
		
		function validation_shipping() {
						
			var blnMissingInfo = true;
			var blnEmail = true;
			
			if ($('#shipping_fullName').val() == "") {
				$('#ship_nameT_ro').fadeIn(300);
				blnMissingInfo = false;
			} else {
				$('#ship_nameT_ro').fadeOut(300);
			}
			
			if ($('#shipping_address').val() == "") {
				$('#ship_addT_ro').fadeIn(300);
				blnMissingInfo = false;
			} else {
				$('#ship_addT_ro').fadeOut(300);
			}
			
			if ($('#shipping_city').val() == "") {
				$('#ship_cityT_ro').fadeIn(300);
				blnMissingInfo = false;
			} else {
				$('#ship_cityT_ro').fadeOut(300);
			}
			
			if ($('#shipping_zip').val() == "") {
				$('#ship_zipT_ro').fadeIn(300);
				blnMissingInfo = false;
			} else {
				$('#ship_zipT_ro').fadeOut(300);
			}
			
			if ($('#shipping_state').val() == "") {
				$('#ship_stateT_ro').fadeIn(300);
				blnMissingInfo = false;
			} else {
				$('#ship_stateT_ro').fadeOut(300);
			}
			
			if ($('#shipping_countryId').val() == "none") {
				$('#ship_countryT_ro').fadeIn(300);
				blnMissingInfo = false;
			} else {
				$('#ship_countryT_ro').fadeOut(300);
			}
			
			if ($('#shipping_contactno').val() == "") {
				$('#ship_phoneT_ro').fadeIn(300);
				blnMissingInfo = false;
			} else {
				if (isInteger($('#shipping_contactno').val())) {
					$('#ship_phoneT_ro').fadeOut(300);
				} else {
					$('#ship_phoneT_ro').fadeIn(300);
					blnMissingInfo = false;
				}
			}
			
			if ($('#shipping_email').val() == "") {
				$('#ship_emailT_ro').fadeIn(300);
				blnMissingInfo = false;
			} else {
				if (!echeck($('#shipping_email').val())) {
					$('#ship_emailT_ro').fadeIn(300);
					blnEmail = false;
				} else {
					$('#ship_emailT_ro').fadeOut(300);
				}
			}
			
			if (!blnMissingInfo || !blnEmail) {
				$('#shipfd_error').fadeOut(300);
				$('#shipfd_logo_no').fadeIn(300);
				if (!blnMissingInfo) {
					$('#shipfd_error1').css('display', 'block');
				} else {
					$('#shipfd_error1').css('display', 'none');
				}
				if (!blnEmail) {
					$('#shipfd_error2').css('display', 'block');
				} else {
					$('#shipfd_error2').css('display', 'none');
				}
				$('#shipfd_error').fadeIn(300);
				return false;
			} else {
				$('#shipfd_logo_no').fadeOut(300);
				$('#shipfd_error').fadeOut(300, function() {
					$('#shipfd_error1').css('display', 'none');
					$('#shipfd_error2').css('display', 'none');
				});
				return true;
			}
			
		}
		
		function updateShippingInfo(blnChangeShippingInfo) {
			
			if (blnChangeShippingInfo) {
				if (validation_shipping()) {
					
					openLoadingBar();
					
					$.getJSON('./json/check_shippingaddress.json.php', {shippingAddress:$('#shipping_address').val()}, function(data) { 
						if (data.blnShippingAddress) {
							$.post("./json/update_shippingInfo.json.php?action=updateShipping", 
									  {
										  fullName: $('#shipping_fullName').val(),
										  address: $('#shipping_address').val(),
										  city: $('#shipping_city').val(),
										  zip: $('#shipping_zip').val(),
										  state: $('#shipping_state').val(),
										  countryId: $('#shipping_countryId').val(),
										  contactno: $('#shipping_contactno').val(),
										  email: $('#shipping_email').val()
									  }, function (data) {
										  
											$('#bill_fullName').html(data.fullName);
											$('#bill_address').html(data.address);
											$('#bill_city').html(data.city);
											$('#bill_zip').html(data.zip);
											$('#bill_state').html(data.state);
											$('#bill_country').html(data.country);
											$('#bill_contactno').html(data.contactno);
											$('#bill_email').html(data.email);
									
											divHidden('#bill-table-edit');
													
											if (data.shop_mode == "store") {
												calStoreTotalPrice();
												//getShippingFee();
												//getTotal();
											} else {
												if (data.shop_mode == "outlet") {
													calOutletTotalPrice();
													//getOutletShippingFee();
													//getOutletTotal();
												}
											}
											
											$('#ship_button').fadeOut(300, function() {
												divShow('#shipfd_btn');										
												divShow('#payment_icon');										
												divShow('#bill-table');		
												closeLoadingBar();
											});
									  }, 
									  "json"
									  );
				
						} else {
							$('#shipfd_error').fadeOut(300);
							$('#shipfd_logo_no').fadeIn(300);
							$('#shipfd_error3').css('display', 'block');
							$('#shipfd_error').fadeIn(300);
							
							closeLoadingBar();
						}
					});
				}
				
			} else {
				$.post("./json/update_shippingInfo.json.php?action=updateShipping&change=no", 
						    function (data) {
								
								  $('#shipping_fullName').val('');
								  $('#shipping_address').val('');
								  $('#shipping_city').val('');
								  $('#shipping_zip').val('');
								  $('#shipping_state').val('');
								  $('#shipping_countryId').val('none');
								  $('#shipping_contactno').val('');
								  $('#shipping_email').val('');
							  
								$('#bill_fullName').html(data.fullName);
								$('#bill_address').html(data.address);
								$('#bill_city').html(data.city);
								$('#bill_zip').html(data.zip);
								$('#bill_state').html(data.state);
								$('#bill_country').html(data.country);
								$('#bill_contactno').html(data.contactno);
								$('#bill_email').html(data.email);
						
								divHidden('#bill-table-edit');
										
								if (data.shop_mode == "store") {
									calStoreTotalPrice();
									//getShippingFee();
									//getTotal();
								} else {
									if (data.shop_mode == "outlet") {
										calOutletTotalPrice();
										//getOutletShippingFee();
										//getOutletTotal();
									}
								}
								
								$('#ship_button').fadeOut(300, function() {
									divShow('#shipfd_btn');										
									divShow('#payment_icon');										
									divShow('#bill-table');
									closeLoadingBar();
								});
						  }, 
						  "json"
				  );
			}
						  
		}
		
		function updateProductList() {
			$.getJSON("./json/get_product.json.php",
			  {
				action: "getProduct",
				productBrandId: $('input[name="brand"]:checked').val(),
				productTypeId: $('input[name="store_type"]:checked').val(),
			  },
			  function(data) {
				  if (data.products.length > 0) {
					  productList = data.products;
					  //addNewStore(data.products);
				  }
			  });
		}
		
		function getProduct(needAnimate) {
			
			nowPositionInt = 0;
			
			if (needAnimate) {
				$('#productHint1').fadeOut(300);
				$('#limit_hint').fadeOut(300);
				$('#centerPic').fadeOut(300);
				$('#productText').fadeOut(300);
			}
			$('#productHint1').css('display','none');
			$('#limit_hint').css('display','none');
			
			$('#sizeBtn').animate({top: '558px', height: '23px'}, 200);
			$('#colorBtn').animate({top: '558px', height: '23px'}, 200);
			$('#brandBtn').animate({top:'16px', height: '23px'}, 200);
			$('#typeBtn').animate({top:'16px', height: '23px'}, 200);
			
			$.getJSON("./json/get_product.json.php",
			  {
				action: "getProduct",
				productBrandId: $('input[name="brand"]:checked').val(),
				productTypeId: $('input[name="store_type"]:checked').val(),
			  },
			  function(data) {
				  if (data.products.length > 0) {
					  addNewStore(data.products);
					  genProductContent(0, data.products);
				  }
				  if ($('input[name="brand"]:checked').val() == 'none' || $('input[name="brand"]:checked').val() === undefined) {
					  $('#brandBtnOff').attr('src', 'img/store/brand_btn.gif');
					  $('#brandBtnOn').attr('src', 'img/store/brand_btn_ro.gif');
				  } else {
					  $('#brandBtnOff').attr('src', 'product/brand/'+data.productBrand.button);
					  $('#brandBtnOn').attr('src', 'product/brand/'+data.productBrand.button_ro);
				  }
				  if ($('input[name="store_type"]:checked').val() == 'none' || $('input[name="store_type"]:checked').val() === undefined) {
					  $('#top_btn').attr('src', 'img/store/brand_btn.gif');
					  $('#top_btn_ro').attr('src', 'img/store/brand_btn_ro.gif');
				  } else {
					  $('#top_btn').attr('src', 'product/type/'+data.productType.button);
					  $('#top_btn_ro').attr('src', 'product/type/'+data.productType.button_ro);
				  }
			  });
						
		}
		
		function genProductContent(i, product) {
			  $('#product_img_slide #centerPicContainer #centerPic').attr('src', 'product/'+product[i].colorlist[0].imagePath);
			  $('#productText #desc').html(product[i].description);
			  $('#productText #price').html("HK $ "+product[i].price);
			  $('#current_productId').val(product[i].productId);
			  $('#store_size_ul li').remove();
			  if (product[i].sizeTotal > 0) {
				  var maxsize = product[i].sizelist.length - 1;
				  $.each(product[i].sizelist, function(i,size){
					  if (i < maxsize) {
						  $('#store_size_ul').append('<li class="clearfix"><span class="radio btn_size" style="width:19px;"><input type="radio" value="'+size.productSizeId+'" name="size"></span><label>'+size.name+'</label></li>');
					  } else {
						  $('#store_size_ul').append('<li class="last clearfix"><span class="radio btn_size" style="width:19px;"><input type="radio" value="'+size.productSizeId+'" name="size"></span><label>'+size.name+'</label></li>');
					  }
				  });
				  $("#store_size_ul .radio").dgStyle();
					$('#store_size_ul .radio').bind('click', function() {
						$('#productHint1').fadeOut(300);
					});
			  }
			  $('#store_color_ul li').remove();
			  if (product[i].colorTotal > 0) {
				  var maxcolor = product[i].colorlist.length - 1;
				  $.each(product[i].colorlist, function(i,color){
					  if (i < maxcolor) {
						  $('#store_color_ul').append('<li><span class="radio" style="width:19px;"><input type="radio" value="'+color.productColorId+'" name="color" class="s_rdoColor"></span><label>'+color.name+'</label></li>');
					  } else {
						  $('#store_color_ul').append('<li class="last"><span class="radio" style="width:19px;"><input type="radio" value="'+color.productColorId+'" name="color" class="s_rdoColor"></span><label>'+color.name+'</label></li>');
					  }
				  });
				  $("#store_color_ul .radio").dgStyle();
					$('#store_color_ul .radio').bind('click', function() {
						$('#productHint1').fadeOut(300);
						var getIndex = $('#store_color_ul .radio').index(this);
						$('#product_img_slide #centerPicContainer').fadeOut(300, function() {
							$('#product_img_slide #centerPicContainer #centerPic').attr('src', 'product/'+product[i].colorlist[getIndex].imagePath);
							$('#product_img_slide #centerPicContainer').fadeIn(300);
						});
					});
			  }
			  if (product[i].limitedEdition == 'TRUE') {
			  	$('#limited_edition').val('TRUE');
				$('#limit_hint').fadeIn(300);
			  } else {
			  	$('#limited_edition').val('FALSE');
				$('#limit_hint').fadeOut(300);
			  }
			  if (product[i].sizeTotal <= 0 && product[i].colorTotal <= 0) {
				$('#productHint1 img').attr('src', 'img/store/product_soldout.gif');
				$('#productHint1').fadeIn(300);
			  } else {
				$('#productHint1').fadeOut(300, function(){
					$('#productHint1 img').attr('src', '');
				});
			  }
			$('#centerPic').fadeIn(0, function(){
				var minusX = -480;
				
				$('#centerPicContainer').animate({'left': '0'}, 1);
				$('#otherPicContainer').animate({'left': minusX + "px"}, 1);
				
//				document.getElementById("centerPicContainer").style.left = "0";
//				document.getElementById("otherPicContainer").style.left = minusX + "px";
				
				$('#productText').fadeIn(300);
			});
		}
		
		function addNewStore(data) {
			productList = data;
		}
		
		function check_empty_cart() {
			
			if ($('#cart_table_div table.cart_row').length > 0) {
				changeBtnClickHandler('checkout');
			}
			
			return false;
		}
		
		//-------------------------------photoSlide--------------------------------
		var nowPicMoveType = "next";
		function nextBtnClickHandler(){
			
			if ($('#productHint1').css('display') == 'block') {
			  	$('#productHint1').fadeOut(300);
			}

			$('#sizeBtn').animate({top: '558px', height: '23px'}, 200);
			$('#colorBtn').animate({top: '558px', height: '23px'}, 200);
			$('#brandBtn').animate({top:'16px', height: '23px'}, 200);
			$('#typeBtn').animate({top:'16px', height: '23px'}, 200);
			
			if ( !moveFlag ){
				updateProductList();
				moveFlag = true;
				nowPositionInt++;
				if (nowPositionInt > productList.length - 1){
					nowPositionInt = 0;
				}
				
				divHidden('#productHint1');
				divHidden('#limit_hint');
				divHidden('#productText');
				
				nowPicMoveType = "prev";
				picLoadHandler();
			}
		}
		
		
		function prevBtnClickHandler(){
						
			if ($('#productHint1').css('display') == 'block') {
			  	$('#productHint1').fadeOut(300);
			}
			
			$('#sizeBtn').animate({top: '558px', height: '23px'}, 200);
			$('#colorBtn').animate({top: '558px', height: '23px'}, 200);
			$('#brandBtn').animate({top:'16px', height: '23px'}, 200);
			$('#typeBtn').animate({top:'16px', height: '23px'}, 200);
			if (!moveFlag){
				updateProductList();
				moveFlag = true;
				nowPositionInt--;
				if (nowPositionInt < 0){
					nowPositionInt = productList.length - 1;
				}

				divHidden('#productHint1');
				divHidden('#limit_hint');
				divHidden('#productText');

				nowPicMoveType = "next";
				picLoadHandler();
			}
		}
		
		
		var nextPositionInt = 0;
		var prevPositionInt = 0;
		var loadImgChecSetInterval;
		function picLoadHandler(){
			
			clearInterval( loadImgChecSetInterval );
			
			if (nowPicMoveType == "next"){
				nextPositionInt = nowPositionInt;
				if( prevPositionInt > productList.length - 1 ){
					nextPositionInt = productList.length + nextPositionInt;
				}
				
				//alert('next: '+'product/'+productList[nextPositionInt].imagePath);
				
				document.getElementById("otherPic").setAttribute("src", 'product/'+productList[nextPositionInt].colorlist[0].imagePath);
				if (document.getElementById("otherPic").complete == true) {
					picLoadHandlerSetInterval = setInterval( picMoveHandler, 800 );
				} else {
					loadImgChecSetInterval = setInterval( picLoadHandler, 500 );
				}
				
			}else{
				
				prevPositionInt = nowPositionInt;
				if( nextPositionInt < 0 ){
					prevPositionInt = prevPositionInt - productList.length;
				}
				
				//alert('prev: '+'product/'+productList[prevPositionInt].imagePath);

				document.getElementById("otherPic").setAttribute("src", 'product/'+productList[prevPositionInt].colorlist[0].imagePath);
				if (document.getElementById("otherPic").complete == true) {
					picLoadHandlerSetInterval = setInterval( picMoveHandler, 800 );
				} else {
					loadImgChecSetInterval = setInterval( picLoadHandler, 500 );
				}
				
			}
		}
		
		var picMoveSetIntervalHandler;
		function picMoveHandler(){
			clearInterval(picLoadHandlerSetInterval);
			
			var plusX = 480;
			var minusX = -480;
			
			if (nowPicMoveType == "next") {
				$('#centerPicContainer').animate({left: String( plusX ) + 'px'}, 500);
				
				document.getElementById("otherPicContainer").style.left = String( minusX ) + "px";
				$('#otherPicContainer').animate({left: '0'}, 500, function(){
					//$('#centerPic').css('display', 'none');
					//document.getElementById("centerPic").setAttribute("src", '');														   
				});
				
				picMoveSetIntervalHandler = setInterval( picMoveCompHandler, 600 );
			}else{
				
				$('#centerPicContainer').animate({left: String( minusX ) + 'px'}, 500);
				
				document.getElementById("otherPicContainer").style.left = String( plusX ) + "px";
				$('#otherPicContainer').animate({left: '0'}, 500, function(){
					//$('#centerPic').css('display', 'none');
					//document.getElementById("centerPic").setAttribute("src", '');														   
				});
				
				picMoveSetIntervalHandler = setInterval( picMoveCompHandler, 600 );
			}
		}
		
		function picMoveCompHandler(){
			
			$('#centerPic').fadeOut(0);
			
			clearInterval( picMoveSetIntervalHandler );
			
			moveFlag = false;
			
			genProductContent(nowPositionInt, productList);
			
			if (productList[nowPositionInt].limitedEdition == 'TRUE') {
				$('#limited_edition').val('TRUE');
				divShow('#limit_hint');
			} else {
				$('#limited_edition').val('FALSE');
			}

			divShow('#productText');
			
			document.getElementById("centerPic").setAttribute("src", 'product/'+productList[nowPositionInt].colorlist[0].imagePath);
	
//			if (nowPicMoveType == "next") {
//				document.getElementById("centerPic").setAttribute("src", 'product/'+productList[nowPositionInt].colorlist[0].imagePath);
//			}else{
//				document.getElementById("centerPic").setAttribute("src", 'product/'+productList[nowPositionInt].colorlist[0].imagePath);
//			}
			
		}

		
		//-------------------------------sizeBtnClickHandler--------------------------------
		
		var sizeBtnOpened = false;
		function sizeBtnClickHandler(sizeBtnFlag){
			if (!moveFlag) {
				var getTotal = $('#store_size_ul li').length;
				if (sizeBtnFlag == false) {
					var totalHeight = String((getTotal * 20) + 23);
					var totalTop = String(558 - (getTotal * 20));
					if (!sizeBtnOpened) {
						$('#sizeBtn').animate({
							top: totalTop + 'px',
							height: totalHeight + 'px'
						}, (getTotal * 100), function(){sizeBtnOpened=true;});
					}
				} else{
					if (sizeBtnOpened) {
						$('#sizeBtn').animate({
							top: '558px',
							height: '23px'
						}, (getTotal * 100), function() {sizeBtnOpened = false;});
					}
				}
			}
		}
		
		
		//-------------------------------colorBtnClickHandler--------------------------------
		
		var colorBtnOpened = false;
		function colorBtnClickHandler(colorBtnFlag){
			if (!moveFlag) {
				var getTotal = $('#store_color_ul li').length;
				if (colorBtnFlag == false) {
					var totalHeight = String((getTotal * 20) + 23);
					var totalTop = String(558 - (getTotal * 20));
					if (!colorBtnOpened) {
						$('#colorBtn').animate({
							top: totalTop + 'px',
							height: totalHeight + 'px'
						}, (getTotal * 100), function(){colorBtnOpened=true;});
					}
				} else{
					if (colorBtnOpened) {
						$('#colorBtn').animate({
							top: '558px',
							height: '23px'
						}, (getTotal * 100), function() {colorBtnOpened = false;});
					}
				}
			}
		}
		
		
		//-------------------------------brandBtnClickHandler--------------------------------
		
		var brandBtnOpened = false;
		function brandBtnClickHandler(brandBtnFlag){
			var getTotal = $('#store_brand_ul li').length;
			if (brandBtnFlag == false) {
				var totalHeight = String((getTotal * 20) + 23);
				if (!brandBtnOpened) {
					$('#brandBtn').animate({
						top: '16px',
						height: totalHeight + 'px'
					}, (getTotal * 100), function(){brandBtnOpened=true;});
				}
			} else{
				if (brandBtnOpened) {
					$('#brandBtn').animate({
						top:'16px',
						height: '23px'
					}, (getTotal * 100), function() {brandBtnOpened = false;});
				}
			}
		}
		
		
		//-------------------------------TypeBtnClickHandler--------------------------------
		
		var typeBtnOpened = false;
		function typeBtnClickHandler(typeBtnFlag){
			var getTotal = $('#store_type_ul li').length;
			if (typeBtnFlag == false) {
				var totalHeight = String((getTotal * 20) + 23);
				if (!typeBtnOpened) {
					$('#typeBtn').animate({
						top: '16px',
						height: totalHeight + 'px'
					}, (getTotal * 100), function(){typeBtnOpened=true;});
				}
			} else{
				if (typeBtnOpened) {
					$('#typeBtn').animate({
						top:'16px',
						height: '23px'
					}, (getTotal * 100), function() {typeBtnOpened = false;});
				}
			}
		}
		
		
		//---------------------------add_to_cart---------------------------------------------
		
		function addToCartHandler() {
			
			var blnSuccess = true;
			
			if (!moveFlag) {
				
				moveFlag  = true;
				
				$('#sizeBtn').animate({top: '558px', height: '23px'}, 200);
				$('#colorBtn').animate({top: '558px', height: '23px'}, 200);
				$('#brandBtn').animate({top:'16px', height: '23px'}, 200);
				$('#typeBtn').animate({top:'16px', height: '23px'}, 200);
							
				if ($('input[name="color"]:checked').val() === undefined && $('input[name="size"]:checked').val() === undefined) {
					  if ($('#productHint1').css('display') == 'block') {
						  $('#productHint1').fadeOut(300);
					  }
					  $('#productHint1 img').attr('src', 'img/store/cho_size_color.gif');
					  $('#productHint1').fadeIn(300);
					  blnSuccess = false;
					  moveFlag = false;
				} else {
					if ($('input[name="color"]:checked').val() !== undefined && $('input[name="size"]:checked').val() === undefined) {
					  if ($('#productHint1').css('display') == 'block') {
						  $('#productHint1').fadeOut(300);
					  }
					  $('#productHint1 img').attr('src', 'img/store/size_add_cart.gif');
					  $('#productHint1').fadeIn(300);
					  blnSuccess = false;
					  moveFlag = false;
					}
					if ($('input[name="color"]:checked').val() === undefined && $('input[name="size"]:checked').val() !== undefined) {
					  if ($('#productHint1').css('display') == 'block') {
						  $('#productHint1').fadeOut(300);
					  }
					  $('#productHint1 img').attr('src', 'img/store/color_add_cart.gif');
					  $('#productHint1').fadeIn(300);
					  blnSuccess = false;
					  moveFlag = false;
					}
				}
				
				
				
				if (blnSuccess) {
					openLoadingBar();
					
					$.getJSON("./json/check_stock.json.php", 
						{
							  productId: $('#current_productId').val(),
							  colorId: $('input[name="color"]:checked').val(),
							  sizeId: $('input[name="size"]:checked').val()
						}, function(data) {
							
							if(data.blnSuccess) {
																
								$('#productHint1').fadeOut(300, function(){
									$('#productHint1 img').attr('src', '');
								});
							
								$.get('gen_content/cart_row.php', 
									  {
										  action: 'addToCart',
										  productId: $('#current_productId').val(),
										  colorId: $('input[name="color"]:checked').val(),
										  sizeId: $('input[name="size"]:checked').val(),
										  limited: $('input#limited_edition').val(),
										  shop_mode: "store"
									  
									  }, function(data) {
										  
										  if (data != '') {
				
											  if ($('#productHint1').css('display') == 'block') {
												  $('#productHint1').fadeOut(300);
											  }
																					
											//store page
											$('div#cart_table_div').append(data);
																					
											var lastIndex = $('#cart_table_div table.cart_row').length - 1;
											
											$('#cart_table_div table.cart_row:eq('+lastIndex+')').fadeIn(300);;
											
											$('#cart_table_div table.cart_row:eq('+lastIndex+')').removeAttr('style');
										
											$("#cart_table_div table.cart_row img.store_remove_btn").unbind();
											
											$("#cart_table_div table.cart_row img.store_remove_btn").click(function () {
																					  
												removeFromCart(this);
												
											});
														
											$("#cart_table_div table.cart_row input#store_qty").unbind();
											
											$("#cart_table_div table.cart_row input#store_qty").change(function () {
																					  
												updateProducPriceFromCart(this);
												
											});
														
											if ($('#cart_table_div table.cart_row').length > 8) {
												$('#cart_table_div').css('height', '264px');
												$('#store_arrow').css('display', 'block');
											} else {
												$('#cart_table_div').css('height', 'auto');
												$('#store_arrow').css('display', 'none');
											}
											
											//check out page
											$('div#checkout_table_div').append(data);
																					
											var lastIndex = $('#checkout_table_div table.cart_row').length - 1;
											
											$('#checkout_table_div table.cart_row:eq('+lastIndex+')').fadeIn(300);
											
											$('#checkout_table_div table.cart_row:eq('+lastIndex+')').removeAttr('style');
										
											$("#checkout_table_div table.cart_row img.store_remove_btn").unbind();
											
											$("#checkout_table_div table.cart_row img.store_remove_btn").bind('click', function() {
						
												removeFromCheckout(this);
												
											});
														
											$("#checkout_table_div table.cart_row input#store_qty").unbind();
											
											$("#checkout_table_div table.cart_row input#store_qty").change(function () {
																					  
												updateProducPriceFromCheckout(this);
												
											});
														
											if ($('#checkout_table_div table.cart_row').length > 8) {
												$('#checkout_table_div').css('height', '264px');
												$('#checkout_arrow').css('display', 'block');
											} else {
												$('#checkout_table_div').css('height', 'auto');
												$('#checkout_arrow').css('display', 'none');
											}
												
											calStoreTotalPrice();
											//getSubTotal();
											//getShippingFee();
											//getTotal();
											
											moveFlag = false;
											closeLoadingBar();
										} else {
											
											  if ($('input#limited_edition').val() == "TRUE") {
												  $('#productHint1 img').attr('src', 'img/store/product_hint1.gif');
												  $('#productHint1').fadeIn(300);
											  }
											
											$.getJSON('./json/update_cart.json.php', 
									  
												  {
													  action: 'addOneMore',
													  productId: $('#current_productId').val(),
													  colorId: $('input[name="color"]:checked').val(),
													  sizeId: $('input[name="size"]:checked').val(),
													  limited: $('input#limited_edition').val(),
													  shop_mode: "store"
												  
												  }, function(data) {
													  if (data.blnSuccess) {
														var index = data.returnIndex;
									
														$('#cart_table_div table.cart_row:eq('+index+') div#product_total').html("$"+data.price);
														$('#checkout_table_div table.cart_row:eq('+index+') div#product_total').html("$"+data.price);
														
														$('#cart_table_div table.cart_row:eq('+index+') input#store_qty').val(data.qty);
														$('#checkout_table_div table.cart_row:eq('+index+') input#store_qty').val(data.qty);
														
														//getSubTotal();
														//getShippingFee();
														//getTotal();
														
													  }
													  calStoreTotalPrice();
													  moveFlag = false;
													  closeLoadingBar();
												  });
											
										}
								});
							
							  } else {
								$('#productHint1 img').attr('src', 'img/store/product_soldout.gif');
								$('#productHint1').fadeIn(300);
								moveFlag = false;
							    closeLoadingBar();
							  }


					});


				}
			
			}
						
		}
		
		function updateProducPriceFromCart(obj) {
			
			if (obj.value != "0" && obj.value != "00" && obj.value != "") {
				openLoadingBar();
				
				$.getJSON('./json/update_cart.json.php', {action:'updateProduct', index:obj.getAttribute('name'), qty:obj.value}, function(data) {
					
					var total_qty = obj.value;
					
					if (!data.blnSuccess) {
						total_qty = data.total_qty;
					}
					
					var index = data.returnIndex;
					
					$('#cart_table_div table.cart_row:eq('+index+') div#product_total').html("$"+data.price);
					$('#checkout_table_div table.cart_row:eq('+index+') div#product_total').html("$"+data.price);
					
					$('#cart_table_div table.cart_row:eq('+index+') input#store_qty').val(total_qty);
					$('#checkout_table_div table.cart_row:eq('+index+') input#store_qty').val(total_qty);
	
					calStoreTotalPrice();
					//getSubTotal();
					//getShippingFee();
					//getTotal();
					closeLoadingBar();
				
				});
			} else {
				/*$('#cart_table_div table.cart_row:eq('+index+') input#store_qty').val(1);
				$('#checkout_table_div table.cart_row:eq('+index+') input#store_qty').val(1);*/
				
				//alert('0');
				removeFromCartFromInput(obj);
				//removeFromCheckoutFromInput(obj);
			}
												
		}

		function updateProducPriceFromCheckout(obj) {
			
			if (obj.value != "0" && obj.value != "00" && obj.value != "") {
				openLoadingBar();
				$.getJSON('./json/update_cart.json.php', {action:'updateProduct', index:obj.getAttribute('name'), qty:obj.value}, function(data) {
					
					var total_qty = obj.value;
					
					if (!data.blnSuccess) {
						total_qty = data.total_qty;
					}
					
					var index = data.returnIndex;
					
					$('#cart_table_div table.cart_row:eq('+index+') div#product_total').html("$"+data.price);
					$('#checkout_table_div table.cart_row:eq('+index+') div#product_total').html("$"+data.price);
					
					$('#cart_table_div table.cart_row:eq('+index+') input#store_qty').val(total_qty);
					$('#checkout_table_div table.cart_row:eq('+index+') input#store_qty').val(total_qty);
	
					calStoreTotalPrice();
					//getSubTotal();
					//getShippingFee();
					//getTotal();
					closeLoadingBar();
				});
			} else {
				/*$('#cart_table_div table.cart_row:eq('+index+') input#store_qty').val(1);
				$('#checkout_table_div table.cart_row:eq('+index+') input#store_qty').val(1);*/
				
				//alert('0');
				//removeFromCartFromInput(obj);
				removeFromCheckoutFromInput(obj);
			}
												
		}


		function removeFromCartFromInput(obj) {
			
			openLoadingBar();
			
			$.getJSON('./json/update_cart.json.php', {action:'removeProduct', index:obj.getAttribute('alt')}, function(data) {
			
				var removeIndex = $("#cart_table_div table.cart_row input.s_input_qty").index(obj);

				$('#cart_table_div table.cart_row:eq('+removeIndex+')').fadeOut(300, function() {
														   
					$('#cart_table_div table.cart_row:eq('+removeIndex+')').remove();
					$('#checkout_table_div table.cart_row:eq('+removeIndex+')').remove();
	
					if ($('#cart_table_div table.cart_row').length <= 8) {
						$('#cart_table_div').css('height', 'auto');
						$('#checkout_table_div').css('height', 'auto');
						$('#store_arrow').css('display', 'none');
						$('#checkout_arrow').css('display', 'none');
					}
					
					calStoreTotalPrice();
					//getSubTotal();
					//getShippingFee();
					//getTotal();
					closeLoadingBar();
				});
				
				
			});
												
		}
			
		function removeFromCheckoutFromInput(obj, objFrom) {
			
			openLoadingBar();
			
			$.getJSON('./json/update_cart.json.php', {action:'removeProduct', index:obj.getAttribute('alt')}, function(data) {
			
				var removeIndex = $("#checkout_table_div table.cart_row input.s_input_qty").index(obj);
				
				$('#checkout_table_div table.cart_row:eq('+removeIndex+')').fadeOut(300, function() {
														   
					$('#cart_table_div table.cart_row:eq('+removeIndex+')').remove();
					$('#checkout_table_div table.cart_row:eq('+removeIndex+')').remove();
					
					if ($('#checkout_table_div table.cart_row').length <= 8) {
						$('#cart_table_div').css('height', 'auto');
						$('#checkout_table_div').css('height', 'auto');
						$('#store_arrow').css('display', 'none');
						$('#checkout_arrow').css('display', 'none');
					}
					
					calStoreTotalPrice();
					//getSubTotal();
					//getShippingFee();
					//getTotal();
					closeLoadingBar();
				});
				
				
			});
												
		}
		
		function removeFromCart(obj) {
							
			openLoadingBar();
			$.getJSON('./json/update_cart.json.php', {action:'removeProduct', index:obj.getAttribute('alt')}, function(data) {
			
				var removeIndex = $("#cart_table_div table.cart_row img.store_remove_btn").index(obj);

				//alert(removeIndex);
				
				if (removeIndex < 0) {
					var removeIndex = $("#cart_table_div table.cart_row input.s_input_qty").index(obj);
				}
				
				//alert(removeIndex);
				
				$('#cart_table_div table.cart_row:eq('+removeIndex+')').fadeOut(300, function() {
														   
					$('#cart_table_div table.cart_row:eq('+removeIndex+')').remove();
					$('#checkout_table_div table.cart_row:eq('+removeIndex+')').remove();
	
					if ($('#cart_table_div table.cart_row').length <= 8) {
						$('#cart_table_div').css('height', 'auto');
						$('#checkout_table_div').css('height', 'auto');
						$('#store_arrow').css('display', 'none');
						$('#checkout_arrow').css('display', 'none');
					}
					
					calStoreTotalPrice();
					//getSubTotal();
					//getShippingFee();
					//getTotal();
					closeLoadingBar();
				});
				
				
			});
												
		}
			
		function removeFromCheckout(obj, objFrom) {
			
			openLoadingBar();
			$.getJSON('./json/update_cart.json.php', {action:'removeProduct', index:obj.getAttribute('alt')}, function(data) {
			
				var removeIndex = $("#checkout_table_div table.cart_row img.store_remove_btn").index(obj);
				
				if (removeIndex < 0) {
					
					var removeIndex = $("#checkout_table_div table.cart_row input.s_input_qty").index(obj);
				
				}
				
				$('#checkout_table_div table.cart_row:eq('+removeIndex+')').fadeOut(300, function() {
														   
					$('#cart_table_div table.cart_row:eq('+removeIndex+')').remove();
					$('#checkout_table_div table.cart_row:eq('+removeIndex+')').remove();
					
					if ($('#checkout_table_div table.cart_row').length <= 8) {
						$('#cart_table_div').css('height', 'auto');
						$('#checkout_table_div').css('height', 'auto');
						$('#store_arrow').css('display', 'none');
						$('#checkout_arrow').css('display', 'none');
					}
					
					calStoreTotalPrice();
					//getSubTotal();
					//getShippingFee();
					//getTotal();
					closeLoadingBar();
				});
				
				
			});
												
		}
		
		function initCheckoutContainer() {
			
			$('.checkout_row').removeAttr('style');
			
			if ($('#checkout_table_div table.checkout_row').length > 8) {
				$('#checkout_table_div').css('height', '264px');
				$('#checkout_arrow').css('display', 'block');
			} else {
				$('#checkout_table_div').css('height', 'auto');
				$('#checkout_arrow').css('display', 'none');
			}
			$("img.store_remove_btn").unbind();
			
			$("img.store_remove_btn").click(function () {
				removeFromCheckout(this);
			});
			
		}
		
		function openLoadingBar() {
			
			divShow('#loading-bar-container');
			divShow('#loading-bar-Box');
			
		}
		
		function closeLoadingBar() {
			
			divHidden('#loading-bar-container');
			divHidden('#loading-bar-Box');
			
		}
		

