/***********************************************
	general functions
 ***********************************************/	
function formSubmit(formName) {
	document.forms[formName].submit();
}

function formReset(formName) {
	document.forms[formName].reset();
}

function popGallery(gallery_id) {
	var width = 602;
	var height = 550;
	var options = "width="+ width +",height="+ height +",toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=no,copyhistory=no";
	pop("popup_gallery.asp?gallery_id="+ gallery_id, "galleryWin", options)
}

function pop(url, wName, options) {
	if (wName == "") wName = "newwin";
	window.open(url, wName, options);
}

function switchLang () {
	engDelimiter = '/en/';
	freDelimiter = '/fr/';
	newURL = switchLangSearch(engDelimiter, freDelimiter);
	if (newURL != 'null') {
		self.location = newURL;
		return;
	} 
}

function switchLangSearch (engDelim, freDelim) {
	docURL = document.URL;
	newURL = 'null';
	engPosition = 0;
	frePosition = 0;
	charStart = 0;
	charEnd = docURL.length + 1;
	engDelimiter = engDelim;
	freDelimiter = freDelim;
	do {
		var test = docURL.substr(charStart, engDelimiter.length);
		if (test == engDelimiter) { engPosition = charStart;}
		charStart++;
	}
	while (charStart < charEnd - engDelimiter.length);
	if (engPosition > 0) {
		newURL = docURL.substr(0,engPosition) + freDelimiter + docURL.substr((engPosition + engDelimiter.length),docURL.length);
	} else {
		charStart = 0;
		do {
			var test = docURL.substr(charStart, freDelimiter.length)
			if (test == freDelimiter) { frePosition = charStart;}
			charStart++;
		}
		while (charStart < charEnd - freDelimiter.length);
		if (frePosition > 0) {
			newURL = docURL.substr(0,frePosition) + engDelimiter + docURL.substr((frePosition + freDelimiter.length),docURL.length);
		}
	}
	return newURL;
}

/***********************************************
	functions for controlling on/off graphic
	states
 ***********************************************/	
function doswap(name,onoff){
	var imageSrc = document.images[name].src;
	document.images[name].src = swapRoll(imageSrc,onoff,false);
}

function doNavSwap(name,onoff) {
	var imageSrc = document.images[name].src;
	document.images[name].src = swapRoll(imageSrc,onoff,true);
}

function swapRoll(tmp1,onoff,overflag){
	var strOn = "_on.gif"
	var strOff = "_off.gif"
	if (overflag) strOn = "_over.gif"
	
	if(onoff){
		return tmp1.replace(strOff,strOn);
	}else{
		return tmp1.replace(strOn,strOff);
	}
		
}

/***********************************************
	writing whole grain logo layer
 ***********************************************/	
if (!window.saveInnerWidth) {
  window.onresize = resizeIt;
  window.saveInnerWidth = getWindowWidth();
  window.saveInnerHeight = getWindowHeight();
}

function resizeIt() {
	moveLogo();
}

function getWindowWidth() {
	/*
	if (typeof(window.innerWidth) == "number") {
		var width = window.innerWidth;
	} else {
		var width = window.document.body.offsetWidth;
	}	
	*/
	var pageWidth;
	
	if (navigator.appName.indexOf("Microsoft") != -1) pageWidth = 10;//window.document.body.offsetWidth;
	else pageWidth = window.innerWidth;

	return pageWidth;
	alert(pageWidth);
}

function getWindowHeight() {
	//var height = window.innerHeight;
	//if (typeof(height) != "number") height = window.document.body.offsetHeight;
}

function getRef(objName)  {
	var objRef = null;
	
	if (document.layers) {
		// NN4 reference
		objRef = document[objName];
		
	} else if(document.all){
		// IE reference
		objRef = document.all[objName].style;
		
	} else if (document.getElementById){
		// others (mostly NN6+ and Firefox)
		objRef = document.getElementById(objName).style;
	}
	
	return objRef;
}

function returnLanguage() {
	var url = self.location.href;
	if (url.indexOf("/fr/") > -1) {
		return "french";
	} else {
		return "english";
	}
}

function writeLogo() {
	var objRef = document.images['home'];
	
	// set offset values
	var xOffset = 0;
	var yOffset = 0;
	if (document.all) {
		// set offset for ie
		xOffset = -175;
		yOffset = -25;
	} else {
		// set offset for others
		xOffset = -190;
		yOffset = -30;
	}	
	
	// get reference x and y positions
	var xPos = findPosX(objRef) + xOffset;
	var yPos = findPosY(objRef) + yOffset;
	
	// write div layer for logo
	document.write("<div id=\"logo-div\" style=\"position:absolute;top:"+ yPos +";left:"+ xPos +";\">");
	document.write("<img src=\"med/logo_wholeGrain.gif\" border=\"0\">");
	document.write("</div>");
}

function moveLogo() {
	var objRef = getRef("logo-div");
	var anchorRef = document.images['home'];
	var lang = returnLanguage();
	
	if (typeof(objRef) != "undefined" && objRef != null) {
		// set offset values
		var xOffset = -175;
		var yOffset = -15;
		
		if (lang == "french") {
			xOffset = -193;
			yOffset = -17;
		}
		
		// get reference x and y positions
		var xPos = findPosX(anchorRef) + xOffset;
		var yPos = findPosY(anchorRef) + yOffset;
		
		// move logo
		objRef.left = xPos;
		objRef.top = yPos;
	}
	
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	var printstring = '';
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			printstring += ' element ' + obj.tagName + ' has ' + obj.offsetTop;
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	//window.status = printstring;
	return curtop;
}

/***********************************************
	functions for flash detection and writing
 ***********************************************/	
FlashObject = function(swf, id, w, h, ver, c) {
	this.swf = swf;
	this.id = id;
	this.width = w;
	this.height = h;
	this.version = ver || 6; // default to 6
	this.align = "middle"; // default to middle
	this.redirect = "";
	this.sq = document.location.search.split("?")[1] || "";
	this.altTxt = "Please <a href='http://www.macromedia.com/go/getflashplayer'>upgrade your Flash Player</a>.";
	this.bypassTxt = "<p>Already have Flash Player? <a href='?detectflash=false&"+ this.sq +"'>Click here if you have Flash Player "+ this.version +" installed</a>.</p>";
	this.params = new Object();
	this.variables = new Object();
	if (c) this.color = this.addParam('bgcolor', c);
	this.addParam('quality', 'high'); // default to high
	this.addParam('scale', 'exactfit');
	this.doDetect = getQueryParamValue('detectflash');
}

FlashObject.prototype.addParam = function(name, value) {
	this.params[name] = value;
}

FlashObject.prototype.getParams = function() {
    return this.params;
}

FlashObject.prototype.getParam = function(name) {
    return this.params[name];
}

FlashObject.prototype.addVariable = function(name, value) {
	this.variables[name] = value;
}

FlashObject.prototype.getVariable = function(name) {
    return this.variables[name];
}

FlashObject.prototype.getVariables = function() {
    return this.variables;
}

FlashObject.prototype.getParamTags = function() {
    var paramTags = "";
    for (var param in this.getParams()) {
        paramTags += '<param name="' + param + '" value="' + this.getParam(param) + '" />';
    }
    if (paramTags == "") {
        paramTags = null;
    }
    return paramTags;
}

FlashObject.prototype.getHTML = function() {
    var flashHTML = "";
    if (window.ActiveXObject && navigator.userAgent.indexOf('Mac') == -1) { // PC IE
        flashHTML += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' + this.width + '" height="' + this.height + '" id="' + this.id + '" align="' + this.align + '">';
        flashHTML += '<param name="movie" value="' + this.swf + '" />';
        if (this.getParamTags() != null) {
            flashHTML += this.getParamTags();
        }
        if (this.getVariablePairs() != null) {
            flashHTML += '<param name="flashVars" value="' + this.getVariablePairs() + '" />';
        }
        flashHTML += '</object>';
    }
    else { // Everyone else
        flashHTML += '<embed type="application/x-shockwave-flash" src="' + this.swf + '" width="' + this.width + '" height="' + this.height + '" id="' + this.id + '" align="' + this.align + '"';
        for (var param in this.getParams()) {
            flashHTML += ' ' + param + '="' + this.getParam(param) + '"';
        }
        if (this.getVariablePairs() != null) {
            flashHTML += ' flashVars="' + this.getVariablePairs() + '"';
        }
        flashHTML += '></embed>';
    }
    return flashHTML;	
}


FlashObject.prototype.getVariablePairs = function() {
    var variablePairs = new Array();
    for (var name in this.getVariables()) {
        variablePairs.push(name + "=" + escape(this.getVariable(name)));
    }
    if (variablePairs.length > 0) {
        return variablePairs.join("&");
    }
    else {
        return null;
    }
}

FlashObject.prototype.write = function(elementId) {
	if(detectFlash(this.version) || this.doDetect=='false') {
		if (elementId) {
			document.getElementById(elementId).innerHTML = this.getHTML();
		} else {
			document.write(this.getHTML());
		}
	} else {
		if (this.redirect != "") {
			document.location.replace(this.redirect);
		} else {
			if (elementId) {
				document.getElementById(elementId).innerHTML = this.altTxt +""+ this.bypassTxt;
			} else {
				document.write(this.altTxt +""+ this.bypassTxt);
			}
		}
	}		
}

function getFlashVersion() {
	var flashversion = 0;
	if (navigator.plugins && navigator.plugins.length) {
		var x = navigator.plugins["Shockwave Flash"];
		if(x){
			if (x.description) {
				var y = x.description;
	   			flashversion = y.charAt(y.indexOf('.')-1);
			}
		}
	} else {
		result = false;
	    for(var i = 15; i >= 3 && result != true; i--){
   			execScript('on error resume next: result = IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.'+i+'"))','VBScript');
   			flashversion = i;
   		}
	}
	return flashversion;
}

function detectFlash(ver) {	
	if (getFlashVersion() >= ver) {
		return true;
	} else {
		return false;
	}
}

// get value of querystring param
function getQueryParamValue(param) {
	var q = document.location.search;
	var detectIndex = q.indexOf(param);
	var endIndex = (q.indexOf("&", detectIndex) != -1) ? q.indexOf("&", detectIndex) : q.length;
	if(q.length > 1 && detectIndex != -1) {
		return q.substring(q.indexOf("=", detectIndex)+1, endIndex);
	} else {
		return "";
	}
}

/* add Array.push if needed */
if(Array.prototype.push == null){
	Array.prototype.push = function(item){
		this[this.length] = item;
		return this.length;
	}
}

