
function scanRemove(A,u) {
  //remove u item from A array
var i=-1, j=-1, k=A.length
while (++j < k) if (!(A[j]===u)) A[++i]=A[j]
A.length = ++i
return A }


function rcPut(newR) {
	// how many do we want to remember?
	var count = 12; // # to remember
	var delim = "|"; // delimiter for cookie

	// get the previous cookie (if any), split it into an array
	var visited = ($.cookie("r-closet") || "").split(delim);

		  scanRemove(visited,newR);
            visited.push(newR);
            	// remove the oldest, down to our limit
            if (visited.length >= count) {
                visited = visited.slice(visited.length - count);
            }
            // set the new cookie
            $.cookie("r-closet", visited.join(delim), { expires: 1500, path: '/' });
	rcShow();
	return false;
}
function rcRemove(oldR){
  	var delim = "|"; // delimiter for pages in cookie
	// get the previous cookie (if any), split it into an array
	var visited = ($.cookie("r-closet") || "").split(delim);
	scanRemove(visited,oldR);
	$.cookie("r-closet", visited.join(delim), { expires: 1500, path: '/' });
	return true;
}

function rcShow() {
  	var delim = "|"; // delimiter for pages in cookie
	// get the previous cookie (if any), split it into an array
	var rcItems = ($.cookie("r-closet") || "").split(delim);
    var rCloset = "<ul class='rCloset'>";
	var hProto = (("https:" == document.location.protocol) ? "secure" : "http://");
	var imgHost = "halloweencostumes.costumestore.com/"
	var rCount=0;
    for (i in rcItems.reverse())
        {
            var rcItem=rcItems[i].split(':');
            var rcCode=rcItem[0];
            var rcSku=rcItem[1];
			if (rcCode>0) {
			  rCount++;
			  if (hProto=="secure") {
				rCloset+='<li><a href="/p-'+rcCode+'-.aspx"><img data="'+rcItems[i]+'" src="/images/product/icon/'+rcSku+'.jpg" width="80" /></a></li>';			  
			  } else {
				rCloset+='<li><a href="/p-'+rcCode+'-.aspx"><img data="'+rcItems[i]+'"  src="'+hProto+imgHost+rcSku+'_01_Sm.jpg" width="80" /></a></li>';
			  }
			}
        }
    rCloset += "</ul>"
	if (rCount == 1) {
	  $('.r-closet-contents').css('width','86px');
	}
    if (rCount) {
  	  $('.r-closet-contents').html(rCloset);
	  $('.r-closet-button').html('<img id="yourcloset" alt="" src="/skins/Skin_1/images/cs_elements/your_closet_tab.png" />');


	     // options
    var distance = 5;
    var time = 10;
    var hideDelay = 400;
    var hideDelayTimer = null;

    // tracker
    var beingShown = false;
    var shown = false;
    
    var trigger = $('.r-closet-button');
    var popup = $('.r-closet-box'); //.css('opacity', 0);

    // set the mouseover and mouseout on both element
    $([trigger.get(0), popup.get(0)]).mouseover(function () {
      // stops the hide event if we move from the trigger to the popup element
      if (hideDelayTimer) clearTimeout(hideDelayTimer);

      // don't trigger the animation again if we're being shown, or already visible
      if (beingShown || shown) {
        return;
      } else {
        beingShown = true;
		$('#yourcloset').attr({ src: "/skins/Skin_1/images/cs_elements/your_closet_tab_roll.png"});
        // reset position of popup box
        popup.css({
          top: 32,
          //left: -33,
          display: 'block' // brings the popup back in to view
        })

        // (we're using chaining on the popup) now animate it's opacity and position
        .animate({
          top: '-=' + distance + 'px'
          //opacity: 1
        }, time, 'swing', function() {
          // once the animation is complete, set the tracker variables
          beingShown = false;
          shown = true;
        });
      }
    }).mouseout(function () {
      // reset the timer if we get fired again - avoids double animations
      if (hideDelayTimer) clearTimeout(hideDelayTimer);
      
      // store the timer so that it can be cleared in the mouseover if required
      hideDelayTimer = setTimeout(function () {
        hideDelayTimer = null;
        popup.animate({
          top: '+=' + distance + 'px'
          //opacity: 0
        }, time, 'swing', function () {
          // once the animate is complete, set the tracker variables
          shown = false;
          popup.css('display', 'none');
		  $('#yourcloset').attr({ src: "/skins/Skin_1/images/cs_elements/your_closet_tab.png"});
        });
      }, hideDelay);
    });

	  $('.rCloset li').each(function () {
		var rCli=$(this);
		var rCimg=$("img", this);
	    //var hideDelay = 400;
		//var hideDelayTimer = null;
		$(rCli).append("<img class='rCloser' src='/skins/Skin_1/js/resources/remove.png' />");
		 $(rCli.get(0), trigger.get(0)).mouseover(function () {
			$('.rCloser', rCli).css({display: 'block'});			
		  }).mouseout(function () {
			$('.rCloser', rCli).css({display: 'none'});
		 });
		 $('.rCloser',rCli).click(function () {
			//if we can remove the cookie, then remove the row
			if (rcRemove($(rCimg).attr("data"))) $(rCli).remove();
			// ****TODO**** also we should consider hiding the closet completely if the last item is removed, maybe resizing if there is only 1 item
		 });
	  });

	}
    return false;
}






