/*
	---------------------
	** REQUIRES jQuery **
	---------------------
	
	includes scripts to:
		+ add functionality to allow text boxes a default value that will be cleared on focus (ie. 'Search...')
		+ equalize the heights of elements matching the given selector rules

*/


var iomer = {
	searchDefault :		'Search...',	// default value for search boxes if initialized
	
	equalizePanels : function (selector) {
		if (selector==='') return;
		
		var maxHeight = 0;
		$(selector).each (function () {
			var panelHeight = $(this).height();
			if (!$.boxModel) { panelHeight+=Number ($(this).css('padding-top').match (/^\d+/)) + Number ($(this).css('padding-bottom').match (/^\d+/)); }
			maxHeight = panelHeight > maxHeight ? panelHeight : maxHeight;
		});	
		$(selector).height (maxHeight);
	},
	
	initSearchText : function (selector, defValue) {
		// example selectors to pass in: .searchText, .sf_searchText
		if (selector === '') return;
		defValue = defValue || iomer.searchDefault;
		
		$(selector).focus (function () {
			$(this).attr({value: ''});
		})
		.blur (function () {
			if ($(this).attr('value') == '') {
				$(this).attr({value: defValue});
			}
		})
		.attr({value: defValue});
	},
	
	constrain : function (selector, _params) {
		/*
			selector	: represents the CSS selector of object(s) you wish to constrain
			params		: an optional JSON parameter list containing any of the following...
						-	maxWidth	: pixel width to constrain to (unless a word that exceeds this cannot be broken)
						-	minWidth: pixel the mininum constricted width allowed for the element
						-	token	: character that represents break-points (' ', '&', '#', etc...)
									  default is ' ' (space)

			NOTE: little to no error checking. so be careful.
		*/
		if (selector === null) return;
		// Defaults
		var params = {
			maxWidth: 	100,
			minWidth:	0,
			token:		' '
		};

		// transfer any passed parameters
		for (var prop in _params) {	params[prop] = _params[prop]; }
		
		// traverse the elements and perform adjustments
		$(selector).each (function () {
			var elem = $(this);
			// adjustment to implement correctly for non-compliant box models
			var boxModelAdjust = 0;
			if (!$.boxModel) {
				boxModelAdjust = Number (elem.css('padding-left').match (/^\d+/)) + Number (elem.css('padding-right').match (/^\d+/));
			}
			var w = elem.width () + boxModelAdjust;
			//alert (params.maxWidth);
			// too big? make it fit!
			if (w > params.maxWidth + boxModelAdjust) {
				var str = elem.text ();
				var arr = str.split (params.token);
				
				var outputHTML = '';
				var n=0;
				var stableStr = testStr = arr[n];
				var wordCount = 1;
				//alert (elem.width());
				while (n<arr.length && arr.length > 1) {
					elem.text (testStr);
					if (elem.width()+boxModelAdjust < params.maxWidth+boxModelAdjust) {
						stableStr = testStr;
						testStr = testStr + params.token + arr[++n];
						wordCount++;
					} else {
						outputHTML += stableStr + '<br/>';		
						if (wordCount == 1) { n++; } else { wordCount = 1; }
						stableStr = arr[n];
						testStr = arr[n];
					}
				}
				if (stableStr !== undefined) outputHTML += stableStr;
				elem.html (outputHTML);
			}
			// make sure we have the latest (updated) value
			w = elem.width () + boxModelAdjust;

			// too small? let's make it bigger
			if (w < params.minWidth+boxModelAdjust) elem.width (params.minWidth+boxModelAdjust);
		});
	},
	
	setCookie : function (c_name,value,expiredays) {
	    var exdate=new Date();
	    exdate.setDate(exdate.getDate()+expiredays);
	    document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
    },
    
    getCookie : function (c_name) {
	    if (document.cookie.length>0) {
		    c_start=document.cookie.indexOf(c_name + "=");
		    if (c_start!=-1) { 
			    c_start=c_start + c_name.length+1; 
			    c_end=document.cookie.indexOf(";",c_start);
			    if (c_end==-1) c_end=document.cookie.length;
			    return unescape(document.cookie.substring(c_start,c_end));
		    }
	    }
	    return "";
    }

};

