// document ready
$(document).ready(function(){
	disableSpellcheck("body"); 	// disables the spellcheck in most Browsers. Chrome sucks.
	slideFakeSelect(			// animates a fake select dropdown list
		"div.pullparent",			// wrapper around option elements
		"div.pulldown",				// options list
		"div.select",				// element causing the slide-effect on hover
		"#language");				// hidden input field that will be updated
//	selectHTML();				// selects HTML for copy&paste
//	disableWhitespaceWrap();	// does not seem to work with ready event
	removeFakeSelectBG();		// this is only for images=off scenario
	toggleOptions();			// fading in advanced options
	validateForm();				// check form client-side
});

// functions
function implode( glue, pieces ) {  
	return ( ( pieces instanceof Array ) ? pieces.join ( glue ) : pieces );  
}  

function validateForm() {
	$("#code2css").submit(function(){
		var validated = true;
		var yourcode = $("#yourcode").getValue();
		var language = $("#language").getValue();
		var errmsg = "";
		
		if(yourcode.length < 1) {
			validated = false;
			errmsg = errormsg + "\n\n" + "Sorry, I can't turn air into markup.";
		}
		if (language === 'none') {
			validated = false;
			errmsg = errormsg + "\n\n" + "Please tell the language of your code.";
		}
		
		if (validated) {
			$(this).submit();
		} else {
			alert(errormsg);
			return false;
		}
	});
}

function toggleOptions() {
	$("#advanced_opts").show().css({right:"-400px",opacity:"0"});
	$("#toggleopts").toggle(
		function(){
			$("#advanced_opts").css({opacity:"0"});
			$("#advanced_opts").animate({right:"80px",opacity:"1"},700,"easeOutExpo");
			$("div.rightcol div.textarea_wrap").animate({opacity:".5"},1000,"easeOutExpo",function(){
				$("#toggleopts a").text("Hide Advanced Options");
				return false;
			});
		},
		function(){
			$("div.rightcol div.textarea_wrap").animate({opacity:"1"},1000,"easeInExpo");
			$("#advanced_opts").animate({right:"-400px",opacity:"0"},600,"easeInExpo",function(){
				$("#toggleopts a").text("Show Advanced Options");
				//$(this).css();
				return false;
			});
		}
	);
}

function removeFakeSelectBG() {
	$("div.input_innerwrap").css("backgroundColor","transparent");
}

function disableSpellcheck (elm) {
	$(elm).attr("spellcheck",false);
}

function disableWhitespaceWrap() {
	$("textarea").attr("wrap","off"); 
}

function selectHTML() {
	// $("#yourhtml").select(); (optional - early selection on page load)
	$("#yourhtml").focus(function(){
		$(this).select();
	});
}

function slideFakeSelect(optionswrapper,options,selectbox,hiddenfield) {
	// dependency: Easing Plugin
	// dependency: Field Plugin
	$(selectbox+" a").click(function(){
		var langval = $(this).attr("class");
		var langname = $(this).text();
		$(hiddenfield).setValue(langval);
		//console.info($(this).attr("class"));
		$(selectbox+" a.select").attr("href","?l="+langval);
		$(selectbox+" a.select").attr("class","select").addClass(langval);
		$(selectbox+" a.select strong").text(langname);
		$(options).animate({top:'-500px'},400,'easeInExpo').css({bottom:'0'}).parent("div");
		$(optionswrapper).animate({height:'0'},400,'easeInExpo');
		return false;
	});
	$(optionswrapper).css({height:'0'});
	var pulldownheight = $(options).height() + 24;
	//console.log(pulldownheight);
	$(selectbox).hoverIntent({
		sensitivity: 10,
		interval: 100,
		over: function(){
			$(optionswrapper).animate({height:pulldownheight+'px'},500,'easeOutExpo');
			$(options).animate({top:'0'},500,'easeOutExpo');
		},
		timeout: 500,
		out: function(){
			$(options).animate({top:'-500px'},400,'easeInExpo').css({bottom:'0'}).parent("div");
			$(optionswrapper).animate({height:'0'},400,'easeInExpo');
		}
	});
}