document.observe("dom:loaded", function()
{
    BreedSelector.init();
	CompareWindow.init();
});




/** Breed Selector **/

var BreedSelector = new Object;
// encapsulates and namespaces all BreedSelector functions.

// answers is an associative array. The keys correspond to question names/id's, and the values are numerical values. 
// Radio button values are like 1, 2, 3, 4 for each radio button. 
// Checkbox values uses math to allow the page to send just one number to the back end, which is all the checkbox values added up
// "I don't have a preference" always == 0.
BreedSelector.answers = new Hash;

// These properties are various Breed Selector component Id's and classes. Changing a value here will change it globally throughout the Breed Selector.
BreedSelector.breedsNumId = 'matchingBreedsNum'; 				// span around the dynamically updating numbers on the right
BreedSelector.dynamicBreedNum = 'BreedNum';						// this is kept hidden at the bottom of the selector to avoid two ajax calls
BreedSelector.breedSelectorId = 'selectorWrapper'; 				// wrapper around the form and detail view
BreedSelector.dogListContainerId = 'selectorViewMatches';		// container which is dynamically updated with dogs
BreedSelector.dogListId = 'selectorDetailList';					// unordered list containing dogs
BreedSelector.formId = 'selectorForm';							// form on the left
BreedSelector.viewId = 'selectorView';							// display box on the right
BreedSelector.resetButtonId = 'resetButton';					// reset button at bottom of form
BreedSelector.sendButtonId = 'sendBtn';							// send button in header

BreedSelector.moreOptionsButtonClass = 'moreOptions';				// more options button at the top of the form
BreedSelector.compareButtonClass = 'compare';					// two compare buttons on the matching breeds view
BreedSelector.hiddenQuestionClass = 'hiddenQuestions';			// the two divs which contain hidden questions - more options toggles them
BreedSelector.questionClass = 'formLine';						// the div wrapping around each form question
BreedSelector.toolTipButtonClass = 'qmark';						// the question mark triggers a mouseover tooltip

BreedSelector.breedCompareUrl = 'BreedComparison.aspx';			// breed comparison popup
BreedSelector.emailFriendUrl = '../../General/EmailFriend.aspx?url=/dogs/breedselector/index.aspx';		// email a friend page with query string for link
BreedSelector.selectorDetailUrl = 'SelectorDetail.aspx';		// where the updated dogs come from
BreedSelector.toolTipsUrl = 'ToolTips.aspx';					// one page contains all tooltips, it is passed a query string by question id

/*** initialization functions.  init is called when the dom loads, and then init calls the other init functions below. ***/
BreedSelector.init = function()
{
	// returns out immediately if this isn't the breed selector page
	if ($(BreedSelector.formId) == null)
		return false;
	// the breed selector is hidden for users without js
	$(BreedSelector.formId).show();
	$(BreedSelector.viewId).show();
	
	BreedSelector.initIeFlickerFix();
	
	// each 'question' on the form is getting initialized
	var questions = $(BreedSelector.formId).select("." + BreedSelector.questionClass);
	questions.each( function(question, i)
	{			
		BreedSelector.initStripedQuestion(question, i);
		
		// sets the id of the question based on the input names
		BreedSelector.initQuestionId(question);
		
		// reverts the form values back to what is definited in the HTML. based on the checked="checked" attribute
		BreedSelector.initInputs(question);
		
		// sets three different event observers on each of the toolTip ? symbols:
		// - 'click' (to block the link from firing)
		// - 'mouseover' (to spawn tooltip)
		// - 'mouseout' (to kill tooltip)
		BreedSelector.initToolTipButtons(question, BreedSelector.toolTipButtonClass);
		
		// each question contains an array of valid values according to the number of checkboxes or radio buttons
		// it contains. the array is actually attached to each form question element as a custom property
		question.validValues = new Array;
		BreedSelector.initValidValues(question);
	});
	// hides all the questions that should be hidden
	BreedSelector.initHiddenQuestions(BreedSelector.hiddenQuestionClass);
	
	// generates the default list of dogs
	BreedSelector.populateDogList();
	
	// adds a custom 'uncheckDog' method on li tags
	BreedSelector.initCustomMethods();
	
	// sets up listener on the "more options" button
	var moreOptionButtons = $$("." + BreedSelector.moreOptionsButtonClass);
	moreOptionButtons.each( function(button) 
	{
		var question = '';
		button = button.down('a');
		button.pressed = false;
		if (button.identify() == 'moreAboutYou')
			question = $('yourHiddenQuestions');
		else if (button.identify() == 'moreAboutDog')
			question = $('dogHiddenQuestions');
			
		Event.observe(button, 'click', BreedSelector.optionClickListener.bindAsEventListener(button, button, question));
	});
	
	
	// sets up listener on the reset button at the bottom of the form
	var resetButton = $(BreedSelector.resetButtonId).down('a');
	Event.observe(resetButton, 'click', BreedSelector.resetClickListener.bindAsEventListener(resetButton));
	
	// sets up listeners for each of the compare buttons
	var compareButtons = $(BreedSelector.breedSelectorId).select("." + BreedSelector.compareButtonClass);
	compareButtons.invoke('observe', 'click', BreedSelector.compareClickListener);
	
	// sets up a listener on the 'send to a friend' button
	var sendFriendButton = $(BreedSelector.sendButtonId);
	Event.observe(sendFriendButton, 'click', BreedSelector.sendClickListener.bindAsEventListener(sendFriendButton));
}



// this adds a custom "uncheckDog" function to all li tags
BreedSelector.initCustomMethods = function()
{
	Element.addMethods('li',
	{
		uncheck: function(element)
	 	{
		    element = $(element);
		    if (element.down('input'))
		    {
		    	element.down('input').checked = false;
		    }
		    return element;
		}
	});
	
	Element.addMethods('input',
	{
		ifChecked: function(element)
	 	{
		    element = $(element);
		    if (element.checked)
		    {
		    	return element.up('li');
		    }
		    else
		    {
		    	return null;
		    }
	  	}
	});
}

BreedSelector.initStripedQuestion = function(question, i)
{
	if (i % 2)
	{
		question.addClassName("alt");
		question.setStyle({backgroundImage: 'url((../../i/breedselector/questionBackgroundAlt.gif)'});
	}	
}

BreedSelector.initIeFlickerFix = function()
{
	try {
		document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {}
}

// sets three different event observers on each of the toolTip ? symbols:
// - 'click' (to block the link from firing)
// - 'mouseover' (to spawn tooltip)
// - 'mouseout' (to kill tooltip)
BreedSelector.initToolTipButtons = function(question)
{
	var toolTipButtons = $(question.identify()).select("." + BreedSelector.toolTipButtonClass);
	toolTipButtons.each( function(toolTipButton)
	{
		toolTipButton.observe('click', BreedSelector.toolTipButtonClickListener.bindAsEventListener(toolTipButton, toolTipButton, question));
		toolTipButton.observe('mouseover', BreedSelector.toolTipButtonMouseOverListener.bindAsEventListener(toolTipButton, toolTipButton, question));
		toolTipButton.observe('mouseout', BreedSelector.toolTipButtonMouseOutListener.bindAsEventListener(toolTipButton, toolTipButton, question));
	});
}

// ensures that the inputs are set to their default values (whatever is marked "checked" in the HTML)
BreedSelector.initInputs = function(question)
{
	var questionInputs = $(question.identify()).select("input");
	questionInputs.each( function(input)
	{
		input.observe('click', BreedSelector.formClickListener.bindAsEventListener(input, input, question));
		if (input.readAttribute("checked") == "checked")
		{
			BreedSelector.initAnswerValue( question, parseInt(input.readAttribute("value")) );
			input.checked = true;
		}
		else
		{
			input.checked = false;
		}
	});
}

// builds out the answers associative array with default values
BreedSelector.initAnswerValue = function(question, value)
{
	var questionName = $(question).identify();
	BreedSelector.answers.set(questionName.capitalize(), parseInt(value));
}

// hides the hidden questions
BreedSelector.initHiddenQuestions = function()
{
	//$(BreedSelector.formId).select("." + BreedSelector.hiddenQuestionClass).invoke('hide');
	$(BreedSelector.formId).select("." + BreedSelector.hiddenQuestionClass).each( function(question)
	{
		question.hide();
		question.hidden = true;
	});
}

// assigns names to each question div in the form based on the 'name' attribute of the input
BreedSelector.initQuestionId = function(question)
{
	var questionId = question.down('input').readAttribute("name");
	question.writeAttribute("id", questionId);
}

// defines an array of valid values (based on the number of radio buttons or hardcoded numbers or checkboxes)
// array is attached directly to the question element itself as a custom property
BreedSelector.initValidValues = function(question)
{
	questionInputs = $(question.identify()).select("input");
	questionInputs.each( function(input) 
	{
		question.validValues.push( parseInt(input.readAttribute("value")) );
	});
	
	if (question.down("input").type == "checkbox")
	{
		if (questionInputs.length == 4)
			var checkboxValidValues = [7, 6, 5, 3];
		else if (questionInputs.length == 6 || questionInputs.length == 5)
			var checkboxValidValues = [3, 6, 12, 24, 15, 7, 14, 28, 31, 27, 27, 17, 5, 9, 10, 18, 19, 22, 26, 13, 11, 29, 30];
		checkboxValidValues.sort().each( function(value)
		{
			question.validValues.push(value);			
		});
	}
}
/*** end initialization functions ***/

/** Listener functions. These set listeners and prevent default actions, but forward the actual functionality on to another function **/
BreedSelector.compareClickListener = function(e)
{
	Event.stop(e);
	BreedSelector.processComparison(BreedSelector.dogList);
}

BreedSelector.toolTipButtonClickListener = function(e, clickedButton, question)
{
	Event.stop(e);
	// Since the tool tip ?'s are links, an event listener is needed to keep their default action from firing
}

BreedSelector.toolTipButtonMouseOverListener = function(e, clickedButton, question)
{
	BreedSelector.spawnToolTip(question);
}

BreedSelector.toolTipButtonMouseOutListener = function(e, clickedButton, question)
{
	BreedSelector.closeToolTip(question);
}

BreedSelector.formClickListener = function(e, clicked, question)
{
	var processStatus = BreedSelector.process(clicked, question);
}

BreedSelector.optionClickListener = function(e, button, question)
{
	Event.stop(e);
	if (button.pressed == false)
	{
		BreedSelector.showHiddenQuestion(question);
		BreedSelector.showFewerButton(button);
		BreedSelector.scrollView(button, question);
		BreedSelector.toggleOthers(button, question);
	} 
	else if (button.pressed == true)
	{
		BreedSelector.hideHiddenQuestion(question);
		BreedSelector.showMoreButton(button);
		BreedSelector.scrollView(button, question);
		BreedSelector.toggleOthers(button, question);
	}
}

BreedSelector.scrollView = function(button, question)
{

	if (button.identify() == 'moreAboutDog' && button.pressed == true)
	{
		BreedSelector.moveViewDown();
		$('aboutDog').scrollTo();
	}
	else
	{
		BreedSelector.moveViewUp();
		$('selectorWrapper').scrollTo();
	}
}

BreedSelector.toggleOthers = function(button, question)
{
	if (button.identify() == 'moreAboutYou')
	{
		var otherQuestion = $('dogHiddenQuestions');
		var otherButton = $('moreAboutDog');
		if (otherQuestion.hidden == false)
		{
			BreedSelector.hideHiddenQuestion(otherQuestion);
			BreedSelector.showMoreButton(otherButton);
		}
	}
	else if (button.identify() == 'moreAboutDog')
	{
		var otherQuestion = $('yourHiddenQuestions');
		var otherButton = $('moreAboutYou');
		if (otherQuestion.hidden == false)
		{
			BreedSelector.hideHiddenQuestion(otherQuestion);
			BreedSelector.showMoreButton(otherButton);
		}
	}
}

BreedSelector.moveViewDown = function()
{
	$(BreedSelector.viewId).setStyle({ marginTop : '382px'});
}

BreedSelector.moveViewUp = function()
{
	$(BreedSelector.viewId).setStyle({ marginTop : '0px'});	
}

BreedSelector.resetClickListener = function(e)
{
	Event.stop(e);
	BreedSelector.resetForm();
}

BreedSelector.sendClickListener = function(e)
{
	BreedSelector.sendFriend();
}

/*** end listener functions ***/

/*** error messaging functions ***/

BreedSelector.clearErrors = function()
{
	var errors = $(BreedSelector.breedSelectorId).select(".error");
	if (errors)
	{
		errors.invoke('remove');
	}
}

// arguments: 
// errorMessageId is the ID of the error message itself
// errorMessageText is the content of the error message
// containerId is the box that holds the error message - it is inserted at the top of the container
BreedSelector.displayErrorMessage = function(errorMessageId, errorMessageText, containerId)
{
	var errorMessage = Builder.node('p', {className: 'error', id: errorMessageId}, errorMessageText);
	$(containerId).insert({top: errorMessage});
	new Effect.Highlight(errorMessageId);
}
/*** end error messaging ***/

// processes user input (checkboxes) for the dog detail view and spawns the comparison window. 
// note that any JS triggered inside the compare window is in a seperate object, CompareWindow 
BreedSelector.processComparison = function()
{
	var dogs = $(BreedSelector.dogListId).select('li.leftDog, li.centerDog, li.rightDog');
	var dogsChecked = dogs.invoke('down', 'input').invoke('ifChecked').without(null);
	console.log(dogsChecked);
	
	if (dogsChecked.size() < 2 ) // no dogs triggers error msg
	{
		BreedSelector.clearErrors();
		BreedSelector.displayErrorMessage('errorDogsUnchecked', 'Please select between two and four dogs.', BreedSelector.dogListContainerId);
	}
	else if (dogsChecked.size() > 4) // more than 4 dogs triggers error msg
	{
		BreedSelector.clearErrors();
		BreedSelector.displayErrorMessage('errorTooManyDogsChecked', 'Please select between two and four dogs.', BreedSelector.dogListContainerId);
	}
	else if (dogsChecked.size() >= 2 && dogsChecked.size() <= 4) // just right
	{
		BreedSelector.clearErrors();
		BreedSelector.spawnCompareWindow(dogsChecked);
	}
	else // protect against out of bounds data through firebug etc.
	{
		return false;
	}
}

// pulls in which dogs are currently checked and passes it to the pop up window in a query string
BreedSelector.spawnCompareWindow = function(dogsChecked)
{
	var queryString = "";
	var breedIds = dogsChecked.invoke('down', 'input').invoke('readAttribute', 'name');
	breedIds.each( function (breed, i) 
	{
		if ( i < (breedIds.length - 1) )
		{
			queryString += breed + ",";	
		}
		else
		{
			queryString += breed;
		}	
	});
	var url = BreedSelector.breedCompareUrl + '?breeds=' + queryString;
	console.info("url: ", url);
	if (window.print) // object detection on window.print which safari/mac does not support. spawns a version with a toolbar instead for mac browsers
		compareWin = window.open(url, "breedComparison", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=no,resizable=0,width=750,height=470");
	else
		compareWin = window.open(url, "breedComparison", "toolbar=1,location=0,directories=0,status=0,menubar=0,scrollbars=no,resizable=0,width=750,height=470");
	BreedSelector.uncheckDogs();
	if(!compareWin)
	{
		alert('Your popup blocker is preventing you from seeing this content. Please allow popup windows from http://www.purina.com.');
	}
}
/*** end compare functions ***/

// triggered by the "send to a friend" button in the upper right hand corner
BreedSelector.sendFriend = function()
{
	emailFriend(BreedSelector.emailFriendUrl);
}


// these functions are related to the more options/fewer options button in the form
BreedSelector.showHiddenQuestion = function(question)
{
	question.hidden = false;
	question.show();
}

BreedSelector.hideHiddenQuestion = function(question)
{
	question.hidden = true;
	question.hide();
}

BreedSelector.showFewerButton = function(button)
{
	button.pressed = true;
	BreedSelector.toggleOptionsButton(button);
}

BreedSelector.showMoreButton = function(button)
{
	button.pressed = false;
	BreedSelector.toggleOptionsButton(button);
}


BreedSelector.toggleOptionsButton = function(button)
{
	var currentButtonUrl= $(button).down('img').readAttribute('src');
	if (currentButtonUrl.include('more'))
	{
		currentButtonUrl = currentButtonUrl.gsub('more', 'fewer'); // replaces word in 'src' string
	}
	else
	{
		currentButtonUrl = currentButtonUrl.gsub('fewer', 'more');
	}
	$(button).down('img').src = currentButtonUrl;
}
// end more options/hidden questions functions

// tool tip functionality. this pulls in content from a seperate URL
// takes: a form question element
// returns: nothing
BreedSelector.spawnToolTip = function(question)
{
	var toolTipId = question.identify() + 'ToolTip'; // examples: spendToolTip, exerciseToolTip
	// first we ditch the old one
	if($(toolTipId))
	{
		$(toolTipId).remove();
	}
	
	// build the tool tip container
	var element = Builder.node('div',{className:'breedToolTip', id: toolTipId});
	Element.insert($(question), {top: element});

	var toolTipId = $(toolTipId);
	$(toolTipId).hide(); // hidden at first
	/// update the tool tip container with content from external url - sends the question id as a parameter in query string
	new Ajax.Updater(toolTipId, BreedSelector.toolTipsUrl, { method: 'get', parameters: { question: question.identify()} });
	
	
	pauseComp(200); // this is to give the ajax call time to load in the content. should probably be replaced with an ajax callback to trigger the toggle
	$(toolTipId).toggle();
}

BreedSelector.closeToolTip = function(question)
{
	// hides the tooltip
	var toolTipId = $(question.identify() + 'ToolTip');
	if ($(toolTipId))
	{
		$(toolTipId).toggle();
	}

}

BreedSelector.resetForm = function()
{
	if (window.confirm("Are you sure you want to reset this form?"))
	{
		$('breedSelectorForm').reset();
		// the below functions are necessary to ensure the data outputted from the form reflects the reset form values
		var questions = $(BreedSelector.formId).select("." + BreedSelector.questionClass);
		questions.each( function(question)
		{
			BreedSelector.initInputs(question);
			BreedSelector.initValidValues(question);
		});
		BreedSelector.updateDogCount();
		BreedSelector.populateDogList();
	}
	else
	{
		return false;
	}
}

BreedSelector.setAnswerValue = function(question, value)
{
	valid = BreedSelector.validate(question, value); // first validates data
	if (valid)
	{
		BreedSelector.answers.set($(question).identify().capitalize(), parseInt(value)); // it will only write an answer value if it is valid
	}
}

BreedSelector.validate = function(question, value)
{
	// if the value is the same as ANY of the values in the validValues array, the value is valid.
	var validity = $(question).validValues.any(function(validValue)
	{
		return value == validValue;
	});
	if (validity)
		return true;
	else
		return false;
}

BreedSelector.clearCheckboxes = function(question)
{
	var checkboxes = $(question).descendants().findAll( function (node)
	{
		// this findAll functionc cleans out any descendants of $(question) that aren't checkboxes
		nodeTypeIsCheckBox = (node.readAttribute("type") == "checkbox") ? true : false;
		return nodeTypeIsCheckBox;
	});	
	checkboxes.each( function(checkbox)
	{
		if ( parseInt(checkbox.readAttribute("value")) == 0 ) // value 0 means "i don't have a preference"
		{
			checkbox.checked = true;
		}
		else
		{
			checkbox.checked = false;
		}
	});
}

BreedSelector.fillLastCheckboxIfAllEmpty = function(question)
{
	var checkboxes = $(question).descendants().findAll( function (node)
	{
		// this findAll functionc cleans out any descendants of $(question) that aren't checkboxes
		nodeTypeIsCheckBox = (node.readAttribute("type") == "checkbox") ? true : false;
		return nodeTypeIsCheckBox;
	});	
	
	var anyCheckboxesChecked = checkboxes.any( function(checkbox) 
	{
		if(checkbox.checked)
			return true;
		else
			return false;
	});
	
	if (anyCheckboxesChecked == false)
	{
		var lastCheckbox = $(question).down('li:last-child').down('input');
		lastCheckbox.checked = true;
	}
}

BreedSelector.clearLastCheckbox = function(question)
{
	var lastCheckbox = $(question).down('li:last-child').down('input'); // selects last checkbox
	if (lastCheckbox.checked)
	{
		lastCheckbox.checked = false; // if it is checked, clear it
	}
}

BreedSelector.writeCheckboxTotal = function(question)
{
	var checkboxes = $(question).descendants().findAll( function (node)
	{
		nodeTypeIsCheckBox = (node.readAttribute("type") == "checkbox") ? true : false;
		return nodeTypeIsCheckBox;
	});	
	
	var total = 0;		
	checkboxes.each( function(checkbox)
	{
		if (checkbox.checked)
		{
			total += parseInt(checkbox.readAttribute("value"));
			// totals the values of each checked checkbox
		}
	});
	BreedSelector.setAnswerValue(question, total);
}

BreedSelector.process = function(clickedInput, question)
{
	if (clickedInput.readAttribute("type") == "radio")
	{
		BreedSelector.setAnswerValue(question, clickedInput.readAttribute("value"));
		// sets the answer value of this question to whatever radio button is clicked
	}
	else if (clickedInput.readAttribute("type") == "checkbox")
	{
		// clears all checkboxes but the last, if the last checkbox is clicked
		if ( parseInt(clickedInput.readAttribute("value")) == 0 )
		{
			BreedSelector.clearCheckboxes(question);
		}
		else
		{
			// if another checkbox is clicked, clear the last checkbox
			BreedSelector.clearLastCheckbox(question);
		}
		// the last checkbox should be checked if the others are all unchecked
		BreedSelector.fillLastCheckboxIfAllEmpty(question);
		
		// total the values of all checked checkboxes
		BreedSelector.writeCheckboxTotal(question);
	}
	else
	{
		return false;
	}

	//BreedSelector.maskDogList(); // this covers the existing dogs to let the user know they've been updated. It also fires the populateDogList() function on completion
	BreedSelector.populateDogList();
}

//update dog count updates the number at the top of the detail view
BreedSelector.updateDogCount = function()
{
	var numBreeds = $(BreedSelector.dynamicBreedNum).innerHTML;
	console.log("number of breeds: ", numBreeds);
	$(BreedSelector.breedsNumId).update(numBreeds);
	new Effect.Highlight(BreedSelector.breedsNumId); // yellow fade to show it has been updated
}

BreedSelector.maskDogList = function()
{
	console.log("Ajax request started");
	BreedSelector.unmaskDogList();
	// scrolls container back to top
	$(BreedSelector.dogListContainerId).scrollTop = 0;
	// this is the grey mask over the dogs
	var mask = Builder.node('div', {className: 'mask', id: 'dogMask'});
	Element.insert($(BreedSelector.dogListContainerId), {top: mask});
	$(BreedSelector.dogListContainerId).setStyle({overflow: 'hidden'}); // overflow hidden keeps the user from being able to scroll off the mask
	
	// absolutely positioned container around the dog message and button
	var messageContainer = Builder.node('div', {id: 'messageContainer'});
	Element.insert($(BreedSelector.dogListContainerId), {top: messageContainer});
	
	var ajaxBar = Builder.node('img', {id: 'ajaxBar', src: '../../i/breedselector/ajax-loader.gif', style: 'display: block;'});
	Element.insert($(messageContainer), {bottom: ajaxBar});
}

BreedSelector.unmaskDogList = function()
{
	if ($('dogMask'))
	{
		$('dogMask').remove();
		$('ajaxBar').remove();
	}
}

BreedSelector.populateDogList = function()
{
	var jsonData = BreedSelector.answers.toJSON();
	console.info(jsonData);
	new Ajax.Updater(BreedSelector.dogListContainerId, BreedSelector.selectorDetailUrl, {method: 'get', parameters: {id: jsonData}, onCreate: BreedSelector.maskDogList, onComplete: BreedSelector.prepDogList});
}

BreedSelector.prepDogList = function()
{
	console.log("Ajax request finished");
	$(BreedSelector.dogListContainerId).setStyle({overflow: 'auto'}); // there may be more dogs than can fit in the container.
	BreedSelector.unmaskDogList();
	BreedSelector.updateDogCount(); // updates the number at the top of the detail view
}

BreedSelector.uncheckDogs = function()
{
	$(BreedSelector.dogListId).select('li').invoke('uncheck');
}



/*** END OF BREED SELECTOR OBJECT ***/

/*** COMPARE WINDOW OBJECT ***/
var CompareWindow = new Object;

CompareWindow.printButtonId = 'printBtn';
CompareWindow.dogLinkClass = 'dogTitle';

CompareWindow.init = function()
{
	if ($(CompareWindow.printButtonId) == null)
		return false;
		
	if (window.print) // object detection on window.print - if it doesn't exist, hide the print button
		$(CompareWindow.printButtonId).observe('click', CompareWindow.printButtonClickListener.bindAsEventListener(CompareWindow.printButtonId));
	else
		$(CompareWindow.printButtonId).hide();
		
	//$$("." + CompareWindow.dogLinkClass).invoke('observe', 'click', CompareWindow.dogTitleClickListener.bindAsEventListener(this, this.href));
	$$("." + CompareWindow.dogLinkClass).each( function(link)
	{
		link.observe('click', CompareWindow.dogTitleClickListener.bindAsEventListener(this, link));
	});
}

CompareWindow.printButtonClickListener = function(e)
{
	CompareWindow.print();
}

CompareWindow.dogTitleClickListener = function(e, link)
{
	console.log(link);
	Event.stop(e);
	openerWin = window.opener.open(link.href);
	if(!openerWin)
	{
		alert('Your popup blocker is preventing you from seeing this content. Please allow popup windows from http://www.purina.com.');
	}
}

CompareWindow.print = function()
{
	window.print();
}
/*** END COMPARE WINDOW OBJECT ***/


/*** hide console statements from non-firebug browsers **/
if (!window.console || !console.firebug)
{
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
    "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];

    window.console = {};
    for (var i = 0; i < names.length; ++i)
        window.console[names[i]] = function() {}
}