jQuery.extend({
	getPosition : function(e, forceIt)
	{
		var x = 0;
		var y = 0;
		var es = e.style;
		var restoreStyles = false;
		if (forceIt && jQuery.curCSS(e,'display') == 'none') {
			var oldVisibility = es.visibility;
			var oldPosition = es.position;
			restoreStyles = true;
			es.visibility = 'hidden';
			es.display = 'block';
			es.position = 'absolute';
		}
		var el = e;
		if (el.getBoundingClientRect) { // IE
			var box = el.getBoundingClientRect();
			x = box.left + Math.max(document.documentElement.scrollLeft, document.body.scrollLeft) - 2;
			y = box.top + Math.max(document.documentElement.scrollTop, document.body.scrollTop) - 2;
		} else {
			x = el.offsetLeft;
			y = el.offsetTop;
			el = el.offsetParent;
			if (e != el) {
				while (el) {
					x += el.offsetLeft;
					y += el.offsetTop;
					el = el.offsetParent;
				}
			}
			if (jQuery.browser.safari && jQuery.curCSS(e, 'position') == 'absolute' ) {
				x -= document.body.offsetLeft;
				y -= document.body.offsetTop;
			}
			el = e.parentNode;
			while (el && el.tagName.toUpperCase() != 'BODY' && el.tagName.toUpperCase() != 'HTML') 
			{
				if (jQuery.curCSS(el, 'display') != 'inline') {
					x -= el.scrollLeft;
					y -= el.scrollTop;
				}
				el = el.parentNode;
			}
		}
		if (restoreStyles == true) {
			es.display = 'none';
			es.position = oldPosition;
			es.visibility = oldVisibility;
		}
		return {x:x, y:y};
	},
	getPositionLite : function(el)
	{
		var x = 0, y = 0;
		while(el) {
			x += el.offsetLeft || 0;
			y += el.offsetTop || 0;
			el = el.offsetParent;
		}
		return {x:x, y:y};
	},
	getSize : function(e)
	{
		var w = parseInt(jQuery.curCSS(e,'width'), 10);
		var h = parseInt(jQuery.curCSS(e,'height'), 10);
		var wb = 0;
		var hb = 0;
		if (jQuery.curCSS(e, 'display') != 'none') {
			wb = e.offsetWidth;
			hb = e.offsetHeight;
		} else {
			var es = e.style;
			var oldVisibility = es.visibility;
			var oldPosition = es.position;
			es.visibility = 'hidden';
			es.display = 'block';
			es.position = 'absolute';
			wb = e.offsetWidth;
			hb = e.offsetHeight;
			es.display = 'none';
			es.position = oldPosition;
			es.visibility = oldVisibility;
		}
		return {w:w, h:h, wb:wb, hb:hb};
	},
	getSizeLite : function(el)
	{
		return {
			wb:el.offsetWidth||0,
			hb:el.offsetHeight||0
		};
	},
	getClient : function(e)
	{
		var h, w;
		if (e) {
			w = e.clientWidth;
			h = e.clientHeight;
		} else {
			var de = document.documentElement;
			w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
			h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
		}
		return {w:w,h:h};
	},
	getScroll : function (e)
	{
		var t=0, l=0, w=0, h=0, iw=0, ih=0;
		if (e && e.nodeName.toLowerCase() != 'body') {
			t = e.scrollTop;
			l = e.scrollLeft;
			w = e.scrollWidth;
			h = e.scrollHeight;
		} else  {
			if (document.body) {
				t = document.body.scrollTop;
				l = document.body.scrollLeft;
				w = document.body.scrollWidth;
				h = document.body.scrollHeight;
			} else if (document.documentElement) {
				t = document.documentElement.scrollTop;
				l = document.documentElement.scrollLeft;
				w = document.documentElement.scrollWidth;
				h = document.documentElement.scrollHeight;
			}
			iw = self.innerWidth||document.documentElement.clientWidth||document.body.clientWidth||0;
			ih = self.innerHeight||document.documentElement.clientHeight||document.body.clientHeight||0;
		}
		return { t: t, l: l, w: w, h: h, iw: iw, ih: ih };
	},
	getMargins : function(e, toInteger)
	{
		var t = jQuery.curCSS(e,'marginTop') || '';
		var r = jQuery.curCSS(e,'marginRight') || '';
		var b = jQuery.curCSS(e,'marginBottom') || '';
		var l = jQuery.curCSS(e,'marginLeft') || '';
		if (toInteger)
			return {
				t: parseInt(t, 10)||0,
				r: parseInt(r, 10)||0,
				b: parseInt(b, 10)||0,
				l: parseInt(l, 10)
			};
		else
			return {t: t, r: r,	b: b, l: l};
	},
	getPadding : function(e, toInteger)
	{
		var t = jQuery.curCSS(e,'paddingTop') || '';
		var r = jQuery.curCSS(e,'paddingRight') || '';
		var b = jQuery.curCSS(e,'paddingBottom') || '';
		var l = jQuery.curCSS(e,'paddingLeft') || '';
		if (toInteger)
			return {
				t: parseInt(t, 10)||0,
				r: parseInt(r, 10)||0,
				b: parseInt(b, 10)||0,
				l: parseInt(l, 10)
			};
		else
			return {t: t, r: r,	b: b, l: l};
	},
	getBorder : function(e, toInteger)
	{
		var t = jQuery.curCSS(e,'borderTopWidth') || '';
		var r = jQuery.curCSS(e,'borderRightWidth') || '';
		var b = jQuery.curCSS(e,'borderBottomWidth') || '';
		var l = jQuery.curCSS(e,'borderLeftWidth') || '';
		if (toInteger)
			return {
				t: parseInt(t, 10)||0,
				r: parseInt(r, 10)||0,
				b: parseInt(b, 10)||0,
				l: parseInt(l, 10)||0
			};
		else
			return {t: t, r: r,	b: b, l: l};
	},
	getPointer : function(event)
	{
		var x = event.pageX || (event.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft)) || 0;
		var y = event.pageY || (event.clientY + (document.documentElement.scrollTop || document.body.scrollTop)) || 0;
		return {x:x, y:y};
	},
	traverseDOM : function(nodeEl, func)
	{
		func(nodeEl);
		nodeEl = nodeEl.firstChild;
		while(nodeEl){
			jQuery.iUtil.traverseDOM(nodeEl, func);
			nodeEl = nodeEl.nextSibling;
		}
	},
	purgeEvents : function(nodeEl)
	{
		jQuery.iUtil.traverseDOM(
			nodeEl,
			function(el)
			{
				for(var attr in el){
					if(typeof el[attr] === 'function') {
						el[attr] = null;
					}
				}
			}
		);
	},
	getInnerWidth :  function(el, scroll) {
		var offsetW = el.offsetWidth;
		return scroll ? Math.max(el.scrollWidth,offsetW) - offsetW + el.clientWidth:el.clientWidth;
		/*return (scroll ? Math.max(el.scrollWidth,el.offsetWidth) : el.offsetWidth)
					- (parseInt($.curCSS(el, 'borderLeftWidth'), 10)||0)
					- (parseInt($.curCSS(el, 'borderRightWidth'), 10)||0);*/
	},
	getInnerHeight : function(el, scroll) {
		var offsetH = el.offsetHeight;
		return scroll ? Math.max(el.scrollHeight,offsetH) - offsetH + el.clientHeight:el.clientHeight;
		/*return (scroll ? Math.max(el.scrollHeight,el.offsetHeight) : el.offsetHeight)
					- (parseInt($.curCSS(el, 'borderTopWidth'), 10)||0)
					- (parseInt($.curCSS(el, 'borderBottomWidth'), 10)||0);*/
	},
	getExtraWidth : function(el) {
		if($.boxModel)
			return (parseInt($.curCSS(el, 'paddingLeft'), 10)||0)
				+ (parseInt($.curCSS(el, 'paddingRight'), 10)||0)
				+ (parseInt($.curCSS(el, 'borderLeftWidth'), 10)||0)
				+ (parseInt($.curCSS(el, 'borderRightWidth'), 10)||0);
		return 0;
	},
	getExtraHeight : function(el) {
		if($.boxModel)
			return (parseInt($.curCSS(el, 'paddingTop'), 10)||0)
				+ (parseInt($.curCSS(el, 'paddingBottom'), 10)||0)
				+ (parseInt($.curCSS(el, 'borderTopWidth'), 10)||0)
				+ (parseInt($.curCSS(el, 'borderBottomWidth'), 10)||0);
		return 0;
	},
	isChildOf: function(parentEl, el, container) {
		if (parentEl == el) {
			return true;
		}
		if (!el) {
			return false;
		}
		if (parentEl.contains && !$.browser.safari) {
			return parentEl.contains(el);
		}
		if ( parentEl.compareDocumentPosition ) {
			return !!(parentEl.compareDocumentPosition(el) & 16);
		}
		var prEl = el.parentNode;
		while(prEl && prEl != container) {
			if (prEl == parentEl)
				return true;
			prEl = prEl.parentNode;
		}
		return false;
	},
	getParent: function(el, container, type) {
		var lastEl = false;
		if(el.parentNode) {
			while (el.parentNode && el.parentNode != container) {
				el = el.parentNode;
				if (el.nodeName.toUpperCase() == type) {
					lastEl = el;
				}
			}
		}
		return lastEl;
	}
});

$(document).ready(function(e){
		$('input[@name=use_contact_info]').bind('change', changeShippingInfo).trigger('change');
		if (document.getElementById("map")) {
			var map = new GMap2(document.getElementById("map"));
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());
			map.setCenter(new GLatLng(51.703422, 5.286446), 14);
			var marker = new GMarker(new GLatLng(51.703422, 5.286446));
			map.addOverlay(marker);
			marker.openInfoWindowHtml('<strong>Nederland Veilingen</strong><br />Veemarktkade 8 - 005 5222 AE \'s Hertogenbosch');
			/*var geocoder = new GClientGeocoder();
			geocoder.getLatLng(
				'Veemarktkade 8 5222 \'s-Hertogenbosch Netherlands',
				function(point) {
					for(i in point) {
						console.log(i + ':' + point[i]);
					}
					if (!point) {
						alert(" not found");
					} else {
						map.setCenter(point, 13);
						var marker = new GMarker(point);
						map.addOverlay(marker);
						marker.openInfoWindowHtml('Share Of Mind - Hoofdkantoor');
					}
				}
			);*/
		}
	$('#newsletterForm button').bind('click', function(){
		$('#newsletterSubmit').hide();
		$('#newsletterLoader').show();
		nameVal = document.getElementById('newsletterName').value;
		emailVal = document.getElementById('newsletterEmail').value;
		$.ajax(
			{
				type			: 'POST',
				url				: '/ajaxserver.php',
				data			: 'request=newsletter&email=' + emailVal + '&name=' + nameVal + '&language=nl',
				complete		: function()
				{
					$('#newsletterSubmit').show();
					$('#newsletterLoader').hide();
				},
				
				success			: function(xml)
				{
					errors = $('error', xml);
					okies = $('ok', xml);
					
					errors.each(
						function()
						{
							attrId = this.getAttribute('id');
							errorTag = $('#' + attrId + '_error');
							alert(this.firstChild.nodeValue);
						}
					);
					
					
					if (errors.size() == 0) {
						var message = $('message', xml).text();
						document.getElementById('newsletterName').value = '';
						document.getElementById('newsletterEmail').value = '';
						alert(message);
					}
				}
			}
		);
		return false;
	});
	
	$('#tellAFriend').bind(
		'click',
		function() {
			tellForm = $('#tellafriend_form');
			if (tellForm.css('display') == 'none') {
				tellForm.show().animate({top:1}, 300);
			} else {
				tellForm.animate({top:-400}, 300, function(){$(this).hide()});
			}
			this.blur();
			return false;
		}
	);
	$('#tell_cancel').bind(
		'click',
		function() {
			tellForm = $('#tellafriend_form');
			if (tellForm.css('display') == 'block') {
				tellForm.animate({top:-400}, 300, function(){$(this).hide()});
			}
			this.blur();
			return false;
		}
	);
	$('#tell_send').bind(
		'click',
		tellAFriend
	);
	menu.build();
})


var tellAFriend = function()
{
	$('#tell_send, #tell_cancel').attr('disabled', true);
	$('#tell_ajax_indicator').css('display', 'inline');
	
	$.ajax(
		{
			type			: 'POST',
			url				: '/ajaxserver.php',
			data			: 'request=tellafriend&email=' + $('#tell_email').val() + '&name=' + $('#tell_name').val() + '&friend_email=' + $('#tell_friends_email').val() + '&friend_name=' + $('#tell_friends_name').val()  + '&message=' + $('#tell_message').val()  + '&url=' + window.location.href,
			complete		: function()
			{
				$('#tell_send, #tell_cancel').attr('disabled', false);
				$('#tell_ajax_indicator').hide();
			},
			
			success			: function(xml)
			{
				errors = $('error', xml);
				okies = $('ok', xml);
				
				errors.each(
					function()
					{
						alert(this.firstChild.nodeValue);
					}
				);
								
				if (errors.size() == 0) {
					var message = $('message', xml)[0].firstChild.nodeValue;
					alert(message);
				}
			}
		}
	);
	this.blur();
	return false;
}

var changeShippingInfo = function(e) {
	if ($(this).attr('checked') === true) {
		$('input[@name=shipping_street]')
			.val($('input[@name=street]').val())
			.attr('disabled', true);
		/*$('input[@name=shipping_suburb]')
			.val($('input[@name=suburb]').val())
			.attr('disabled', true);*/
		$('input[@name=shipping_city]')
			.val($('input[@name=city]').val())
			.attr('disabled', true);
		/*$('input[@name=shipping_state]')
			.val($('input[@name=state]').val())
			.attr('disabled', true);*/
		$('input[@name=shipping_postcode]')
			.val($('input[@name=postcode]').val())
			.attr('disabled', true);
		$('select[@name=shipping_country]')
			.val($('select[@name=country]').val())
			.attr('disabled', true);
	} else {
		$('input, select', this.parentNode.parentNode.parentNode.parentNode).attr('disabled', false);
	}
}

var menu = function() {
	var _container, 
		_lastover, 
		_firstHover = false,
		_menus = {};
	var mousemove =  function(e) {
		if (!_firstHover) {
			_firstHover = true;
		}
		if ((menuItem = $.getParent(e.target, _container[0], 'LI')) !== false) {
			if (_lastover != menuItem) {
				setLastOver(menuItem);
			}
		} else {
			setLastOver(null);
		}
	};
	var mouseout = function(e) {
		if (_lastover && !$.isChildOf(_lastover, e.relatedTarget, _container[0])) {
			setLastOver(null);
		}
		if (!$.isChildOf(_container[0], e.relatedTarget, _container[0])) {
			_firstHover = false;
		}
	};
	var setLastOver = function(value) {
		if (_lastover) {
			$('ul', _lastover)
				.stop()
				.fadeOut(400);
			$('>a', _lastover).removeClass('highlighted');
		}
		_lastover = value;
		if (_lastover) {
			var trg = $('>a', _lastover).addClass('highlighted').get(0);
			var pos = $.getPosition(trg, true);
			$('ul', _lastover)
				.css({
					top: pos.y + trg.offsetHeight + 'px',
					left: pos.x + 'px',
					opacity: 0,
					display: 'block'
				})
				.stop()
				.fadeTo(400, 1);
		}
	};
	var hide = function(doHide){
		if (doHide == true) {
			_container.hide();
		} else {
			_container.show();
		}
	};
	return {
		build: function() {
			_container = $('#navigation')
				.bind('mousemove', mousemove)
				.bind('mouseout', mouseout)
		}
	};
}();

Number.prototype.formatMoney = function(c, d, t){
    var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t,
    i = parseInt(n = (+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    return (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t)
    + (c ? d + (n - i).toFixed(c).slice(2) : "");
};

$(document).ready(function(){
	$('#placeBid').bind('click', function(){
		var val = parseFloat($('#bid').val()) || 0;
		$('#bid').val(val);
		
		return confirm(bidConfirm1 + ' ' + val.formatMoney() + ' EURO ' + bidConfirm2);
	});
	$('#autoBid').bind('click', function(){
		var val = parseFloat($('#autobid_val').val()) || 0;
		$('#autobid_val').val(val);
		
		return confirm(bidConfirm1 + ' ' + val.formatMoney() + ' EURO ' + bidConfirm2);
	});
});