/**
 * WJBoursinNavigator is the class to do ajax navigation (with bookmarking, back and forward) in the boursin site
 *
 * @since Wed Aug 27 2008
 * @revision $Revision$
 * @author Ron Rademaker
 **/

var WJBoursinNavigator = Class.create({
	/**
	 * initialize
	 *
	 * Creates a new WJBoursinNavigator
	 *
	 * @since Wed Aug 27 2008
	 * @access public
	 * @return void
	 **/
	initialize: function() {
		this.spin = new WJSpin();

	},

	/**
	 * rewriteJavascriptLinks
	 *
	 * Rewrites all a links meant for use with javascript / ajax
	 *
	 * @since Tue Sep 09 2008
	 * @access public
	 * @return void
	 **/
	rewriteJavascriptLinks: function(rewrites) {
		var searchsmall = $$(".pos_searchsmall");
		if (searchsmall.length > 0) { // small top, reload entire page
			return;
		}

		while (rewrite = rewrites.pop() ) {
			var loc = rewrite.href.replace(/[^:]*:\/\/[^\/]*\//, "/");
			var elem = rewrite.firstDescendant();
			if (!elem) {
				elem = new Element("span").update(rewrite.getTextContent() );
			}

			loc = loc.replace(/^\/recepten\//, "/recept/");
			loc = loc.replace(/^\/producten\//, "/product/");
			elem.observe("click", this.navigateTo.bind(this, loc, []) );
			if (rewrite.parentNode) {
				rewrite.replace(elem);
			}
		}
	},

	/**
	 * navigateTo
	 *
	 * Navigates to the passed location, can be either an object or query string
	 *
	 * @since Wed Aug 27 2008
	 * @access public
	 * @param mixed query
	 * @param array callbacks - pass additional callbacks into the navigator
	 * @return void
	 **/
	navigateTo: function(query, callbacks) {
		var callbacks = callbacks || new Array();
		if (query == "") {
			return;
		}
		var url;
		if ( (typeof(query) == "string") && (query.match(/^\//) ) ) {
			var link = query;

			if (typeof(aeroplane) != "undefined" ) {
				if (aeroplane.menu) {
					document.location.href = link;
					return;
				}
			}
			if (/MSIE (5|6)/.test(window.navigator.userAgent) ) {
					document.location.href = link;
					return;
			}

			query = {http: query};
			if (this.history) {
				this.history.setValue(0, query);
			}

			if (link.match(/\/recept/) ) {
				query = {
					"ct": "wmdynamic",
					"dt": "recipe",
					"module": "Wmrecipeboursin",
					"type": "xml"
				};
				if (link.match(/\/[^\/]*\/([0-9]*)\//) ) {
					query["offset"] = link.match(/\/[^\/]*\/([0-9]*)\//)[1];
				}
				link = link.replace(/\/recept\//, "/recepten/");
			}

			url = new WJUrl(query, link);
		}
		else {
			url = new WJUrl();

			if (typeof(query) == "string") {
				query = query.toQueryParams();
			}
			
			
			if (query.mode == "dosearch" && (Prototype.Browser.IE || (typeof(aeroplane) != "undefined") ) ) {
				if (Prototype.Browser.IE || aeroplane.menu) {
					query.ct = query.module.replace("Wm", "").replace("boursin", "");
					delete(query["uft[]"]);
					delete(query.dt);
					delete(query.module);
					delete(query["type"]);
					delete(query["wmtrigger[]"]);
					query["nav_active"] = "4";
					document.location.href = "/?" + Object.toQueryString(query);
					return;
				}
			}

			for (var key in query) {
				url.addParameter(key, query[key]);
			}
			if (this.history) {
				this.history.setValue(0, query);
			}
		}

		this.trackQuery(Object.clone(query) );

		url.addParameter("nocache", Math.random() );
		url.addParameter("disableuft[]", "aeroplane" );

		callbacks.push($("content") );
		callbacks.push(function() { $("content").innerHTML.evalScripts(); });
		callbacks.push(this.rewriteJavascriptLinks.bind(this, $("content").select(".javascript") ) );
		callbacks.push(activeLinkHandler.update(url.getParameter("nav_active") ) );
		callbacks.push(function() { if (document.body.canParseHover) {document.body.parseHover();} } );
		var bodydiv = $$("div.pos_body").first();
		if (Object.isElement(bodydiv) ) {
			callbacks.push(bodydiv.scrollTo.bind(bodydiv) );
		}

		if (query["module"] == "Wmrecipeboursin" && query["mode"] == "dosearch") {
			query["nav_active"] = 4;
		}
		if (query["module"] == "Wmrecipeboursin" && query["mode"] != "dosearch") {
			if (window.animation) {
				callbacks.push(window.animation.showRecipe.bind(window.animation) );
			}
		}
		this.spin.content(url, callbacks, [], "get");
	},

	/**
	 * trackQuery
	 *
	 * Rewrites query to a clickable link and tracks it at google analytics
	 *
	 * @since Thu Sep 18 2008
	 * @access public
	 * @param object query
	 * @return void
	 **/
	trackQuery: function(query) {
		var mod = query.module;
		delete query.module;
		delete query.ct;
		delete query.dt;
		delete query.type;
		delete query["commodules[]"];

		switch (mod) {
			case "Wmrecipeboursin":
				query.ct = "recipe";
				break;
		}
		pageTracker._trackPageview("/?" + $H(query).toQueryString() );
	}
});

var boursinNavigator = new WJBoursinNavigator();
