/***************************************
Global JS file for Monogram Chef's Site
***************************************/


//jquery init
$(document).ready(function() {
	
	
	// for preventing burnthrough of multiple pseudo-selects
	var zCounter = 10000;
	
	$(".formTable select").each(function() {
		
		var selectContent = "";
		var selectedText = "";
		var selectedValue = "";
		$(this).children("option").each(function() {
			selectContent += '<div value="' + $(this).attr("value") + '">' + $(this).text() + '</div>';
			if($(this).attr("selected")) {
				selectedText = $(this).text();
				selectedValue = $(this).attr("value");
			}
		});
		
		$(this).before('<div style="position:relative; z-index:' + zCounter + ';"><div class="pseudoSelectList">' + selectContent + '</div><div class="pseudoSelect">' + selectedText + '</div></div>');
		
		// for preventing burnthrough of multiple pseudo-selects
		zCounter--;
		
		replacementInput = document.createElement('input');
		replacementInput.name = $(this).attr("name");
		replacementInput.type = "hidden";
		replacementInput.value = selectedValue;
		
		$(this).replaceWith(replacementInput);
		
	});
		
	$(".pseudoSelect").click(function() {
		$(this).siblings(".pseudoSelectList").toggle();
	});
	$(".pseudoSelect").mouseleave(function() {
		$(this).siblings(".pseudoSelectList").hide();
	});
	$(".pseudoSelectList").mouseover(function(){
		$(this).show();
	});
	$(".pseudoSelectList").mouseleave(function(){
		$(this).hide();
	});
	$(".pseudoSelectList div").mouseover(function(){
		if($(this).attr("value") != "") {
			$(this).css("background", "#cccccc");
		}
	});
	$(".pseudoSelectList div").mouseleave(function(){
		$(this).css("background", "#ffffff");
	});
	$(".pseudoSelectList div").click(function(){
		if($(this).attr("value") != "") {
			$(this).parent().siblings(".pseudoSelect").html( $(this).text() );
			$(this).parent().toggle();
			$(this).parent().parent().siblings("input").attr("value",$(this).attr("value"));
		}
	});
	$(".pseudoSelectList div[value='']").css("font-style","italic");
});


//cufon init
var i;
var danielText = new Array(
	"h1",
	"#mastheadSubTitle",
	"h2",
	".secondaryHeadingText",
	"h3",
	"#rightHeading",
	".pagination .label"
);

/*** supports Cufon ***/
function hideDanielElements() {
	var styleBlock = '<style type="text/css">';
	for (i in danielText) {
		if (i > 0) { styleBlock += ", "; }
		styleBlock += danielText[i] ;
	}
	styleBlock += ' { visibility:hidden; }</style>';
	document.write(styleBlock);
}
function showDanielElements() {
	for (i in danielText) {
		$(danielText[i]).css("visibility","visible");
	}
}
/*** end supports Cufon ***/

// do the Cufon! (has document.ready built-in)
for (i in danielText) {	Cufon.replace(danielText[i]); }
hideDanielElements(); //until the cufon hits...
$(document).ready(function() { showDanielElements(); }); // cufon has hit, so show the elements


// global vars
var popupWin;
var scrnwidth=screen.width;
var scrnheight=screen.height;



// define Base_Href variable for testing purposes
var Base_Href = "//www.monogram.com/all-in-good-food";
if (window.location.host.indexOf('teamsite') != -1) {
	var Base_Href = "//"+ window.location.host +"/all-in-good-food";
}//Close if



// "only 1 open at a time" popup function.
//-----------------------------------------------------------------------------
// The fifth argument, windowname, is optional.  If specified, it is passed as
// the second argument to window.open().  This is required in order to leave
// the popup opener page (without closing the popup), come back to it, and NOT
// have a duplicate popup window.
//-----------------------------------------------------------------------------
function openPop (url, width, height, addloptions, windowname) {
	if (windowname == null) {
		windowname = "";
	}
	var xspot = Math.round((scrnwidth/2)-(width/2));
	var yspot = Math.round((scrnheight/2)-(height/2)-30);
	features = "height="+height+",width="+width+",top="+yspot+",screenY="+yspot+",left="+xspot+",screenX="+xspot;
	if (addloptions && addloptions!="") { features += ","+addloptions; }
	if (!popupWin || popupWin.closed) {
		popupWin = window.open(url, windowname, features);
	}
	else {
		window.popupWin.close();
		popupWin = window.open(url, windowname, features);
	}
}


// sets a class of selected on an element (using jQuery selection engine)
function setSelectedClass(obj) {
	$(obj).addClass("selected");
}


//get url parameter value by name
function getParamFromURL(paramName)
{
  var regexS = "[\\?&'&amp;']"+ paramName +"=([^&#]*)";
  var regex = new RegExp( regexS );
  var tmpURL = window.location.href;
  var results = regex.exec( tmpURL );
  if( results == null )
    return "";
  else
    return results[1];
}


/*********** validation ***********/

// sub-function used to validate email address
function isValidEmailAddress (string) {
	return /^\w+((-\w+)|(\.\w+)|(\+\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9][A-Za-z0-9]+$/.test(string);
}




/******************* FOOTER FUNCTIONS *******************/
function validateFooterSubscribeForm (form) {
	if (!form.i_emailgeneric) {
		return true;
	}
	if(!(/^\w+((-\w+)|(\.\w+)|(\+\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9][A-Za-z0-9]+$/.test(form.i_emailgeneric.value))) {
		alert("This does not appear to be a valid e-mail address.  Please try again.");
		form.i_emailgeneric.select();
		return false;
	}
	return true;
}

function emailFieldOnFocus(obj) { if( obj.value == obj.defaultValue) { obj.value = ""; } }
function emailFieldOnBlur(obj) { if( obj.value == "" ) { obj.value = obj.defaultValue; } }

function openPrivacyPolicyPopup (url) {
	return openPop(url, 640, 500, "scrollbars,resizable",
		       "GEAPrivacyPolicy");
}

function openTermsPopup (url) {
	return openPop(url, 640, 500, "scrollbars,resizable",
		       "GEATerms");
}
/***************** END FOOTER FUNCTIONS *****************/

/**************** BEGIN GOOGLE ANALYTICS CODE ****************/
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-23305009-1']);
_gaq.push(['_trackPageview']);

(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
/**************** END GOOGLE ANALYTICS CODE ****************/


