
function onlineHelp(pagename){
	helpWindow = window.open('/help/'+pagename, "DrinknationHelp", "scrollbars=yes,resizable=yes,width=600,height=400");
	helpWindow.focus();
}
function hov(loc,cls){
   if(loc.className) loc.className=cls;
}

//Works with the multiselector for bar ingredient searches
function selectAll(subBox) {
    for (i = 0; i < subBox.options.length; i++) {
        subBox.options[i].selected = true;
    }
}


/* Recipe Browser resizable divs */
/* Borrowed with thanks from Webmaster World forums:  http://www.webmasterworld.com/forum91/2030.htm */

//global variables used to track status
var curHeight=0
var curPos=0
var newPos=0
var mouseStatus='up'

function initResizeBar(event)
{
	document.body.onmousemove = getPos;
	document.body.onmouseup = resizeBarOnMouseUp;
	setPos(event);
}

function clearResizeBar()
{
	document.body.onmousemove = null;
	document.body.onmouseup = null;
}

function resizeBarOnMouseUp() {
	mouseStatus = 'up';
}

//this function gets the original div height
function setPos(e){
	//for handling events in ie vs. w3c
	curevent=(typeof event=='undefined'?e:event)
	//sets mouse flag as down
	mouseStatus='down'
	//gets position of click
	curPos=curevent.clientY
	//accepts height of the div
	tempHeight=document.getElementById('divRecipeBrowser').style.height
	//these lines split the height value from the 'px' units
	heightArray=tempHeight.split('p')
	curHeight=parseInt(heightArray[0])
}

//this changes the height of the div while the mouse button is depressed
function getPos(e){
	if(mouseStatus=='down'){
		curevent=(typeof event=='undefined'?e:event)
		//get new mouse position
		newPos=curevent.clientY
		//calculate movement in pixels
		var pxMove=parseInt(newPos-curPos)
		//determine new height
		var newHeight=parseInt(curHeight+pxMove)
		//conditional to set minimum height to 5
		newHeight=(newHeight<5?5:newHeight)
		//set the new height of the div
		document.getElementById('divRecipeBrowser').style.height=newHeight+'px'
		document.getElementById('divResults').style.height=newHeight+'px'
	}
}

/* End recipe browser div stuff */


/* Recipe Browser AJAXey stuff */

function getAjaxDrink(relPath)
{
	$.ajax({
		url: '/drinkAjax'+relPath,
		cache: true,
		success: function(data,textStatus) { $('#divDrink').html(data) }
	});
}

function rbIndexLinks(sel)
{
	$("#idxLinks").css("visibility","hidden");
	$(document).ready(function(){
		var s = $('#selBrowse');
		$('#idxLinks li').each(function(idx){
			var t = $(this).children('a');
			//This way makes blank options in IE6
			//$('#selBrowse').append(new Option(t.text(),t.attr('href')));
			s.append('<option value="'+t.attr('href')+'">'+t.text()+'</option>');
			$(this).remove();
		});
		if (sel) {
			//s.children("option[value='"+sel+"']").attr('selected','selected');
			//forced to do it manually for IE6
			var s = document.getElementById('selBrowse');
			for(var i = 0; i < s.options.length; i++) {
				if (s.options[i].value == sel) {
					//can't use s.options[i].selected = true, causes an error in, you guessed it, IE6.
					s.options[i].setAttribute('selected',true);
					break;
				}
			}
		}
	});
}

function moveDrink(slug,forward)
{
	var list = $('#rList').children('li');
	if (list.length > 0) {
		for(var i=0; i < list.length; i++) {
			var a = $(list[i]).children('a');
			var s = a.attr('href').split('/');
			if (s[s.length-1] == slug) {
				if (forward) {
					var name = (i + 1 < list.length) ? list[i+1] : list[0];
				} else {
					var name = (i - 1 >= 0) ? list[i-1] : list[list.length-1];
				}
				getAjaxDrink($(name).children('a').attr('href'));
				break;
			}
		}
	}
}

function ajaxFav(nid)
{
	prependMsg('divRecipe'+nid,"<div class=\"dnbox info\"><div><div>Adding...</div></div></div>");
	$.ajax({
		url: "/user/addFavourite/"+nid,
		success: function(data,args) {
			prependMsg('divRecipe'+nid,data,true);
		}
	});
	return false;
}

function prependMsg(eId,msg,fade)
{
	var d = document.getElementById(eId);
	if (d && d.childNodes && d.childNodes[0]) {
		msgId = '_divAjax_' + eId;
		var msgDiv = document.getElementById(msgId);
		if (! msgDiv) {
			tmp = document.createElement('div');
			tmp.id = msgId;
			tmp.innerHTML = msg;
			d.insertBefore(tmp,d.childNodes[0]);
		}
		else {
			msgDiv.style.display = 'block';
			msgDiv.style.opacity = 1;
			msgDiv.innerHTML = msg;
		}
		if (fade) {
			$('#'+msgId).fadeTo(1000,1).fadeOut(3000);
		}
	}
}

function ajaxVote()
{
	var f = document.forms['formVote'];
	var vote = null;
	for (var i=0; i < f.uservote.length; i++) {
		if (f.uservote[i].checked) {
			vote = f.uservote[i].value;
			break;
		}
	}
	if (vote == null) {
		alert("You didn't rate the drink!  Choose a value first.");
		return false;
	}
	var nid = document.forms['formVote'].nameid.value;
	$.ajax({
		url: "/drinkAjax/vote/" + nid + "?uservote="+vote,
		success: function(data,args) {
			prependMsg('divRecipe'+nid,data,true);
			//update rating
			$('#r_rating').load("/drinkAjax/getRating/" + nid);
		}
	});
	closeModal();	
	return false;
}

function ajaxComment(nameId)
{
	$.ajax({
		url: '/drinkAjax/comment/'+nameId,
		type: 'post',
		data: $('#frmDrinkComment').serialize(),
		success: function(data)
		{
			if ($('#divAjaxResponseHTML')) $('#divAjaxResponseHTML').remove();
			var div = document.createElement('div');
			$(div).attr('id','divAjaxResponseHTML');
			$(div).html(data);
			$(div).prependTo($('#leaveComment'));
			if (data.indexOf('dnbox ok') == -1) {
				reloadCaptcha();
			}
			else{ 
				$('#frmDrinkComment').get(0).reset();
				$(div).fadeTo(4000,1).fadeOut(4000);
			}
		}
	});
	return false;
}

function reloadCaptcha()
{
	$('#divCaptcha').load("/drinkAjax/reloadCaptcha",{});
}

/* End AJAX stuff */


/* FB stuff */

function fbDrinkPublish(nameId,buttonType)
{
	var dname = $('#r_name').text();
	var char = dname.charAt(0);
	if (char.match(/[aeiou]/i)) var aOrAn = 'An ';
	else var aOrAn = 'A ';
	if (buttonType == 'drinking') {
		var message = 'Delicious!';
		var caption = '{*actor*} is kicking back with '+ aOrAn.toLowerCase() + dname + ' drink, made from a recipe at Drinknation.com.';
	} 
	else if (buttonType == 'making') {
		var message = 'This is going to be good...';
		var caption = '{*actor*} is mixing up '+aOrAn.toLowerCase() + dname + ', made from a drink recipe at Drinknation.com.';
	}
	
	/*
	var desc = '';
	var s = $(".r_ingredients span[rel]");
	if (s.length > 0) {
		if (s.length == 1) var sep = '';
		else if (s.length <= 2) var sep = ' and ';
		else var sep = ', ';
		var i = 1;
		s.each(function() {
			desc += $(this).attr('rel');
			if (i < s.length) desc += sep;
			i++;
		});
		desc += '.';
	}
	if (desc != '') {
		desc = aOrAn + dname + ' is made with ' + desc;
	}
	*/
	
	var attachment = {
		'name':$(document).attr('title'),
		'href':$('#divRecipe'+nameId).attr('rel'),
		'caption':caption
		//'description':desc
	};
	if ($('#r_pic').length > 0) {
		attachment.media = [{'type':'image','src':$('#r_pic').attr('src'),'href':$('#divRecipe'+nameId).attr('rel')}];
	}
	var action_links = [{'text':'Mix one up!','href':'test'}];
	FB.Connect.streamPublish(message, attachment, action_links);
}

/* Begin tooltip stuff */

/***********************************************
* Cool DHTML tooltip script- � Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
*
* Modified by Drinknation.com to work better with large content in the tooltip.
***********************************************/

var offsetxpoint=-20 //Customize x offset of tooltip
var offsetypoint=20 //Customize y offset of tooltip
var ie=document.all
var ns6=document.getElementById && !document.all
var enabletip=false
/* Copied this part to the awstatstracker.js script included at the bottom of every page.
if (ie||ns6)
var tipobj=document.all? document.all["dhtmltooltip"] : document.getElementById? document.getElementById("dhtmltooltip") : ""
*/

function ietruebody() {
return (document.compatMode && (document.compatMode!="BackCompat")) ? document.documentElement : document.body;
}

function ddrivetip(thetext, thecolor, thewidth){
if (ns6||ie){
	if (typeof thewidth!="undefined") {
		 tipobj.style.width=thewidth+"px"
	}
	else {
		tipobj.style.width = 'auto';
		tipobj.style.maxWidth = '500px';
	}
	if (ie) tipobj.style.width = '500px';
	if (typeof thecolor!="undefined" && thecolor!="") tipobj.style.backgroundColor=thecolor
	tipobj.innerHTML=thetext
	enabletip=true
	return false
}
}

function positiontip(e){
if (enabletip){
var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
//Find out how close the mouse is to the corner of the window
var rightedge=ie&&!window.opera? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20
var bottomedge=ie&&!window.opera? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20

var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000

//if the horizontal distance isn't enough to accomodate the width of the context menu
if (rightedge<tipobj.offsetWidth)
//move the horizontal position of the menu to the left by it's width
tipobj.style.left=ie? ietruebody().scrollLeft+event.clientX-tipobj.offsetWidth+"px" : window.pageXOffset+e.clientX-tipobj.offsetWidth+"px"
else if (curX<leftedge)
tipobj.style.left="5px"
else
//position the horizontal position of the menu where the mouse is positioned
tipobj.style.left=curX+offsetxpoint+"px"

//same concept with the vertical position
if (bottomedge<tipobj.offsetHeight)
tipobj.style.top=ie? ietruebody().scrollTop+event.clientY-tipobj.offsetHeight-offsetypoint+"px" : window.pageYOffset+e.clientY-tipobj.offsetHeight-offsetypoint+"px"
else
tipobj.style.top=curY+offsetypoint+"px"
tipobj.style.visibility="visible"
}
}

function hideddrivetip(){
if (ns6||ie){
enabletip=false
tipobj.style.visibility="hidden"
tipobj.style.left="-1000px"
tipobj.style.backgroundColor=''
tipobj.style.width=''
}
}

document.onmousemove=positiontip

/* End tooltip stuff */


function checkAll(f,cname) {
	l = f.elements.length;
	for (var i=0; i<l; i++){
		var current = f.elements[i];
		if (current.name == cname && current.type=="checkbox"){
			current.checked = true;
		}
	}
	return false;
}

function unCheckAll(f,cname) {
	l = f.elements.length;
	current = f.elements[i];
	for (var i=0; i<l; i++){
		var current = f.elements[i];
		if (current.name == cname && current.type=="checkbox"){
		     current.checked = false;
		}
	}
	return false;
}


/* Modal dialog box stuff */
messageObj = null;

function createModalObj() {
	if (messageObj == undefined) {
		messageObj = new DHTML_modalMessage();	// We only create one object of this class
		messageObj.setShadowOffset(5);	// Large shadow
	}
}

function displayModal(url)
{	
	createModalObj();
	messageObj.setSource(url);
	messageObj.setCssClassMessageBox(false);
	messageObj.setSize(400,'auto');
	messageObj.setShadowDivVisible(true);	// Enable shadow for these boxes
	//keybind the esc key to close the modal window
	document.onkeypress = function(e) { modalHandleKey(e); };
	messageObj.display();
}

function displayStaticModal(messageContent,cssClass)
{
	createModalObj();
	messageObj.setHtmlContent(messageContent);
	messageObj.setSize(300,150);
	messageObj.setCssClassMessageBox(cssClass);
	messageObj.setSource(false);	// no html source since we want to use a static message here.
	messageObj.setShadowDivVisible(false);	// Disable shadow for these boxes
	messageObj.display();
}

function modalHandleKey(e) {
  if (! e) e = window.event;
  key = (e.keyCode) ? e.keyCode : e.which;
  if (key == 27) closeModal();
}

function closeModal()
{
	document.onkeypress = null;
	messageObj.close();	
}