$(document).ready(function(){
  /*$('#scrollOpts li').each(function(){
  		getScroller($(this).attr('id'));	
  });*/
	
  $(".datepicker").datepicker({
    showOn: "both",
    buttonImage: "/images/calendar.gif",
    buttonImageOnly: true
  });

  $("#filterOrder").change(function(){
    $("#filterForm").submit();
  });

  $("#scrollerList").jCarouselLite({
    auto: 800,
    speed: 800,
    scroll: 1,
    visible: 10
  });
  
  displayDetail();
  
  //makeButton($('#btnContinueShoppingTop'),'120px');
  //makeButton($('#btnCheckOutNowTop'),'180px');
});

var WindowObject = null;
function imagepopup(image,w,h) {
	var strUrl = 'images/product/large/' + image + '.jpg';
	if (WindowObject == null || WindowObject.closed) {
		WindowObject = window.open(strUrl,'Popup9524','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width='+w+',height='+h+',left=0,top=0');
		WindowObject.focus();
		return true;
	} else {
		WindowObject = window.open(strUrl,'Popup9524','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width='+w+',height='+h+',left=0,top=0');
		WindowObject.resizeTo(w+6,h+49);
		WindowObject.focus();
		return true;
	}
}

function displayDetail(){
	var xOffset = 231;
	var yOffset = 4;
	var t = null;
	$(".displayDetail").hover(function(e){
    //$("#scrollerList").trigger('stop');
    t = $(this).children("a").children("img.prodImgIcon").attr("alt");
    $(this).children("a").children("img.prodImgIcon").attr("alt", "");
    $(document).trigger("forceStop");
    $("#staticDetail").remove();
    var staticDetail = $('<div id="staticDetail">' + $(this).children(".hiddenDetail").html() + '</div>').css("top", (e.pageY + yOffset)+"px").css("left", (e.pageX - xOffset)+"px").hide();
    $("body").append(staticDetail.fadeIn("fast"));
  }, function(){
    //$("#scrollerList").trigger('start');
    $(this).children("a").children("img.prodImgIcon").attr("alt", t);
    t = null;
    $(document).trigger("forceStart");
    $("#staticDetail").fadeOut("fast", function(){ $(this).remove(); });
  });
  $(".displayDetail").mousemove(function(e){
    $("#staticDetail").css("top", (e.pageY + yOffset)+"px").css("left", (e.pageX - xOffset)+"px");
  });
}

function getScroller(idString){
	var qry = {
		New: 0,
		Closeout: 0,
		Discount: 0,
		Subjects: 0
	}
	switch (idString) {
		case "new":
			qry.New = 1;
			break;
		case "close":
			qry.Closeout = 1;
			break;
		case "best":
			qry.New = 0;
			break;
		case "top":
			qry.Discount = 1;
			break;
		case "sub":
			qry.Subjects = 1;
			break;
	}
	$('#' + idString).click(function(){
		$.ajax({
			type: "GET",
			url: "ScrollerService.asmx/getScroller",
			data: qry,
			success: function(xml){
				$('#scrollerList').empty();
				var ul = $('<ul>');
				product = $(xml).find('ScrollerProduct');
				$('#scrollOpts li').removeClass('current');
				product.each(function(){
					var name = $('Name', $(this)).text();
					var url = $('Url', $(this)).text();
					var sku = $('SKU', $(this)).text();
					var imgSmall = $('SmallImageSrc', $(this)).text();
					var imgIcon = $('IconImageSrc', $(this)).text();
					var author = $('AuthorName', $(this)).text();
					var authorUrl = $('AuthorUrl', $(this)).text();
					var msrp = $('Msrp', $(this)).text();
					var price = $('Price', $(this)).text();
					var discount = $('Discount', $(this)).text();
					var li = '<li class="displayDetail"><a href="' + url + '"><img class="prodImgIcon" src="' + imgIcon + '" alt="' + name + '" /></a><div class="hiddenDetail"><a href="' + url + '"><img class="prodImgSmall" src="' + imgSmall + '" alt="' + name + '" /></a><p><a href="' + url + '">' + name + '</a><br />by: <a href="' + authorUrl + '">' + author + '</a><br />Retail: <span class="detailMsrp">' + msrp + '</span><br /><span class="detailSell">Our Price: ' + price + '</span><br />';
					li += '<span class="detailDiscount">' + discount + 'Off!</span></p></div></li>';
					ul.append(li);
				});
				
				$("#scrollerList").append(ul);
				$("#scrollerList").jCarouselLite({
					auto: 800,
					speed: 800,
					scroll: 1,
					visible: 10
				});
				
				$('#' + idString).addClass('current');
				displayDetail();
			}
		});
	});
}

function makeButton(inputObject,width) {
	inputObject.parent('td').append('<br class="clr" />');
	$('.clr').css({'clear':'both','line-height':'1px'});
	inputObject.parent('div').css({'width':width});
}

