$(document).ready(function () {
	
	
	
});

function iframe_get_text_offset_div(iframe, linenum, offset) {

	//alert(linenum + ":" + offset);
	//alert($(iframe).contents().find("body").html());
	//alert($(iframe).contents().find("body").text());
	
	// get the first span
	var target = $(iframe).contents().find("body").children().eq(0);
	
	// get to the start of the desired line
	while (linenum > 0) {
		linenum--;
		while (target.is("span")) {
			target = target.next();
		}
		target = target.next();
	}
	
	// find the span that contains the target text position, and update the offset to make it relative
	while(offset >= target.text().length && offset > 0 && target.next().length > 0) {
		offset -= target.text().length;
		target = target.next();
		//alert(offset + ": " + target.html());
	}
	
	//target.css({'border':'5px solid #FF0000'});

	// TODO: need to do something more with &nbsp; here?
	target.html(target.text().slice(0, offset) + "<span id='errorlocation' style='border:1px solid #FFFFFF; width:0px; height:9px; margin-left:-2px; '></span>" + target.text().slice(offset));
	
	return target.children("#errorlocation");
	
}

function iframe_get_text_coords(iframe, linenum, offset) {
	
	iframe_offset = $(iframe).offset();
	target_offset = iframe_get_text_offset_div(iframe, linenum, offset).offset();

	var coords = new Object();
	
	coords.top = target_offset.top + iframe_offset.top;
	coords.left = target_offset.left + iframe_offset.left;
	
	return coords;
	
}

function jslint_run(code, option) {
	var no_errors, errs;
	
	if (!option)
		option = new Object();
	
	option.white = option_strict_whitespace;
	
	no_errors = JSLINT(code, option);
	
	if (no_errors) return new Array();
	
	errs = JSLINT.errors;
	
	for (var i=0; i<errs.length; i++) {
		if (errs[i] == null || errs[i].raw=="") {
			errs.splice(i,1);
		}
	}
	
	return errs;
}

function filterJSLintError(errorobj) {
	
	switch (errorobj.raw) {
		case "Missing semicolon.":
			if (errorobj.line==0) return false; // to allow for simple expressions on the first line
			errorobj.character--;
			return errorobj;
		case "Unbegun comment.":
			return errorobj;
		case "Expected '{a}' and instead saw '{b}'.":
			if (errorobj.a=="{") {
				if (option_strict_block_braces) {
					errorobj.reason = "Expected '{' and instead saw '" + errorobj.b + "' (it's safest to use braces even for a single line of code).<br>(You can disable this type of error in the settings, if you're logged in.)"
					return errorobj;
				} else {
					return false;
				}
			}
			return errorobj;
		case "Expected a small integer and instead saw '{a}'.":
			return errorobj;		
		case "Unexpected early end of program.":
			return errorobj;		
		case "Expected an identifier and instead saw '{a}'.":
			return errorobj;		
		case "Expected an operator and instead saw '{a}'.":
			return errorobj;
		case "Bad assignment.":
			return errorobj;
		case "Unexpected '{a}'.":
			return errorobj;
		case "Missing '{a}'.":
			return errorobj;
		case "A regular expression literal can be confused with '/='.":
			return errorobj;
		case "Use the array literal notation [].":
			return errorobj;
		case "Expected to see a statement and instead saw a block.":
			return errorobj;
		case "Variable {a} was not declared correctly.":
			return errorobj;
		case "Each value should have its own case label.":
			return errorobj;
		case "Missing ':' on a case clause.":
			return errorobj;
		case "Missing ']' to match '[' from line {a}.":
			return errorobj;
		case "Unexpected comma.":
			return errorobj;
		case "A css file should begin with @charset \"UTF-8\";":
			return false;
		case "A dot following a number can be confused with a decimal point.":
			return errorobj;
		case "Unmatched '{a}'.":
			return errorobj;
		case "Expected '{a}' to match '{b}' from line {c} and instead saw '{d}'.":
			return errorobj;
		case "A leading decimal point can be confused with a dot: '.{a}'.":
			return errorobj;
		case "Expected an assignment or function call and instead saw an expression.":
			return false;
		case "Unexpected space after '{a}'.":
			return errorobj;
		case "Bad line breaking before '{a}'.":
			return errorobj;
		case "Expected '{a}' to have an indentation of {b} instead of {c}.":
			return errorobj;
		case "Line breaking error '{a}'.":
			return errorobj;
		case "Bad line breaking before '{a}'.":
			return errorobj;
		case "Unexpected use of '{a}'.":
			return errorobj;
		case "Expected an identifier in an assignment and instead saw a function invocation.":
			return errorobj;
		case "Missing name in function statement.":
			return errorobj;
		case "Inner functions should be listed at the top of the outer function.":
			return errorobj;
		case "Unreachable '{a}' after '{b}'.":
			return errorobj;
		case "Unnecessary semicolon.":
			return errorobj;
		case "Use '{a}' to compare with '{b}'.":
			return false;
		case "eval is evil.":
			errorobj.reason = "Please do not use 'eval'.";
			return errorobj;
		case "Extra comma.":
			return errorobj;
		case "Expected a conditional expression and instead saw an assignment.":
			return errorobj;
		case "Expected an identifier and instead saw '{a}'.":
			return errorobj;
		case "Expected a conditional expression and instead saw an assignment.":
			return errorobj;
		case "Expected a 'break' statement before 'case'.":
			return errorobj;
		case "This 'switch' should be an 'if'.":
			return errorobj;
		case "Bad for in variable '{a}'.":
			return errorobj;
	}
			
}
