$(document).ready(function() {
	var Site = Site || { 'options': {} };
	
	Site.init = function (options) {
		$.merge(Site.options, (options||{}));
		Site.setupForm();
		Site.setupCompleteTheLook();
	};
		
	Site.setupForm = function () {
		var elements = $('select.quick_color2_Michael');
		$.each(elements, function (i, e) {
			$(e).change(function (event) {
				var name = $(event.target).attr('name').split('_')[0];
				var id   = $(event.target).attr('name').split('_')[1];
				
				switch (name.toLowerCase()) {
					case 'variantlist':
						//Site.updateColour(name, id, $(event.target).val());
						break;
					case 'selectcolor':
						Site.updateQuantity(e);
						break;
				}
			});
		});
	};
	
	Site.setupCompleteTheLook = function () {
		var elements = $('select.quick_color3_Michael');
		$.each(elements, function (i, e) {
			$(e).change(function (event) {
				var name = $(event.target).attr('name').split('_')[0];
				
				switch (name.toLowerCase()) {
					case 'blselectcolor':
						Site.switchColourForCompleteTheLook($(event.target));
						
						var rowId = $(event.target).attr('name').split('_')[1];
						var variantId = $('#BLVariantList_' + rowId).val();
						if (variantId > 0) {
							var colour = $(event.target).val();
							Site.doStockCheck(variantId, colour, 'BLselectQuantity_' + rowId);
						}
						break;
						
					case 'normalpopselectcolor':
						Site.switchColourForCompleteTheLook($(event.target));
					
						var id 	   = $(event.target).attr('name').split('_')[1];
						var colour = $(event.target).val();
															
						Site.doStockCheck(id, colour, 'NormalselectQuantity_' + id);					
						break;			
				}			
			});
		});	
	};
	
	Site.switchColourForCompleteTheLook = function (select) {
		var colour = select.val().replace(" ", "").toLowerCase();
		var parentForm = select.parent().parent().parent().parent().parent();
		var productId = parentForm.attr('id').split('_')[1];
		var productNo = parentForm.attr('id').split('_')[2];
		
		var previewImage = $('#PopProduct_' + productId + '_BLandNormarl_' + productNo);
		var thumbImage = $('#popup' + colour + productId);
			
		if (thumbImage.length > 0 && previewImage.length > 0) {		
			previewImage.attr('src', thumbImage.attr('src')); 
		}
	};
	
	Site.removeLoadingIcon = function (variantId) {
	   if ($('#loadingIcon_' + variantId)) {
	     $('#loadingIcon_' + variantId).remove();
     }
	};
	
	Site.showLoadingIcon = function (variantId, quantityId) {
	   var qty = $('#' + quantityId);
	   var loadingContainer = qty.parent().parent().children('td.pricecss');
	   
	   if (loadingContainer) {
	     loadingContainer.append('<img id="loadingIcon_' + variantId + '" src="/images/loading.gif" />');
     }
	};
	          	
	Site.updateColour = function (name, id, value) {
		$.ajax({
			url: 'AjaxProductDetail.aspx?ProductID='+CollectProductID,
			data: 'type=colour&VariantID='+value,
			dataType: "xml",
			success: function(data) {
				if (data.length == 0) {
					alert('No Products Found');
					return;
				}
			}
		});
	};
		
  Site.updateQuantity = function (element) {
    var colourIndex = element.selectedIndex;  
    
    if (colourIndex > 0) {
      var colourId = element.options[colourIndex].value;
      var variantId = 0;
      
      var rowId = element.id.replace("selectColor_","");     
      if (0 == $('#VariantList_' + rowId).length) {
        variantId = rowId; 
      } else {
        variantId = $('#VariantList_' + rowId).val();        
      }
           
		var quantityId = 'selectQuantity_' + rowId;
		
		Site.doStockCheck(variantId, colourId, quantityId);
     }
	};
	
	Site.doStockCheck = function (variantId, colourId, quantityId) {
      if (variantId && colourId && quantityId) {
        Site.showLoadingIcon(variantId, quantityId);
        
        $.ajax({
          url: 'AjaxProductStockCheck.aspx?ColourID='+colourId+'&VariantID='+variantId,
          dataType: "text",
          success: function(data) {
			Site.removeLoadingIcon(variantId);
			
			var qty = $('#' + quantityId);
			var selectOptions = qty.children('option');
			             
	          qty.val('-');
	            
	          if ('' != data) {            
	            var stockOptions = data.split(',');
	              
	            // first remove all options
	            selectOptions.remove();
	            qty.append('<option value="-">Select</option>');
	               
	            for (i = 0; i < stockOptions.length; i++) {
	              qty.append('<option value="' + stockOptions[i] + '">' +  stockOptions[i] + '</option>');
	            }
	          } else {
				selectOptions.remove();
	            	qty.append('<option value="-">Out of stock</option>');              
	          }            
          }
        });        
      }
			
  }; 
	
	Site.init();
});
