﻿/* Keeps browsers without firebug from throwing an error */
if (!("console" in window) || !("firebug" in console))
{
    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() {}
}

function rollOverListener(target_id)
{
	if ( $(target_id) )
	{
		var rollOverImgs = $$("#" + target_id + " img");
		for (i = 0; i < rollOverImgs.length; i++)
		{	
			Event.observe( rollOverImgs[i], 'mouseover', rollOver.bindAsEventListener(rollOverImgs[i]) );
			Event.observe( rollOverImgs[i], 'mouseout', rollOut.bindAsEventListener(rollOverImgs[i]) );
		}
		
	}
}

function rollOver(e)
{
	Event.stop(e);
	orig_src = this.src;
	console.log("rollOver: " + orig_src);
	var subsrc = orig_src.substring(0, (orig_src.length - 4) ); // 4 characters = ".gif"
	subsrc += "_roll.gif";
	this.src = subsrc;
}

function rollOut(e)
{	
	Event.stop(e);
	var orig_src = this.src;
	console.log("rollOut: " + orig_src);
	if ( orig_src.indexOf("_roll") != -1)
	{
		var subsrc = orig_src.substring(0, (orig_src.length - 9) ); // 9 characters = "_roll.gif"
		subsrc += ".gif";
		this.src = subsrc;
	}
}

function initMovies()
{
	var page_id = $$('body')[0].identify(); // returns the id (if any) on the body tag.
	
	switch (page_id)
	{
		case "hlhome":
			var so = new SWFObject("_res/swf/Home.swf", "home_movie", "960", "465", "8", "#FFFFFF", "best");
			so.addParam("wmode", "transparent");
    		so.addParam("FlashVars","subPath=_res/swf/revealHome.swf");
			so.addParam('FlashVars', 'bodyLink=Body/Default.aspx&mindLink=Mind/Default.aspx&spiritLink=Spirit/Default.aspx&discoverHow=Cat-Food/Default.aspx');
			var flashTest = so.write("homestage");
			if (flashTest)
				$("homestage").setStyle({backgroundImage: 'url(_res/i/home_bg_flash.jpg)'});
			break;	
		
		case "hlbody":
			var so = new SWFObject("../_res/swf/ballGameHome.swf", "body_movie", "715", "333", "8", "#FFFFFF", "high");
			so.addParam("wmode", "transparent");
			so.addParam("FlashVars","subPath=../_res/swf/ballGame.swf");
			var flashTest = so.write("mainstage");
			if (flashTest)
				$("mainstage").setStyle({backgroundImage: 'none'});
			break;
		
		case "hlmind":
			var so = new SWFObject("../_res/swf/chimesHome.swf", "mind_movie", "715", "333", "8", "#FFFFFF", "best");
			so.addParam("wmode", "transparent");
			so.addParam("FlashVars","subPath=../_res/swf/chimes.swf");
			var flashTest = so.write("mainstage");
			if (flashTest)	
				$("mainstage").setStyle({backgroundImage: 'none'});
			break;

		case "hlspirit":
			var so = new SWFObject("../_res/swf/revealHome.swf", "spirit_movie", "715", "333", "8", "#FFFFFF", "best");
			so.addParam("wmode", "transparent");
			so.addParam("FlashVars","subPath=../_res/swf/waterreveal.swf");
			var flashTest = so.write("mainstage");
			if (flashTest)
				$("mainstage").setStyle({backgroundImage: 'none'});
			break;
		
		default:
			break;
	}
}

function popUpListener(target_class, window_name, window_specs)
{
	if ( $$(target_class) )
	{
		var popUpLinks = $$("a." + target_class);
		for (i = 0; i < popUpLinks.length; i++)
		{
			Event.observe(popUpLinks[i], 'click', popUp.bindAsEventListener(popUpLinks[i], target_class, window_name, window_specs), false);
		}
		
	}
}

function popUp(link, target_class, window_name, window_specs)
{
	if ( $$(target_class) )
	{
		Event.stop(link);
		link = window.open(this.href, window_name, window_specs );
		
	} 
}

function sendToPrinterListener(target_class)
{
	if ( $$(target_class) && (window.print) )
	{
		var printLinks = $$("a." + target_class);
		for (i = 0; i < printLinks.length; i++)
		{
			printLinks[i].style.display = "block";
			Event.observe(printLinks[i], 'click', sendToPrinter.bindAsEventListener(printLinks[i]), false);
		}
		
	}
}

function sendToPrinter(link)
{
	Event.stop(link);
	window.print();
}

document.observe("dom:loaded", function()
	{
 		rollOverListener("kibble_explorer");
		popUpListener("legal_link", "legal", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=yes,width=560,height=600");
		popUpListener("other_link", "other", "toolbar=1,location=0,directories=0,status=0,menubar=1,scrollbars=yes,resizable=yes,width=800,height=500" );
		popUpListener("form_link", "form",	"toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=yes,width=750");
		popUpListener("external", "external", "toolbar=1, location=1, menubar=1, scrollbars=yes, resizable=yes");
		if(window.print)
		{
		    popUpListener("printAllTips", "printall", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=yes,width=510,height=600");
        }
        else
        {
            popUpListener("printAllTips", "printall", "toolbar=1,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=yes,width=510,height=600");
        }
		sendToPrinterListener("sndToPrnt");
		initMovies();
		sfHover();
	}
);

function sfHover() 
{
	var browserName=navigator.appName;
	if (browserName=="Microsoft Internet Explorer" && document.getElementById("topnav"))
	{
		var sfEls = document.getElementById("tnav").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" hover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" hover\\b"), "");
			}
		}
	}
}

/*** PRINT ALL TIPS OBJECT ***/
var printWindow = new Object;

printWindow.popUp = function()
{
    if (window.print) // object detection on window.print which safari/mac does not support. spawns a version with a toolbar instead for mac browsers
		window.open(url, "printalltips", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=no,resizable=yes,width=750,height=460");
	else
		window.open(url, "printalltips", "toolbar=1,location=0,directories=0,status=0,menubar=0,scrollbars=no,resizable=yes,width=750,height=460");
}
/*** END PRINT ALL TIPS OBJECT ***/

var maxLength=300;
var blockPress = true;

function suppress(tipsArea, max)
{
    var userLength = tipsArea;
    if (userLength<=maxLength)
        $('charCount').innerHTML = maxLength - userLength;

    if (userLength>=maxLength)
    {
        $$("textarea.comments_input")[0].value = $$("textarea.comments_input")[0].value.substring(0,maxLength);
        blockPress = true;
    }
    else
    {
     blockPress = false;
    }

}

function blockKeyboard(tipsArea)
{
    if(blockPress)
    {
        $$("textarea.comments_input")[0].value = $$("textarea.comments_input")[0].value.substring(0,maxLength);
    }
}