var doWatchLot;
var doSaveLot;

function watchLot(lotRef,mID,wwwURL)
	{
		//abort previous ajax call for this method if its still running
		var j=jQ(this);
		var watchHTML="<p class='saveLot left2'>You are <a href='" + wwwURL + "/myLive/myWatchedLots.cfm'>watching this lot</a></p>";
		if (doWatchLot) 
			{
				doWatchLot.abort();
			}
		doWatchLot = jQ.getJSON(
			'/lib/jQueryObj/jFineArtObj.cfc?wsdl',
				{ 
					method : 'watchLot',
					lotRef : lotRef,
					mID : mID
				},
				function(data)
					{
						if(data!="")
							{
								alert(data);
							}
						else
							{
								jQ("#results.lotDetails .callToAction .btns #watchThisLot").html(watchHTML);
							}
					}
		  );
	}

function saveLot(lotRef,mID,wwwURL)
	{
		//abort previous ajax call for this method if its still running
		var j=jQ(this);
		var saveHTML="<p class='saveLot left2'>Lot Saved in <a href='" + wwwURL + "/features/viewArtistAlertGallery.cfm?savedLots=true'>My Saved Lots</a></p>";
		if (doSaveLot) 
			{
				doSaveLot.abort();
			}
		doSaveLot = jQ.getJSON(
			'/lib/jQueryObj/jFineArtObj.cfc?wsdl',
				{ 
					method : 'saveLot',
					lotRef : lotRef,
					mID : mID
				},
				function(data)
					{
						if(data!="")
							{
								alert(data);
							}
						else
							{
								jQ("#results.lotDetails .callToAction .btns #saveMyLot").html(saveHTML);
							}
					}
		  );
	}

function showDetails()
	{
		var h=jQ(this).next(".fieldName").height();
		jQ(this).parent().animate({ height : h},500).children(".expand").removeClass("expand").addClass("hide").html("<a href='#'>- Hide</a>");
		 
		return false;
	}			

function hideDetails()
	{
		jQ(this).parent().animate({ height : "26px"},500).children(".hide").removeClass("hide").addClass("expand").html("<a href='#'>+ Expand</a>");
		 
		return false;
	}			

function showAddress() 
	{
		var $address=jQ("#locDetails");
		var j=jQ(this);
		var linkText =j.text();

		switch(linkText)
			{
				case "+ Show Location Details":
					j.html("<a href='##'>- Hide Location Details</a>");				
				break;
				
				case "- Hide Location Details":
					j.html("<a href='##'>+ Show Location Details</a>");
				break;
			}		
		$address.toggle(500);
		return false;
	}
	
function setExpand()
	{
		var j=jQ(this);
		var height =  j.height();
		if(height > 28) {
			j.parent().children(".expand").show("slow");
		}
	}

function closeModal()
	{	
		jQ("#modal").stop().fadeOut().remove();
		
		jQ("#opaque")
			.css({opacity:0})
			.fadeOut()
			.remove();
		jQ("#footerButtons").fadeIn("fast");
		
		jQ("#largePicFrame").stop().fadeOut().attr("src", "").css("marginLeft", "-9999px").css({
			width: 1,
			height: 1
		}).fadeIn("fast");
	}

function resizeIframe()
	{
		if(jQ(this).attr("src") == ""){return;}
		
		var newHeight = this.contentWindow.document.body.scrollHeight;
		var newWidth = this.contentWindow.document.body.scrollWidth;
		var newLeft = 485-(newWidth/2);
		var screenHeight = jQ(window).height();
		var newTop;

		if(screenHeight > newHeight)
			{
				newTop = (screenHeight/2)-(newHeight/2);
			}
		else
			{
				newTop = 0;
			}
			
		jQ("#modal").animate({
			height: newHeight,
			top: newTop
		}).animate({
			width: newWidth,
			left: newLeft
		}, function(){
			jQ("#largePicFrame").hide().css("marginLeft", 0).height(newHeight).width(newWidth).fadeIn("slow");
			jQ("#modal").removeClass("modalload");
		});	
	}

function thumbClick()
	{	
		jQ("#mainColumn").after("<div id='modal' class='modalload'></div>")/* .after("<div id='opaque'></div>") */;	
		jQ("#modal").append("<img id='closeWindow' src='/wwwImages/lotDetails/icnCloseWindow.png' alt='icnCloseWindow' width='30' height='30'/>");
		jQ("#modal #closeWindow").after("<iframe id='largePicFrame' src='' scrolling='no'></iframe>");
		jQ("body").append("<div id='opaque'></div>");
		var src = this.id;
		jQ("#largePicFrame").attr("src", src);
		jQ("#opaque")
			.css({opacity:0.5})
			.fadeIn();
		jQ("#footerButtons").fadeOut("fast");
		jQ("div#modal").addClass("modalload").fadeIn();
		jQ("#largePicFrame").load(resizeIframe);
	}

function thumbBox()
	{
		jQ("#thumbBox").toggle("slow");
	}

function onBorder()
	{
		jQ(this).css(
			{
				borderColor: "#000"
			}
		);
	}
	
function offBorder()
	{
		jQ(this).css(
			{
				borderColor: "#ccc"
			}
		);
	}

jQ(function(){	
	jQ("#results.lotDetails  p.expand").live("click",showDetails);
	jQ("#results.lotDetails  p.hide").live("click",hideDetails);
	jQ("#results.lotDetails p.showDetails").click(showAddress);
	jQ("#results.lotDetails div.descriptionText p.fieldName").each(setExpand);
	jQ("#modal #closeWindow, #opaque").live("click",closeModal);
	jQ(".modalThumb").live("click",thumbClick);
	//jQ("#largePicFrame").live("load",resizeIframe);
	jQ("#imgInfo, #closeThumbBox").click(thumbBox);
	jQ("#thumbBox ul li, #results.lotDetails .thumbBoxContainer #singleThumbBox").hover(onBorder, offBorder);
});

jQ(function(){
	var pdNew, pd, pdFinal, logo, newImg, dtls, newPrice, newContent;
	pdNew="<div id='printable'></div>";
	logo=jQ("#logo").clone();
	if ( jQ("#singleThumbBox").length)
		{
	/*  alert("Lot Details Page!"); */
			newImg = jQ("#singleThumbBox").clone();
			newPrice = jQ("#priceInfo").clone();
			dtls=jQ("#lotcontent").clone();
			pdFinal = jQ(pdNew).append(newImg).append(newPrice).append(dtls);
		}
	else if ( jQ("#catalogCoverTop").length)
		{
	/* alert("Auction Details Page!"); */
			newContent = jQ("#content").clone();
			pdFinal = jQ(pdNew).append(newContent);
		}
				
	
	jQ("body").append(pdFinal);
	jQ("#printable div, #printable span").each(function(){
		var newID;
		newID = jQ(this).attr("id");
		if (newID.length!==0){
			newID += "-printable";
			jQ(this).attr("id", newID);			
		}
	});
});