// Global EMart JS defenition

var EMart = {
	ie6: /msie (\d)/.test(navigator.userAgent.toLowerCase())&&RegExp.$1==6,

	init: function(){
		this.addCorners();
		this.initTreeMenues();
		this.equalHeight($(".same-height"));

		/* Footer PayPal icon */
		$('#i_paypal, #i_paypal_infopage').click(function(e){
			window.open('https://www.paypal.com/us/cgi-bin/webscr?cmd=xpt/Marketing/popup/OLCWhatIsPayPal-outside','olcwhatispaypal','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=450, height=450');
		});

//		$("a[codename='ShippingRates-Internal'], a[codename='PaymentMethods-inline']").each(function(){


		$("a.inline-ajax").each(function(){
			var el = $(this);
//			alert(this);
			$.get(this, function(data){
				el.after(data);
				if(el.is('[codename="ShippingRates-Internal"]')){
					// collapse/expand shipping rates on help page
					var group = $('.shipping-rates-group');
					$('.shipping-rate-block', group).hide();
					$('h2', group).prepend('<span class="rate-status"><b class="sr1">open</b><b class="sr2">close</b></span>').click(function(){
						$(this).toggleClass('open').next('.shipping-rate-block').slideToggle();
					});
					$('.shipping-rate-block:first', group).show();
					$('h2:first', group).toggleClass('open');
				}
				el.remove();
			});
		});
		

		//$("#BreadCrumbsPane").prepend('<div class="santa2009"></div>');
	},
/*
	toggleShippingRate: function(){
	},
*/
	addCorners: function(){
		$(".wrapper").prepend('<b class="tr"></b><b class="bl"></b><b class="br"></b><b class="tl"></b>');
		$(".corners-top").prepend('<b class="tr"></b><b class="tl"></b>');
		$(".corners-bottom").prepend('<b class="bl"></b><b class="br"></b>');
		if ( $.browser.msie && $.browser.version <= 7 ){
			$(".wrapper, .corners-top, .corners-bottom").each(function(i){
				if ( this.offsetHeight/2 != parseInt(this.offsetHeight/2)){
					$(".bl:eq(0), .br:eq(0)", this).addClass("bFix");
				}
				if ( this.offsetWidth/2 != parseInt(this.offsetWidth/2)){
					$(".tr:eq(0), .br:eq(0)", this).addClass("rFix");
				}
			});
		}
	},

	initTreeMenues: function(){
		$("ul.treemenu li span.control-closed, ul.treemenu li span.control-opened").click(function(){
			$(this).toggleClass("control-closed").toggleClass("control-opened").nextAll("ul").toggleClass("closed").toggleClass("opened");
		});
	},

	equalHeight: function (group) {
		if(group){
			tallest = 0;
			group.css({height: "auto"});
			group.each(function() {
				thisHeight = $(this).height();
				if(thisHeight > tallest) {
					tallest = thisHeight;
				}
			});
			group.height(tallest);
		}
	},

	resetJSXCache: function(script){
		var s = script.src;
		var parameter = s.substring(s.indexOf('?'), s.length);
		if(parameter.length>1){
			jsx.ConstructURL.construct = function(string, type){
				type = type || 'js';
				string = string.replace(/\./g, '/');
				function replacealias(match){
					var alias = match.substr(1, match.length - 2);
					return jsx.aliases[alias];
				}
				string = string.replace(/\{[^\}]+\}/ig, replacealias);
				var http = string.match(/^https?\:\/\//i) || '';
				string = string.replace(/^https?\:\/\//i, '');
				string = string.replace(/\/\//ig, '/');
				return http + string + '.' + type + parameter;
			};
		}
	}
}

if (window.jsx) jsx.getScriptByFileName('global.js', EMart.resetJSXCache);


function debug(o){
	console.log(o);
}



$(document).ready( function(){
	EMart.init();
});


/* for png32 transparent icons
if(EMart.ie6)
	jsx.require('iepngfix', function(){
		DD_belatedPNG.fix('#nav1 a');
	});
*/



