// JavaScript Document

var WineListModule = null;
var showWineListRegion = null;
var AttendeeModule = null;
var showAttendeeRegion = null;
var PhotoModule = null;
var showPhotoRegion = null;
var QuickViewModule = null;
var uploadDialog = null;
var toolTip = null;
var PictureModule = null;
var searchTerm = '';

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;

var showAromaID = 0;

var LoadingContainer = 
		new YAHOO.widget.Panel("loadingDialog",  
			{ width:"80px", 
			  fixedcenter:true, 
			  close:false, 
			  draggable:false, 
			  zindex:15000,
			  visible:false
			} 
	);
// ------------------------------------ Dialog for delete warning ----------------------------------------------

var promptWarning = function(warningText)
{
	promptWarningDialog.setHeader("<div class=\"hd\"><div class=\"tl\"></div><span>Are you sure?</span><div class=\"tr\"></div></div>");
	
	var bodyText = '<div class="dialogWrap"><div class="column_D"><table class="warningFormat" border="0" cellspacing="0" cellpadding="0"><tr><td>' + warningText + '</td></tr></table></div></div>';
	
	promptWarningDialog.setBody(bodyText);
	promptWarningDialog.render(document.body);		
	promptWarningDialog.show();
}

var promptYes = function() 
{
	promptWarningDialog.hide();
	var callback = 
	{
		success: deleteReviewSuccess,
		failure: deleteReviewFailure,
		customevents: { onStart: deleteReviewStart },
		timeout: 5000
	}
	
	YAHOO.util.Connect.initHeader('X-Signature', transaction_sig); 
	var cObj = YAHOO.util.Connect.asyncRequest('POST', '/winedeletereview_s.php', callback, 'reviewID='+delete_review_id);
}
var promptNo = function() 
{
	promptWarningDialog.hide();
}

promptWarningDialog = 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:"Delete!", handler:promptYes, isDefault:true },
					  				   { text:"Cancel", handler:promptNo} ]
						});	

// ----------------Full View - link to wine page-------------------------------------------------------------

function fullView(wineID)
{
	window.location = '/wine/'+wineID;
}

// ----------------------------------------------------For QuickView --------------------------------------------
function quickViewOut()
{
	QuickViewModule.hide();
}
var quickViewLoaded = function(o)
{
	LoadingContainer.hide();
	QuickViewModule.show();	
};
var quickViewStart = function(o)
{ 
	LoadingContainer.show();
};
var quickViewSuccess = function(o)
{ 
	var Response = o.responseText;
	if (Response.substring(0,7) == "[error]")
	{
		wineWarning(Response.substring(7,Response.length));	
	}
	else
	{
		QuickViewModule.setBody(o.responseText);
		YAHOO.util.Event.addListener("QVWineImage", "load", quickViewLoaded);   // once the wine image is loaded, we're good to show
	}
};
var quickViewFailure = function(o)
{ 
	LoadingContainer.hide();
	wineWarning("There was a communication error on the WineMcGee servers. Please try again later.");
};
var quickViewAbort = function(o)
{ 
	LoadingContainer.hide();
	wineWarning("There was a communication error on the WineMcGee servers. Please try again later.");
};

function quickView(wineID)
{
	var callback = 
	{
		success:quickViewSuccess,
		failure:quickViewFailure,
		customevents: { onStart: quickViewStart },
		timeout: 5000
	}
	
	YAHOO.util.Connect.initHeader('X-Signature', transaction_sig); 
	var cObj = YAHOO.util.Connect.asyncRequest('POST', '/quickview_s.php', callback, 'wineID='+wineID); 
	return false;
}

// ------------------------------------ Warning Dialog -----------------------------------------------------------

var wineWarning = function(warningText)
{
	wineWarningDialog.setHeader("<div class=\"hd\"><div class=\"tl\"></div><span>Wine Review Issue</span><div class=\"tr\"></div></div>");

	var bodyText = '<div class="dialogWrap"><div class="column_D"><table class="warningFormat" border="0" cellspacing="0" cellpadding="0"><tr><td>' + warningText + '</td></tr></table></div></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 Review -----------------------------------------------------------
var deleteReviewStart = function(o)
{ 
	LoadingContainer.show();
};
var deleteReviewSuccess = function(o)
{ 
	var Response = o.responseText;
	if (Response.substring(0,7) == "[error]")
	{
		wineWarning(Response.substring(7,Response.length));	
	}
	else
	{
		// now call load review script		
		callback = 
		{
			success: reviewsSuccess,
			failure: reviewsFailure,
			customevents: { onStart: reviewsStart },
			timeout: 5000
		}
		
		YAHOO.util.Connect.initHeader('X-Signature', transaction_sig); 
		var cObj = YAHOO.util.Connect.asyncRequest('POST', '/wineloadreviewhistory_s.php', callback, 'userID='+userID); 								
	}
};
var deleteReviewFailure = function(o)
{ 
	LoadingContainer.hide();
	wineWarning("There was a communication error on the WineMcGee servers. Please try again later.");
};

function deleteReview(review_id)
{
	delete_review_id = review_id;
	promptWarning("This will permanently delete your review from our database. Are you sure you want to delete this review?");
}

// ------------------------------------ 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(review_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', '/winereportabuse_s.php', callback, 'reviewID='+review_id); 	
}
// ------------------------------------ Load Reviews -----------------------------------------------------------
	
var reviewsStart = function(o)
{ 
	LoadingContainer.show();
};
var reviewsSuccess = function(o)
{ 
	var Response = o.responseText;
	if (Response.substring(0,7) == "[error]")
	{
		wineWarning(Response.substring(7,Response.length));	
	}
	else
	{		
		document.getElementById("wineSearchResults").innerHTML = o.responseText;
				
		window.location = "#PageAnchor";
	}
	LoadingContainer.hide();
};
var reviewsFailure = function(o)
{ 
	LoadingContainer.hide();
	wineWarning("There was a communication error on the WineMcGee servers. Please try again later.");
};
	
// ----------------------------------------search reviews ---------------------------------------------------------------
function prepareSubmit()
{
	searchTerm = document.getElementById("wineSearchEdit").value;	
	getReviews(0);	
}
	
// ---------------------------------------------------- get Next/Previous reviews --------------------------------------------
function getReviews(review_id)
{
	callback = 
	{
		success: reviewsSuccess,
		failure: reviewsFailure,
		customevents: { onStart: reviewsStart },
		timeout: 50000
	}	
	
	YAHOO.util.Connect.initHeader('X-Signature', transaction_sig); 
	var cObj = YAHOO.util.Connect.asyncRequest('POST', '/wineloadreviewhistory_s.php', callback, 'userID='+userID+'&reviewStart='+review_id+'&searchTerm='+searchTerm); 									
}



var showAromasSuccess = function(o)
{ 
	var Response = o.responseText;
	if (Response.substring(0,7) == "[error]")
	{
		wineWarning(Response.substring(7,Response.length));	
	}
	else
	{		
		document.getElementById("aromaRegion"+showAromaID).innerHTML = o.responseText;
	}
	LoadingContainer.hide();
};

function showAromas(review_id)
{
	var regionVal = document.getElementById("aromaRegion"+review_id).innerHTML;
	if (regionVal != "")
		document.getElementById("aromaRegion"+review_id).innerHTML = "";

	else
	{
	
		showAromaID = review_id;

		callback = 
		{
			success: showAromasSuccess,
			failure: reviewsFailure,
			customevents: { onStart: reviewsStart },
			timeout: 50000
		}	
		
		YAHOO.util.Connect.initHeader('X-Signature', transaction_sig); 
		var cObj = YAHOO.util.Connect.asyncRequest('POST', '/showreview_s.php', callback, 'reviewID='+review_id); 									
	}
	
}
	
// ------------------------------------ Init -----------------------------------------------------------

function init()
{
	LoadingContainer.setBody('<img src="/Images/ajaxload.gif" />');
	LoadingContainer.render(document.body);
	
	if (!browserIE)
	{
	    QuickViewModule = new YAHOO.widget.Panel("QVPanel", { 
										  fixedcenter:true,	
										  effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25},
										  width: "600px", 
										  visible:false 
  	 								      });   	
		// fade causes text to not anti-alias in IE (<= 7)
	}
	else
	{
	    QuickViewModule = new YAHOO.widget.Panel("QVPanel", { 
										  fixedcenter:true,	
										  width: "600px", 
										  visible:false 
  	 								      });   		
	}

	QuickViewModule.setHeader("<div class=\"hd\"><div class=\"tl\"></div><span>Wine Quick View</span><div class=\"tr\"></div></div>");
	QuickViewModule.setBody("");
	QuickViewModule.render(document.body);													  
	YAHOO.util.Event.addListener("QVPanel", "click", quickViewOut);
			

// -------- 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);


