var makePopupLinks = new domFunction(windowLinks);
//var makeRoundedImages = new domFunction(roundedImages);
var searchRecipe = new domFunction(advancedSearch,{"radioRecipes":"id"});
var duoFix = new domFunction(fixDuos);
var collapseForum = new domFunction(forumCollapse);


addLoadEvent(function() {
	infoPop();
	removeAlert();
	alertClose();
	collapseMyInfo();
	rating();
	searchRating();
	moreOrLess();
	valNumbers();
//	test();
	showHideCalendar();
});
function fixDuos(){
	if(!document.getElementsByTagName) return;
	if(!document.all) return;
	divs = document.getElementsByTagName("div");
	for(var i=0;i<divs.length;i++){
		if(divs[i].className == "duo"){
			if (divs[i].parentNode.className.indexOf("duoWrap") != -1){
				maxHeight = Math.max(divs[i].firstChild.offsetHeight,divs[i].firstChild.nextSibling.offsetHeight);
				divs[i].firstChild.style.height = maxHeight + "px";
				divs[i].firstChild.nextSibling.style.height = maxHeight + "px";
			}
		}
		if(divs[i].className == "trio") {
			maxHeight = Math.max(divs[i].firstChild.offsetHeight,divs[i].firstChild.nextSibling.offsetHeight,divs[i].firstChild.nextSibling.nextSibling.offsetHeight);
			divs[i].firstChild.style.height = maxHeight + "px";
			divs[i].firstChild.nextSibling.style.height = maxHeight + "px";
			divs[i].firstChild.nextSibling.nextSibling.style.height = maxHeight + "px";
		}
	}
}
function test(){
}
function valNumbers() {
	if(!document.getElementsByTagName) return;
	inputs = document.getElementsByTagName("input");
	for(var i=0;i<inputs.length;i++){
		if (inputs[i].className == "numbers"){
			inputs[i].onchange=function() {
				theWrong = /,/g;
				theWrong2 = / /g;
				theValue = this.value.replace(theWrong, ".");
				theValue = theValue.replace(theWrong2, "");
				theValue = theValue.trim();
				if (theValue == ""){
					this.value = "";
				} else if (parseFloat(theValue) != theValue) {
					alert(this.alt);
					this.value = "";
					this.focus();
				} else {
					this.value = theValue;
				}
			}
		}
	}
}
function moreOrLess() {
	if(!document.getElementsByTagName) return;
	theLinks = document.getElementsByTagName("a");
	if(!moreOptionsText) var moreOptionsText = "More options"
	if(!lessOptionsText) var lessOptionsText = "Less options"
	var moreText = document.createTextNode(moreOptionsText)
	var lessText = document.createTextNode(lessOptionsText)
	var i
	for(i=0;i<theLinks.length;i++) {
		if(theLinks[i].className == "moreOrLess") {
			theLinks[i].onclick = function() {
				if(this.parentNode.parentNode.className == "simple") {
					this.parentNode.parentNode.className = "advanced";
					this.replaceChild(lessText,this.firstChild);
				} else {
					this.parentNode.parentNode.className = "simple";
					this.replaceChild(moreText,this.firstChild);
				}
				return false;
			}
		}
	}
}
function advancedSearch() {
	if(!document.getElementById) return;
	if(document.getElementById("radioRecipes")) {
		document.getElementById("radioRecipes").onfocus=function() {
			this.parentNode.parentNode.className = "advanced"
		}
		document.getElementById("radioFoodstuff").onfocus=function() {
			this.parentNode.parentNode.className = "simple"
		}
		document.getElementById("radioAll").onfocus=function() {
			this.parentNode.parentNode.className = "simple"
		}
		document.getElementById("radioRecipes").onclick=function() {
			this.parentNode.parentNode.className = "advanced"
		}
		document.getElementById("radioFoodstuff").onclick=function() {
			this.parentNode.parentNode.className = "simple"
		}
				document.getElementById("radioAll").onclick=function() {
			this.parentNode.parentNode.className = "simple"
		}
	}
}
var locked = 0;
function clearSearchRating() {
	if(!document.getElementsByTagName) return;
	if(!document.getElementById("searchRating")) return;
	var theRatings = document.getElementById("searchRating").getElementsByTagName("img")
	theSearchRaterInput = document.getElementById("searchRaterInput");
	var i;
	for(i=0;i<theRatings.length;i++) {
		var theNum = i 
		re = /full/g;
		theRatings[i].src = theRatings[i].src.replace(re,"empty")
		theSearchRaterInput.value = ""
	}
	locked = 0;
}
function searchRating() {
	if(!document.getElementsByTagName) return;
	if(!document.getElementById("searchRating")) return;
	var theRatings = document.getElementById("searchRating").getElementsByTagName("img")
	theSearchRaterInput = document.getElementById("searchRaterInput");
	var i;
	for(i=0;i<theRatings.length;i++) {
		var theNum = i 
		theRatings[i].onmouseover = function() {
			if(locked == 0){
				re = /empty/g;
				this.src = this.src.replace(re,"full")
				var j
				for(j=0;j<this.alt;j++){
					this.parentNode.getElementsByTagName("img")[j].src = this.src.replace(re,"full")
				}
			}
		}
		theRatings[i].onclick = function() {
			searchRatingValue = this.getAttribute("alt");
			
			nuRatings = this.parentNode.getElementsByTagName("img")
			re = /full/g;
			var k
			for(k=0;k<nuRatings.length;k++){
				nuRatings[k].src = nuRatings[k].src.replace(re,"empty")
			}
			re = /empty/g;
			this.src = this.src.replace(re,"full")
			var j
			for(j=0;j<this.alt;j++){
				this.parentNode.getElementsByTagName("img")[j].src = this.src.replace(re,"full")
			}
			locked = 1;
			theSearchRaterInput.value = searchRatingValue
		}
		theRatings[i].onmouseout = function() {
			if(locked == 0){
				re = /full/g;
				this.src = this.src.replace(re,"empty")
			}
		}
	}
}
function rating() {
	if(!document.getElementsByTagName) return;
	if(!document.getElementById("theRating")) return;
	var theRatings = document.getElementById("theRating").getElementsByTagName("img")
	var ratingScale = theRatings.length
	var i;
	var origVoted = 0;
//	for(i=0;i<ratingScale;i++) {
//	}
	for(i=0;i<ratingScale;i++) {
		if(theRatings[i].src.indexOf("full")!=-1){
			origVoted++
		}
		var theNum = i 
		theRatings[i].onmouseover = function() {
			var re = /empty/g;
//			this.src = this.src.replace(re,"full")
			var newSrc = this.src.replace(re,"full");
			var j;
			for(j=0;j<this.alt;j++){
				this.parentNode.parentNode.getElementsByTagName("img")[j].src = newSrc
			}			
		}
		theRatings[i].onmouseout = function() {
			for(j=0;j<ratingScale;j++){
				var re = /full/g;
				var re2 = /empty/g;
				if(j < origVoted){
					this.parentNode.parentNode.getElementsByTagName("img")[j].src = this.parentNode.parentNode.getElementsByTagName("img")[j].src.replace(re2,"full")	
				} else {
					this.parentNode.parentNode.getElementsByTagName("img")[j].src = this.parentNode.parentNode.getElementsByTagName("img")[j].src.replace(re,"empty")
				}
			}
		}
	}
	
}
function searchRater() {
	if(!document.getElementsByTagName) return;
	var searchRatingValue = 0
	if(!document.getElementById("searchRater")) return;
	theSearchRaterImg = document.getElementById("searchRaterImg");
	theSearchRaterImg.src = "/images/v3/clear.gif";
	theSearchRaters = document.getElementById("searchRater").getElementsByTagName("area");
	theSearchRaterInput = document.getElementById("searchRaterInput");
	var i
	for(i=0;i<theSearchRaters.length;i++) {
		theSearchRaters[i].onmouseover = function() {
			theSearchRaterImg.className = this.className;
		}
		theSearchRaters[i].onmouseout = function() {
			theSearchRaterImg.className = "rated" + searchRatingValue;
		}
		theSearchRaters[i].onclick = function() {
			searchRatingValue = this.getAttribute("alt");
			theSearchRaterInput.value = searchRatingValue
			return false;
		}
	}
}
function rater() {
	if(!document.getElementsByTagName) return;
	if(!document.getElementById("rater")) return;
	theRaterImg = document.getElementById("raterImg");
	//if(theRaterImg.className != "rated0") return;
	theRaterImg.src = "../images/v3/clear.gif";
	theRaters = document.getElementById("rater").getElementsByTagName("area");
	var i
	for(i=0;i<theRaters.length;i++) {
		theRaters[i].onmouseover = function() {
			theRaterImg.className = this.className;
		}
	}
}
function infoPop() {
	if(!document.getElementsByTagName) return;
	if(!document.getElementById("infoBubble")) return;
	if(document.all){
		document.getElementById("infoBubble").getElementsByTagName("img")[0].style.cursor = "hand"
	} else {
		document.getElementById("infoBubble").getElementsByTagName("img")[0].style.cursor = "pointer"
	}
	document.getElementById("infoBubble").onclick = function () {
		this.style.display = "none";
	}
/*	if(!document.getElementsByTagName) return;
	if(document.getElementById("infoBubble")) return;
	theInfoLink = document.getElementById("info").getElementsByTagName("a")[0];
	theInfoLink.onmouseover = function() {
		document.getElementById("infoBubble").style.display = "block";
	}
	theInfoLink.onmouseout = function() {
		document.getElementById("infoBubble").style.display = "none";
	}*/
}
function removeAlert() {
	if(!deleteConfirmMessage) {
		var deleteConfirmMessage = "are you sure";
	}
	theLinks = document.getElementsByTagName("a");
	var i;
	for(i=0;i<theLinks.length;i++){
		if(theLinks[i].className == "remove") {
			theLinks[i].onclick = function() {
				removeConfirm(deleteConfirmMessage);
			}
		}
	}
}
function removeConfirm(deleteConfirmMessage){
	if (confirm(deleteConfirmMessage)) {
		return true;
	} else {
		return false;
	}
}



function alertClose() {
	if(!document.getElementsByTagName) return;
	if(!document.getElementById("alertBubble")) return;
	if(document.all){
		document.getElementById("alertBubble").getElementsByTagName("img")[0].style.cursor = "hand"
	} else {
		document.getElementById("alertBubble").getElementsByTagName("img")[0].style.cursor = "pointer"
	}
	document.getElementById("alertBubble").onclick = function () {
		this.style.display = "none";
	}
}

function collapseMyInfo() {
	if(!document.getElementsByTagName) return;
	if(!document.getElementById("myInfoHead")) return;
	theInfoHead = document.getElementById("myInfoHead")
	theInfoHead.style.cursor="pointer"
	theInfoHead.onclick = function() {
        if (document.getElementById("myInfo").style.display == "none") {
			document.getElementById("myInfo").style.display = "block"
			re = /\.right\./g;
			newSrc = this.getElementsByTagName("img")[0].src.replace(re, ".down.")
			this.getElementsByTagName("img")[0].src = newSrc
		} else {
			document.getElementById("myInfo").style.display = "none"
			re = /\.down\./g;
			newSrc = this.getElementsByTagName("img")[0].src.replace(re, ".right.")
			this.getElementsByTagName("img")[0].src = newSrc;
		}
	}
}

function forumCollapse() {
	if(!document.getElementsByTagName) return;
	theDivs = document.getElementsByTagName("div");
	var i;
	for(i = 0;i < theDivs.length;i++) {
		if(theDivs[i].className == "forumCategory") {
			theHeading = theDivs[i].getElementsByTagName("h1")[0];
			theHeading.style.cursor = "pointer";
			theHeading.onclick = function() {
				theChanger = this.parentNode;
				if(theChanger.className == "simple") {
					theChanger.className = "advanced";
					re = /\.right\./g;
					newSrc = this.getElementsByTagName("img")[0].src.replace(re, ".down.")
					this.getElementsByTagName("img")[0].src = newSrc
				} else {
					theChanger.className = "simple";
					re = /\.down\./g;
					newSrc = this.getElementsByTagName("img")[0].src.replace(re, ".right.")
					this.getElementsByTagName("img")[0].src = newSrc
				}
			}
		}
	}
}
function showHideCalendar() {
	if(!document.getElementsByTagName) return;
	theLinks = document.getElementsByTagName("a");
	var i
	for(i=0;i<theLinks.length;i++) {
		if(theLinks[i].className =="openCalendar"){
			theLinks[i].onclick = function() {
				if(document.getElementById("calendar")) {
					if(document.getElementById("calendar").style.display == "block") {
						document.getElementById("calendar").style.display = "none";
					} else {
						document.getElementById("calendar").style.display = "block";
					}
					return false;
				}
			}
		}
		if(theLinks[i].className =="closeCalendar"){
			theLinks[i].onclick = function() {
				if(document.getElementById("calendar")) {
					document.getElementById("calendar").style.display = "none";
					return false;
				}
			}
		}
	}
}
function showCalendar() {
	if(!document.getElementsByTagName) return;
	document.getElementById("calendar").style.display = "block";
}
function roundedImages() {
	var imgs = document.getElementsByTagName("img");
	for (var i = 0; i < imgs.length; i++) {
		if (imgs[i].className.indexOf("photo") != -1) {
			var wrapper = document.createElement('span');
			wrapper.className = "photoWrapper";
			if (imgs[i].className.indexOf("floatLeft") != -1) {
				wrapper.style.cssFloat = "left"
				wrapper.style.styleFloat = "left"
			}
			if (imgs[i].className.indexOf("floatRight") != -1) {
				wrapper.style.cssFloat = "right" 
				wrapper.style.styleFloat = "right" 
			}
			wrapper.style.width = imgs[i].width + "px";
			
			var original = imgs[i];
			if(original.parentNode.tagName.toUpperCase()=='A') original = original.parentNode;
			original.parentNode.replaceChild(wrapper, original);
			
			
			var tl = document.createElement("span");
			tl.className = "tl";
			var br = document.createElement("span");
			br.className = "br";
			var tr = document.createElement("span");
			tr.className = "tr";
			var bl = document.createElement("span");
			bl.className = "bl";
			wrapper.appendChild(tl);
			wrapper.appendChild(tr);
			wrapper.appendChild(bl);
			wrapper.appendChild(br);
			
			wrapper.appendChild(original);
		}
	}
}

function createElement(element) {
  if (typeof document.createElementNS != 'undefined') {
    return document.createElementNS('http://www.w3.org/1999/xhtml', element);
  }
  if (typeof document.createElement != 'undefined') {
    return document.createElement(element);
  }
  return false;
}

String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

function addEvent( obj, type, fn ) {
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

function removeEvent( obj, type, fn ) {
	if (obj.removeEventListener)
		obj.removeEventListener( type, fn, false );
	else if (obj.detachEvent)
	{
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
}

function windowOpen(url,width,height,type) {
	if (!url) {
		return;
	}
	if (!width) {
		width = '300';
	}
	if (!height) {
		height = '300';
	}
	if (!type) {
		type = '3';
	}
	if (type == '1') {
		params = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=' + width + ',height=' + height;
	} else if (type == '2') {
		params = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=' + width + ',height=' + height;
	} else if (type == '3') {
		params = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=' + width + ',height=' + height;
	} else if (type == '4') {
		params = 'toolbar=no,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,width=' + width + ',height=' + height;
	} else if (type == '5') {
		params = 'toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=' + width + ',height=' + height;
	} else {
		params = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=' + width + ',height=' + height;
	}
	msgWindow = window.open(url,'msgWindow',params);
	msgWindow.focus();
//	return false;
}

 
/* Simon Willison's addLoadEvent function allows you to stack up 'window.onload' events 
without them stepping on each other's toes. 
It's explained here - http://www.sitepoint.com/blog-post-view.php?id=171578 */

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
// DF1.1 :: domFunction 
// *****************************************************
// DOM scripting by brothercake -- http://www.brothercake.com/
// GNU Lesser General Public License -- http://www.gnu.org/licenses/lgpl.html
//******************************************************
function domFunction(f, a) {
	var n = 0;
	var t = setInterval(function() {
		var c = true;
		n++;
		if(typeof document.getElementsByTagName != 'undefined' && (document.getElementsByTagName('body')[0] != null || document.body != null)) {
			c = false;
			if(typeof a == 'object') {
				for(var i in a) {
					if((a[i] == 'id' && document.getElementById(i) == null) || (a[i] == 'tag' && document.getElementsByTagName(i).length < 1)) { 
						c = true; 
						break; 
					}
				}
			}
			if(!c) { f(); clearInterval(t); }
		}
		if(n >= 60) {
			clearInterval(t);
		}
	}, 250);
};



function popMeUp(strURL,strType,strWidth,strHeight) {
	var strOptions="";
	if (strType=="console") strOptions="scrollbars,resizable,height="+strHeight+",width="+strWidth;
	theWin = window.open(strURL, 'popupWin', strOptions);
	theWin.focus()
}
function windowLinks() {
    if(!document.getElementsByTagName) {
         return;
    }
	
    var anchors = document.getElementsByTagName("a");
    for (var i = 0; i < anchors.length; i++) {
         var anchor = anchors[i];
         var relIndex = anchor.rel;
		 if (relIndex){
			var relSplit = relIndex.split("|");
			if (relSplit[0] == "external") {
	            anchor.target = "_blank";
				if (anchor.className == "") {
					anchor.className = "external";
				} else {
					anchor.className = anchor.className + " external"
				}
//				anchor.title = "Load in new window: "+ anchor.href;  
	
	   			} else if (relSplit[0] == "popup") {
				if (anchor.className == "") {
					anchor.className = "popup";
				} else {
					anchor.className = anchor.className + " popup"
				}
	
//				anchor.title = "Loads in a Popup Window";
				if(relSplit[1]){
					anchor.popupWidth = relSplit[1];
					anchor.popupHeight = relSplit[2];
		        } else {
					anchor.popupWidth = 435;
					anchor.popupHeight = 350;
	        	}
				anchor.onclick = function() {popMeUp(this.href,'console',this.popupWidth,this.popupHeight);return false;};
			}
		}
	} 
}

function popIt(elm, width, height) {
	if(!width){
		width = 435
		height = 350
	}
	popMeUp(elm.href,'console',width,height);
	return false;
}

function linkReWrite(className, extraParams) {
    var anchors = document.getElementsByTagName("a");
    if (anchors) {
        if (anchors.length) {
            var completeUrl;
            for (var i = 0; i < anchors.length; i++) {
                if (anchors[i].className) {
                    if (anchors[i].className.indexOf(className) != -1) {
                        completeUrl = anchors[i].href;
                        if (hasQuestionMark(completeUrl)) {
                            if (hasParams(completeUrl)) {
                                completeUrl += "&" + extraParams;
                            } else {
                                completeUrl += extraParams;
                            }
                        } else {
                            completeUrl += "?" + extraParams;
                        }
                        anchors[i].href = completeUrl;
                    }
                }
            }
        }
    }
}

function hasQuestionMark(url) {
    var result = false;
    if (url) {
        result = url.indexOf("?") != -1;
    }
    return result;
}

function hasParams(url) {
    var result = false;
    if (url) {
        result = (url.length - 1) > url.indexOf("?");
    }
    return result;
}