function init(){
	if($("#btnEmbed").hasClass('embedded')){
		return;
		//if the page is embedded, we don't need any of this!
	}
	$('#scriptRadio').change(updateCode);
	$('#iframeRadio').change(updateCode);
	$('#widthType').change(updateCode);
	$('#heightType').change(updateCode);
	$('#roomname').blur(updateCode);
	$('#width').blur(updateCode);
	$('#height').blur(updateCode);
	$('#txtCopy').focus(updateCode);
	$('#txtCopy').focus(triggerSelection);
	$('#txtCopy').click(triggerSelection);
	$('#embedbody').click(closeOverlay);
	$('#btnEmbed').click(openOverlay);
	$('#btnClose').click(closeOverlay);
	updateCode();
	selectCopy();
	
	testURL();
}

function testURL(){
	var str = window.location.href;
	if(str.indexOf("#showEmbed") != -1){
		openOverlay();
	}
}

function openOverlay(){
	$('#embedbody').fadeIn(800);
	$('#embedder').delay(300).fadeIn(300);
	$('#btnEmbed').animate({marginTop:"20px"},300);
}

function closeOverlay(){
	$('#embedbody').fadeOut(800);
	$('#embedder').fadeOut(300);
	$('#btnEmbed').animate({marginTop:"-47px"},300);
	window.location.href = "#";
}

function getType(){
	return $('input:radio[name=embedType]:checked').val();
}

function getCode(){
	var type = getType();
	if(type=="script"){
		return script;
	}
	else{
		return iframe;
	}
}

function validateInputs(){
	//roomname
	var roomname = removeInvalidChars($("#roomname").val(), validRoomChars);
	$("#roomname").val(roomname);
	
	//width
	var width = $("#width").val();
	width = replace(',','.',width);
	$("#width").val(width);
	if(!isNumber(width)){
		if($("#widthType").val()=="px"){
			$("#width").val("800");
		}else{
			$("#width").val("100");
		}
	}
	
	//height
	var height = $("#height").val();
	height = replace(',','.',height);
	$("#height").val(height);
	if(!isNumber(height)){
		if($("#heightType").val()=="px"){
			$("#height").val("500");
		}else{
			$("#height").val("100");
		}
	}
}

function isNumber(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

function removeInvalidChars(subject,valids){
	var result = "";
	for(var i=0;i<subject.length;i++){
		var current = subject.substr(i,1);
		if(valids.indexOf(current)!=-1){
			result += current;
		}
	}
	return result;
}

function replace(search,replace,subject){
	var result = subject.split(search).join(replace);
	return result;
}

function getWidthType(){
	var type = $('#widthType').val();
	if(type== "px"){
		return "px";
	}	
	else {
		return "%";
	}
}

function getHeightType(){
	var type = $('#heightType').val();
	if(type== "px"){
		return "px";
	}	
	else {
		return "%";
	}
}
function triggerSelection(){
	setTimeout("selectCopy()",80);
}
function selectCopy(){
	$('#txtCopy').select();
}

function updateCode(){
	validateInputs();
	var code = getCode();
	var type="";
	var room = $("#roomname").val();
	
	var width = $("#width").val();	
	var widthType = getWidthType();
	
	var height = $("#height").val();
	var heightType = getHeightType();
	
	if(widthType != "px"){
		width += "%"
	}
	if(heightType != "px"){
		height += "%"
	}
	
	code = replace("%ROOM%",room,code);
	code = replace("%WIDTH%",width,code);
	code = replace("%HEIGHT%",height,code);
	
	$('#txtCopy').val(code);
}


//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
///////////////////////////start app//////////////////////////////
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////


var iframe = '<iframe src=\"http://spreadtheword.nocreativity.com/?embed&room=%ROOM%&width=%WIDTH%&height=%HEIGHT%\" width=\"%WIDTH%\" height=\"%HEIGHT%\" frameborder=\"0\" webkitAllowFullScreen allowFullScreen></iframe>';
var script = '<script type="text/javascript" src="http://spreadtheword.nocreativity.com/api/embed.php?width=%WIDTH%&height=%HEIGHT%&room=%ROOM%"></script>';
var validRoomChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
var validNumbers = "1234567890";
$(document).ready(init);


