// JavaScript Document
$(function(){
	$("#wlallin").sortable({
		start: function(){$(".ajaxLayerCloseLink").click()},
		stop: sortItems,
		handle: ".wlsortdrag"
	});
	$("#wlnewlyAdded").sortable({
		connectWith:'#wlallin',
		stop: sortItems,
		handle: ".wlsortdrag"
	});
	
	$(".wlSBaddnote").live("click", function(){
		noteDaddy = $(this).parents(".wlnote");
		$(this).hide();
		textField = noteDaddy.find(".noteInput").show().find("input[type='text']").one("focus", function(){$(this).val("");});
		sampleFormText = textField.val();
		noteDaddy.find("form input[type='submit']").click(function(){
			if(textField.val() == sampleFormText || textField.val() == "") return false; else return true;
		});
		return false;
	});
	$(".wlSBsave").live("click", function(){
		noteForm = $(this).parents(".noteInput");
		$(this).parents(".noteInput").hide();
		DWRHelper.setItemNote($("#wishList").find("input[name='listId']").val(),
								$(this).parents(".ui-state-default").find("input[name='sku']").val(),
								$(this).parents(".ui-state-default").find("input[name='is']").val(),
								$(this).parents(".noteInput").find("input[type='text']").val(),
						function(){
							noteForm.siblings(".wlNoteText").show().find("p").text(noteForm.find("input[type='text']").val());
							noteForm.parents(".ui-state-default").find("input[name='hiddenNote']").val(noteForm.find("input[type='text']").val());
						});
		$(this).parents(".wlnote").find(".wlSBaddnote").remove();
		return false;
	});
	$(".wishQuantUpdate").live("click", function(){
		if($(this).siblings("#qty").val() == 0)
			alert('Please enter a valid quantity');
		else
		{
			$(this).loading();
			DWRHelper.setQtyWished($("#wishList").find("input[name='listId']").val(),
								$(this).parents(".ui-state-default").find("input[name='sku']").val(),
								$(this).parents(".ui-state-default").find("input[name='is']").val(),
								$(this).siblings("#qty").val(),
						$.proxy(function(data){
							dwr.util.setValue("wishListTotal", data, { escapeHtml:false });
							$(this).stopLoading();
						}, this));
		}
		return false;
	});
	$(".wlNoteText a").live("click", function(){
		$(this).parents(".wlNoteText").hide()
			.siblings(".noteInput").show()
			.find("input[type='text']").val($(this).siblings("p").text());
		return false
	});
	$("#printPage").click(function(){
		window.print();
		return false;
	});
	$(".wlSBdelete").live( "click", function(){
		deleteItem = $(this);
		DWRHelper.deleteFromWishList($("#wishList").find("input[name='listId']").val(),
								$(this).parents(".ui-state-default").find("input[name='sku']").val(),
								$(this).parents(".ui-state-default").find("input[name='is']").val(),
					function(data){
							dwr.util.setValue("wishListTotal", data, { escapeHtml:false });
							ajaxLoaderLayer.appendTo("body");
							deleteItem.parents(".addToCartForm").remove();
						});
		return false;
	});	
	$("input[name='PorP']").bind(($.browser.msie ? "click" : "change"), function(){
		if ($(this).val() == 'pub'){
			$(this).loading();
			DWRHelper.setIsShared($("#wishList").find("input[name='listId']").val(), true);
			$("#pubUrl").show();
		}else{
			$("#pubUrl").loading();
			DWRHelper.setIsShared($("#wishList").find("input[name='listId']").val(), false);
			$("#pubUrl").hide();
		}
		$("#ajaxLoadingImg").hide();
	});
	$(".noteInput input[type='text']").keypress(function(evt){
		  evt = (evt) ? evt : event;
		  var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
			  ((evt.which) ? evt.which : 0));
		  if(charCode == 13){
			  $(this).siblings("input[type='submit']").click();
			  return false;
		  }
		  return true;
	 });
	$("a.wlNoteEditCancel").live("click", function(){
		daddy = $(this).parents(".wlnote");
		if(daddy.find(".wlSBaddnote").length){ //if "create note" button exists
			daddy.find(".wlSBaddnote").show();
		}else{
			daddy.find(".wlNoteText").show();
		}
		$(this).parents(".noteInput").hide();
		return false;
	});
	$("#pubUrl").click(function(){
		$(this).focus();
		$(this).select();
	});
	$("#wlDetails input.wlPrchDate").datepicker({dateFormat: "MM d yy"});
	$("#wlDetails input.wlPrchDate").click(function(){$(this).focus();});
	$("a.prodNote").click(function(){
		$(this).parents(".impNoteHolder").find(".hiddenNote").show();
		return false;
	});
	$("a.prodAvail").click(function(){
		$(this).parent().siblings(".impNoteHolder").show().find(".hiddenNote").show();
		return false;
	});
	$(".hiddenNote a.hiddenNoteCloseLink").click(function(){
		$(this).parents(".hiddenNote").hide();
		return false;
	});
	$("#editWishDetlsBtn").click(function(){
		$(this).hide();
		$("#wlDisplayDetails").slideUp(400, function(){
			$("#wlEditDetails").slideDown(400);
		});
	});
	$("a#wishSaveCancel").click(function(){
		$("#editWishDetlsBtn").show();
		$("#wlEditDetails").slideUp(400, function(){
			$("#wlDisplayDetails").slideDown(400);
		});
		return false;
	});
	$(".addAddrss a").click(function(){
		$("#wlNewAdres").slideDown();
		$(".addAddrss").hide();
		$(".existingAddress").hide();
		$(".existAddrss").show();
		$("#new").val('new');
		return false;
	});
	$(".existAddrss a").click(function(){
		$("#wlNewAdres").slideUp();
		$(".addAddrss").show();
		$(".existingAddress").show();
		$(".existAddrss").hide();
		$("#new").val('');
		return false;
	});
	$(".addtcrt .quantity input[type='text']").keypress(function(evt){
		  evt = (evt) ? evt : event;
		  var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
			  ((evt.which) ? evt.which : 0));
		  if (charCode > 31 && (charCode < 48 || charCode > 57)) {
			  alert("Enter numbers only in this field.");
			  return false;
		  }
		 if(charCode == 13){
			 $(this).siblings("input.wishQuantUpdate").click();
				return false;
		 }
		  return true;
	  });
	$("#deleteConfirm").bhLayer({openerSelector: 0})
		.find("span.sprBtnYES").click(function(){
		newLoc = $("#wishListTotal a.wlSBdeleteall").attr("href");
		window.location = newLoc;
	});
	$("#wishListTotal a.wlSBdeleteall").click(function(){
		$("#deleteConfirm").bhLayer("show");
		return false;
	}); 
	if($.browser.msie){
		$(".atcLayerLoader, .atwLayerLoader").load(function(){
			layerTop = $(this).offset().top;
			layerBottom = layerTop + $(this).find(".ajaxLayer").height() + 45;
			$(".alParent").each(function(){
				if($(this).offset().top < layerBottom && $(this).offset().top > layerTop){
					$(this).css("position", "static").addClass("staticDude");
				}
			});
			$(this).find(".ajaxLayerCloseLink").one("click", function(){
				 $(".staticDude").css("position", "relative").removeClass("staticDude");
			});
			
		});
		$("#wlallin, #wlnewlyAdded").bind("sortstart", function(){
			$(this).find(".alParent").css("position", "static")
				.find("input[type='submit']").one("click", function(){
					$(this).parents(".alParent").css("position", "relative");
				}
			);
		});
	}
	$(".wlSBmove").live("click", function(){
		ajaxLoaderLayer.data("currButton", $(this).parents(".alParent"));
		$(".openBhLayer").bhLayer("autoHide");
		$(this).loading();
		//myLoader = $(this).parent().siblings(".atwLayerLoader");
		//myButton = $(this);
		$(this).findInAlParent(".atwLayerLoader").append(ajaxLoaderLayer);
		formElems = $(this).parents(".addToCartForm");
		DWRHelper.getWishListsForMoveWishListItem(formElems.find("[name=sku]").val(),formElems.find("[name=is]").val(),
		 formElems.find("[name=q]").val(),$("#wishList").find("input[name='listId']").val(),
		  function(data) {
		    dwr.util.setValue("atcAtwLayerMainContent", data, { escapeHtml:false });
		    ajaxLoaderLayer.bhLayer("show"); 			
		    //myLoader.find(".atwLayer").load().show(1,runLive); 
		});
		return false;
	});
	
	$(".wlSBmoveall").live("click", function(){
											 
		var amountOfItemChecked = $("input:checkbox[value=selectedItem]:checked").length;
		
		if(amountOfItemChecked == 0){
			$(this).siblings(".moveItemWLLayerTopError").bhLayer("show");
		}else{
				
			ajaxLoaderLayer.data("currButton", $(this).parents(".alParent"));
			$(".openBhLayer").bhLayer("autoHide");
			$(this).loading();
			$(this).findInAlParent(".atwLayerLoader").append(ajaxLoaderLayer);
			/*myLoader = $(this).siblings(".atwLayerLoader");
			myButton = $(this);*/
			
			DWRHelper.getWishListsForMoveWishList($("#wishList").find("input[name='listId']").val(),
			 function(data) {
				dwr.util.setValue("atcAtwLayerMainContent", data, { escapeHtml:false });
				ajaxLoaderLayer.bhLayer("show"); 
				$(".amountOfItemToMove").text( amountOfItemChecked );
			});
		}
		return false;
	});
		
});


function sortItems(){
	$("#wlallin").find(".wlunsorted").addClass("wlsorted").removeClass("wlunsorted");
	
	var newList = new Array();
	var count=0;
	var children = getImmediateChildren(document.getElementById('wlallin'), 'form');
	
	for(; count < children.length; count++) {
		itemId = children[count].sku.value + "-" + children[count].is.value;
	 	document.getElementById(itemId).innerHTML = count + 1;
	 	children[count].setAttribute("priority", count +1 ); 
		newList[count] = itemId;
    }
     
	if(count == 0)
	{
		firstItem = $("#wlnewlyAdded .wlunsorted:first");
		if(firstItem.find("input[name='sku']").val() != undefined)
		{
			newList[0] = firstItem.find("input[name='sku']").val() + '-' +firstItem.find("input[name='is']").val();
			$("#wlallin").append(firstItem.parent());
			firstItem.addClass("wlsorted").removeClass("wlunsorted").find(".wlsortlist").html('1');
		}
	}
	
	DWRHelper.setItemsPriorities($("#wishList").find("input[name='listId']").val(),newList);
	/*if($.browser.msie){
		$("#wishList").css("float", "left");
	}*/
}
function getImmediateChildren(node, tagName)
{
	if(!node || !node.childNodes) return new Array();
	if(!tagName) tagName = "*";
	var elements = document.getElementsByTagName(tagName);
	var nodeCount = elements.length;
	var children = new Array();
	for(var i=0;i<nodeCount;i++)
	{
		if(elements[i].parentNode == node)
		{
			children[children.length] = elements[i];
		}
	}
	return children;
}
