// javascript for quick-shop functions
// relies on jQuery -- jquery-1.3.2.js


var current=1;



		/********************************************************
			POST AJAX LOOKUP OF ITEM NAME and SKU VARIATIONS
		*********************************************************/

		$.fn.doItemLookup = function(item_id, node_id) {
					
					// find offset and show " ... searching... " panel
					var offset = $("input[name="+node_id+"]").offset();
					$("div#pendingLayer").css("top",(offset.top)+5);
					$("div#pendingLayer").css("left",(offset.left)+70);
					$("div#pendingLayer").fadeIn("50");
					
			var map = {}; 
			map['item_id'] = item_id;
			map['ajax'] = 'Y';
			
			
			$.post("/shop/QuickShop", map,
  				function(data){
					$().doItemResponse(data,node_id);
				},"xml");

		}
		
		
		
		
		/************************************************************
			HANDLE AJAX RESPONSE OF ITEM NAME and SKU VARIATIONS
		*************************************************************/

		$.fn.doItemResponse = function(data, target_node) {
					
			var list = data.getElementsByTagName("item");
			
			if(list.length>0){
				
				// if only one item, show it as final result		
				// othewise, make sku-choice layer
				if(list.length==1){
					$().renderFinishedItem(target_node,list[0].getAttribute("item_id"),list[0].getAttribute("name"),list[0].getAttribute("avail"));
				} else {
				
					// empty any choices from last use
					$("ul#choice_list").find("li[name]").each(function(i){
						$(this).remove();
					});
					
					// fill with sku-choice entries from this ajax response
					for(var i=0; i<list.length; i++){
									
						var choiceAnchor = $("a#sku");
						
						choiceAnchor.attr("type", target_node);
						choiceAnchor.attr("id", list[i].getAttribute("item_id"));
						choiceAnchor.attr("date", list[i].getAttribute("avail"));
						choiceAnchor.html(list[i].getAttribute("item_id")+ " : " +list[i].getAttribute("name"));
						
						var choiceLine = $("li#sku_li").clone(true);
						choiceLine.removeAttr("id");
						choiceLine.attr("name","cloned");
						$("li#sku_li").before(choiceLine);
						choiceLine.show();

						choiceAnchor.attr("id","sku");						
					}
					
					// find offset and render panel
					$("div#pendingLayer").hide();
					var offset = $("input[name="+target_node+"]").offset();
					$("div#Layer3").css("top",(offset.top)+5);
					$("div#Layer3").css("left",(offset.left)+70);
					$("div#Layer3").fadeIn("50");
				
					
				} // end if single or multiple results
				
			} // end if at least one result

		} // end doItemResponse function
		
	
	
		/***********************************
		  PUT A CHOSEN ITEM IN THE LINE
		***********************************/
		$.fn.renderFinishedItem = function(node_id, item_id, item_name, date_str) {
			$("div#pendingLayer").hide();
			$("input[name="+node_id+"]").val(item_id);
			$("span#nameof"+node_id).html(item_name);
			$("span#dateof"+node_id).html(date_str);
		}


// start event triggers
$(document).ready(function(){
   


   /********************************************
   			 key up event on item id field
   	********************************************/
   	$("input[name^=item]").keyup(function(){
		
			// determine which <input> tag we're dealing with and its contents
			var node_id = $(this).attr("name");
			var item_id = $(this).val();
			
			// if item_id is at least four chars, check
			if(item_id.length > 3){
				$().doItemLookup(item_id, node_id);
			}	
	});
		
		
		
		     
   /********************************************
   		choice <li> clicked on sku-choice layer
   	********************************************/

   	$("a.sku_choice").click(function(){
	
		$("div#Layer3").fadeOut("50");
		var node_id = $(this).attr("type");
		var item_id = $(this).attr("id");
		var date_str = $(this).attr("date");
		var item_name = jQuery.trim($(this).text().split(" : ")[1]);
		$().renderFinishedItem(node_id,item_id,item_name,date_str);

	});
 
   	
   	
   	
   /*******************************************
         'ADD MORE LINES' LINK CLICKED
    *******************************************/
   			
   	$("a#add-more-lines").click(function(){
   	
   			current++;
   			
	
   			var $cln = $("table#item-grid").clone(true);
			$("table#item-grid").after($cln);
			$cln.removeAttr("id");

			$cln.find("span,input").each(function(i){
			
			if($(this).attr("id").length){
	
					$(this).attr("id", $(this).attr("id").replace(/-1-/,"-"+current+"-"));

					if($(this).attr("id").indexOf("item-")>-1){
						$(this).val("");
					}
					if($(this).attr("id").indexOf("nameofitem")>-1){
						$(this).html("");
					}
					if($(this).attr("id").indexOf("dateofitem")>-1){
						$(this).html("");
					}					
				}
				
				if($(this).attr("name")){
					
					$(this).attr("name", $(this).attr("name").replace(/-1-/,"-"+current+"-"));
					
					if($(this).attr("name").indexOf("item-")>-1){
						$(this).val("");
					}

				}
					
				
			});
   
   		});
});
// end event triggers





		

		