﻿// Load Functions
$(document).ready(function() {
	/* Replacement calls. Please see documentation for more information. */
	if(typeof sIFR == "function"){
		// This is the preferred "named argument" syntax
		sIFR.replaceElement(named({sSelector:".sIFR", sFlashSrc:"http://www.3horseranchvineyards.com/assets/flash/_p22-cezanne-altone.swf", sColor:"#ab965e", sLinkColor:"#ab965e", sBgColor:"#FFFFFF", sHoverColor:"#ab965e", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=left&offsetTop=0&offsetLeft=0", sWmode:"transparent"}));
	}
	
	$('input:text').hint();

	// PNG Fix for IE6
	$('div#masthead').ifixpng();
	$('.png_image').ifixpng();

	// Initialize XHTML External Link replacement
	externalLinks();

	// Preload Images
	preloadImages(
		'http://www.3horseranchvineyards.com/assets/images/headers/header_home.png',
		'http://www.3horseranchvineyards.com/assets/images/headers/header_theranch.png',
		'http://www.3horseranchvineyards.com/assets/images/headers/header_rootstock.png',
		'http://www.3horseranchvineyards.com/assets/images/headers/header_winemaker.png',
		'http://www.3horseranchvineyards.com/assets/images/headers/header_ourlabel.png',
		'http://www.3horseranchvineyards.com/assets/images/headers/header_theclub.png',
		'http://www.3horseranchvineyards.com/assets/images/headers/header_buyourwines.png',
		'http://www.3horseranchvineyards.com/assets/images/headers/header_errorpages.png',
		'http://www.3horseranchvineyards.com/assets/images/content/side-montage_home.gif',
		'http://www.3horseranchvineyards.com/assets/images/content/side-montage_theranch.png',
		'http://www.3horseranchvineyards.com/assets/images/content/side-montage_rootstock.png',
		'http://www.3horseranchvineyards.com/assets/images/content/side-montage_winemaker.png',
		'http://www.3horseranchvineyards.com/assets/images/content/side-montage_ourlabel.png',
		'http://www.3horseranchvineyards.com/assets/images/content/side-montage_theclub.png',
		'http://www.3horseranchvineyards.com/assets/images/content/side-montage_buyourwines.png'
	);
	
	// IE 6 Drop Down Navigation
	ieHover();
});

// Unload Functions
$(window).unload(function() {

});

// Generate Anti-SPAM E-Mail
function generateAddress(appearance, username, hostname, tld)
{
	var atsign = "&#64;";

	// If SAMEADDRSS is present, then the E-mail address will be shown instead of a custom value
	if (appearance == "SAMEADDRESS")
	{
		appearance = username + atsign + hostname + "." + tld;
	}

	var addr = username + atsign + hostname + "." + tld;
	document.write("<" + "a" + " " + "href=" + "mail" + "to:" + addr + " \/>" + appearance + "<\/a>");
}

// IE Hover Element Function
function ieHover() {
	var sfEls = document.getElementById("nav_main").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			$(this).addClass("sfHover");
		}
		sfEls[i].onmouseout=function() {
			$(this).removeClass("sfHover");
		}
	}
}

/* XHTML Compliant Open Window */
function externalLinks() {
	var anchors = $('.external');

	if (anchors) {
		for (var i = 0; i < anchors.length; i++)
		{
			anchors[i].target = "_blank";
		}
	}
}

/* Image Pre-Loader */
function preloadImages()
{
	if (document.images)
	{
		if (typeof(document.preload) == 'undefined')
		{
			document.preload = new Object();
		}
		document.preload.loadedimages = new Array();
		var arglength = preloadImages.arguments.length;
		for(arg=0; arg < arglength; arg++)
		{
			document.preload.loadedimages[arg] = new Image();
			document.preload.loadedimages[arg].src = preloadImages.arguments[arg];
		}
	}
}

// jQuery Textbox Hint
jQuery.fn.hint = function() {
	return this.each(function() {
		// get jQuery version of 'this'
		var t = jQuery(this);
		// get it once since it won't change
		var title = t.attr('title');
		// only apply logic if the element has the attribute
		if (title) {
			// on blur, set value to title attr if text is blank
			t.blur(function() {
				if (t.val() == '') {
					t.val(title);
					t.addClass('blur');
				}
			});
			// on focus, set value to blank if current value 
			// matches title attr
			t.focus(function() {
				if (t.val() == title) {
					t.val('');
					t.removeClass('blur');
				}
			});

			// clear the pre-defined text when form is submitted
			t.parents('form:first()').submit(function() {
				if (t.val() == title) {
					t.val('');
					t.removeClass('blur');
				}
			});

			// now change all inputs to title
			t.blur();
		}
	});
}