$(document).ready( function() {

	function loadRoomsList() {
		
		var ajax_url	= "/webservices/live-chat.php?a=rooms_list";
		
		$.ajax({
			cache: false,
			url: ajax_url,
			type: 'GET',
			dataType: 'xml',
			timeout: 10000,
			error: function(){
			},
			success: function(xml){
			
				//
				//	Create the rooms list
				//
				var html = '';
			
				$(xml).find('performer').each( function() {
				
					var $entry = $(this);
					
					if( $entry.find('login_group_id').text() == '7' ) {
						//alert( 'skipping ' + $entry.find('model_id').text() );
					} else if( $entry.find('model_id').text() == MODEL_INFO.model_id ) {
						//alert( 'skipping ' + $entry.find('model_id').text() );
					} else {
					
						html += '<div class="left performer">';
						html += '<a href="/rooms/' + $entry.find('name_seo').text() + '/">';
						html += '<img height="66" border="0" width="88" alt="' + $entry.find('name_display').text() + '" title="' + $entry.find('tagline').text() + '" src="' + CONFIG.CDN_URL + '/images/models/samples-120x90/' + $entry.find('sample_image_id').text() + '.jpg" />';
						
						if( $entry.find('room_status_char').text() == 'P' || $entry.find('room_status_char').text() == 'F' ) {
						
							html += '<div class="special_overlay">&nbsp;</div>';
							html += '<div class="private_overlay">In Show</div>';
						
						} else if( $entry.find('login_group_id').text() == '6' ) {
						
							html += '<div class="special_overlay">&nbsp;</div>';
							html += '<div class="premiere_overlay">Premiere</div>';
						
						}
						
						if( $entry.find('audio_enabled').text() == 'Y' ) {
						
							html += '<div class="audio_overlay"><img src="/images/chat/icons/sound.png" /></div>';
						
						}
						
						html += '</a>';
						html += '<br />' + $entry.find('name_display').text() + '';
						html += '</div>';
					
					}
				
				});
				
				$('#performers').html( html );
			
			}
		
		});
		
		//alert('reloaded');
	
	}
	
	setInterval(loadRoomsList, 60000);
	
	$("#refresh_chat_interface").click(function(event) {
	
		event.preventDefault();
		
		$("#chat_interface_column").hide();
		
		setTimeout(	function() {
						$("#chat_interface_column").show();
					}, 20 );
	
	});
	
	$("#refresh_rooms_list").click(function(event) {
	
		event.preventDefault();
		
		loadRoomsList();
	
		$("#rooms_list_column").hide();
		
		setTimeout(	function() {
						$("#rooms_list_column").show();
					}, 20 );
	});

});

var document_width		= 0;
var document_height		= 0;
var chat_area_width		= 0;
var chat_area_height	= 0;

function determineWindowDimensions() {

	$(document).ready( function() {
		
		//
		//	Define User's Browser Information
		//
		var browserUserAgent	= navigator.userAgent.toLowerCase();
		var isMSIE				= ( browserUserAgent.indexOf( "msie" ) != -1 ) ? true : false;
	
		//
		//	Define Height and Width of Window
		//
		document_width		= ( isMSIE ) ? document.body.clientWidth : window.innerWidth;
	
		document_height		= $(window).height() - 200;
		
		//var height		= ( isMSIE ) ? document.body.clientHeight : window.innerHeight;
	
		/* Special condition for halloween promo */
		
		if( displayBanner ) {
			
			document_height		-= 50;
		}
	
		//document.write(document_height);
		/* End Special condition */
	
		//
		//	Account for bad data
		//
		if ( document_width <= 0 )		document_width	= 800;
		if ( document_height <= 0  )	document_height	= 600;
		
		//
		//	Define the Flash Movie width & height
		//	The height of the Flash movie should be 75% of the available area.
		//
		chat_area_width		= document_width - 250;
		
		if( chat_area_width < 780 ) chat_area_width = 780;	// Make sure the user gets some width
		
		chat_area_height	= Math.round( document_height + 5 );
		
		if( chat_area_height < 480 ) chat_area_height = 480;	// Make sure the user gets some height
		
		//For the fixed width used by freeones.
		if( SKIN.sitekey == 'freeones' )	chat_area_width = 780;
		
		//
		// Changes for testing.
		//
		//$('#guest_register_form').width( chat_area_width - 320 );
		
		
		
		/* Special condition when no promo running */
		if ( SKIN.sitekey == 'vivid' ) {
			
			var roomslist_height	= chat_area_height + 45;
		
		} else if ( SKIN.sitekey == 'freeones' ) {
			
			var roomslist_height	= chat_area_height - 20;
			
		} else if ( SKIN.sitekey == 'whitelabel' ) {
			
			var roomslist_height	= chat_area_height - 25;
			
		} else {
		
			var roomslist_height	= chat_area_height + 68;
		
		}
		/*
		if( $('#chat_interface_column').height() < roomslist_height ) {
		
			$('#chat_interface_column').height( roomslist_height );
		
		}
		*/
		
		$('#rooms_list_column').height( roomslist_height );
	
	});

}


//
//	initializeChatInterface()		Load the new chat interface with the necessary 
//									configuration parameters.
//
function initializeChatInterface() {
	
	$(document).ready( function() {
	
		var free_user_ad	= 'free-user-ad-v3.swf';
		
		if( ROOM.cti == 8 ) {
			var welcome_message		= 'Welcome to ' + PERFORMER.model_info.name_display + '\'s room.  Choose a nickname to start chatting.';
		} else {
			var welcome_message		= 'Welcome to ' + PERFORMER.model_info.name_display + '\'s chat room. Type your message in the box below and press SEND to start chatting.';
		}
		
		if( ROOM.cti == 6 || ROOM.cti == 7 ) {
			var disable_whisper	= 'false';
			var disable_ad		= 'true';
		} else {
			var disable_whisper	= 'true';
			var disable_ad		= 'false';
		}
		
		if( ROOM.video_host == "ofms-dev01.vs.com" || ROOM.video_host == "ofms-dev02.vs.com" ) {
		
			var chat_debug		= true;
		
		} else {
		
			var chat_debug		= false;
		
		}
		
		
		//	SWFObject Embed Code
		var flashvars = {
			host:				ROOM.host,
			port:				ROOM.xml_port,
			token:				ROOM.token,
			mp_code:			mp_code,
			service:			service,
			model_id:			ROOM.model_id,
			model_name: 		PERFORMER.model_info.name,
			sample_image_id: 	PERFORMER.model_info.sample_image_id,
			source_code: 		'guest-chat-' + ROOM.model_id + '-flash',
			sitekey:			SKIN.sitekey,
			params: 			params,
			allow_controls:		'0',
			PHPSESSID:			PHPSESSID,
			_debug:				chat_debug,
			object_name: 		'chatInterfaceObject',
			target_element: 	'chatInterface',
			width:				chat_area_width,
			height:				chat_area_height,
			background_color:	flash_background_color,
			text_color:			flash_text_color,
			version:			'9.0.115.0',
			express_install_src:'',
			swfobject_vers: 	'2',
			source_file: 		SWF_Domain,
			video_multiplier: 	1.25,
			user_id_enc:		user_id_enc,
			reg_auth_key:		reg_auth_key,
			reg_email:			reg_email,
			reg_message:		reg_message,
			mute_audio:			mute_audio,
			fms_cdn_host_enc:	fms_cdn_host_enc,
			fms_cdn_instance_enc:	fms_cdn_instance_enc,
			fms_cdn_auth_enc:	fms_cdn_auth_enc,
			ad_link:			ad_link
		};
		
		var attributes = {
			swf:				SWF_Domain,
			width:				chat_area_width,
			height:				chat_area_height,
			id:					'chatInterfaceObject',
			name:				'chatInterfaceObject',
			SWLiveConnect:		true,
			AllowScriptAccess: 	'always'
		};
		
		var parameters			= false;
		
		swfobject.embedSWF(SWF_Domain+'/chat/flash/guest-open-interface-v3.swf', 'chatInterface', chat_area_width, chat_area_height, '9.0.115.0','/flash/playerProductInstall.swf', flashvars, parameters, attributes);
	
	});

	return true;

}



//
//	Needed for Layout 02 on some skins
//
function newWindowAll( mypage, myname, w, h, scroll ) {

	leftPos		= ( screen.width ) ? ( ( screen.width - w ) / 2 ) : 0;
	topPos		= ( screen.height ) ? ( ( screen.height - h ) / 2 ) : 0;
	
	settings	= 'height=' + h + ',width=' + w + ',top=' + topPos + ',left=' + leftPos + ',scrollbars=' + scroll + ',menubar=1,location=1,directories=1,channelmode=1,dependent=1,copyhistory=1,resizable=1,target=' + myname + ',fullscreen=0,status=1,toolbar=1';
	
	win			= window.open( mypage, myname, settings );
	
	if( win.window.focus ) win.window.focus();
}

//
//  Load New Window Centered
//
function newWindowPlain( mypage, myname, w, h, scroll ) {

	leftPos		= ( screen.width ) ? ( ( screen.width - w ) / 2 ) : 0;
	topPos		= ( screen.height ) ? ( ( screen.height - h ) / 2 ) : 0;
	
	settings	= 'height=' + h + ',width=' + w + ',top=' + topPos + ',left=' + leftPos + ',scrollbars=' + scroll + ',menubar=0,location=0,directories=0,channelmode=0,dependent=0,copyhistory=0,resizable=1,target=' + myname + ',fullscreen=0,status=1,toolbar=0';
	
	win			= window.open( mypage, myname, settings );
	
	if( win.window.focus ) win.window.focus();
}

//
//  Load New Window (Right Aligned)
//
function newWindowRightPlain( mypage, myname, w, h, scroll ) {

	leftPos		= ( screen.width )  ? ( ( screen.width - w  ) ) : 0;

	settings	= 'height=' + h + ',width=' + w + ',top=0,left=' + leftPos + ',scrollbars=' + scroll + ',menubar=0,location=0,directories=0,channelmode=0,dependent=0,copyhistory=0,resizable=1,target=' + myname + ',fullscreen=0,status=1,toolbar=0';
	
	win			= window.open( mypage, myname, settings );
	
	if( win.window.focus ) win.window.focus();
	
}

function launchScreenNames() {
	
	var SCREEN_NAMES = '/my-account/secure/?a=screen_names&small=1&' + COMMON_RAW;
	
	newWindowPlain( SCREEN_NAMES, "SCREEN_NAMES", 660, 420, 1 );
	
}

function launchSupport() {
	
	var SC_URL = '/support.php?' + COMMON_RAW + '&small=1';
	
	newWindowPlain( SC_URL, "ScreenNames", 660, 420, 1 );

}

function launchRates() {
	
	var RATES	= '/help-popups/rates.php?' + COMMON_RAW + '&small=1#rates';
	
	newWindowRightPlain( RATES, 'RATES', 600, screen.height, 1 );

}

function launchHomepage() {

	var HOMEPAGE	= '/index.php?' + COMMON_RAW;

	newWindowAll( HOMEPAGE, 'HOMEPAGE', 800, 600, 1 );

}

function launchVIPUpgrade() {

	var VIP_UPGRADE = '/click-track.php?a=outbound&product=vip&dhd_page=FullPay&sitekey='+SKIN.sitekey+'' + COMMON_RAW;
	
	newWindowPlain( VIP_UPGRADE, 'VIP_UPGRADE', 800, 600, 1 );
}

function launchBuyTime() {
	
	var BUY_TIME = '/buy-time.php?' + CONFIG.COMMON_RAW + '&small=1';
	
	newWindowRightPlain( BUY_TIME, 'BUY_TIME', 660, screen.height, 1 );

}


