/**
 * Javascript library for template ExtremeMagento
 * @copyright 2007 Quick Solution LTD. All rights reserved.
 * @author Giao L. Trinh <giao.trinh@quicksolutiongroup.com>
 */

(function() {
	
// EM.tools {{{
	
if (typeof BLANK_IMG == 'undefined') 
	var BLANK_IMG = '';
if (typeof BASE_URL == 'undefined') 
	BASE_URL = '';

// declare namespace() method
String.prototype.namespace = function(separator) {
  this.split(separator || '.').inject(window, function(parent, child) {
    var o = parent[child] = { }; return o;
  });
};


'EM.tools'.namespace();


function decorateNav() {
	var baseurl = BASE_URL.replace(/index.php\/?/, '');
	//console.debug("BaseURL: ", baseurl);
	var url = window.location.href.replace(baseurl, '').replace(/^\/?(index.php)?\/?/, '');
	var isHome = url == '';
	//console.debug("isHome: ", isHome);
	var nav = $('nav_top');
	nav.select('a').each(function(a) {
		var href = a.getAttribute('href').replace(baseurl, '').replace(/^\/?(index.php)?\/?/, '').replace(/\?(.*)$/, '').replace(/#(.*)$/, '').replace('.html', '');
		var isHomeLink = href == '';
		//console.debug("href: ", href);
		//console.debug("isHomeLink: ", isHomeLink);
		
		if (isHome && isHomeLink || url.substr(0, href.length) == href && !isHome && !isHomeLink) {
			var el = a;
			while (el != nav) {
				if (el.tagName == 'A' || el.tagName == 'LI')
					Element.addClassName(el, 'active');
				el = el.parentNode;
			}
		}
	});
	
}
	
document.observe('dom:loaded', function() {		
	function main() {
		decorateNav();
	}
	
	// work around dom:loaded bug on IE8
	if (Prototype.Browser.IE && Prototype.Version < '1.6.1')
		window.setTimeout(main, 100);
	else
		main();
});


})();
