// JavaScript Document

var PhotoModule = null;
var showPhotoRegion = null;
var QuickViewModule = null;
var uploadDialog = null;
var toolTip = null;
var PictureModule = null;
var lastWineSearch = "";

var DownArrowGray = new Image;
var DownArrow = new Image;
DownArrow.src="/Images/downarrow.gif";
DownArrowGray.src="/Images/downarrowgray.gif";
var UpArrowGray = new Image;
var UpArrow = new Image;
UpArrow.src="/Images/uparrow.gif";
UpArrowGray.src="/Images/uparrowgray.gif";

var uploadImage = new Image;
var uploadImageOff = new Image;
uploadImage.src="/Images/uploadButton.gif";
uploadImageOff.src="/Images/uploadButtonOff.gif";

var MagModule = null;
var bigImageOut = 1;
var overImage = 0;

function regionMouseOver(e, imageElement)
{
	this.className = 'tabTextOver'; 
	if (imageElement)
	{
		imageElement.src = DownArrowGray.src;
	}
}
function regionMouseOut(e, imageElement)
{
	this.className = 'tabText'; 
	if (imageElement)
	{
		imageElement.src = DownArrow.src;
	}
}
function regionMouseOverUp(e, imageElement)
{
	this.className = 'tabTextOver'; 
	if (imageElement)
	{
		imageElement.src = UpArrowGray.src;
	}
}
function regionMouseOutUp(e, imageElement)
{
	this.className = 'tabText'; 
	if (imageElement)
	{
		imageElement.src = UpArrow.src;
	}
}

// ----------------------------------------------------- Photo expand and contract ---------------------------
function showPhotos()  
{
	if (!PhotoModule.cfg.getProperty("visible"))
	{
		PhotoModule.show();
		document.getElementById("showPhotoRegion").innerHTML = "<img id=\"upArrowPhoto\" src=\"/Images/uparrow.gif\" alt=\"up arrow\"/>Hide Additional Photos";
		YAHOO.util.Event.addListener("showPhotoRegion", "mouseover", regionMouseOverUp, document.getElementById("upArrowPhoto"));
		YAHOO.util.Event.addListener("showPhotoRegion", "mouseout", regionMouseOutUp, document.getElementById("upArrowPhoto"));
	}
	else
	{
		PhotoModule.hide();
		document.getElementById("showPhotoRegion").innerHTML = "<img id=\"downArrowPhoto\" src=\"/Images/downarrow.gif\" alt=\"down arrow\"/>Show All Photos";
		YAHOO.util.Event.addListener("showPhotoRegion", "mouseover", regionMouseOver, document.getElementById("downArrowPhoto"));
		YAHOO.util.Event.addListener("showPhotoRegion", "mouseout", regionMouseOut, document.getElementById("downArrowPhoto"));
	}
	document.getElementById("showPhotoRegion").className = 'tabText';
}


var LoadingContainer = 
		new YAHOO.widget.Panel("loadingDialog",  
			{ width:"80px", 
			  fixedcenter:true, 
			  close:false, 
			  draggable:false, 
			  zindex:15000,
			  visible:false
			} 
	);


// ------------------------------------ Warning Dialog -----------------------------------------------------------

var wineWarning = function(warningText, header)
{
	if (!header)
	{
		header = "Retailer Issue"
	}
	wineWarningDialog.setHeader("<div class=\"hd\"><div class=\"tl\"></div><span>" + header + "</span><div class=\"tr\"></div></div>");

	var bodyText = '<div class="dialogWrap"><table class="popupTable" border="0" cellspacing="0" cellpadding="0"><tr><td>' + warningText + '</td></tr></table></div>';
	
	wineWarningDialog.setBody(bodyText);
	wineWarningDialog.render(document.body);		
	wineWarningDialog.show();
}
var handleOK = function() 
{
	wineWarningDialog.hide();
	LoadingContainer.hide();
}

var wineWarningDialog = new YAHOO.widget.SimpleDialog("warningDialog", 
						{	 
						width: "350px", 
//							effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.25}, 
							fixedcenter:true,
					    	zIndex:15000,
					  		modal: true,
	              			constraintoviewport : true, 
						    visible:false,
							draggable:false, 
							buttons: [ { text:"OK", handler:handleOK, isDefault:true }]
						});	

// ------------------------------------ Zoom for Images ----------------------------------------------

// when they clicked on a zoomed in event image
function wineImageOut()
{
	bigImageOut = 1;
	document.getElementById("bigImage").style.cursor = "default";
	document.getElementById("picMod").style.cursor = "default";
	PictureModule.hide();
}

var magnifyOver = function()
{
	timeoutID = setTimeout("magnifyStillIn()",500);
	overImage = 1;
	document.getElementById("magnifyImg").src='/wineempty.gif';
};
var magnifyOut = function(o)
{
	document.getElementById("magnifyImg").style.cursor = "url('/magcursor.cur'), default";
	document.getElementById("magnifyImg").src='/wineempty.gif';
	overImage = 0;
	clearTimeout(timeoutID);
};
function magnifyStillIn()
{
	wineBigImageIn();
	overImage = 2;
}

var wineSmallImageOut = function(o)
{
	document.getElementById("magnifyImg").src='/wineempty.gif';
};

var wineImageLoaded = function(o)
{
	PictureModule.show();	
};
// show zoomed in event image
function wineImageIn(imageElement, bigImage)
{
	bigImageFile = bigImage;
	xyID = imageElement.id;
	x = YAHOO.util.Dom.getX(xyID);
	y = YAHOO.util.Dom.getY(xyID);
	
	PictureModule.cfg.setProperty("x", x);
	PictureModule.cfg.setProperty("y", y);
	
	if (bigImageOut == 0)  // if it's 0, then we never left the zoom feature, so load the new image.
	{
		wineBigImageIn();
	}
	else
	{
		MagModule.cfg.setProperty("x",x+50);
		MagModule.cfg.setProperty("y",y+50);	
	MagModule.setBody("<div class=\"magModule\"><img id='magnifyImg' onmouseover='magnifyOver();' onmouseout='magnifyOut();' onclick='wineBigImageIn();' src='/macgif2.gif'></div>");
		MagModule.render(document.body);													  
		MagModule.show();
	}
}
var wineBigImageIn = function()
{		
	bigImageOut = 0;
	PictureModule.setBody("<div id=\"picMod\" class=\"pictureModule\"><img id=\"bigImage\" src=\""+bigImageFile+"\" /></div>");
	document.getElementById("bigImage").style.cursor = "url('/zoomout.cur'), default";
	document.getElementById("picMod").style.cursor = "url('/zoomout.cur'), default";
	YAHOO.util.Event.addListener("bigImage", "load", wineImageLoaded);   // once the wine image is loaded, we're good to show
	document.getElementById("magnifyImg").style.cursor = "url('/zoomout.cur'), default";
};


// ------------------------------------ Delete Photo -----------------------------------------------------------

var deletePhotoStart = function(o)
{ 
	LoadingContainer.show();
};
var deletePhotoSuccess = function(o)
{ 
	var Response = o.responseText;
	if (Response.substring(0,7) == "[error]")
	{
		wineWarning(Response.substring(7,Response.length));	
	}
	else
	{
		// now call photo layout script		
		var callback = 
		{
			success: photoLayoutSuccess,
			failure: photoLayoutFailure,
			customevents: { onStart: photoLayoutStart },
			timeout: 5000
		}
		
		YAHOO.util.Connect.initHeader('X-Signature', transaction_sig); 
		var cObj = YAHOO.util.Connect.asyncRequest('POST', '/photolayout_s.php', callback, 'type=13&uniqueID='+byobID)	}
};
var deletePhotoFailure = function(o)
{ 
	LoadingContainer.hide();
	wineWarning("There was a communication error on the WineMcGee servers. Please try again later.");
};

function deletePhoto(photo_id)
{
	var callback = 
	{
		success: deletePhotoSuccess,
		failure: deletePhotoFailure,
		customevents: { onStart: deletePhotoStart },
		timeout: 5000
	}
		
	YAHOO.util.Connect.initHeader('X-Signature', transaction_sig); 
	var cObj = YAHOO.util.Connect.asyncRequest('POST', '/deletephoto_s.php', callback, 'photoID='+photo_id); 	
}

// ------------------------------------ Delete Comment -----------------------------------------------------------
var deleteCommentStart = function(o)
{ 
	LoadingContainer.show();
};
var deleteCommentSuccess = function(o)
{ 
	var Response = o.responseText;
	if (Response.substring(0,7) == "[error]")
	{
		wineWarning(Response.substring(7,Response.length));	
	}
	else
	{
		// now call load comments script		
		callback = 
		{
			success: commentsSuccess,
			failure: commentsFailure,
			customevents: { onStart: commentsStart },
			timeout: 5000
		}
		
		YAHOO.util.Connect.initHeader('X-Signature', transaction_sig); 
		var cObj = YAHOO.util.Connect.asyncRequest('POST', '/loadcomments_s.php', callback, 'uniqueID='+byobID+'&type=6'); 								
	}
};
var deleteCommentFailure = function(o)
{ 
	LoadingContainer.hide();
	wineWarning("There was a communication error on the WineMcGee servers. Please try again later.");
};

function deleteComment(comment_id)
{
	var callback = 
	{
		success: deleteCommentSuccess,
		failure: deleteCommentFailure,
		customevents: { onStart: deleteCommentStart },
		timeout: 5000
	}
		
		YAHOO.util.Connect.initHeader('X-Signature', transaction_sig); 
		var cObj = YAHOO.util.Connect.asyncRequest('POST', '/deletecomment_s.php', callback, 'commentID='+comment_id); 	
}
// ------------------------------------ Report Abuse -----------------------------------------------------------
var reportAbuseStart = function(o)
{ 
	LoadingContainer.show();
};
var reportAbuseSuccess = function(o)
{ 
	var Response = o.responseText;
	if (Response.substring(0,7) == "[error]")
	{
		wineWarning(Response.substring(7,Response.length));	
	}
	else
	{
		wineWarning("Thank you for reporting abuse.  We will review this posting.");	
	}
};
var reportAbuseFailure = function(o)
{ 
	LoadingContainer.hide();
	wineWarning("There was a communication error on the WineMcGee servers. Please try again later.");
};

function reportAbuse(comment_id)
{
	var callback = 
	{
		success: reportAbuseSuccess,
		failure: reportAbuseFailure,
		customevents: { onStart: reportAbuseStart },
		timeout: 5000
	}
	
	YAHOO.util.Connect.initHeader('X-Signature', transaction_sig); 
	var cObj = YAHOO.util.Connect.asyncRequest('POST', '/reportabuse_s.php', callback, 'commentID='+comment_id+'&type=6'); 	
}

// ------------------------------------ Post Comment -----------------------------------------------------------
var postCommentStart = function(o)
{ 
	LoadingContainer.show();
};
var postCommentSuccess = function(o)
{ 
	var Response = o.responseText;
	if (Response.substring(0,7) == "[error]")
	{
		wineWarning(Response.substring(7,Response.length));	
	}
	else
	{
		// now call load comments script		
		callback = 
		{
			success: commentsSuccess,
			failure: commentsFailure,
			customevents: { onStart: commentsStart },
			timeout: 5000
		}
		
		YAHOO.util.Connect.initHeader('X-Signature', transaction_sig); 
		var cObj = YAHOO.util.Connect.asyncRequest('POST', '/loadcomments_s.php', callback, 'uniqueID='+byobID+'&type=6'); 								
		
		document.getElementById("commentText").value = ""; // clear comment text
	}
};
var postCommentFailure = function(o)
{ 
	LoadingContainer.hide();
	wineWarning("There was a communication error on the WineMcGee servers. Please try again later.");
};

function postComment()
{
	comment = document.getElementById("commentText").value;
	if (comment.length > 0 && comment!="Enter your answer")
	{
		var callback = 
		{
			success: postCommentSuccess,
			failure: postCommentFailure,
			customevents: { onStart: postCommentStart },
			timeout: 5000
		}
		comment = comment.replace("&", "%26");	
		YAHOO.util.Connect.initHeader('X-Signature', transaction_sig); 
		var cObj = YAHOO.util.Connect.asyncRequest('POST', '/postcomment_s.php', callback, 'comment='+comment+'&uniqueID='+byobID+'&type=6'); 	
	}
}

// ------------------------------------ Comments -----------------------------------------------------------
	
var commentsStart = function(o)
{ 
	LoadingContainer.show();
};
var commentsSuccess = function(o)
{ 
	var Response = o.responseText;
	if (Response.substring(0,7) == "[error]")
	{
		wineWarning(Response.substring(7,Response.length));	
	}
	else
	{
		document.getElementById("comments").innerHTML = o.responseText;
		
		// set up tool tips
			
			/*
		var elements = YAHOO.util.Dom.getElementsByClassName('theEventImage', 'img'); 	
		// img is optional and speeds up the search		
		var imgElementArray = new Array();
		var a = 0;
		for (a = 0; a < elements.length; a++)
		{
			imgElementArray[a] = elements[a].id;
		}	
		
		if (toolTip)
		{
			toolTip.cfg.setProperty("context", imgElementArray);
			toolTip.forceUnderlayRedraw();
		}
		else if (imgElementArray.length > 0)
		{
			toolTip = new YAHOO.widget.Tooltip("tooltip", 
											{ context: imgElementArray,
									  		  effect: {effect:YAHOO.widget.ContainerEffect.FADE,duration:0.20}
											});					
		}
		*/
	}
	LoadingContainer.hide();
};
var commentsFailure = function(o)
{ 
	LoadingContainer.hide();
	wineWarning("There was a communication error on the WineMcGee servers. Please try again later.");
};
	
// ------------------------------------ Photo Layout -----------------------------------------------------------
	
var photoLayoutStart = function(o)
{ 
	LoadingContainer.show();
};
var photoLayoutSuccess = function(o)
{ 
	var Response = o.responseText;
	if (Response.substring(0,7) == "[error]")
	{
		wineWarning(Response.substring(7,Response.length));	
	}
	else
	{
		document.getElementById("photoArea").innerHTML = o.responseText;
		
		// set up tool tips
			
		/*
		var elements = YAHOO.util.Dom.getElementsByClassName('theEventImage', 'img'); 	
		// img is optional and speeds up the search		
		var imgElementArray = new Array();
		var a = 0;
		for (a = 0; a < elements.length; a++)
		{
			imgElementArray[a] = elements[a].id;
		}	

		if (toolTip)
		{
			toolTip.cfg.setProperty("context", imgElementArray);
			toolTip.forceUnderlayRedraw();
		}
		else if (imgElementArray.length > 0)
		{
			toolTip = new YAHOO.widget.Tooltip("tooltip", 
											{ context: imgElementArray,
									  		  effect: {effect:YAHOO.widget.ContainerEffect.FADE,duration:0.20}
											});					
		}
		*/
		if (typeof(document.getElementById("PhotoRegion")) != 'undefined')
		{
			PhotoModule = new YAHOO.widget.Module("PhotoRegion", { visible: false });  
			PhotoModule.render();   
			showPhotoRegion = new YAHOO.widget.Module("showPhotoRegion", { visible: true });  
			showPhotoRegion.render();   
			YAHOO.util.Event.addListener("showPhotoRegion", "mouseover", regionMouseOver, document.getElementById("downArrowPhoto"));
			YAHOO.util.Event.addListener("showPhotoRegion", "mouseout", regionMouseOut, document.getElementById("downArrowPhoto"));
		}		
	}
	LoadingContainer.hide();
};
var photoLayoutFailure = function(o)
{ 
	LoadingContainer.hide();
	wineWarning("There was a communication error on the WineMcGee servers. Please try again later.");
};
	

// ------------------------------------ Upload Dialog -----------------------------------------------------------

var handleSubmit = function()   //when the submit button is clicked, submit the form
{   
	var formObject = document.getElementById("imageform");  
	YAHOO.util.Connect.setForm(formObject, true);  
	
	var callback = 
	{
		upload: onUpload,
		failure: onFailure,
		timeout: 80000,
		cache: false
	};
	if (uploadType == 2)
		var cObj = YAHOO.util.Connect.asyncRequest('POST', '/byobimageuploadprofile_s.php', callback, 'byobID='+byobID); 
	else if (uploadType == 1)
		var cObj = YAHOO.util.Connect.asyncRequest('POST', '/byobimageupload_s.php', callback, 'byobID='+byobID); 	
		
	uploadDialog.hide();
	LoadingContainer.show();

};   
var handleCancel = function()   // if the cancel button is clicked, cancel the form
{   
    uploadDialog.cancel();   
};   
var onFailure = function(o)
{
	LoadingContainer.hide();
	wineWarning("Your upload timed out and was aborted. Your image may be too big, slick.");
};

// onUpload is called upon completion of upload transaction
var onUpload = function(o)
{
	LoadingContainer.hide();
	var Response = o.responseText;
	if (Response.substring(0,7) == "[error]")
	{
		wineWarning(Response.substring(7,Response.length));	
	}
	else
	{	
		if (uploadType == 2)  // upload for wine label image
		{
			imageUploaded = true;
			document.getElementById("mainBYOBImage").innerHTML = o.responseText;		
		}
		else if (uploadType == 1)   // upload for general wine image
		{
			// now call photo layout script		
			var callback = 
			{
				success: photoLayoutSuccess,
				failure: photoLayoutFailure,
				customevents: { onStart: photoLayoutStart },
				timeout: 5000
			}
			
			YAHOO.util.Connect.initHeader('X-Signature', transaction_sig); 
			var cObj = YAHOO.util.Connect.asyncRequest('POST', '/photolayout_s.php', callback, 'type=13&uniqueID='+byobID);
		}
	}	
}

// -------------------------- get comments (using summarized links on bottom of page------------------------------------
function getComments(comment_id)
{
	callback = 
	{
		success: commentsSuccess,
		failure: commentsFailure,
		customevents: { onStart: commentsStart },
		timeout: 5000
	}
		
	YAHOO.util.Connect.initHeader('X-Signature', transaction_sig); 
	var cObj = YAHOO.util.Connect.asyncRequest('POST', '/loadcomments_s.php', callback, 'uniqueID='+byobID+'&type=6&commentStart='+comment_id); 								
		
	document.getElementById("commentText").value = ""; // clear comment text
}


// ------------------------------------ Init -----------------------------------------------------------

function init()
{
	LoadingContainer.setBody('<img src="/Images/ajaxload.gif" />');
	LoadingContainer.render(document.body);
	
	PhotoModule = new YAHOO.widget.Module("PhotoRegion", { visible: false });  
	PhotoModule.render();   
	showPhotoRegion = new YAHOO.widget.Module("showPhotoRegion", { visible: true });  
	showPhotoRegion.render();   
	YAHOO.util.Event.addListener("showPhotoRegion", "mouseover", regionMouseOver, document.getElementById("downArrowPhoto"));
	YAHOO.util.Event.addListener("showPhotoRegion", "mouseout", regionMouseOut, document.getElementById("downArrowPhoto"));
			
	uploadDialog = new YAHOO.widget.Dialog("uploadDialog",  
	            { width : "350px", 
	              fixedcenter : true, 
	              visible : false,  
				  zIndex:15000,
				  modal: true,
	              constraintoviewport : true, 
	              buttons : [ { text:"Submit", handler:handleSubmit, isDefault:true }, 
	                          { text:"Cancel", handler:handleCancel } ] 
	             } ); 

	uploadDialog.render(document.body);
	
	document.getElementById("sig").value = transaction_sig; // set the form security signature for image uploads
	
// -------- overlay for picture expand ------------------------
    PictureModule = new YAHOO.widget.Overlay("picture1", { 
										  xy:[-2000,-2000],	
								  		  effect: {effect:YAHOO.widget.ContainerEffect.FADE,duration:0.40},
										  visible:false 
  	 								      });   	
										  												  
	PictureModule.setBody("<div id=\"picMod\" class=\"pictureModule\"></div>");
	PictureModule.render(document.body);													  
	YAHOO.util.Event.addListener("picture1", "click", wineImageOut);
	YAHOO.util.Event.addListener("picture1", "mouseOut", wineImageOut);
	PictureModule.cfg.setProperty("zindex", 99999);
	
    MagModule = new YAHOO.widget.Overlay("magnify", { 
										  xy:[-2000,-2000],	
										  visible:false 
  	 								      });   	

	MagModule.cfg.setProperty("zindex", 88888);
	MagModule.setBody("<div class=\"magModule\"><img id='magnifyImg' onmouseover='magnifyOver();' onmouseout='magnifyOut();' onclick='wineBigImageIn();' src='/macgif2.gif'></div>");
	MagModule.render(document.body);	

/*
	// load toolTips
	var elements = YAHOO.util.Dom.getElementsByClassName('theEventImage', 'img'); 	
	// img is optional and speeds up the search		
	var imgElementArray = new Array();
	var a = 0;
	for (a = 0; a < elements.length; a++)
	{
		imgElementArray[a] = elements[a].id;
	}	

	if (toolTip)
	{
		toolTip.cfg.setProperty("context", imgElementArray);
		toolTip.forceUnderlayRedraw();
	}
	else if (imgElementArray.length > 0)
	{
		toolTip = new YAHOO.widget.Tooltip("tooltip", 
										{ context: imgElementArray,
								  		  effect: {effect:YAHOO.widget.ContainerEffect.FADE,duration:0.20}
										});					
	}	
	*/
}

YAHOO.util.Event.onDOMReady(init);

