// prevent background image flicker in IE
try {
	document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

$(document).ready(function() {
	var start = new Date();
	
	// insert top shadow div
	$("#wrapper").before('<div id="shadow-top"></div>').css("marginTop", "0px");
	
	highlightNav = function(p) {
		$(p).addClass("on");
		$(p).parents("li").addClass("open");
		$(p).filter(":has(ul)").addClass("open");
	}

	// expand nav
	if($("body#prod-detail, body#brand").size()) {
		// product detail and brand pages: match brand ID on query string with brand ID in nav to highlight
		$("#nav a").not("[@href$='#']").each(function() {
			var h = this.href.toLowerCase();
			if(h.indexOf("brand=" + query.brand) != -1 && h.indexOf("cat=" + query.cat) != -1) {
				highlightNav($(this).parent());
			}
		});
	} else {
		// other pages: match page URL with left nav URL to highlight
		$("#nav a").not("[@href$='#']").each(function() {
			if(location.href.toLowerCase() == this.href.toLowerCase()) {
				highlightNav($(this).parent());
			}
		});
	}
	
	// set up level 2 nav expand/collapse
	var n = $("#nav a[@href$='#']");
	n.click(function() {$(this).parent().toggleClass("open"); this.blur(); return false;});
	n.focus(function() {window.status=" "; return true;});
	n.mouseover(function() {window.status=" ";return true;});
	
	// insert text sizer
	$("#search-global").after('<p id="text-sizer">' + _("text size") + ' <a href="#" id="font-small" title="' + _("small text") + '">A</a> | <a href="#" id="font-medium" title="' + _("medium text") + '">A</a> | <a href="#" id="font-large" title="' + _("large text") + '">A</a></p>');
	$("#font-small").click(function(){fontSize('small'); this.blur(); return false;});
	$("#font-medium").click(function(){fontSize('medium'); this.blur(); return false;});
	$("#font-large").click(function(){fontSize('large'); this.blur(); return false;});
	
	// insert print link
	if(!$("#recipe-detail").size() && window.print) $("#tell-friend").after('<p id="print-page"><a href="#" onclick="window.print(); return false;">' + _("print this page") + '</a></p>');
	
	// style related recipes list
	$("#related-recipes li:last-child").addClass("last");
	
	// style promotions list
	$("#promotion-list h2:first-child").addClass("first");
	
	// hook up tab nav events
	bindSearchTabs = function() {
		$("#tabs a").unbind().bind("click", function() {
			this.blur();
			var cat = this.id.split("-");
			$("#tabs a").removeClass("on");
			$("#tab-" + cat[1]).addClass("on");
			$("div.results").hide();
			$("#results_" + cat[1]).show();
			return false;
		});
		$("#tabs").removeClass("disabled");
	}
	
	initSearchResults = function() {
		// style search results tables
		$("#search-results th:first").addClass("first");
		$("#search-results th:last").addClass("last");
		$("#search-results tbody tr").hover(function() {
			$(this).addClass("over");
		}, function() {
			$(this).removeClass("over");
		}).bind("click", function() {
			location.href = $(this).find("a")[0].href;
		});
		
		// add AJAX calls for next/previous
		$("p.search-nav a").each(function() {
			$(this).bind("click", function() {
				// disable the tabs while search content is loading
				$("#tabs a").unbind().bind("click", function() {return false;});
				var tabs = $("#tabs").addClass("disabled");
				var loading = $("#loading");
				loading.show();
				var href = $(this).attr("href");
				var container = $(this).parents("div.results");
				container.fadeTo(250, 0.01, function() {container.load(href, function() {
						container.fadeTo(250, 1.0, function() {
							loading.hide();
							bindSearchTabs();
						});
				//added for browser back functionality on advanced search pages only
						if (!tabs.size()) {
							dhtmlHistory.add(escape(href));
							currentLocation = href;
					}
				//
						initSearchResults();
					});
				});
				window.scrollTo(0, $("#search-results").offset().top);
				return false;
			});
		});
	}
	
	// search results page?
	if($("body#search, body#recipe-search, body#newsroom table.search-results").size()) {
		if (!$("#tabs").size() && !$("body#newsroom").size()) {
			// initialize the DHTML History framework
			// on the advanced search pages only
			dhtmlHistory.initialize();
			
			historyChange = function(newLocation, historyData) {
				//try{console.log("cur: " + currentLocation); console.log("new: " + newLocation);} catch(err){};
				if(newLocation && newLocation != currentLocation) {
					$("#ajax-results").load(unescape(newLocation), function() {
						initSearchResults();
					});
				}
			};
			
			// subscribe to DHTML history change
			// events
			dhtmlHistory.addListener(historyChange);
			
			//added for browser back functionality
			// add a history entry if we're on page one of the results
			var href = $(".search-nav a");
			if (location.href.indexOf("#http") == -1 && href.size()) {
				href = href[0].href;
				href = href.replace(/page=2/, "page=1");
			dhtmlHistory.add(escape(href));
			var currentLocation = href;
			//try{console.log("cur: " + currentLocation); console.log("new: " + newLocation);} catch(err){};
		} else {
			var currentLocation = "";
		}
		}
		
		// init search results
		initSearchResults();
		// set up tabs on general search page
		var cat = query.cat || "general"; // show general tab by default
		$("#tabs").show();
		$("#tab-" + cat).addClass("on");
		$("#results_" + cat).show();
		bindSearchTabs();
	}
	
	// clean up promotion detail content
	try {
		$("#content-detail div.para > li").wrap("<ul></ul>");
	} catch(err) {}
	
	// style site map
	$("#content-sitemap > ul > li > a").append("<span>&nbsp;&raquo;</span>").addClass("first");
	$("#content-sitemap a[@href$='#']").each(function() {
		var h = $(this).html();
		$(this).before(h);
		$(this).remove();
	});
	
	// activate selected size style sheet
	var cookie = readCookie("kcomStyles");
	var title = cookie || "small";
	createCookie("kcomStyles", title, 365);
 	setActiveStyleSheet(title);
	
	// init form validation
	if($("form.validate").size()) initFormValidation();
	
	// form hints
	$("input.hint").hint();
	
	//alert(new Date() - start);
});

ajaxError = function(req) {
	//monitorAjax("Error");
	alert(_("Sorry, the server was unable to process your request. Please try again."));
	//alert(req);
}

goToSite = function() {
	var u = $("#selSites")[0].value;
	if(u) window.open(u);
	return false;
}


// sidebar featured content object
var rotator = {
	init: function(options) {
		rotator.current = 0;
		rotator.loaded = -1;
		rotator.animating = false;
		rotator.container = options.container;
		rotator.content = options.content;
		rotator.elements = new Array();
		rotator.last = rotator.content.length - 1;
		rotator.delay = options.delay || 5000;
		rotator.fade = options.fade || 1000;
		rotator.textPrev = options.textPrev || "&laquo; prev";
		rotator.textNext = options.textNext || "next &raquo;";
		rotator.imagePrev = options.imagePrev || "";
		rotator.imageNext = options.imageNext || "";
		
		// remove original banner
		$(rotator.container).find("a").remove();
		
		//load all banners
		$.each(rotator.content, function() {
			var a = $("<a></a>").appendTo(rotator.container).attr("href", rotator.stripEntities(this.url));
			if(this.url.indexOf("http") > -1) {
				a.attr("target", "_blank");
			}
			a.attr("title", this.alt).css("position", "absolute").hide();
			rotator.elements.push(a);
			$("<img>").appendTo(a).load(rotator.check).attr("src", this.image).attr("alt", this.alt);
		});
	},
	
	check: function() {
		rotator.loaded += 1;
		if(rotator.loaded == rotator.last) {
			rotator.insertNav();
			rotator.start();
		}
	},
	
	insertNav: function() {
		var p = $("<p>").insertAfter(rotator.container);
		// prev link
		var a = $("<a></a>").appendTo(p).attr("href", "#").click(function() {
			this.blur();
			if(rotator.animating) rotator.stop();
			rotator.prev();
			return false;
		});
		if(rotator.imagePrev) {
			$("<img>").appendTo(a).attr("src", rotator.imagePrev);
		} else {
			a.html(rotator.textPrev)
		}
		// next link
		var a = $("<a></a>").appendTo(p).attr("href", "#").click(function() {
			this.blur();
			if(rotator.animating) rotator.stop();
			rotator.next();
			return false;
		});
		if(rotator.imageNext) {
			$("<img>").appendTo(a).attr("src", rotator.imageNext);
		} else {
			a.html(rotator.textNext)
		}
	},
	
	prev: function() {
		$(rotator.elements[rotator.current]).fadeOut(rotator.fade);
		rotator.current = rotator.current==0 ? rotator.last : rotator.current-1;
		$(rotator.elements[rotator.current]).fadeIn(rotator.fade);
	},
	
	next: function() {
		$(rotator.elements[rotator.current]).fadeOut(rotator.fade);
		rotator.current = rotator.current==rotator.last ? 0 : rotator.current+1;
		$(rotator.elements[rotator.current]).fadeIn(rotator.fade);
	},
	
	start: function() {
		$(rotator.elements[rotator.current]).show();
		rotator.animating = true;
		rotator.timer = window.setInterval(rotator.next, rotator.delay);
	},
	
	stop: function() {
		rotator.animating = false;
		window.clearInterval(rotator.timer);
	},
	
	stripEntities: function(str) {
		str = str.replace(/&amp;/g, "&");
		str = str.replace(/&gt;/g, ">");
		return str;
	}
};

// handle missing images
imageMissing = function(i) {
	if (!i.src.match(/(www2\.kelloggs\.com)|(kelloggs\.ca)/i)) {
		// look for image on production server
		i.src = i.src.replace(/\/[^\/]+\//, "/www2.kelloggs.com/");
	} else {
		// can't find the image anywhere
		i.src = "/images/image_not_available.gif";
	}
}

// utility functions
////////////////////////////////////////

// translation function
_ = function (s) {
	if (typeof i18n != "undefined" && i18n[s]) {
		return i18n[s];
	}
	return s;
};

setActiveStyleSheet = function(title) {
	if(document.getElementById && document.createTextNode) {
		var i, a;
		for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
			if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
			 	a.disabled = true;
				if(a.getAttribute("title") == title) a.disabled = false;
			}
		}
		$("#text-sizer").find("a").each(function() {$(this).removeClass("on");});
		$("#font-" + title).addClass("on");
	}
}

fontSize = function(fs) {
	createCookie("kcomStyles", fs, 365);
	setActiveStyleSheet(fs);
}

createCookie = function(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

readCookie = function(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

parseQuery = function() {
	var returnVals = new Array();
	qString = new String(window.location);
	var queryStart = qString.indexOf('?');
	if (queryStart==-1) {
		return returnVals;
	}
	var query = qString.substring(queryStart + 1, qString.length);
	var parts = query.split("&");
	for (var i=0; i<parts.length; i++) {
		bits = parts[i].split("=");
		if(bits[1]) {
			subbits = bits[1].split("#"); // added by T.D. to handle fragment identifier in URL
			returnVals[bits[0].toLowerCase()] = bits[1]; // query[] indexes are now lowercase!
		}
	}
	return returnVals;
}

var query = parseQuery();

// validation prototypes
////////////////////////////////////////

String.prototype.trim = function () {
	return this.replace(/^\s+|\s+$/g, "");
}

String.prototype.isEmpty = function() {
	return ((this == null) || (this.length == 0) || /^\s+$/.test(this));
}

String.prototype.isEmail = function() {
	return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(this);
}

// validation functions
////////////////////////////////////////

initFormValidation = function() {
	var start = new Date();
	
	var foc; // where to put focus on form load
	
	// hide empty error spans
	$("span.error").each(function(){
		if(this.innerHTML == "") {
			$(this).hide();
		} else {
			// error condition, so place focus on first error field
			if(!foc) {
				foc = $(this).siblings("input")[0];
				if(!foc) foc = $(this).siblings("select")[0];
			}
		}
	});
	
	// add validation handlers to forms
	$("form.validate").submit(function(e) { 
		return validateForm(this);
	});
	
	// set tab index, disable autocomplete
	var tab = 1;
	$("form.validate input, form.validate select, form.validate textarea").each(function() {
		$(this).addClass("tabme");
	});
	$("form.validate .tabme").each(function() {
		if(this.type != "hidden") {
			this.setAttribute("autocomplete", "off");
			this.tabIndex = tab;
			if(tab == 1 && !foc) {
				foc = this;
			}
			tab++;
		}
	});
	
	// show form
	$("form.validate").css("visibility", "visible");
	
	// focus first input or first error input
	if(foc) foc.focus();
	
 //alert(new Date() - start);
}

validateForm = function(f) {
	// validate required fields
	var result = $.map($(f).find(".required").not("span"), function(el) {
		return testField(el, true) ? null : false;
	});
	
	// validate optional fields
	var result2 = $.map($(f).find(".optional"), function(el) {
		return testField(el, false) ? null : false;
	});
	
	if(result[0] == false || result2[0] == false) {
		// focus on first error field
		var errfield = $(".errorfield")[0];
		if(errfield) {
			errfield.focus();
		}
		return false;
	}
	
	return true;
}

testField = function(el, required) {
	var id = el.id;
	
	switch(id) {
		
		case "email":
		case "femail":
			var v = $(el).val();
			var eid = $("#" + id + "_error");
			if(required) {
				if(v.isEmpty()) {
					showErr(el, eid, _("Please type an e-mail address."));
					return false;
				}
			}
			if(!v.isEmpty()) {
				if(!v.isEmail()) {
					showErr(el, eid, _("Please type an e-mail address in the following format: yourname@example.com"));
					return false;
				}
			}
			hideErr(el, eid);
			break;
			
		case "fname":
		case "ffname":
			var v = $(el).val();
			var eid = $("#" + id + "_error");
			if(required) {
				if(v.isEmpty()) {
					showErr(el, eid, _("Please enter first name."));
					return false;
				} 
			}
			hideErr(el, eid);
			break;
			
		case "message":
			var v = $(el).val();
			var eid = $("#" + id + "_error");
			if(required) {
				if(v.isEmpty()) {
					showErr(el, eid, _("Please enter a message."));
					return false;
				} 
			}
			hideErr(el, eid);
			break;
			
		case "txtKeyword":
			var v = $(el).val();
			var eid = $("#" + id + "_error");
			if(required) {
				// part of a validation group? (product and promo advanced search)
				if($(el).parents("fieldset.validate-group").size()) {
					if(v.isEmpty()) { // validate the additional fields in the group
						var result = false;
						$(el).parents("fieldset.validate-group").find(".group").each(function() {
							if(testField(this, false)) result = true;
						});
						if(!result) {
							showErr(el, eid, _("Please select or enter at least one search criterion."));
							return false;
						}
					}
				} else if(v.isEmpty()) {
					showErr(el, eid, _("Keyword is required."));
					return false;
				}
			}
			hideErr(el, eid);
			break;
			
		case "ddlCategories":
		case "ddlBrands":
		case "ddlTypes":
			var v = $(el).val();
			if(v.isEmpty()) {
				return false;
			}
			break;

	}
	return true;
}

hideErr = function(input, el) {
	$(input).removeClass("errorfield");
	el.hide();
	el.html("");
}

showErr = function(input, el, msg) {
	$(input).addClass("errorfield");
	el.hide();
	el.html(msg);
	el.fadeIn(1000);
}

/* Copyright (c) 2006 Brandon Aaron (http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 */
/**
 * This returns an object with top, left, width, height, borderLeft,
 * borderTop, marginLeft, marginTop, scrollLeft, scrollTop, 
 * pageXOffset, pageYOffset.
 *
 * The top and left values include the scroll offsets but the
 * scrollLeft and scrollTop properties of the returned object
 * are the combined scroll offets of the parent elements 
 * (not including the window scroll offsets). This is not the
 * same as the element's scrollTop and scrollLeft.
 * 
 * For accurate readings make sure to use pixel values.
 *
 * @name offset	
 * @type Object
 * @param HTMLElement refElement The offset returned will be relative to this elemen.
 * @cat DOM
 * @author Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)
 */
jQuery.fn.offset = function(refElem) {
	if (!this[0]) throw 'jQuery.fn.offset requires an element.';

	refElem = (refElem) ? jQuery(refElem)[0] : null;
	var x = 0, y = 0, elem = this[0], parent = this[0], sl = 0, st = 0;
	do {
		if (parent.tagName == 'BODY' || parent.tagName == 'HTML') {
			// Safari and IE don't add margin for static and relative
			if ((jQuery.browser.safari || jQuery.browser.msie) && jQuery.css(parent, 'position') != 'absolute') {
				x += parseInt(jQuery.css(parent, 'marginLeft')) || 0;
				y += parseInt(jQuery.css(parent, 'marginTop'))	|| 0;
			}
			break;
		}

		x += parent.offsetLeft || 0;
		y += parent.offsetTop	|| 0;

		// Mozilla and IE do not add the border
		if (jQuery.browser.mozilla || jQuery.browser.msie) {
			x += parseInt(jQuery.css(parent, 'borderLeftWidth')) || 0;
			y += parseInt(jQuery.css(parent, 'borderTopWidth'))	|| 0;
		}

		// Need to get scroll offsets in-between offsetParents
		var op = parent.offsetParent;
		do {
			sl += parent.scrollLeft || 0;
			st += parent.scrollTop	|| 0;
			parent = parent.parentNode;
		} while (parent != op);
	} while (parent);

	if (refElem) {
		var offset = jQuery(refElem).offset();
		x = x - offset.left;
		y = y - offset.top;
		sl = sl - offset.scrollLeft;
		st = st - offset.scrollTop;
	}

	// Safari and Opera do not add the border for the element
	if (jQuery.browser.safari || jQuery.browser.opera) {
		x += parseInt(jQuery.css(elem, 'borderLeftWidth')) || 0;
		y += parseInt(jQuery.css(elem, 'borderTopWidth')) || 0;
	}

	return {
		top: y - st,
		left: x - sl,
		width: elem.offsetWidth,
		height: elem.offsetHeight,
		borderTop:	parseInt(jQuery.css(elem, 'borderTopWidth')) || 0,
		borderLeft: parseInt(jQuery.css(elem, 'borderLeftWidth')) || 0,
		marginTop:	parseInt(jQuery.css(elem, 'marginTopWidth')) || 0,
		marginLeft: parseInt(jQuery.css(elem, 'marginLeftWidth')) || 0,
		scrollTop:	st,
		scrollLeft: sl,
		pageYOffset: window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0,
		pageXOffset: window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0
	};
};

// from http://remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/
jQuery.fn.hint = function () {
	return this.each(function () {
		// get jQuery version of 'this'
		var t = jQuery(this); 
		// get it once since it won't change
		var title = t.attr('title'); 
		// only apply logic if the element has the attribute
		if (title) { 
			// on blur, set value to title attr if text is blank
			t.blur(function () {
				if (t.val() == '') {
					t.val(title);
					t.addClass('blur');
				}
			});
			// on focus, set value to blank if current value 
			// matches title attr
			t.focus(function () {
				if (t.val() == title) {
					t.val('');
					t.removeClass('blur');
				}
			});
			
			// clear the pre-defined text when form is submitted
			t.parents('form:first()').submit(function() {
				if (t.val() == title) {
					t.val('');
					t.removeClass('blur');
				}
			});
			
			// now change all inputs to title
			t.blur();
		}
	});
};