var om = {
/*

  ___ ___  _ __ ___ 
 / __/ _ \| '__/ _ \
| (_| (_) | | |  __/
 \___\___/|_|  \___|

*/
	// master container
	mC : Object,
	mCid: String,
	// pointer x,y
	pX : Number,
	pY : Number,
	// viewport width,height
	vW : Number,
	vH : Number,
	
	config : new Hash(),
	
	init: function(mCid, debug) { // pass in the id of the group, entry master container elements
	
		if (debug==true) { om.initDebug(); } else { om.debugOn = false; }

		// get the master master group container span that all group containers go in
		om.mCid = mCid;
		om.mC = $("#"+mCid);
	
		// viewport scrolling/resizing
		window.onscroll = function() { om.onViewportChange(); };
		window.onresize = function() { om.onViewportChange(); };
	
		// mouse
		om.setupMouse();
		
		// ingest the hash at the end of the initial url
		om.url.ingestHash();

		// fire it up
		omT.init();
		
		// fire the viewport setup, hot on init's tail
		om.onViewportChange();

		},

	modeCur : String,
	mode : function( mode ) {
		if (!om.modeCur) {
			om.modeCur = "";
			}
		if( !mode ) {
		} else {
			if ( om.modeCur != mode ) {
				switch( mode ) {
				case "group":
					om.gC.hide();
					om.eC.show();
					break;
				case "entry":
					om.gC.hide();
					om.eC.show();
					break;
				default:
					om.gC.hide();
					om.eC.hide();
					break;
					}
				} else {
					}
				om.modeCur = mode;
			}
			return om.modeCur;
		},

	setupMouse : function() {
		$('html').mousemove(function(event) {
			om.pX = event.pageX;
			om.pY = event.pageY;
			omT.scrollerDo();
		});
		},
		
	onViewportChange : function() {
		om.vH = $(window).height();
		om.vW = $(window).width();
		
		// check browser H and set margins
		if ( om.vH >= 600 ) {
			om.mH = 90;
		} else {
			om.mH = Math.max( 20, 90 - Math.floor( ( 600 - om.vH ) / 3 ) );
			}

		// check browser W and set margins
		if ( om.vW >= 800 ) {
			om.mW = 90;
		} else {
			om.mW = Math.max( 20, 90 - Math.floor( ( 800 - om.vW ) / 2 ) );
			}

		omT.onViewportChange();

		},

/*

 _               _     
| |             | |    
| |__   __ _ ___| |__  
| '_ \ / _` / __| '_ \ 
| | | | (_| \__ \ | | |
|_| |_|\__,_|___/_| |_|

*/

	indexGroup : new Array(),
	indexEntry : new Array(),
	hashGroup : new Hash(),
	hashEntry : new Hash(),

	// FUNCTION TO INTERPRET XML RESPONSE INTO hashMash INDEXED indexMash
	hashMashXML : function( mashBase, xmlNodes, hashClear ) {
		if (hashClear) {
			if (mashBase=="group") {
				om.hashGroup = new Hash();
				om.indexGroup = new Array();
			} else {
				om.hashEntry = new Hash();
				om.indexEntry = new Array();
				}
			}
		var g_Id, data;
		
		for (var i = 0; i < xmlNodes.length; i++ ) {

			// grab the id of incoming data
			g_Id = xmlNodes[i].getAttribute('id');
			if (hashClear) {
				if (mashBase=="group") {
					om.indexGroup.push(g_Id);
				} else {
					om.indexEntry.push(g_Id);
					}
				}			

			// add xml key/value pairs to hashMash
			// example: id "5" + node "meta_title" --> stored at key "5.metatitle"
			for ( var j = 0; j < xmlNodes[i].childNodes.length; j++ ) {
				if ( xmlNodes[i].childNodes[j].nodeType == 1) {
					if (xmlNodes[i].childNodes[j].childNodes[0] != null) {
						data = xmlNodes[i].childNodes[j].childNodes[0].data;
					} else {
						data = " ";
						}
					if (mashBase=="group") {
						om.hashGroup.set( g_Id + "." + xmlNodes[i].childNodes[j].nodeName, data );
					} else {
						om.hashEntry.set( g_Id + "." + xmlNodes[i].childNodes[j].nodeName, data );
						}
					}
				}

			}
			
			return xmlNodes.length;
			
		},

/*
     _      _                 
    | |    | |                
  __| | ___| |__  _   _  __ _ 
 / _` |/ _ \ '_ \| | | |/ _` |
| (_| |  __/ |_) | |_| | (_| |
 \__,_|\___|_.__/ \__,_|\__, |
                         __/ |
                        |___/ 
*/
	
	initDebug : function() {
		om.debugOn = true;
		$("body").append("<div id='debug'></div>");
		},

	debugOn : Boolean,

	debug: function(strS, strA, strB) {
		$("#debug").append(strS+"<br/>\n");
		},

/*
  ___     ___  ___  __   __
 / _ \   |_  |/ _ \ \ \ / /
/ /_\ \    | / /_\ \ \ V / 
|  _  |    | |  _  | /   \ 
| | | |/\__/ / | | |/ /^\ \
\_| |_/\____/\_| |_/\/   \/
                           
*/

	/* XML FOR GROUP/ENTRY */

	ajaxGetAreaXML : function( url_xml ) {
		$.ajax({
			url: url_xml,
			method: "post",
			data: "refresh=" + Math.floor(Math.random() * 99999 ).toString(),
			cache: false,
			success: om.ajaxReceiveAreaXML
			});
		},
		
		ajaxReceiveAreaXML : function( XMLreq, status ) {
			if ( status == "success" ) {
				if(om.debugOn){ om.debug("<strong>XML| http success / code " + XMLreq.status + "<br/></strong>"); }
				
				// get CMS
				var x_publish = $(XMLreq).find('publish')[0];
				var x_publish_action = x_publish.getAttribute('action');
				var x_publish_echo = x_publish.getAttribute('echo');
				var x_publish_success = x_publish.getAttribute('success');
	
				// hash mash group + entry
				var x_publish_total_group = om.hashMashXML( "group", $(XMLreq).find('group'), true);
				var x_publish_total_entry = om.hashMashXML( "entry", $(XMLreq).find('entry'), true);
	
				// action
				omT.action( "init_WorkArea" );
	
				// DEBUG
				if(om.debugOn){
					if (x_publish_action.length) { om.debug ( "XML| <i>confirm</i> " + x_publish_action ); }
					if (x_data_group.length) { om.debug ( "XML| loaded " + x_data_group.length + ( x_data_group.length|1?" groups":" group") ); }
					if (x_data_entry.length) { om.debug ( "XML| loaded " + x_data_entry.length + ( x_data_entry.length|1?" entries":" entry") ); }
					if (x_publish_echo.length) { om.debug ( "XML| <i>action</i> " + x_publish_echo ); }
					}
				
			} else {
				if(om.debugOn){ om.debug("<strong>XML| http fail / code " + XMLreq.status + "<br/></strong>"); }
				}
	
			},

	/* XML FOR HOME */

	ajaxGetHomeXML : function( url_xml ) {
		$.ajax({
			url: url_xml,
			type: "POST",
			cache: false,
			success: om.ajaxReceiveHomeXML
			});
		},

		ajaxReceiveHomeXML : function( XMLreq, status ) {


			if ( status == "success" ) {
				if(om.debugOn){ om.debug("<strong>XML| http success / code " + $(XMLreq).status + "<br/></strong>"); }
				
				// get CMS
				var x_publish = $(XMLreq).find('publish')[0];
				var x_publish_action = x_publish.getAttribute('action');
				var x_publish_echo = x_publish.getAttribute('echo');
				var x_publish_success = x_publish.getAttribute('success');

				// hash mash group + entry
				var x_publish_total_group = om.hashMashXML( "group", $(XMLreq).find('group'), true);
				var x_publish_total_entry = om.hashMashXML( "entry", $(XMLreq).find('entry'), true);
	
				// action
				omT.action( "go_Home" );
	
				// DEBUG
				if(om.debugOn){
					if (x_publish_action.length) { om.debug ( "XML| <i>confirm</i> " + x_publish_action ); }
					if (x_data_group.length) { om.debug ( "XML| loaded " + x_data_group.length + ( x_data_group.length|1?" groups":" group") ); }
					if (x_data_entry.length) { om.debug ( "XML| loaded " + x_data_entry.length + ( x_data_entry.length|1?" entries":" entry") ); }
					if (x_publish_echo.length) { om.debug ( "XML| <i>action</i> " + x_publish_echo ); }
					}
				
			} else {
				if(om.debugOn){ om.debug("<strong>XML| http fail / code " + $(XMLreq).status + "<br/></strong>"); }
				}

			},

/*

______ ________  ___
|  _  \  _  |  \/  |
| | | | | | | .  . |
| | | | | | | |\/| |
| |/ /\ \_/ / |  | |
|___/  \___/\_|  |_/

*/
	dom : {
		prune: function( obj, conformArray ) {
			// force the children of obj to be the elements by id conformArray, in that order, and kill any others.
			// kill elements not on the list
			for ( i=0; i<obj.childNodes.length; i++ ) {
				if ( obj.childNodes[i].id != undefined && conformArray.indexOf( obj.childNodes[i].id ) < 0 ) {
					obj.childNodes[i].remove();
					if(om.debugOn){ om.debug( "removed " + obj.childNodes[i].id); }
				} else {
					}
				}
			// place a "rake" span at position 0.
			var my_firstChild = obj.firstChild;
			var my_Rake = new Element( "span" );
			obj.insertBefore(my_Rake, my_firstChild);
			// organize the elements which are in our list
			for ( var i=0; i<conformArray.length; i++ ) {
				obj.insertBefore( $("#"+conformArray[i]), my_Rake );
				}
			// kill the rake
			my_Rake.remove();
			}
	},

/*
 _   _______ _     
| | | | ___ \ |    
| | | | |_/ / |    
| | | |    /| |    
| |_| | |\ \| |____
 \___/\_| \_\_____/ 

*/
	url : {
		pageView: function( area, group_id, entry_id, name ) {
			var hash ="";
			if ( area != undefined ){
				hash += area;
				if ( group_id != undefined ){ hash += ( "-g" + group_id ); }
				if ( entry_id != undefined ){ hash += ( "-e" + entry_id ); }
				if ( name != undefined ){ hash += ( "-" + om.url.slugify(name) ); }
				}
			if ( hash.length ) {
				pageTracker._trackPageview( '/#' + hash );
				window.location.hash = hash;
			} else {
				pageTracker._trackPageview();
				window.location.hash = '';
				}
			},
		ingestHash : function( hash ) {
			if ( hash == undefined ) { hash = window.location.hash; }
			var area = hash.match(/#([a-zA-Z_]*)/);
				if ( area !== null ) { om.url.initial.area = area[1]; }
			var group_id = hash.match(/-g([0-9]*)/);
				if ( group_id !== null ) { om.url.initial.group_id = Number(group_id[1]); }
			var entry_id = hash.match(/-e([0-9]*)/);
				if ( entry_id !== null ) { om.url.initial.entry_id = Number(entry_id[1]); }
			},
		initial : {
			area : "",
			group_id : 0,
			entry_id : 0
			},
		encode : function(c){var o='';var x=0;c=c.toString();var r=/(^[a-zA-Z0-9_.]*)/;
			while(x<c.length){var m=r.exec(c.substr(x));
			if(m!=null && m.length>1 && m[1]!=''){o+=m[1];x+=m[1].length;
			}else{if(c[x]==' ')o+='+';else{var d=c.charCodeAt(x);var h=d.toString(16);
			o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;}}return o;
			},
		decode : function(s){var o=s;var binVal,t;var r=/(%[^%]{2})/;
			while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){b=parseInt(m[1].substr(1),16);
			t=String.fromCharCode(b);o=o.replace(m[1],t);}return o;
			},
		slugify : function(t) {
			var slug = String(t);
			slug = slug.replace(/[ \\\/_]+/g,'-');
			slug = slug.replace(/^[a-zA-Z0-9\-]+/g,'');
			return slug;
			}
		}
		
	};
