var body_el, head_el;
var connectInterval = false;

document.observe('dom:loaded', commonLoad);
Event.observe(window, 'load', windowLoad);

Prototype.Browser.IE6 = Prototype.Browser.IE && parseFloat(navigator.appVersion.split("MSIE")[1]) >= 6 && parseFloat(navigator.appVersion.split("MSIE")[1]) < 7;
var cookies = new CookieJar({
	expires: 3600,
	path : '/'
});

function commonLoad()
{
	body_el = $$('body').first();
	head_el = $$('head').first();
	correctPNG();
	if($('sameas'))
	{
		Event.observe('sameas', 'click', copyAddress);
	}
	//script_url = $('prototype').src.replace(/prototype(.*)\.js.*$/, '');
	//base_url = script_url.replace(/^(.+\/)([^/]+\/)$/, '$1');
}

function windowLoad()
{
}

function changeElements() { // replace necessary submit elements with nicer versions since we can
	if($('country_update')) {
		$('country_update').hide(); // onchange event will take care of form submission
	  }
	if($('search_submit')) {
		$('search_submit').innerHTML='<a href="javascript:document.forms[\'search\'].submit();">GO</a>';
	}
}

function relPopup() { // standards compliant popups
	// use rel="popup" instead of target attribute on anchor links
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "popup") anchor.target = "_blank";
	}
}

function correctPNG() {
	var arVersion = navigator.appVersion.split("MSIE")
	var version = parseFloat(arVersion[1])
	if ((version >= 5.5) && (document.body.filters) && (version < 7))
	{
		for(var i=0; i<document.images.length; i++)
		{
			var img = document.images[i]
			var imgName = img.src.toUpperCase()
			if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
			{
				var imgID = (img.id) ? "id='" + img.id + "' " : ""
				var imgClass = (img.className) ? "class='" + img.className + "' " : ""
				var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
				var imgStyle = "display:inline-block;" + img.style.cssText
				if (img.align == "left") imgStyle = "float:left;" + imgStyle
				if (img.align == "right") imgStyle = "float:right;" + imgStyle
				if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
				var strNewHTML = "<span " + imgID + imgClass + imgTitle
				+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
				+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
				+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
				img.outerHTML = strNewHTML
				i = i-1
			}
		}
	}
}

function MM_preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

var imgSwap = function(opt){
  var o = opt;
  var Rollover = function() {
	$A(o.selector).each(function(s) {
	  $$(s).each(
		function(i) {
		  var os = i.src, ni = document.createElement('img');
		  $(i).observe('mouseout', function(e){i.src = os;});
		  ni.src = o.srcFunction(i,o);
		  i.observe('mouseover', function(e){i.src = ni.src;});
		}
	  );
	});
  };
	Event.observe(window, 'load', Rollover);
}
imgSwap({
  selector : ['img.rollover'],
  srcFunction : function(i, o) {
	return i.src.replace(/(.*)\.(jpg|gif|png)$/i, "$1-on.$2");
  }
});

function MM_openBrWindow(theURL,winName,features) { //v2.0
	TheNewWin = window.open(theURL,winName,features);
	setTimeout('TheNewWin.focus();',250);
}

function disableForm(theform) {
		if (document.all || document.getElementById) {
			for (i = 0; i < theform.length; i++) {
				var tempobj = theform.elements[i];
				if (tempobj.type.toLowerCase() == "submit") {
					tempobj.disabled = true;
				}
			}
			//alert("Your form has been submitted.	Notice how the submit and reset buttons were disabled upon submission.");
			return true;
		}
		else {
			//alert("The form has been submitted.	But, since you're not using IE 4+ or NS 6, the submit button was not disabled on form submission.");
			return true;
		}
}


function debug(text)
{
	$('debug').update($('debug').innerHTML += text + "<br/>");
}

function randomString(length)
{
	var chars = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
	var str = '';
	for(var i = 0; i < length; i++)
	{
		str += chars.substr(Math.floor(Math.random() * chars.length), 1);
	}
	return str;
}

/*
 * Orginal: http://adomas.org/javascript-mouse-wheel/
 * prototype extension by "Frank Monnerjahn" <themonnie@gmail.com>
 */
Object.extend(Event, {
	mouseWheelUp: function(event){
		return event.wheel() > 0;
	},
	mouseWheelDown: function(event){
		return event.wheel() < 0;
	},
	wheel:function (event){
		var delta = 0;
		if (!event) event = window.event;
		if (event.wheelDelta) {
			delta = event.wheelDelta/120;
			if (window.opera) delta = -delta;
		} else if (event.detail) { delta = -event.detail/3;	}
		return Math.round(delta); //Safari Round
	}
});

// handle default values on input fields
Event.observe(window, 'load', function() {
	var default_values = new Array();
	$$("input.replace-default").each(function(s) {
		$(s).observe('focus', function() {
			if (!default_values[s.id]) {
				default_values[s.id] = s.value;
			}
			if (s.value == default_values[s.id]) {
				s.value = '';
				s.removeClassName("replace-default");
			}
			$(s).observe('blur', function() {
				if (s.value == '') {
					s.value = default_values[s.id];
					s.addClassName("replace-default");
				}
			});
		});
	});
});


function copyAddress()
{
	var sameas = $('sameas');
	if(sameas.checked)
	{
		//Get all billing address fields and map them across to shipping details
		var div = sameas.up('form').select('.billing').first().select('input, select').each(function(item){
			var id = item.identify() + '1';
			if($(id))
			{
				$(id).value = item.value;
			}
		});
	}
}

function demoSwapImg(el) {
    demoImg = el.src;
    if (demoImg.match("-WHT")) {
        el.src = el.src.replace("-WHT", "-BLK");
    }
    else {
        el.src = el.src.replace("-BLK", "-WHT");
    }
}
function demoRestoreImg(el) {
   el.src = demoImg;
}
function liveSwapImg(el) {
    liveImg = el.src;
    el.src = el.src.replace("-1.", "-2.");
}
function liveRestoreImg(el) {
    el.src = liveImg;
}
