(function($){
	
	$.ccntrSupport = new Object();
	
	var c = $.ccntrSupport;
	
	c.wid = undefined;
	c.uname = undefined;
	c.ccntrPage = false;
	c.guestUser = false;
	c.securePath = '/';
	c.cdxIsReady = false;
	c.naMode = false;
	
	c.spyModePriceRatio = .33;
	
	c.didTest = true; //false;
	
	// initialize objs
	c.ccntrDims = new Object();
	c.sessHistory = new Object();
	c.srchRslts = new Object();
	c.hashWatcher = new Object();

	// CAMCENTER DIMENSIONS ------------------------------------------------------------

	c.ccntrDims.current = {
		//display: 'block',	
		minWidth: '1180px',
		width: '98%',
		height: '758px'
	}
		
	c.ccntrDims.defaults = {
		//display: 'block',	
		minWidth: '1180px',
		width: '98%',
		height: '520px'
	}
	
	c.ccntrDims.expandedHeightOffest = -20;
	
	/* CAMCENTER Base/Enlarged height boolean */
	c.ccntrDims.baseHeight = true;
	
	c.ccntrDims.expandHeight = function(){
		
		$('#ccntrHldr').css('height', ( $('body').outerHeight() + this.expandedHeightOffest ));
		$('#ccntrWrap').css('height', ( $('body').outerHeight() + this.expandedHeightOffest ));
		
	}
	
	c.ccntrDims.resetHeight = function(){
		
		$.ccntrSupport.ccntrDims.current.height = $.ccntrSupport.ccntrDims.defaults.height;
		
		$('#ccntrHldr').css( $.ccntrSupport.ccntrDims.current );
		$('#ccntrWrap').css( $.ccntrSupport.ccntrDims.current );
		
	}
		
	c.ccntrDims.reset = function() {
			
		for(var i in this.defaults){
			this.current[i] = this.defaults[i];
		};
	}

	// SESSION HISTORY ------------------------------------------------------------

	c.sessHistory = {

		writeTarg: '#sessHistory',
		hist: new Array(),
		histOrder: new Array(),
		histLen: 0

	}

	c.sessHistory.addEntry = function(uName, uid, thumbLoc, rel, rating){

		if (this.hist[uid]) {

			var s = this.hist[uid].stackPos;

			this.histOrder.splice( s, 1 );

			var histLen = this.histOrder.length;

			for( i = s; i < histLen; i++ ){
				this.histOrder[i].stackPos = i;
			}

		}

		this.hist[uid] = {

			uname: uName,
			thumbLoc: thumbLoc,
			rel: rel,
			rating: rating,
			uid: uid

		}

		var histOrderLen = this.histOrder.push(this.hist[uid]);
		this.hist[uid].stackPos = histOrderLen - 1;

		this.render();

	}

	c.sessHistory.removeEntry = function(uid){

	}

	c.sessHistory.render = function(){

		$(this.writeTarg).empty();

		var i = this.histOrder.length;

		while( --i > -1 ){

			$(this.writeTarg).append(
				'<div class="' + this.writeTarg + '_' + i + ' sessHistoryEntry"><div class="thumbCol"><img src="' + this.histOrder[i].thumbLoc + '"></div><div class="infoCol"><span class="uname"><a href="#" onclick="js2cdx_openUserID(\'' + this.histOrder[i].uid + '|' + this.histOrder[i].uname + '\'); $.ccntrSupport.hashWatcher.updateHash(\'' + this.histOrder[i].uid + '|' + this.histOrder[i].uname + '\'); return false;">' +this.histOrder[i].uname  + '</a></span></div><span class="clearSpan"><!-- --></span></div>'
			);

		}
	}

	c.sessHistory.dumpStack = function(){

		var i = this.histOrder.length;
		var msg = '';
		while (--i > -1) {
			msg += this.histOrder[i].uname + ' (' + this.histOrder[i].stackPos + '), ';
		}
		alert(msg);

	}

	// SEARCH RESULTS ------------------------------------------------------------
	
	c.srchRslts = {

		dataProvider: undefined,
		dataLoaded: false,
		lastGender: 'F',
		isNewSearch: false,
		page: 1,
		thumbLoc: undefined, 
		rfrshIntv_member: 5000,
		rfrshIntv_guest: 15000,
		rfrshFuse: undefined,
		rfrshFuseLit: false,
		polling: false,
		writeTarg: '#srchRslts',
		results: new Array(),
		resultsOrder: new Array(),
		resultsLen: 0,
		currFocusID: undefined,
		currFocusIndex: -1,
		paused: false,
		last_cam_page: 1,
		targMode: 'default'

	}
	
	c.srchRslts.pauseSearchPoll = function(){
		
		$.ccntrSupport.srchRslts.stopFuse();
		$.ccntrSupport.srchRslts.paused = true;
		$( '#srchActivityState' ).addClass( 'srchPaused' );
		$( '.allResults' ).addClass( 'srchEntryUpdatesPaused' );
	
	}
	
	c.srchRslts.unpauseSearchPoll = function(){
	
		$( '#srchActivityState' ).removeClass( 'srchPaused' );
		$( '.allResults' ).removeClass( 'srchEntryUpdatesPaused' );
		$.ccntrSupport.srchRslts.paused = false;
		$.ccntrSupport.srchRslts.getData();
	
	}
	
	c.srchRslts.clearCurrFocus = function(){
		
		var currFocusRef = document.getElementById( 'user_' + this.currFocusID );
		
		if( currFocusRef ){
			
			$('#user_' + this.currFocusID ).removeClass( 'activeFocus' ).addClass( 'notActiveFocus' );
			
		}
		
		this.currFocusID = undefined;
		this.currFocusIndex = -1;
		
		document.location = '#';
		
		try{
			parent.location.href = String( parent.location.href ).split('#')[0] + '#';
		} catch( e ){ }
		
		$.ccntrSupport.hashWatcher.lastConfirmedFocus = undefined;
		$.ccntrSupport.hashWatcher.lastKnownFocus = undefined;
		
	}
	
	c.srchRslts.setNewFocus = function( newFocusID ){
		
		this.clearCurrFocus();
		
		//if( isNaN(newFocusID) ){
			
			//return;
		//}
		
		
		
		this.currFocusID = newFocusID.split('|')[0];
		
		var currFocusRef = document.getElementById( 'user_' + this.currFocusID );
		
		this.focusPage = this.page;
		
		if( currFocusRef ){
			
			this.currFocusIndex = this.results[ this.currFocusID ].stackPos;
			
			$('#user_' + this.currFocusID ).removeClass( 'notActiveFocus' ).addClass( 'activeFocus' );
			
		} else {
			
			this.currFocusID = newFocusID ? newFocusID : -1;
			
		}
		
		$.ccntrSupport.hashWatcher.lastConfirmedFocus = this.currFocusID;
		$.ccntrSupport.hashWatcher.updateHash( this.currFocusID );
		
	}
	
	c.srchRslts.getNextSearchResult = function(){		
		var nextIndex;
		var nextPage = 1;
		
		if (this.currFocusIndex == -1) {
			
			nextIndex = this.resultsOrder.length - 1;
		
		} else {
			
			nextIndex = this.currFocusIndex - 1;
			
		}
	
	
		if(this.resultsOrder[nextIndex] && this.resultsOrder[nextIndex].wid == $.ccntrSupport.wid)
			nextIndex = nextIndex - 1;
	
		if(this.focusPage == this.page) {
			if(nextIndex < 0) {
				this.pageMoveDirection = 'forward';
				if(this.page < this.last_cam_page) {
					nextPage = this.page+1;
				}
				$.ccntrSupport.srchRslts.loadPage(nextPage);
			} else {
				if(this.resultsOrder[nextIndex].status != 'V'){
					if(this.page > 1) {
						this.pageMoveDirection = 'forward';
						$.ccntrSupport.srchRslts.loadPage(1);
					} else {
						var len = this.resultsOrder.length;
						for(idx=len-1;idx >= 0; idx--) {
							if(this.resultsOrder[idx].status == 'V' && this.resultsOrder[idx].wid != $.ccntrSupport.wid && this.resultsOrder[idx].isInOneOnOne == false && this.resultsOrder[idx].isInGroupPrivate == false) {
								nextIndex = idx;
								break;
							}
						}
						js2cdx_openUserID( this.resultsOrder[nextIndex].wid + '|' + this.resultsOrder[nextIndex].disp_name );
					}
				} else {
					js2cdx_openUserID( this.resultsOrder[nextIndex].wid + '|' + this.resultsOrder[nextIndex].disp_name );
				}
			}
		} else {
			this.pageMoveDirection = 'next';
			$.ccntrSupport.srchRslts.loadPage(this.focusPage);
		}
	}
	
	c.srchRslts.getPrevSearchResult = function(){
		
		var nextIndex;
		var nextPage = this.last_cam_page;
		
		if (this.currFocusIndex == -1) {
			
			nextIndex = this.resultsOrder.length - 1;
		
		} else {
			
			nextIndex = this.currFocusIndex + 1;
			
		}
	
	
	
		var len = this.resultsOrder.length;
		for(idx=nextIndex;idx < len; idx++) {
			if(this.resultsOrder[idx].status == 'V' && this.resultsOrder[idx].wid != $.ccntrSupport.wid && this.resultsOrder[idx].isInOneOnOne == false && this.resultsOrder[idx].isInGroupPrivate == false) {
				break;
			}
		}
		nextIndex = idx;
	
		if(this.focusPage == this.page) {
			if(nextIndex >= this.resultsOrder.length) {
				this.pageMoveDirection = 'back';
				if(this.page > 1) {
					nextPage = this.page-1;
				}
				$.ccntrSupport.srchRslts.loadPage(nextPage);
			} else {
				js2cdx_openUserID( this.resultsOrder[nextIndex].wid + '|' + this.resultsOrder[nextIndex].disp_name );
			/*
				if(this.resultsOrder[nextIndex].status != 'V'){
					if(this.page > 1) {
						this.pageMoveDirection = 'forward';
						$.ccntrSupport.srchRslts.loadPage(1);
					} else {
						nextIndex = this.resultsOrder.length - 1;
						js2cdx_openUserID( this.resultsOrder[nextIndex].wid + '|' + this.resultsOrder[nextIndex].disp_name );
					}
				} else {
					js2cdx_openUserID( this.resultsOrder[nextIndex].wid + '|' + this.resultsOrder[nextIndex].disp_name );
				}
				*/
			}
		} else {
			this.pageMoveDirection = 'prev';
			$.ccntrSupport.srchRslts.loadPage(this.focusPage);
		}
	}	
	
	
	c.srchRslts.updateRelationships = function(){
		c.relationships.loadRelations();
	}
	
	c.srchRslts.removeRelationship = function( relation, wid ){
		this._modifyRelationship(relation, 'D', wid);
	}

	c.srchRslts.addRelationship = function( relation, wid ){
		this._modifyRelationship(relation, 'A', wid);
	}
	
	c.srchRslts.removeRelationship = function( relation, wid ){
		this._modifyRelationship(relation, 'D', wid);
	}


	c.srchRslts._modifyRelationship = function( relation, how, wid ){
		var cssClass;
		switch(relation) {
			case 'friend' :
				c.relationships.modifyFriends(how,wid);
				break;
			case 'favorite' :
				c.relationships.modifyFavorites(how,wid);				
				break;
			case 'ignored' :
				c.relationships.modifyIgnored(how,wid);
				break;
			case 'banned' :
				c.relationships.modifyBanned(how,wid);				
				break;
			default:
				return;
		}
		
		var relations = c.relationships.getRelationships(wid);
		
		if(relations.isBanned) {
			cssClass = "stateIcon-onBanned";
		} else if(relations.isIgnored) {
			cssClass = "stateIcon-onIgnored";
		} else if(relations.isFriend && relations.isFavorite) {
			cssClass = "stateIcon-onFriendFavorite";
		} else if(relations.isFriend) {
			cssClass = "stateIcon-onFriend";
		} else if(relations.isFavorite) {
			cssClass = "stateIcon-onFavorite";
		}
		
		
		$('#olay_relation_' + wid).removeClass().addClass(cssClass);
	
	}
	
	c.srchRslts.setDataProvider = function( dProv, thumbLoc, tid, wid ){
		
		// if this is a guest user, pass g=1 to the data provider
		if( c.guestUser ) dProv = dProv + ( dProv.indexOf( '?' ) == -1 ? '?' : '&' ) + 'g=1';
		
		this.dataProvider = dProv;
		this.thumbLoc = thumbLoc;

		c.tid = tid;
		c.wid = wid;
		this.isNewSearch = true;
		c.relationships.loadRelations();
		
		if( !this.polling ) this.getData();
	}
	
	c.srchRslts.buildLwidStr = function(){
	
		var lwidStr = '';
		var i = this.resultsOrder.length;

		while( --i > -1 ){
			
			lwidStr += this.resultsOrder[i].ss + '|' + this.resultsOrder[i].wid + ':';

		}
		
		lwidStr = lwidStr.substring( 0, lwidStr.length - 1 );
	
	}


	c.srchRslts.getData = function(){
		
		this.polling = true;
		
		c.relationships.init();
		this._getData();
	}

	c.srchRslts._getData = function(){
	
		if( this.dataProvider == undefined ){

			return false;

		}

		var showMeVal = $('#showMe').val();
		var genderVal = $('input:radio[name=srch_gender]:checked').val();
		var countryVal = $('#srchCountry').val();
		
		if( genderVal != this.lastGender ) this.isNewSearch = true;
		
		// if there is no gender value (will only happen if the gender form elements has not loaded or is missing from the page)
		if( !genderVal ) genderVal = this.lastGender;
		
		this.lastGender = genderVal;
		
		var reqVars = new Object();
		//reqVars.lwid = this.buildLwidStr();
		reqVars.lwid = '';
		reqVars.order = 'AB';
		reqVars.pn = this.page;
		if( genderVal != 'all' ) reqVars.gender = genderVal;
		reqVars.action = 'U';
		reqVars.rseed = Math.floor(Math.random() * 1000000);
		if( countryVal != 'all'  ) reqVars.country = countryVal;
		
		if ( !this.paused ) {
		
			$.post( this.dataProvider, reqVars, function( data ){
				$.ccntrSupport.srchRslts.dataLoaded = true;
				$.ccntrSupport.srchRslts.rfrshFuseLit = false;
				$.ccntrSupport.srchRslts.processSrchData( data );
				
			}, 'text');
			
		}
		
	}

	c.srchRslts.processSrchData = function( data){
		
		// reset results array... we're pulling the whole list for now
		this.results = new Array();
		this.resultsOrder = new Array();
		
		var dataObj = $.parseJSON( data );
		
		if( !dataObj ) return;
		
		if( dataObj.code != undefined ){

			this.renderSrchError( dataObj.code, dataObj.description );

		} else {
			
			if( dataObj.adds == undefined || dataObj.adds.length == '0' ){
				
				// only clear the search results and display the 'no results' message if this is a new search
				// cost: if a natural event occurs that somehow clears out the entire results set (not very likely but possible)
				// the data will not be deleted until the next poll
				if ( c.srchRslts.isNewSearch ) $(this.writeTarg).empty().append( 'We\'re sorry, your search did not return any results.' );
				
			} else {
				
				this.renderPagInfo( dataObj.curr_page, dataObj.total_pages );
				
				this.last_cam_page = dataObj.last_cam_page;
				
				// process removes...
				if( dataObj.removes != undefined ) this.processRemoves( dataObj.removes );
				
				// process adds...
				if( dataObj.adds != undefined ) this.processAdds( dataObj.adds );
				
				this.render();
				
				if(this.pageMoveDirection) {
					var pageDir = this.pageMoveDirection;
					delete this.pageMoveDirection;
					
					switch(pageDir) {
						case 'forward':
							var wid = this.resultsOrder[this.resultsOrder.length -1].wid;
							var dispName = this.resultsOrder[this.resultsOrder.length -1].disp_name;
							var len = this.resultsOrder.length;
							for(idx=len-1;idx >= 0; idx--) {
								if(this.resultsOrder[idx].status == 'V' && this.resultsOrder[idx].wid != $.ccntrSupport.wid && this.resultsOrder[idx].isInOneOnOne == false && this.resultsOrder[idx].isInGroupPrivate == false) {
									wid = this.resultsOrder[idx].wid;
									dispName = this.resultsOrder[idx].disp_name;
									break;
								}
							}
							js2cdx_openUserID( wid + '|' + this.resultsOrder[this.resultsOrder.length -1].disp_name );
							break;
						case 'next':
							this.getNextSearchResult();
							break;
						case 'back':
							var wid = -1;
							var len = this.resultsOrder.length;
							for(idx=0;idx < len; idx++) {
								if(this.resultsOrder[idx].status == 'V' && this.resultsOrder[idx].wid != $.ccntrSupport.wid && this.resultsOrder[idx].isInOneOnOne == false && this.resultsOrder[idx].isInGroupPrivate == false) {
									wid = this.resultsOrder[idx].wid;
									break;
								}
							}
							js2cdx_openUserID( wid + '|' + this.resultsOrder[idx].disp_name );
							break;
						case 'prev':
							this.getNextSearchResult();
							break;							
					}					
				}		
				
			}
			
			if ( c.srchRslts.isNewSearch )  c.srchRslts.isNewSearch = false;
			
			// light the fuse for the next request
			if( !this.rfrshFuseLit ){
				
				this.lightFuse();
				
			}
		}
	}
	
	c.srchRslts.stopFuse = function(){
		
		window.clearTimeout( this.rfrshFuse );
		this.rfrshFuseLit = false;
		
	}
	
	c.srchRslts.lightFuse = function(){
		
		this.rfrshFuse = window.setTimeout( '$.ccntrSupport.srchRslts.getData()', ( c.guestUser ? this.rfrshIntv_guest : this.rfrshIntv_member ));
		this.rfrshFuseLit = true;
		
	}
	
	c.srchRslts.render = function(){
		
		if (!this.dataLoaded) {
			$(this.writeTarg).empty();
			$(this.writeTarg).append( 'Retrieving results...' );
			return;
		}
		
		var i = this.resultsOrder.length;
		var aState;
		var targLink;
		
		var oput = [];
		var caretIndex = 0;
		
		var nonCcntrPgTarg = $.ccntrSupport.guestUser ? '/register.php' : $.ccntrSupport.uname != undefined ? '/profile/' + $.ccntrSupport.uname : '/?p=help&d=1';
		
		if( this.resultsOrder.length > 0 ) $(this.writeTarg).empty();
		
		while( --i > -1 ){
			
			var resultsData = this.resultsOrder[i];
			
			var aStateClass = resultsData.wid == this.currFocusID ? 'activeFocus' : 'notActiveFocus';
						
			//Get relationships
			var relations = c.relationships.getRelationships( resultsData.wid );
			
			// verify that the desired mode is supported and override if not
			if( this.mode == 'proxyLauncher' && document.proxyLauncherMech == undefined ) this.mode = 'default';
			
			if(resultsData.isOnline) {
				
				switch( this.mode ){
				
					case 'proxyLauncher':
						targLink = '<a href="#" onclick="document.proxyLauncherMech.openLocation( \'' + ( $.ccntrSupport.ccntrPage ? '' : ( $.ccntrSupport.uname != undefined ? $.ccntrSupport.securePath : '/' ) + 'camcenter/index.php' + ( $.ccntrSupport.guestUser ? '?g=1' : '' )) + '#' + (resultsData.wid == $.ccntrSupport.wid ? 'me' : resultsData.wid) + '|' + resultsData.disp_name + '\'); return false">';
						break;
						
					default:
						targLink ='<a href="' + ( $.ccntrSupport.ccntrPage ? '' : ( $.ccntrSupport.uname != undefined ? $.ccntrSupport.securePath : '/' ) + 'camcenter/index.php' + ( $.ccntrSupport.guestUser ? '?g=1' : '' )) + '#' +  (resultsData.wid==$.ccntrSupport.wid?'me':resultsData.wid) + '|' + resultsData.disp_name + '"' + ( $.ccntrSupport.ccntrPage ? '' : ' target="cams"' ) + '>' ;
						break;
						
				
				}
				
				
				//targLink = '<a href="' + c.utils.linkBuilder(resultsData.wid) + '"' + ( $.ccntrSupport.ccntrPage ? '' : ' target="cams"' ) + '>' ;
			
			} else {
				
				targLink = '<a href="/profile.php?u_id=' + resultsData.wid + '"' + ($.ccntrSupport.ccntrPage ? ' target="subPage"' : '') + '>';
			
			}
			
			oput[ caretIndex++ ] = '<div class="srchEntry ' + aStateClass + '" id="user_' + resultsData.wid + '">' + targLink + '<span class="uname">' + resultsData.disp_name + '</span><span class="imgHldr">'; 
			
			oput[ caretIndex++ ] = '<img src="' + this.thumbLoc + '/' + resultsData.thumb + '" class="srchRsltImg">';
			
			// online status
			if( resultsData.isOnline ) {
				if(resultsData.isInOneOnOne) {
					oput[ caretIndex++ ] = '<span class="olay-topCornerGroupSingle"><!-- --></span>';
				} else if (resultsData.isInGroupPrivate) {
					oput[ caretIndex++ ] = '<span class="olay-topCornerGroupPrivate"><!-- --></span>';
				} else if (resultsData.isOnCam) {
					oput[ caretIndex++ ] = '<span class="olay-topCornerLive"><!-- --></span>';
				} else {
					oput[ caretIndex++ ] = '<span class="olay-topCornerOnline"><!-- --></span>';
				}
			}
			
			oput[ caretIndex++ ] = '</span>';
			
			// user state
			oput[caretIndex++] = '<span class="userState">';
			if(relations.isBanned) {
				oput[caretIndex++] = '<span class="stateIcon-onBanned" id="olay_relation_' + resultsData.wid + '"><span class="iconHldr" title="Banned"><!-- --></span></span>';
			} else if(relations.isIgnored) {
				oput[caretIndex++] = '<span class="stateIcon-onIgnored" id="olay_relation_' + resultsData.wid + '"><span class="iconHldr" title="Ignored"><!-- --></span></span>';
			} else if(relations.isFavorite && relations.isFriend) {
				oput[caretIndex++] = '<span class="stateIcon-onFriendFavorite" id="olay_relation_' + resultsData.wid + '"><span class="iconHldr" title="Friend/Favorite"><!-- --></span></span>';
			} else if(relations.isFavorite) {
				oput[caretIndex++] = '<span class="stateIcon-onFavorite" id="olay_relation_' + resultsData.wid + '"><span class="iconHldr" title="Favorite"><!-- --></span></span>';
			} else if(relations.isFriend) {
				oput[caretIndex++] = '<span class="stateIcon-onFriend" id="olay_relation_' + resultsData.wid + '"><span class="iconHldr" title="Friend"><!-- --></span></span>';
			}
			
			oput[caretIndex++] = '</span>';
			
			oput[ caretIndex++ ] = '</a></div>';
			
		}
		
		$( this.writeTarg ).append( '<div class="allResults">' + oput.join('') + '</div>');
		
	}
	
	c.srchRslts.processRemoves = function( removes ){

		$.each( removes, function(){

			$.ccntrSupport.srchRslts.dbug( 'processing remove...' );

		});

	}
	
	c.srchRslts.processAdds = function( adds ){

		$.each( adds, function(){

			//if( this.wid != $.ccntrSupport.wid ) $.ccntrSupport.srchRslts.addEntry( this );
			$.ccntrSupport.srchRslts.addEntry( this );

		});

	}
	
	c.srchRslts.addEntry = function( thisEntry ){
		
		// if the result is already in the list, remove it and update position numbers
		if (this.results[thisEntry.wid]) {

			var s = this.results[thisEntry.wid].stackPos;

			this.resultsOrder.splice( s, 1 );

			var resultsLen = this.resultsOrder.length;

			for( i = s; i < resultsLen; i++ ){
				this.resultsOrder[i].stackPos = i;
			}

		}

		this.results[thisEntry.wid] = {

			wid: thisEntry.wid,
			disp_name: thisEntry.disp_name,
			gender: thisEntry.gender,
			status: thisEntry.status,
			thumb: thisEntry.thumb,
			portrait: thisEntry.portrait,
			idle_time: thisEntry.idle_time,
			ss: thisEntry.ss,
			privateStatus: thisEntry.private_status,
			isOnline: thisEntry.status != 'X',
			isOnCam: thisEntry.status == 'V',
			isInOneOnOne: false,
			isInGroupPrivate: false,
			cam_score: thisEntry.cam_score
		}

		var pstatus = thisEntry.private_status;
		if(pstatus) {
			if(pstatus == 'PG' || pstatus == 'FG')
				this.results[thisEntry.wid].isInGroupPrivate = true;

			if(pstatus == 'PO' || pstatus == 'FO' || pstatus == 'IP')
				this.results[thisEntry.wid].isInOneOnOne = true;
			}
		
		var resultsOrderLen = this.resultsOrder.push( this.results[thisEntry.wid] );
		this.results[thisEntry.wid].stackPos = resultsOrderLen - 1;

	}
	
	c.srchRslts.renderSrchError = function( eCode, eDesc ){

		$( '#srchErr' ).css('display', 'block');
		$( '#srchErrCode' ).append( eCode );
		$( '#srchErrDesc' ).append( eDesc );

	}
	
	c.srchRslts.renderPagInfo = function( thisPg, ttlPgs ){
		
		thisPg = parseInt( thisPg );
		ttlPgs = parseInt( ttlPgs );
		
		
		var pgStr = '<a href="#" onclick="$.ccntrSupport.srchRslts.loadPage(\'' + ( thisPg == 1 ? ttlPgs : ( thisPg - 1 )) + '\'); $(document).scrollTop(); return false;"><< Previous</a>';
		pgStr += '&nbsp;&nbsp;page ' + thisPg + ' of ' + ttlPgs + '&nbsp;&nbsp;';
		pgStr += '<a href="#" onclick="$.ccntrSupport.srchRslts.loadPage(\'' + ( thisPg == ttlPgs ? '1' : ( thisPg + 1 )) + '\'); $(document).scrollTop(); return false;">Next >></a>';
		
		$('.pgHldr').empty().append( pgStr + '<br>' );
		
	}
	
	c.srchRslts.loadPage = function( pgNum ){
		
		this.page = pgNum;
		this.stopFuse();
		this.getData();
		
	}
	
	c.srchRslts.registerSearchForm = function(){
		
		var srch = this;
		
		$('#showMe').change(function(){
			srch.newSearch();
		});
		$('input:radio[name=srch_gender]').change(function(){
			srch.newSearch();
		});
		$('#srchCountry').change(function(){
			srch.newSearch();
		});
		
	}
	
	c.srchRslts.newSearch = function(){
		
		$(this.writeTarg).empty();
		$(this.writeTarg).append('Getting search results...');
		
		this.clearAll();
		this.getData();
		
	}
	
	c.srchRslts.clearAll = function(){
		
		this.results = new Array(),
		this.resultsOrder = new Array(),
		this.resultsLen = 0;
		
		$('.pgHldr').empty();

	}
	
	c.srchRslts.dbug = function( msg ){
		$( '#dbugOut' ).append( '<br>' + msg );
	}
	
	c.srchRslts.hideExtNav = function( forceHide ){
		
		if ( forceHide ) $('.extNavMainTblHldr').css( 'display', 'none' );
		
		$('.btn-hide').css( 'display', 'none' );
		$('.btn-show').css( 'display', 'block' );
		
		c.ccntrDims.expandHeight();
		
	}
	
	c.srchRslts.showExtNav = function(){
		
		$('.extNavMainTblHldr').css( 'display', 'inherit' );
		
		$('.btn-hide').css( 'display', 'block' );
		$('.btn-show').css( 'display', 'none' );

		c.ccntrDims.resetHeight();
		
	}
			
	c.srchRslts.getFeatured = function(){
		
		var highList = new Array();
		var allList = new Array();
		
		var i = this.resultsOrder.length;
		
		while( --i > -1 ){
			
			if( this.resultsOrder[i].status == 'V' && this.resultsOrder[i].gender == 'F' && this.resultsOrder[i].wid != $.ccntrSupport.wid && this.resultsOrder[i].isInOneOnOne == false && this.resultsOrder[i].isInGroupPrivate == false ) {
				

				var minScore = 1;
				//High score
				if(this.resultsOrder[i].cam_score >= minScore) {
					highList.push(i);
				}
				//Top Five in list
				if(allList.length < 5) {
					allList.push(i);
				}
			}
		}
		
		//Use highList unless too few
		var avail = highList;
		if(avail.length < 5) {
			avail = allList;
		}
		
		if(avail.length == 0) {
			idx = this.resultsOrder.length - 1;
		} else {
			idx = avail[Math.floor(Math.random()*avail.length)];
			
		}
		return this.resultsOrder[ idx ].wid + '|' + this.resultsOrder[ idx ].disp_name;
	}
	
	// RELATIONSHIP DATA -------------------------------------------------------
	c.relationships = {
		loaded: false,
		loading: false,
		favorites: [],
		friends: [],
		ignored: [],
		banned: []
	}
	
	
	c.relationships.isLoaded = function() {
		//alert( 'c.relationships.isLoaded() > this.loaded: ' + this.loaded );
		return this.loaded;
	}
	
	c.relationships.init = function() {
		this.loaded = false;
		this.loading = false;
	}
	
	c.relationships._modifyArray = function(mod_array,how,id) {
		if(!mod_array){
			return;
		}
			
		if(how == 'A') {
			mod_array[id] = 1;
		} else {
			delete mod_array[id];
		}
			
	}
	
	c.relationships.modifyFriends = function(how,id) {
		this._modifyArray(this.friends,how,id);	
	}
	
	c.relationships.modifyFavorites = function(how,id) {
			this._modifyArray(this.favorites,how,id);		
	}
	
	c.relationships.modifyIgnored = function(how,id) {
			this._modifyArray(this.ignored,how,id);		
	}
	
	c.relationships.modifyBanned = function(how,id) {
			this._modifyArray(this.banned,how,id);		
	}
	
	
		
	c.relationships.reindexData = function(in_array) {
		var out_array = new Object();
		
		if(in_array) {
			for(var i=0; i<in_array.length;i++) {
				out_array['wid_' + in_array[i]] = 1;
			}
		}
		return out_array;
		
	}
	
	c.relationships.loadRelations = function(id) {
		
		if(this.loading || $.ccntrSupport.guestUser) return;
		
		this.loading = true;
		
		var reqVars = {returnas: 'json'};
		
		$.post('/camcenter/get_rels.php', reqVars, function (data) {c.relationships.processRelations(data)}, 'text');
	}
	
	c.relationships.processRelations = function(data) {
		
		var dataObj;
		
		try {
			dataObj = $.parseJSON( data );
		} catch (e) {
		}
		
		if(!dataObj) dataObj = new Object();
		
		this.favorites = this.reindexData( dataObj.bookmarked );
		this.friends = this.reindexData( dataObj.friends );
		this.ignored = this.reindexData( dataObj.ignored );
		this.banned = this.reindexData( dataObj.banned );
		this.loading = false;
		this.loaded = true;
		
		$.ccntrSupport.srchRslts.render();
		
	}
	
	c.relationships.getRelationships = function(id) {
		
		var relationships = {
			isFriend: false,
			isFavorite: false,
			isBanned: false,
			isIgnored: false
		}
		
		/*
		if(!this.loaded){
			return relationships;
		}
		*/
		
		relationships.isFriend = this.friends[ 'wid_' + id ] == 1 ? true : false;
		relationships.isFavorite = this.favorites[ 'wid_' + id ] == 1 ? true : false;
		relationships.isBanned = this.banned[ 'wid_' + id ] == 1 ? true : false;
		relationships.isIgnored = this.ignored[ 'wid_' + id ] == 1 ? true : false;
		return relationships;
	}
	
	// UTILS ------------------------------------------------------------

	c.utils = {};

	c.utils.linkBuilder = function( wid, locOverride ){
		var newLoc;
		var currLoc = String( document.location );
		
		if( currLoc.length == 0 || locOverride != undefined ) currLoc = ( locOverride != undefined ) ? locOverride : '/camcenter/index.php';
		
		newLoc = (currLoc.indexOf('#') != -1) ? currLoc.split('#')[0] : currLoc;
		
		if ( $.ccntrSupport.guestUser ) {
			var parts = newLoc.split('?');
			if(parts[1])
				params = parts[1].split('&');
			else
				params = new Array();
				
			var gotG = false;
			if(params.length==0) {
				newLoc = newLoc + "?g=1";
			} else {
				$.each(params,function(index,value) { 
					if(value == 'g=1') {
						gotG = true;
						return false;
					}
				});
				if(!gotG) {
					newLoc = newLoc + "&g=1";
				}
			}
		}
		
		if( newLoc.length == 0 ) newLoc = '/camcenter/index.php';
		
		if(wid ==  $.ccntrSupport.wid ) {
			newLoc = newLoc + '#me';
		} else {
			newLoc = newLoc + '#' + wid;
		}
		
		return newLoc;
	
	}
	
	// HASH WATCHER ------------------------------------------------------------
	
	c.hashWatcher = {
		
		lastKnownFocus: undefined,
		lastConfirmedFocus: undefined,
		hashInterval: undefined,
		hashIntervalLen: 750,
		paused: false
		
	}
	
	c.hashWatcher.hashTest = function(){
		
		var docHash = document.location.hash;
		var docLoc = String( document.location );
		
		if( this.paused || !c.srchRslts.dataLoaded ) return;
		
		if ( (!docHash && docLoc.indexOf('#') == -1)) {
			
			this.updateHash( c.srchRslts.getFeatured() );
			js2cdx_openUserID( this.lastKnownFocus );
			return;
			
		}
		
		if(docHash == '#')
			return;
		
		var hashWid = docHash.substring( 1, docHash.length );
		
		if (hashWid != this.lastKnownFocus && hashWid.length > 0 || this.lastKnownFocus != this.lastConfirmedFocus ) {
			if(hashWid == 'me' && this.lastKnownFocus == 'me')
				return 
				
			this.lastKnownFocus = hashWid;
			
			js2cdx_openUserID(this.lastKnownFocus);
			
		}
		
	}
	
	c.hashWatcher.updateHash = function( wid ){
		
		var newLoc = c.utils.linkBuilder(wid);
		
		this.paused = true;
		document.location = newLoc;
		this.lastKnownFocus = wid;
		
		if( String( parent.location.href ).indexOf( wid ) == -1 ){
			try{
				parent.location.href = String( parent.location.href ).split('#')[0] + '#goto_' + wid;
			} catch( e ){}
		}
		
		
		this.paused = false;
		
	}
	
	c.hashWatcher.activate = function(){
		this.hashInterval = setInterval( '$.ccntrSupport.hashWatcher.hashTest()', this.hashIntervalLen );
	}

	c.srchRslts.dbug = function( msg ){
		$( '#dbugOut' ).append( '<br>' + msg );
	}
	
	c.hashWatcher.dbug = function( msg ){
		$( '#dbugOut' ).append( '<br>' + msg );
	}

})(jQuery);


