


function doOnLoad() {

	//
	tell_a_friend_link();


	// load hover images
	loadRolloverImages();

	// preload images


}

function tell_a_friend_link() {
	var a = document.getElementById('tell_a_friend_link');
	if(!a) return;

	//var title = document.title.substr(0, document.title.indexOf(' - '));
	var title = document.title;

	if(a.href.indexOf('?') > -1) a.href += '&';
	else a.href += '?';
	a.href += 'page_title=' + escape(title);
}

function doPostBodyLoad() {

	window.mainmenu = new MainMenu();
	window.mainmenu.init();
	window.mainmenu.select_page(document.location.href);
}

function MainMenu() {
	this.menuitems = new Array();

	this.init = function() {

		var tblmainmenu = document.getElementById('tblmainmenu');
		if(!tblmainmenu) return;

		var tblmainmenuitems = tblmainmenu.getElementsByTagName('td');
		for(var i=0; i<tblmainmenuitems.length; ++i) {
			if(tblmainmenuitems[i].id.indexOf('mainmenu_') == 0) {
				var menukey = tblmainmenuitems[i].id.substr(9);
				this.menuitems[menukey] = new this.MenuItem();

				var hreflist = new Array();
				this.menuitems[menukey].root_href = tblmainmenuitems[i].getElementsByTagName('a')[0];
				this.menuitems[menukey].add_href(this.menuitems[menukey].root_href);
				this.menuitems[menukey].div = tblmainmenuitems[i].getElementsByTagName('div')[0];
				if(this.menuitems[menukey].div) {
					var sublist = this.menuitems[menukey].div.getElementsByTagName('a');
					for(var n=0; n<sublist.length; ++n) {
						this.menuitems[menukey].add_href(sublist[n]);
					}
				}
			}
		}
	}

	this.select_page = function(href) {
		var filename = href_to_filename(href);
		if(!filename) return;
		for(var k in this.menuitems) {
			if(!this.menuitems[k].hreflist) continue;
			for(var n=0; n<this.menuitems[k].hreflist.length; ++n) {
				if(this.menuitems[k].hreflist[n].filename == filename) {
					this.menuitems[k].hreflist[n].objref.className = 'on';
					this.menuitems[k].root_href.className = 'on';
					if(this.menuitems[k].div) {
						this.menuitems[k].div.style.display = 'block';
					}
				}
			}
		}

	}

	this.MenuItem = function() {
		this.root_href = null;
		this.hreflist = new Array();
		this.div = null;

		this.add_href = function(objref) {
			this.hreflist[this.hreflist.length] = new this.MenuItemLink(objref);
		}

		this.MenuItemLink = function(objref) {
			this.objref = objref;
			this.filename = href_to_filename(objref.href);
		}
	}


}



function href_to_filename(href) {
	href = href.replace(/^https?:\/\/[^\/]*/, '');
	if(href.match(/^\/shop\//)) {
		if(href.match(/category_\d+/)) {

		} else {
			href = '/shop/';
		}
	}
	return href;
}

function href_to_filename_old(href) {
	var m = href.match(/([^\/]*)$/);
	return m[1];
}


function loadRolloverImages() {

	var allimgs = document.getElementsByTagName('img');
	for(var i=0; i<allimgs.length; ++i) {
		if(allimgs[i].getAttribute('hoversrc')) {
			_loadRolloverImage(allimgs[i]);
		}
	}

	var allinputs = document.getElementsByTagName('input');
	for(var i=0; i<allinputs.length; ++i) {
		if(allinputs[i].getAttribute('hoversrc')) {
			_loadRolloverImage(allinputs[i]);
		}
	}

}

function _loadRolloverImage(img) {
	preloadImage(img.getAttribute('hoversrc'));

	img.onmouseover = function() {
		var src = this.getAttribute('src');
		if(src) {
			this.setAttribute('__src_orig', src);
			this.setAttribute('__filter_orig', this.style.filter);

			if(this.style.filter && navigator.userAgent.toUpperCase().indexOf('MSIE') >= 0) {
				this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+this.getAttribute('hoversrc')+"', sizingMethod='scale')";
			} else {
				this.setAttribute('src', this.getAttribute('hoversrc'));
			}
		}
	};
	img.onmouseout = function() {
		var src = this.getAttribute('__src_orig');
		if(src) {
			if(this.style.filter && navigator.userAgent.toUpperCase().indexOf('MSIE') >= 0) {
				this.style.filter = this.getAttribute('__filter_orig');
			} else {
				this.setAttribute('src', src);
			}
		}
	};

}

function preloadImage(href) {
	if(!window.preloadImageList) window.preloadImageList = new Array();
	var i = window.preloadImageList.length;

	window.preloadImageList[i] = new Image();
	window.preloadImageList[i].src = href;
}


function popupWin(that, w, h, options) {
	if(!w) w = 500;
	if(!h) h = 500;

	var x = (screen.availWidth - w) / 2;
	var y = (screen.availHeight - h) / 2;

	if(!options) options = '';

	var optslist = new Array();
	optslist[optslist.length] = 'width='+w;
	optslist[optslist.length] = 'height='+h;
	optslist[optslist.length] = 'scrollbars=auto';
	optslist[optslist.length] = 'resizable=yes';
	optslist[optslist.length] = 'left='+x;
	optslist[optslist.length] = 'top='+y;

	var bits = options.split(/,/);
	for(var i=0; i<bits.length; ++i) {
		var b = bits[i].split(/=/);
		optslist[optslist.length] = b[0] + '=' + b[1];
	}
	var optionstring = optslist.join(',');



	var href;
	if(that.href) href = that.href;
	else href = that;

	var win = window.open(that.href, 'popwin', optionstring);

	win.resizeTo(w, h);
	win.focus();

	return false;
}

function getLeft(obj) {
	return (obj.offsetParent==null ? obj.offsetLeft : obj.offsetLeft + getLeft(obj.offsetParent));
}
function getTop(obj) {
	return (obj.offsetParent==null ? obj.offsetTop : obj.offsetTop + getTop(obj.offsetParent));
}

// error class
function Errors() {
	this.errors = new Array();

	this.add = function(msg) {
		this.errors[this.errors.length] = msg;
	}
	this.alert = function() {
		if(!this.errors.length) return false;

		var msg = 'The following error(s) have occurred:\n\n';

		for(var i=0; i<this.errors.length; ++i) {
			msg += '*  ' + this.errors[i];
			if(i < this.errors.length - 1) msg += '\n\n';
		}

		alert(msg);
		return true;
	}
}

function validate_email(email) {
	if(email.length <= 5) return false;
	if(email.indexOf('@', 0) == -1) return false;
	return true;
}

function validate_optin(f) {
	var e = new Errors();
	if(!validate_email(f.email.value)) e.add("Sorry, Invalid Email Address!");

	return e.alert() ? false : true;
}



//Tell-a-friend

window._showShareTimer = null;
function showShare(that) {
	var div = $('divShare');
	if(!div) return;

	showShareClearTimer();
	if(div.getStyle('display') == 'none') {
		div.setStyles({
			'overflow': 'hidden',
			'height': 1,
			'display': 'block',
			'top': $(that).getTop() + $(that).getHeight() + 2,
			'left': $(that).getLeft()
		});
		new Fx.Tween(div, {
			duration: 220
		}).start('height', div.getScrollHeight());
	}
}
function showShareClearTimer() {
	clearTimeout(window._showShareTimer);
}

function showShareStartTimer() {
	window._showShareTimer = setTimeout(function() {
		showShareDoHide();
	}, 1000);
}

function showShareDoHide() {
	showShareClearTimer();
	$('divShare').setStyle('display', 'none');
}

function shareItemOver(that) {
	showShareClearTimer();
	$(that).addClass('over');

	var img = that.getElement('img');
	if(img) img.src = img.src.replace(/(\.[^\.]+)$/, '_a$1');
}

function shareItemOut(that) {
	showShareStartTimer();
	$(that).removeClass('over');

	var img = that.getElement('img');
	if(img) img.src = img.src.replace(/_a(\.[^\.]+)$/, '$1');
}

function shareFacebook(title) {
	showShareDoHide();
	var href = 'http://www.facebook.com/sharer.php?u='+escape(document.location.href)+'&t='+escape(title)+'';
	window.open(href, '_blank');
}

function shareTwitter(title) {
	showShareDoHide();

	var str = '';
	if(title) str += title + ' - ';
	str += document.location.href;
	var href = 'http://twitter.com/home?status=' + escape(str);
	window.open(href, '_blank');
}

function shareLinkedin(title) {
	showShareDoHide();
	var href = 'http://www.linkedin.com/shareArticle?mini=true&url='+escape(document.location.href)+'&title='+escape(title)+'';
	window.open(href, '_blank');
}

function shareBlogger(title) {
	showShareDoHide();
	var href = 'http://www.blogger.com/blog_this.pyra?t&u='+escape(document.location.href)+'&n='+escape(title)+'&pli=1';
	window.open(href, '_blank');
}

function shareEmail(title) {
	showShareDoHide();

	var str = '';
	if(title) str += title + ' - ';
	str += document.location.href;
	var href = 'http://www.levendiwinery.com/forward_to_a_friend.html?page_title=' + escape(str);
	window.open(href);
}




