var join_url = '';

$(document).ready(function() {
						   
	if(document.domain.indexOf("mywebcamcrush") != -1)
		join_url = 'https://secure.mywebcamcrush.com';
	else if(document.domain.indexOf("webcamcrush") != -1)
		join_url = 'http://secure.webcamcrush.com';
		
	digits = new Array(10);
	for ( i=0; i < 10; i++ ) {
		digits[i] = new Image();
		digits[i].src = "/images/timer/" + i + ".gif";
	}

	// Start the countdown
	$(document).everyTime(1000, function(i) {
		if(--current_seconds < 0) {
			current_seconds = start_seconds;
			if(--current_minutes < 0) {
				current_minutes = start_minutes;
				current_seconds = start_seconds;
			}
		}
		
		var i = current_minutes % 10;
		document.images["d0"].src = digits[((current_minutes - i) / 10)].src;
		document.images["d1"].src = digits[i].src;
	
		i = current_seconds % 10;
		document.images["d3"].src = digits[((current_seconds - i) / 10)].src;
		document.images["d4"].src = digits[i].src;
	});
	
	$('#webcam').flash({
		// swf: 'http://ec.vs.com/_special/live-video-ads/livead-interface.swf',
		swf: spoof_cam,
		params: {
			allowscriptaccess: 	'always',
			wmode: 				'transparent',
			loop:				'false'
		},
		flashvars: {   
			width: '320',
			height: '240',
			mp_code: 'cmw9',
			service: 'girls',
			source_code: 'live-video-ad',
			sitekey: 'flirt4free',
			creative: '320x240-all-0001',
			target: '_blank',
			cats: ad_categories,
			show_all: 'Y',
			dev_mode: 'false',
			report_error: 'false',
			loop: 'false'
		},
		height: 240,
		width: 320
	});
	
	$(document).everyTime(500, function(i) {
		var position = $("#webcam").offset();
		$("#webcam-overlay").css({ 
			position: "absolute", 
			top: position.top, 
			left: position.left, 
			width: "320px",
			height: "240px",
			margin: 0,
			padding: 0,
			cursor: "pointer" 
		});
	});
	
	$("#webcam-overlay").click(function() {
		if(document.domain.indexOf("mywebcamcrush") != -1)
			window.location.replace('https://secure.mywebcamcrush.com/join.php?action=accept_invite');
		else if(document.domain.indexOf("webcamcrush") != -1)
			window.location.replace('http://secure.webcamcrush.com/join.php?action=accept_invite');
	});
	
	$("#chat-send-form").submit(function() {						   
		var msg = $("input#message").val();
		appendChatMessage('<font class="chatOutgoingName">Guest: </font>' + msg);
		sendAgeVerificationLink();
		return false;
	});

	sendInitialMessage();
});

function findChat() {
	var chatFrame = document.getElementById("chat-frame");
	var chatWindow, chatDocument;
	
	if(window.frames && window.frames["chat-frame"]) //IE5, Konq, Safari
		chatWindow = window.frames["chat-frame"];
	else if(chatContents.contentWindow) //IE5.5+, Moz 1.0+, Opera
		chatWindow = chatFrame.contentWindow;
	else //Moz < 0.9 (Netscape 6.0)
		chatWindow = chatFrame;
		
	if(chatWindow.document) //Moz 0.9+, Konq, Safari, IE, Opera
		chatDocument = chatWindow.document;
	else //Moz < 0.9 (Netscape 6.0)
		chatDocument = chatWindow.contentDocument;
		
	return [chatWindow, chatDocument];
}

function appendChatMessage( message ) {
	// find chat contents iframe/container div
	var chatContents = $("#chat-frame").contents().find('#contents');
	// find the chat box
	var chatBox = findChat();
	// create new div element and set the message
	var outputMessage = $("<div>", chatBox[1]).html(message).appendTo(chatContents);
	// scroll to new message
	$("#chat-frame").scrollTo( outputMessage, 500 );
	// clear message text
	$("#message").val("");
}

function sendInitialMessage() {
	$(document).oneTime(random_message_interval, function(i) {
		$('#chat-typing-status').html('<img src="/images/comment_edit.png" />Typing Status: Typing...');
		$(document).oneTime(random_message_interval, function(i) {
			$('#chat-typing-status').html('<img src="/images/comment_edit.png" />Typing Status: Idle...');
			appendChatMessage('<font class="chatIncomingName">' + splashpage_name  + ': </font>' + random_message_text);
		});
	});
}

function sendAgeVerificationLink() {
	$(document).oneTime(1000, function(i) {
		appendChatMessage('<hr><strong><font color=red>Error: </font></strong>You must complete age verification process to communicate with this member.  <a href="' + join_url +'/join.php?action=accept_invite" target="_parent">Click here to verify your age!</a><hr>');
	});
}
