/**...
	@author Dan Wasyluk / Snipt.org
	This script is the intellectual property of its owner and cannot
	be reused without express permission.
*/

function setSelectionRange(input, selectionStart, selectionEnd) {
	if (input.setSelectionRange) {
    	input.focus();
    	input.setSelectionRange(selectionStart, selectionEnd);
	}else if (input.createTextRange) {
		var range = input.createTextRange();
		range.collapse(true);
		range.moveEnd('character', selectionEnd);
		range.moveStart('character', selectionStart);
		range.select();
	}
}

function replaceSelection (input, replaceString) {
	if (input.setSelectionRange) {
		var selectionStart = input.selectionStart;
		var selectionEnd = input.selectionEnd;
		input.value = input.value.substring(0, selectionStart)+ replaceString + input.value.substring(selectionEnd);

    	if (selectionStart != selectionEnd){ 
			setSelectionRange(input, selectionStart, selectionStart + replaceString.length);
		}else{
			setSelectionRange(input, selectionStart + replaceString.length, selectionStart + replaceString.length);
		}
	}else if (document.selection) {

		var range = document.selection.createRange();
		if (range.parentElement() == input) {
			var isCollapsed = range.text == '';
			range.text = replaceString;

			if (!isCollapsed)  {
				range.moveStart('character', -replaceString.length);
				range.select();
			}
		}
	}
}

// We are going to catch the TAB key so that we can use it, Hooray!
//AND ENTER
//AND COUNT TABS
var tabCount = 0;
var itemEle;
var lineChars = 0;
function catchTab(item,e){
	itemEle = item;
	if(navigator.userAgent.match("Gecko")){
		c=e.which;
	}else{
		c=e.keyCode;
	}

	if(c==9){ //TAB
		replaceSelection(item,String.fromCharCode(9));
		setTimeout("document.getElementById('"+item.id+"').focus();",0);	
		//item.scrollTop = item.scrollHeight;
		if(lineChars==0) {
		tabCount++; }
		//return false;
	}else if(c==13) { //ENTER
		//lert(tabCount);
		for(var i=0;i<tabCount;i++) {
			setTimeout("addTabs()",(1*i));
		}
		
		lineChars = 0;
	}else if(c==8) { //DELETE
		if(lineChars==0 && tabCount > 0) {tabCount--};
		
		//alert(tabCount + " - " + lineChars);
	}else{
		lineChars++;
	}
}



function addTabs() {
	replaceSelection(itemEle,String.fromCharCode(9));
}




function getElementsByClassName(classname, node) {
	if(!node) node = document.getElementsByTagName("body")[0];
  	var a = [];
  	var re = new RegExp('\\b' + classname + '\\b');
   	var els = node.getElementsByTagName("*");
   	for(var i=0,j=els.length; i<j;i++)
   		if(re.test(els[i].className))a.push(els[i]);
   		return a;
}

function swapCheckbox() {
	
	var formHTML = "<a href=\"javascript:\" onclick=\"swapForm()\" class=\"twittXLink\"><div class=\"twittX\">X</div></a>&nbsp;Twitter:&nbsp<input type=\"text\" name=\"twittUser\" id=\"twittUser\" style=\"width:80px;\">&nbsp;Password:&nbsp;<input type=\"password\" name=\"twittPass\" id=\"twittPass\" style=\"width:80px;\">";
	
	document.getElementById("uForm").innerHTML = formHTML;
}

function swapForm() {
	
	var formHTML = "<input type=\"checkbox\" name=\"swap\" id=\"swap\" value=\"1\" style=\"width:auto\" onclick=\"swapCheckbox()\"> Add to my <a href=\"http://snipt.tumblr.com/post/75645820/new-features-sniptbox-and-private-snipts\">Snipt Box</a>&nbsp;";
	
	document.getElementById("uForm").innerHTML = formHTML;
}

function clearForm() {
	if(document.getElementById("codeSnippetTitle") != null) {
		document.getElementById("codeSnippetTitle").value = "";
		document.getElementById("codeSnippet").value = "";
		document.getElementById("private").checked = false;
		document.getElementById("swap").checked = false;
	}	
}

function showEmbed() {
	document.getElementById("embed").style.display = "none";
	document.getElementById("embedHTML").style.display = "inline-block";
}

function deletePrompt(key) {
	var theAnswer = confirm("Do you REALLY want to delete this snipt?");

		if (theAnswer){
				window.location = "http://snipt.org/delete/" + key;
		}else{
			return false;
		}
	return false;
}

function swapLogin() {
	document.getElementById("loginMsg").style.display = "none";
	document.getElementById("loginForm").style.display = "block";
}

function doLogin_cb(result) {
	//alert(result);
	if(result == false) {
		alert("Login failed, invalid credentials");
	}else{
		//document.getElementById("navuser").innerHTML = '<span style="color:#BBBBBB;">sniptbox:</span><a href="http://box.snipt.org/' + result + '">' + result + '</a>';
		window.location = window.location;
	}
}

function doLogin() {
	var user;
	var pass;
	
	user = document.getElementById("twitterUsername").value;
	pass = document.getElementById("twitterPassword").value;
	
	x_login(user, pass, doLogin_cb);
}

function showComments() {
	if(	document.getElementById("comments").style.display == "none") {
	document.getElementById("comments").style.display = "block";
	}else{
			document.getElementById("comments").style.display = "none";
		}
}

function maximize() {
	document.getElementById("accordian").style.width = "90%";
	document.getElementById("acc-header").style.backgroundColor = "#44a5fa";
	document.getElementById("acc-header").style.backgroundImage = "none";
	
	document.getElementById("edit_bar_max").innerHTML = '<a href="javascript:" onClick="minimize()" title="MINIMIZE window"><img src="images/magnifier__minus.png" style="border:0;background-color:transparent;margin:0;padding:0"></a>';
}


function minimize() {
	newImage = "url(http://www.snipt.org/wp-content/themes/motion/images/item_bg.jpg)";
	
	document.getElementById("accordian").style.width = "660px";
	document.getElementById("acc-header").style.backgroundColor = "transparent";
	document.getElementById("acc-header").style.backgroundImage = newImage;
	
	document.getElementById("edit_bar_max").innerHTML = '<a href="javascript:" onClick="maximize()" title="MAXIMIZE window"><img src="images/magnifier__arrow.png" style="border:0;background-color:transparent;margin:0;padding:0"></a>';
}

function fontPlus() {
	fontSize = document.getElementById("accordian_child_content").style.fontSize;
	fontSize = fontSize.substr(0, fontSize.length-2);
	fontSize ++;
	document.getElementById("accordian_child_content").style.fontSize = fontSize + "px";
}

function fontMinus() {
	fontSize = document.getElementById("accordian_child_content").style.fontSize;
	fontSize = fontSize.substr(0, fontSize.length-2);
	fontSize --;
	document.getElementById("accordian_child_content").style.fontSize = fontSize + "px";
}

function wrapText() {
	
	var allHTMLTags = new Array();

	//Create Array of All HTML Tags
	var allHTMLTags=document.getElementsByTagName("pre");

	//Loop through all tags using a for loop
	for (i=0; i<allHTMLTags.length; i++) {
		allHTMLTags[i].style.whiteSpace = 'pre-wrap';
		allHTMLTags[i].style.whiteSpace = '-moz-pre-wrap';
		allHTMLTags[i].style.whiteSpace = '-o-pre-wrap';
		allHTMLTags[i].style.breakWord = 'break-word';
	}
	document.getElementById("edit_bar_wrap").innerHTML = '<a href="javascript:" onClick="unwrapText()" title="DISABLE word wrap"><img src="images/wrapoff.png" style="border:0;background-color:transparent;margin:0;padding:0" title="DISABLE word wrap"></a>';
	
}

function unwrapText() {
	
	var allHTMLTags = new Array();

	//Create Array of All HTML Tags
	var allHTMLTags=document.getElementsByTagName("pre");

	//Loop through all tags using a for loop
	for (i=0; i<allHTMLTags.length; i++) {
		allHTMLTags[i].style.whiteSpace = 'pre';
		allHTMLTags[i].style.whiteSpace = '-moz-pre';
		allHTMLTags[i].style.whiteSpace = '-o-pre';
		allHTMLTags[i].style.breakWord = 'normal';
	}
	
	document.getElementById("edit_bar_wrap").innerHTML = '<a href="javascript:" onClick="wrapText()" title="ENABLE word wrap"><img src="images/wrapon.png" style="border:0;background-color:transparent;margin:0;padding:0" title="ENABLE word wrap"></a>';
}