if (typeof salmon != "object") { var salmon = {}; }

/*************************************************************************
* Extensions
*************************************************************************/

Array.prototype.exists = function (x) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == x) return true;
    }
    return false;
}

Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

getElementsByClassName = function(node, classname){
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}

/*************************************************************************
* CMS
*************************************************************************/

salmon.cms = {
	init:function(){
		// if you need to kick things off, stick it here.
	},
	newId:0,
	placeholderIds:[],
	getNewId:function(){// increments and returns an ID
		do {
			++this.newId;
	    } while (this.placeholderIds.exists(this.newId));
	    this.placeholderIds.push(this.newId);
	    return this.newId;
	}
};

/*************************************************************************
* Common
*************************************************************************/

salmon.common = {
	init:function(){},
	showConsoleLog : true, // default set to false, need to set to true in when needed
	logIt:function(logWhat){
		if(this.showConsoleLog){
			if(jQuery.browser.mozilla){
				console.log(logWhat);
			} else {
				if(document.getElementById("logIt") == null){
					$("body").append("<div id=\"logIt\" title=\"console.log\"><div>"+logWhat+"<br/></div></div>");
					var doDialog = false;
					if(typeof $.ui == "object"){
						if(typeof $.ui.dialog == "function"){
							doDialog = true;
						}
					}
					if(doDialog) { 
						$("#logIt").dialog();
						$(".ui-dialog").css({
							top:"250px",
							left:"700px",
							height:"340px"
						});
						$("#logIt h1").css({
							backgroundColor:"#666",
							color:"#eee",
							padding:"2px 3px",
							fontSize:"14px"
						});
					} else {
						$("#logIt").css({
							width:"240px",
							height:"600px",
							position:"absolute",
							border:"inset 3px #666",
							backgroundColor:"#eee",
							right:"25px",
							top:"25px",
							textAlign:"left",
							overflow:"auto",
							zIndex:"32000"
							
						});
					}
					$("#logIt").css({
						height:"311px",
						margin:"0",
						overflowY:"auto"
					});
					$("#logIt div").css({
						padding:"0 10px"
					});
				} else {
					$("#logIt div").append(logWhat+"<br/>");
					var objDiv = document.getElementById("logIt");
					objDiv.scrollTop = objDiv.scrollHeight;				
				}
			}
		}
	}, 
	// shows a message in the corner of the screen that will display for
	// for 1 second if no display time is set
	// needs an empty span in the page with an id of "ajaxStatus"
	doMsg:function(msg, displayTime, displayBgColor, displayFgColor){
		// checks for a time parsed through and if none sets it to one second.
		// if there is no span for the message, create one.
		displayBgColor = (displayBgColor) ? displayBgColor : "ffff00";
		displayFgColor = (displayFgColor) ? displayFgColor : "000000";
		if((document.getElementById("ajaxStatus") == null)){
			// logIt("no span, making one.");
			$("body").append("<span id=\"ajaxStatus\"></span>");
		}
		$("#ajaxStatus").css({
			backgroundColor:"#"+displayBgColor,
			color:"#"+displayFgColor
		});
		if(displayTime !=0){
			displayTime = (displayTime>0) ? displayTime : 1000;
		}
		$("#ajaxStatus").empty().fadeIn(100, function(){
			$("#ajaxStatus").append(msg);
			if(displayTime != 0){
				$("#ajaxStatus").fadeOut(displayTime);
			}
		});
	},
	blocker:function(doWhat){// TODO: AO - this is a bit rubbish and i will rewrite it.
		if(doWhat == "show"){
			$("body").append("<div id=\"blocker\"></div>");
			$("#blocker").css({
				 position:"absolute",
				 top:0,
				 left:0,
				 backgroundColor:"#000",
				 width:"100%",
				 height:"100%",
				 opacity:0.66,
				 zIndex:32000
			});
		} else if (doWhat == "hide") {
			$("#blocker").remove();
		} else if (doWhat == "adjust") {
		}
	},
	getHTMLFirstChild: function(html) {
        var root = document.createElement("div");
        root.innerHTML = html;
        return root.firstChild;
	}
}

/*************************************************************************
* Forms
*************************************************************************/

salmon.forms = {
	prePopulate: function(){
		$("input,textarea").each(function(){
			if($(this).attr("title") !== undefined){
				$(this).attr({
					value:$(this).attr("title")
				});
				$(this).focus(function(){
					if($(this).attr("title") == $(this).val()){
						$(this).val("");
					}
				});
				$(this).blur(function(){
					if($(this).val() == ""){
						$(this).val($(this).attr("title"));
					}
				});
			}
		});
	}
};

/*************************************************************************
* More or less
*************************************************************************/

salmon.moreOrLess = {
	fullClassList:"",
	splitClasses:{},
	moreText:"more...",
	lessText:"less...",
	init: function(){
		$(".moreOrLess").each(function(){
			this.fullClassList = $(this).attr("class");
			this.splitClasses = this.fullClassList.split(" ");
			for(var i = 0; i<this.splitClasses.length; i++){	
				if(this.splitClasses[i].indexOf("limit") >= 0){
					var itemLimit =  this.splitClasses[i].split("limit")[1];
				}
			}
			salmon.moreOrLess.hideItems(this,itemLimit);
		});
	},
	hideItems:function(what,itemLimit){
		if($(what).find("li").length <= itemLimit) {
			return;
		}	
		var counter = 0;
		$(what).find("li").each(function(){
			if(counter>= itemLimit){
				$(this).hide();
			}
			counter++;
		});
		$(what).find(".hideThem").remove();
		$(what).append("<li class=\"showThem\"><a href=\"#\">"+this.moreText+"</a></li>");
		$(what).find(".showThem").click(function (){
			salmon.moreOrLess.showItems($(this).parent(),itemLimit); 
			return false;
		});
	},
	showItems:function(what,itemLimit){
		if($(what).find("li").length <= itemLimit) {
			return;
		}	
		$(what).find("li").each(function(){
			$(this).show();
		});
		$(what).find(".showThem").remove();
		$(what).append("<li class=\"hideThem\"><a href=\"#\">"+this.lessText+"</a></li>");
		$(what).find(".hideThem").click(function (){
			salmon.moreOrLess.hideItems($(this).parent(),itemLimit);
			return false;
		});
	}
};

/*************************************************************************
* Name space
*************************************************************************/

salmon.namespace = {
	addNamespace: function(namespace) {
		var parts = namespace.split(".");
		var root = window;
		for(var i=0;i<parts.length;i++) {
			if(typeof root[parts[i]] != "object") {
				root[parts[i]] = {};
			}
			root = root[parts[i]];
		}
	}
}

/*************************************************************************
* QueryString
*************************************************************************/

salmon.QueryString = new ( function() {
	var queryString = null;
	this.get = function(key) {
		if (key == null || key === "") return null;
		if (queryString == null) {
			queryString = {};
			var qs = location.search.slice(1).split("&"), qsElements;
			for (var i = 0, j = qs.length; i<j; i++) {
				qsElements = qs[i].split("=");
				if (queryString[qsElements[0]]) {
					if (queryString[qsElements[0]] instanceof Array) {
						queryString[qsElements[0]].push(qsElements[1] || "")
					} else {
						queryString[qsElements[0]] = [queryString[qsElements[0]], qsElements[1]];
					}
				} else {
					queryString[qsElements[0]] = qsElements[1] || "";
				}
			}
		}
		return queryString[key];
	};
} )();

/*************************************************************************
* salmon popup window
*************************************************************************/


salmon.popupwindow = {
	init:function(){
		var nodes = getElementsByClassName(document.getElementById("container"),"popup")
		if(nodes.length>0){
			$(".popup").each(function(){
				$(this).click(function(){
					var myHref = this.href;
					var myHeight = 500;
					var myWidth = 500;
					var myTop = 20;
					var myLeft = 20;
					var myToolbar = "no";
					var myLocation = "no";
					var myScrollbars = "no";
					var myVars = myHref.substring(myHref.indexOf("?")+1,myHref.length).split("&");
					for(var i = 0; i < myVars.length; i++) {
				        var pos = myVars[i].indexOf('=');
				        if (pos == -1) continue;
				        if(myVars[i].substring(0,pos) == "width"){
					        myWidth = myVars[i].substring(pos+1);
				        } else if(myVars[i].substring(0,pos) == "height"){
					        myHeight = myVars[i].substring(pos+1);
				        } else if(myVars[i].substring(0,pos) == "top"){
					        myTop = myVars[i].substring(pos+1);
				        } else if(myVars[i].substring(0,pos) == "left"){
					        myLeft = myVars[i].substring(pos+1);
				        } else if(myVars[i].substring(0,pos) == "toolbar"){
					        myToolbar = myVars[i].substring(pos+1);
				        } else if(myVars[i].substring(0,pos) == "location"){
					        myLocation = myVars[i].substring(pos+1);
				        } else if(myVars[i].substring(0,pos) == "scrollbars"){
					        myScrollbars = myVars[i].substring(pos+1);
				        }
				    }
					tools = "resizable,toolbar="+myToolbar+",location="+myLocation+",scrollbars="+myScrollbars+",width="+myWidth+",height="+myHeight+",left="+myLeft+",top="+myTop;
					newWindow = window.open(myHref, 'newPopup', tools);
					newWindow.focus();
					return false;
				});
			});
		}
	}
}



