﻿/* for Mozilla/Opera9 */
if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", init, false);
}
/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
    document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
    var script = document.getElementById("__ie_onload");
    script.onreadystatechange = function() {
        if (this.readyState == "complete") {
            init(); // call the onload handler
        }
    };
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
    var _timer = setInterval(function() {
        if (/loaded|complete/.test(document.readyState)) {
            init(); // call the onload handler
        }
    }, 10)
}

/* for other browsers */
window.onload = init;

/* Golbal scoped vars */


/* dom loaded call */
function init() {

// quit if this function has already been called
	if (arguments.callee.done) return;
// flag this function so we don't do the same thing twice
	arguments.callee.done = true;
// kill the timer
	if (_timer) clearInterval(_timer);


  if(typeof sIFR == "function") {
		initSIFR() ;
	}
  if (getElementsByClass('menu', null, 'div')[0]) {
		initMenu() ;
  }
  if (document.getElementsByTagName('table')[0]) {
		initZebraTable() ;
  }
  if (getElementsByClass('jsLinkList', null, 'ul')[0]) {
    	replaceDDL() ;
  }
  if (document.getElementsByTagName('input')[0]) {
		initClearInputs() ;
  }
  if (getElementsByClass('content', null, 'div')[0]) {
		addPrintBtn()
  }
  if (getElementsByClass('galleryItem', null, 'a')[0]) {
		initGallery() ;
  }
  initFlashBorders();
  initValidator(getElementsByClass('invalid',null,null).length > 0) ;
  initExternalLinks();
}

function initGallery() {
	var items = getElementsByClass('galleryItem', null, 'a') ;
	for (i=0; i<items.length; i++) {
		items[i].onclick = showGalItem ;
	}
}
function showGalItem() {
	ShowPopup(this.href,this.href+'.thumb?w=380&h=500',520,400,true);
	return false ;
}

function addPrintBtn() {
	var content = getElementsByClass('content', null, 'div')[0] ;
	var b = document.getElementsByTagName('body')[0] ;
	var img = document.createElement('img') ;
	img.src = '/Images/buttonPrint.gif' ;
	img.alt = 'Print' ;
	img.className = 'printBtn' ;
	img.onclick = function() {
		var oriClass = b.className ;
		b.className = 'print ' + oriClass ;
		window.print() ;
		b.className = oriClass ;
	}
	content.appendChild(img) ;
}

function initZebraTable() {
	var rows = document.getElementsByTagName('tr') ;
	for (i=0; i < rows.length; i+=2) {
		if ((!rows[i].getElementsByTagName('th')[0]) && (rows[i].className != 'paraControls')) {
			if (rows[i].parentNode.parentNode.className == 'noZebra') {
				continue ;
			}
			var className = 'evenRow ' + rows[i].className ;
			rows[i].className = className ;
		}
	}
}

function initClearInputs() {
  inputs = document.getElementsByTagName('input') ;
  for (i = 0; i < inputs.length; i++) {
    if ((inputs[i].getAttribute('value')) && (inputs[i].getAttribute('type') == 'text')) {
            inputs[i].setAttribute('ovalue',inputs[i].getAttribute('value'));
            inputs[i].onfocus = function() {
				if (this.value == this.getAttribute('value')) {
					this.value = '' ;
				}
			}
			inputs[i].onblur = function() {
				if (this.value == '') {
					this.value = this.getAttribute('ovalue');
				}
			}
    }
  }
}

function initValidator(eventOnly) {
    labels = getElementsByClass('req',null,'label');
    
    for (i=0;i < labels.length;i++)
    {
        var ctlfor = getLabelFor(labels[i]);
        var ctl = getElement(ctlfor);
        
        // Set the actions for supported types
        switch (ctl.getAttribute('type'))
        {
            case "text":
                ctl.title = 'Required Field';
                if (!eventOnly) setInvalid(ctl);
                ctl.onkeyup = function() { if(this.value.length < 1){setInvalid(this);}else{this.className = this.className.replace('invalid','');} }
                ctl.onblur = function() { if(this.value.length < 1){setInvalid(this);}else{this.className = this.className.replace('invalid','');} }
                break;
                
            case "password":
                /* Password must be at least 6 Characters */
                ctl.title = 'Required Field';
                setInvalid(ctl);
                ctl.onkeyup = function() { if(this.value.length < 6){setInvalid(this);}else{this.className = this.className.replace('invalid','');} }
                ctl.onblur = function() { if(this.value.length < 6){setInvalid(this);}else{this.className = this.className.replace('invalid','');} }
                break;
                
            case "select-one":
                ctl.title = 'Required Field';
                if (!eventOnly) setInvalid(ctl);
                ctl.onchange = function() { if(this.selectedIndex == 0){setInvalid(this);}else{this.className = this.className.replace('invalid','');} }
                ctl.onblur = function() { if(this.selectedIndex == 0){setInvalid(this);}else{this.className = this.className.replace('invalid','');} }
                break;
        }   
    }
}

function initSIFR() {
	// Replacement calls. Please see documentation for more information. 
	sIFR.bAutoInit = false;
    sIFR.setup();
    //sIFR.debug();
	// This is the preferred "named argument" syntax
	
	sIFR.replaceElement("Blue h2", named({sSelector:".Blue h1, .Blue h2, h2.BlueTitle", sFlashSrc:"/scripts/sIFR/Dax.swf", sColor:"#004A94", sLinkColor:"#004A94", sBgColor:"#FFFFFF", nPaddingTop:"10", sWmode:"transparent"}));
	sIFR.replaceElement("Blue h2 smaller", named({sSelector:".BlueTitle h1, .BlueTitle h2", sFlashSrc:"/scripts/sIFR/Dax.swf", sColor:"#004A94", sLinkColor:"#004A94", sBgColor:"#FFFFFF", nPaddingBottom:"10", sWmode:"transparent"}));
	sIFR.replaceElement("headers", named({sSelector:"h1", sFlashSrc:"/scripts/sIFR/Dax.swf", sColor:"#005537", sLinkColor:"#005537", sBgColor:"#FFFFFF", nPaddingBottom:"20", sWmode:"transparent"}));
	sIFR.replaceElement("subheaders", named({sSelector:"h2", sFlashSrc:"/scripts/sIFR/Dax.swf", sColor:"#005537", sLinkColor:"#005537", sBgColor:"#FFFFFF", sWmode:"transparent"}));
	sIFR.replaceElement("whatsNew", named({sSelector:".whatsNew strong", sFlashSrc:"/scripts/sIFR/Dax.swf", sColor:"#005537", sLinkColor:"#005537", sBgColor:"#DDE6B7", sWmode:"transparent"}));
	sIFR.replaceElement("foodClub", named({sSelector:".foodClub h4", sFlashSrc:"/scripts/sIFR/Dax.swf", sColor:"#005537", sLinkColor:"#005537", sBgColor:"#9CB93B", nPaddingBottom:"5", sWmode:"transparent"}));
}

function initMenu() {

	var menu = getElementsByClass('menu', null, 'div')[0] ;
	
	/* set all heights to 0 and store original height */

	var subs = menu.getElementsByTagName('ul') ;
	var i ;
	for (i=0; i < subs.length; i++) {	
		var sub = subs[i] ;
		sub.id = 'sub' + i ;
		sub.style.display = 'block' ;
		sub.oHeight = parseInt(sub.offsetHeight) ;
		if ((getElementsByClass('on',sub,'li').length == 0) && (sub.parentNode.className.lastIndexOf('on') == -1)) {
			//sub.style.height = '0' ; // removed in order to leave the menu open not collapsed
		} else {
			sub.style.height = 'auto';
		}
	}
	subs[0].style.height = subs[0].oHeight ;

	/* reference all handles */
	var links = menu.getElementsByTagName('a') ;
	for (i=0; i < links.length; i++) {
		if (links[i].parentNode.getElementsByTagName('ul').length > 0) {
			links[i].onclick = expand ;
		}
	}
	/* force top level to shrink in IE  */	
	/*@cc_on @*/
	/*@if (@_win32)
		subs[0].style.height = '1%' ;
	/*@end @*/
}

function expand() {
	var sub = this.parentNode.getElementsByTagName('UL')[0] ; 	
/* set current height */	
	var ch = (sub.style.height == 'auto' ? sub.offsetHeight : parseInt(sub.style.height)) ;
/* set all parents to auto height */
	var p = sub.parentNode ;
	while (p.nodeName != 'DIV') {
		if (p.nodeName == 'UL') p.style.height = 'auto' ;
		p = p.parentNode ;
	}
/* Determine target height */
	var finH = 0 ;
	if (ch == 0) {
		sub.style.height = 'auto' ;
		finH = sub.offsetHeight ;
		sub.style.height = 0 ;
	} else {
/* contract all immediate children */	
		var childSubs = sub.parentNode.childNodes ;
		for (i=0; i < childSubs.length; i++) {
			if (childSubs[i].nodeName == 'UL') childSubs[i].style.height = 'auto' ;
		}
	}
	
/* Finshing moving if clicked again */
	if (sub.movement) {		
		sub.style.height = (sub.finH == 0 ? 0 : sub.finH) + 'px' ;
/* Finish moving others if clicked */
	} else {
		var p = sub.parentNode.parentNode ;
		while (p.nodeName != 'DIV') {
			if (p.movement) {	
				clearTimeout(p.movement) ;
				p.movement = null ;
				p.style.height = 'auto' ;
			}
			p = p.parentNode ;
		}
/* Call Animation */
		animate(sub.id, finH, 30) ;
		sub.finH = finH ;
	}
	return false ;
}

function animate(subId, finH, interval) {
	var sub = document.getElementById(subId) ;
	var ch = (sub.style.height == 'auto' ? sub.offsetHeight : parseInt(sub.style.height)) ;	
	//if (sub.movement) clearTimeout(sub.movement) ;	
	if (ch == finH) {
		sub.movement = null ;		
		return true ;
	}	
	if (ch < finH) {
		var dist = Math.ceil((finH - ch)/3) ;
		ch += dist ;
	}
	if (ch > finH) {
		var dist = Math.ceil((ch - finH)/3) ;
		ch -= dist ;
	}
	sub.style.height = ch + 'px' ;
	sub.movement = setTimeout(function() {animate(subId, finH, interval)}, interval) ;		
}

/* External Links */
function initExternalLinks() {
    var links = document.getElementsByTagName('a') ;
    for (var i=0; i<links.length; i++) {
	    var link = links[i] ;
	    var rel = link.getAttribute("rel");
    	
	    if (rel == "externalLink") {
	        if (link.title == "") {
			    link.title = '(External link - opens a new window)' ;
		    } else {
			    link.title = link.title + ' (External link - New window)' ;
		    }
		    link.onclick = function(e) {
			    if (!e) e = window.event ;
			    if (e.shiftKey || e.ctrlKey || e.altKey) return ;
			    window.open(this.href) ;
			    return false ;
		    }
	    }
    }
}

/* Link Dropdown Replacement */
function replaceDDL() {
    var ddl = getElementsByClass('jsLinkList', null, 'ul');
    for (i=0;i < ddl.length;i++) {
       createLinkList(ddl[i]);
    }
}
function createLinkList(input) {
    // link list container
    var list = document.createElement('div');
    list.className = 'LinkList';
    
    // link list items
    var items = document.createElement('div');    
    for (i=0; i < input.childNodes.length; i++){
        var link = getChild(input.childNodes[i]);
        items.appendChild(link);
    }
    
    // link list textbox
    var lbl = document.createElement('label');
    var txt = document.createTextNode('Choose...');
    lbl.appendChild(txt);    
    lbl.onclick = toggleList;
    list.appendChild(lbl);
      
    // drop down arrow
    var arr = document.createElement('img');
    arr.setAttribute('src','/Images/GreenDropDownArrow.gif');
    arr.onclick = toggleList;
    list.appendChild(arr);       
    list.appendChild(items);   
      
    // Replace the Link List Element
    input.parentNode.replaceChild(list,input);
    
}
/* Flash Border Removal
===================================================================================== */
function initFlashBorders() {
    if (document.body.outerHTML && (document.getElementsByTagName('object')).length) {
		var objs = document.getElementsByTagName('object') ;
		var i = objs.length - 1 ;
		do {
			if (objs[i].getAttribute('type') == 'application/x-shockwave-flash') {
				var o = objs[i] ;
				var h = o.outerHTML ;
				var params = '' ;
				var j = o.childNodes.length - 1 ;
				do {
					var p = o.childNodes[j] ;
					if (p.tagName == "PARAM") params += p.outerHTML ;
				} while (j--) ;
				var tag = h.split(">")[0] + ">" ;
				o.outerHTML = tag + params + o.innerHTML + " </object>" ;
			}
		} while (i--)
	}
}
	
/* common methods */

function toggleList() {
    var elm = this.parentNode.getElementsByTagName('div')[0];
    elm.style.display = (elm.style.display == 'block' ? 'none' : 'block') ;
}
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	var x ;
	var j ;
	for (x = 0, j = 0; x < elsLen; x++) {
		if ( pattern.test(els[x].className) ) {
			classElements[j] = els[x];
			j++;
		}
	}
	return classElements;
}
function getChild(startParent) {
  var EndChild = startParent.firstChild ;
  while(EndChild.nodeType != 1){
    EndChild = EndChild.nextSibling ;
  }
  return EndChild;
}
function insertAfter(parent, node, referenceNode) {
  parent.insertBefore(node, referenceNode.nextSibling);
}
function getLabelFor(node) {
    if (node.getAttribute('for') != null) {
        return node.getAttribute('for');   
    }
    else
    {
        return node.htmlFor;    
    }
}
function setInvalid(node) {
    node.className = (node.className.replace('invalid','').length > 0 ? node.className += ' invalid' : 'invalid');
}
