// JavaScript Document

var PhotoModule = null;
var showPhotoRegion = null;
var toolTip = null;
var PictureModule = null;

var MagModule = null;
var bigImageOut = 1;
var overImage = 0;


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 learnSearch = "";

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)
{
	wineWarningDialog.setHeader("<div class=\"hd\"><div class=\"tl\"></div><span>Question, Answer, Learn 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";
};

// ----------------------------------------- Add Question --------------------------------------

var questionAddSuccess = function(o)
{ 
	LoadingContainer.hide();
	var Response = o.responseText;
	if (Response.substring(0,7) == "[error]")
	{
		wineWarning(Response.substring(7,Response.length));	
	}
	else
	{
		window.location = "/learn.php";			
	}
}

function prepareQuestionAdd()
{
	var formObject = document.getElementById('learnForm');
	YAHOO.util.Connect.setForm(formObject); 
	var callback = 
	{
		success:questionAddSuccess,
		failure:questionSearchFailure,
		customevents: { onStart: questionSearchStart },
		timeout: 10000
	}
	
	YAHOO.util.Connect.initHeader('X-Signature', transaction_sig); 
	var cObj = YAHOO.util.Connect.asyncRequest('POST', '/questionadd2_s.php', callback); 
	return false;
}


// ----------------------------------------- search for question --------------------------------------


var questionSearchStart = function(o)
{ 
	LoadingContainer.show();
}
var questionSearchSuccess = function(o)
{ 
	LoadingContainer.hide();
	var Response = o.responseText;
	if (Response.substring(0,7) == "[error]")
	{
		wineWarning(Response.substring(7,Response.length));	
	}
	else
	{
		document.getElementById("questionSearchResult").innerHTML = o.responseText;
	}
}
var questionSearchFailure = function(o)
{ 
	wineWarning("There was a communication error on the WineMcGee servers. Please try again later.");
}
function prepareSubmit()
{
	learnSearch = document.getElementById('learnSearchEdit').value;
	var formObject = document.getElementById('learnForm');
	YAHOO.util.Connect.setForm(formObject); 
	var callback = 
	{
		success:questionSearchSuccess,
		failure:questionSearchFailure,
		customevents: { onStart: questionSearchStart },
		timeout: 10000
	}
	
	YAHOO.util.Connect.initHeader('X-Signature', transaction_sig); 
	var cObj = YAHOO.util.Connect.asyncRequest('POST', '/questionsearch2_s.php', callback); 
	return false;
}

// --------------------------------------------- Facebook ---------------------------------------------

function facebook_onlogin()
{
	window.location = "https://www.winemcgee.com/connect.php";
}

// --------------------------------------------- Twitter question ---------------------------------------------

function questionWithTwitter()
{
	childWindow = window.open ("/twittersubmitquestion.php", "mywindow",  "width=800,height=410");		
	
}

// ---------------------------------------block comment ---------------------------------------------------

var blockCommentSuccess = function(o)
{ 
	var Response = o.responseText;
	if (Response.substring(0,7) == "[error]")
	{
		wineWarning(Response.substring(7,Response.length));	
	}
	else
	{
		window.location = "/learn.php";							
	}
};
var blockCommentFailure = function(o)
{ 
	LoadingContainer.hide();
	wineWarning("There was a communication error on the WineMcGee servers. Please try again later.");
};

function blockComment(comment_id)
{
	var callback = 
	{
		success: blockCommentSuccess,
		failure: blockCommentFailure,
		customevents: { onStart: questionSearchStart },
		timeout: 20000
	}
		
		YAHOO.util.Connect.initHeader('X-Signature', transaction_sig); 
		var cObj = YAHOO.util.Connect.asyncRequest('POST', '/blockcomment_s.php', callback, 'commentID='+comment_id); 	
}

// ------------------------------------ Init -----------------------------------------------------------


function init()
{
	LoadingContainer.setBody('<img src="/Images/ajaxload.gif" />');
	LoadingContainer.render(document.body);
			
// -------- 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);				

	YAHOO.util.Event.addListener("learnSubmit", "click", prepareSubmit);

	if (questionSearch != null)
	{
		document.getElementById("learnSearchEdit").value = questionSearch;
		prepareSubmit();
	}

}

YAHOO.util.Event.onDOMReady(init);


