


// core.js
function core() {
	this.force_trace = true;
	this.searchInterval = false;
	this.Fields = []; //field name reference
	this.ActiveFields = []; //maintain data state for inline edits
}//

core.prototype = {

	$:function() { // returns object reference || array of object references
		var loElements = new Array();
		for (var i = 0; i < arguments.length; i++) {
			var loElement = arguments[i];
			if (typeof(loElement) == 'string') {
				loElement = (document.getElementById(loElement)||false);
			}
			if (arguments.length==1) {
				return loElement;
			}
			loElements.push(loElement);
		}
		return loElements;
	}

}
var core = new core();






function RID() { //Scrappy Random ID Generator...
	var result, i, j;
	result = '';
	for(j=0; j<32; j++) {
	if( j == 8 || j == 12|| j == 16|| j == 20)
		result = result + '-';
		i = Math.floor(Math.random()*16).toString(16).toUpperCase();
		result = result + i;
	}
	return result;
}

function $$() { //returns object reference || array of object references.... $$ bill y'all
	//still working on this one...
	var loElements = document.getElementsByTagName(arguments[0]);
	var loElement = loElements[0]; //mostly just want to get the first element in the list
	loElements = null; //discard for now
	return loElement;
}

function insertAfter(loInsertThisElement, loAfterThisElement) { //why is there no DOM.insertAfter?
	if(loAfterThisElement.nextSibling) {
		return (loAfterThisElement.parentNode.insertBefore(loInsertThisElement,loAfterThisElement.nextSibling));
	} else {
		return (loAfterThisElement.parentNode.appendChild(loInsertThisElement));
	}
}
//*****************************************

		function getDimensions() { //set up "cross browser" document dimensions
			this.refresh();			
			this.posX=0;
			this.posY=0;
			this.lastX=this.posX;
			this.lastY=this.posY;
		} getDimensions.prototype = {
			refresh:function() {
				this.IE = (document.all && !window.opera);
				if(document.body) {
					if(typeof(document.body)=='object') {
						this.CommonBody = document.body;
					}	
				} else if(document.documentElement) {
					if(typeof(document.documentElement)=='object') {
						this.CommonBody = (document.compatMode=='CSS1Compat')?(document.documentElement):(document.body);
					}	
				}
				this.scrollbarwidth=window.innerWidth-(this.CommonBody.offsetLeft+this.CommonBody.offsetWidth); //http://www.howtocreate.co.uk/emails/BrynDyment.html
				this.scrollbarwidth=(Number(this.scrollbarwidth))?(this.scrollbarwidth):(0);
				if(this.IE) {
					this.iScrollTop = this.CommonBody.scrollTop;
					this.iScrollLeft = this.CommonBody.scrollLeft;
					this.iDocWidth = this.CommonBody.clientWidth;
					this.iDocHeight = this.CommonBody.clientHeight;
				} else {
					this.iScrollTop = window.pageYOffset;
					this.iScrollLeft = window.pageXOffset;
					this.iDocWidth = window.innerWidth-this.scrollbarwidth;
					this.iDocHeight = window.innerHeight;
				}
				this.iDocHeightComplete = (this.CommonBody.offsetHeight>this.CommonBody.scrollHeight) ? (this.CommonBody.offsetHeight) : (this.CommonBody.scrollHeight);
			},
			coordinates:function(e,llTrackLast) {
				if (e.pageX || e.pageY) {
					this.posX = e.pageX;
					this.posY = e.pageY;
				} else if (e.clientX || e.clientY) {
					this.posX = e.clientX + document.body.scrollLeft;
					this.posY = e.clientY + document.body.scrollTop;
					if(document.documentElement) {
						this.posX += document.documentElement.scrollLeft;
						this.posY += document.documentElement.scrollTop;
					}
				} else {
					this.posX=0;
					this.posY=0;
				}
				if(llTrackLast) {
					this.lastX=this.posX;
					this.lastY=this.posY;
				}
			}
		}
		//*****************************************

//******************************************************
function PassKey(liKey, loElement, lsPrimary, lsRelation, liPrimaryID, liRelationID, liStatusID) {
	if(!liKey||liKey==0) {
		BigLeagueChew(liKey, loElement, lsPrimary, lsRelation, liPrimaryID, liRelationID, liStatusID, 'insert');
	} else {
		BigLeagueChew(liKey, loElement, lsPrimary, lsRelation, liPrimaryID, liRelationID, liStatusID, 'update');
	}
}
function BigLeagueChew(liKey, loElement, lsPrimary, lsRelation, liPrimaryID, liRelationID, liStatusID, lsOperation) {  //When ur in the BigLeague Chew http://www.youtube.com/watch?v=0GyEkvqtHPs
	if (!lsPrimary){return;}
	if (lsOperation=='update') {
		this.YURI='/my-cart/pattern_update_relation/'; //
	} else {
		this.YURI='/my-cart/pattern_insert_relation/'; //
	}
	lsRequest='?_key='+liKey;
	lsRequest+='&_primary='+lsPrimary;
	lsRequest+='&_relation='+lsRelation;
	lsRequest+='&_primary_id='+liPrimaryID;
	lsRequest+='&_relation_id='+liRelationID;
	lsRequest+='&_status_id='+liStatusID;
	this.YURI+=lsRequest;
	ajaxEvent(this.YURI, loElement, false, true);
}

function setTitleFields(lsQualifier,lsValue) {
	if(lsQualifier=='locations::schools') {
		document.getElementById('location').value=lsValue;
		document.getElementById('company').value=lsValue;
		document.getElementById('meta_keywords').value=lsValue;
		setRecordURL(lsValue,document.getElementById('slug'));
	}
	if(lsQualifier=='pages') {
		document.getElementById('meta_keywords').value=lsValue;
		setRecordURL(lsValue,document.getElementById('slug'));
	}
}
//*****************************************

function setRecordURL(lsValue,loElement) {
	if (!loElement){return;}
	if (typeof(lsValue) == 'string') {
		lsValue = lsValue.replace(/\//g,"");
		lsValue = lsValue.replace(/\s/g,"-");
		lsValue = lsValue.replace(/\'/g,"''");
		lsValue = lsValue.replace(/\"/g,"");
		lsValue = encodeRE(lsValue);
	}
	loElement.value=lsValue.toLowerCase();
}
//*****************************************

function encodeRE(lsValue) {
	lsValue=lsValue.replace(/\s/g,"");
	lsValue=lsValue.replace(/\'/g,"");
	lsValue=lsValue.replace(/\"/g,"");
	lsValue = lsValue.replace(/[^a-zA-Z0-9\.\-\_\\]/g,'');
	return lsValue; //.replace(/([~`@#%*^&*=!+?^${}()|[\]\/\\])/g,'');
}
//*****************************************

function escapeSearch(lsValue) {
	if(!lsValue){return;}
	lsValue=lsValue.replace(/\'/g,"''"); //replace these with pg wildcard - doesn't work if not using like/ilike
	lsValue=lsValue.replace(/\"/g,'\"');
	return lsValue;
}

function NewRelation(lsURI) {
	var loContainer = createOverlayDiv(750,500);
	loContainer.appendChild(theLove(lsURI));
}

function UpdateRelationalField(loElement,lsPrimary,lsRelation,liPrimaryID,liRelationID,llCallBack,lsContextArguments) {
	if (!lsPrimary){return;} 
	this.UpdateURI='/my-cart/pattern_insert_saved/';
	lsRequest='?_primary='+lsPrimary;
	lsRequest+='&_relation='+lsRelation;
	lsRequest+='&_primary_id='+liPrimaryID;
	lsRequest+='&_relation_id='+liRelationID;
	this.UpdateURI+=lsRequest;
	ajaxEvent(this.UpdateURI,(llCallBack)?(loElement):(false),false,false);
}

function PrepValue(luValue) {
	if(Number(luValue)) {
		return luValue;
	}
	if (typeof(luValue) == 'string') {
		luValue=luValue.replace(/\'/g,"''");
		luValue=escape(luValue);
		luValue = luValue.replace(/\//g,"%2F");
		luValue = luValue.replace(/\?/g,"%3F");
		luValue = luValue.replace(/=/g,"%3D");
		luValue = luValue.replace(/&/g,"%26");
		luValue = luValue.replace(/@/g,"%40");
		luValue='\'' + luValue + '\'';
		return luValue;
	}
	return luValue;
}


function UpdateSingleField(loElement,lsContext,lsID,lsField,luValue,llCallBack,sFunction) { //Single Field Edit
	if (!lsContext){return;}
	if (!lsID){return;}
	if (!lsField){return;}
	this.UpdateURI='/my-cart/_update_single_field/';
	lsRequest='?_context='+lsContext;
	lsRequest+='&_id='+lsID;
	lsRequest+='&_field='+lsField;
	lsRequest+='&_value='+PrepValue(luValue);
	this.UpdateURI+=lsRequest;
	var lsFunction='';
	var lsArguments='';
	if(sFunction!='') {
		lsFunction = sFunction;
	}
	ajaxEvent(this.UpdateURI, loElement, false, true, lsFunction, lsArguments); //callback, replace contents, synchronous.
	return true;
}

//UI EFFECTS
function changeOpacity(loElement, iOpacity) {
	if(!loElement) {return(false);}
	if(!iOpacity) {return(false);}
	var lsTagName=loElement.tagName.toLowerCase();
	var loElementStyle = loElement.style;
	if(!loElementStyle) {return;}
	loElementStyle.opacity = (iOpacity / 100);
	loElementStyle.MozOpacity = (iOpacity / 100);
	loElementStyle.KhtmlOpacity = (iOpacity / 100);
	loElementStyle.filter = "alpha(opacity=" + iOpacity + ");"; //WOW talk about obscure, if you dont add the semicolon in the filter attrib IE bombs.
	if (iOpacity==0) {
		loElementStyle.display = 'none';
	} else {
		if (lsTagName=='div') {
			loElementStyle.display = 'block';
		} else {
			loElementStyle.display = 'inline';
		}
	}
}

// Style based fading. -MM
function fadeControl(lsItem, iOpacStart, iOpacEnd, iMilliseconds) {
	var speed = Math.round(iMilliseconds / 100); //speed for each frame
	var timer = 0;
	if(iOpacStart > iOpacEnd) { //determine the direction for the blending, if start and end are the same use pulse effect
		for(i = iOpacStart; i >= iOpacEnd; i--) {
			setTimeout("changeOpacity(document.getElementById('" + lsItem + "'),'" + i + "')",(timer*speed));
			timer++;
		}
	} else if(iOpacStart < iOpacEnd) {
		for(i = iOpacStart; i <= iOpacEnd; i++) {
			setTimeout("changeOpacity(document.getElementById('" + lsItem + "'),'" + i + "')",(timer*speed));
			timer++;
		}
	} else if(iOpacStart==iOpacEnd) { //pulse, not finished -- just pulses without considering value from/to which it's pulsing (just from full opacity to zero and back)
		for(i=0; i<3.1; i+=0.1) {
			setTimeout("changeOpacity(document.getElementById('" + lsItem + "'),'" + Math.round(Math.abs(Math.cos(i)*100)) + "')",(timer*speed));
			timer++;
		}
		changeOpacity(document.getElementById(lsItem),iOpacEnd); //hard set back to iOpacEnd, temp...
	}
}


function createOverlayDiv(liWidth,liHeight,lsTitleElement) { // Create Element, Apply Classes, Attach to Body

	loOverlayPortFactory = new cliche('ajaxcontainer', liWidth, liHeight, 'cs_ajax_container');
	loOverlayPort=loOverlayPortFactory.element;

	loBlackScreenFactory = new cliche('blackscreen', windowSize('width'), windowSize('height'), 'blackroot');
	loBlackScreen=loBlackScreenFactory.element;

	loBodyNode = $document.getElementById('body');
	loBodyNode.appendChild(loBlackScreen);
	loBodyNode.appendChild(loOverlayPort);

	fadeControl('blackscreen', 0, 20, 440);
	fadeControl('ajaxcontainer', 0, 94, 770);

	iFullWidth = windowSize("width");
	iFullHeight = windowSize("height");
	iScreenCenter = windowSize("vpcenter");
	iLeftAdjust = Math.round((iFullWidth - loOverlayPortFactory.width) / 2);
	iTopAdjust = Math.round(iScreenCenter - (loOverlayPortFactory.height/2));
	window.onresize = function() {
		iFullWidth = windowSize("width");
		iFullHeight = windowSize("height");
		iScreenCenter = windowSize("vpcenter");
		iLeftAdjust = Math.round((iFullWidth - loOverlayPortFactory.width) / 2);
		iTopAdjust = Math.round(iScreenCenter - (loOverlayPortFactory.height/2));
		loBlackScreen.style.width=iFullWidth+"px";
		loBlackScreen.style.height=iFullHeight+"px";
		loOverlayPort.style.top=iTopAdjust+"px";
		loOverlayPort.style.left=iLeftAdjust+"px";
	}
	loBlackScreen.style.width=iFullWidth+"px";
	loBlackScreen.style.height=iFullHeight+"px";
	loOverlayPort.style.top=iTopAdjust+"px";
	loOverlayPort.style.left=iLeftAdjust+"px";

	return loOverlayPort;

}

function removeOverlayDiv(loElement) {
	if(loElement){
		if(document.getElementById(loElement)){
			loElement.parentNode.removeChild(loElement);
			loElement = null;
		} else {return false;}
	} else {
		var loElement = null;
		loElement =	document.getElementById('blackscreen');
		loElement = loElement.parentNode.removeChild(loElement);
		loElement = document.getElementById('ajaxcontainer');
		loElement = loElement.parentNode.removeChild(loElement);
		loElement = null;
	}
}

// Returns cross-browser window dimensions, offsets liScroll position
function windowSize(lsDimension) {
	var liClientWidth = 0;
	var liClientHeight = 0;
	if(typeof( window.innerWidth ) == 'number' ) {
		liClientWidth = window.innerWidth;
		liClientHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		liClientWidth = document.documentElement.clientWidth;
		liClientHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		liClientWidth = document.body.clientWidth;
		liClientHeight = document.body.clientHeight;
	}
	var liScrollOffX = 0;
	var liScrollOffY = 0;
	var liScrollHeight = 0;
	var liScrollWidth = 0;
	if (typeof(window.pageYOffset) == 'number') {
		liScrollOffY = window.pageYOffset;
		liScrollOffX = window.pageXOffset;
		liScrollHeight = window.scrollMaxY;
		liScrollWidth = window.scrollMaxX;
	} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
		liScrollOffY = document.documentElement.scrollTop;
		liScrollOffX = document.documentElement.scrollLeft;
		liScrollHeight = document.documentElement.scrollHeight;
		liScrollWidth = document.documentElement.scrollWidth;
	} else if (document.body && (document.body.scrollHeight || document.body.scrollTop)) {
		liScrollOffY = document.body.scrollTop;
		liScrollOffX = document.body.scrollLeft;
		liScrollHeight = document.body.scrollHeight;
		liScrollWidth = document.body.scrollWidth;
	}
	liContentHeight=(document.height)?(document.height+liClientHeight):(document.documentElement.offsetHeight); //FF||IE
	liContentWidth=liClientWidth;
	liContentCenter=liContentHeight/2; //actual content center (ish ?), every browser has different values.
	//center of VIEWABLE area
	liViewPortCenter=liContentCenter;
	liViewPortCenter=(document.height)?(liViewPortCenter-liClientHeight/2):(liViewPortCenter); //subtract window height from content
	liViewPortCenter+=liScrollOffY; //oh look at that, magically works in Firefox. Thank You God for Firefox....
	// ! reset content height for IE; // if we add this above it gets thrown way off in ie
	liContentHeight=(document.height)?(liContentHeight):(document.documentElement.offsetHeight+(liClientHeight));
	if (lsDimension == 'width'|| lsDimension == 'x') {
		return liClientWidth;
	} else if (lsDimension == 'height'|| lsDimension == 'y') {
		return liContentHeight;
	} else if (lsDimension == 'center') {
		return liContentCenter;
	} else if (lsDimension == 'vpcenter') {
		return liViewPortCenter;
	} else {
		return true;
	}
}





    function mmmcookies() {
    } mmmcookies.prototype = {
        set:function ( id, value, expires ) {
            if ( !expires ) {
                expires = 7;
            }
    		var date = new Date();
    		date.setTime( date.getTime( ) + ( expires * 24 * 60 * 60 * 1000 ) );
    		expires = "expires=" + date.toGMTString();
        	document.cookie = id + "=" + value + ";" + expires + "; path=/;";
        },
        get:function ( id ) {
            var result = false;
        	var items = document.cookie.split(';');
            var limit = items.length;
            var item, id_value, item_id;
            for(var i=0; i<limit; i++) {
        		item = items[i];
                id_value = item.split('=');
                item_id = id_value[0].replace( /\s/g, "" );
                if(item_id==id) {
                    result = id_value[1];
                }
        	}
        	return result;
        },
        eat:function ( id ) {
        	this.set( id, "", -1 );
        }
	} // * * *

var tookies = new mmmcookies();

