//mySnippets JS Application file
_id=function(_id){return document.getElementById(_id);};

var colors = {
	'function ':'boldviol',
	'echo ':'boldlime',
	'date\\(':'boldgreen',
	'return ':'viol',
	'false':'red',
	'true':'boldgreen',
	'if ':'boldviol',
	'else':'boldviol',
	'switch':'boldviol',
	'array\\(':'boldgreen',
	'explode\\(':'boldgreen',
	'strlen':'boldlime',
	'substr':'boldlime',
	'move_uploaded_file':'boldlime',
	'mysql_query':'boldlime',
	'mysql_fetch_assoc':'boldlime',
	'mysql_fetch_array':'boldlime',
	'count\\(':'boldlime',
	'substr':'boldlime',
	'\\$_POST':'boldkaku',
	'\\$_GET':'boldkaku',
	'\\$_SESSION':'boldkaku',
	'\\$_COOKIE':'boldkaku'
}

function prepareWebsite() {
	_id('query').focus();
	$('#logotype').fadeIn(750);
	$('#footer').fadeIn(1000);
}

function typeSnippet(str) {
	clearInterval(document._to);
	if (str.length>0 && str!=' ') {
	_id('snippet_container').innerHTML = '<div class="loader"></div>';
	_id('add_snippet').style.display = 'none';
	_id('loader').style.display = 'block';
	document._to=setTimeout(function(){searchSnippet(str);},500);	
	
	}else{
		_id('snippet_container').innerHTML = '<strong>Search field has been clear.</strong><br />Please type a keyword to search for a snippet.';
		_id('loader').style.display = 'none';
		_id('add_snippet').style.display = 'block';
	}
}

function searchSnippet(str) {
	$.get (
		'search.php?q='+str, function(data) {
			_id('loader').style.display = 'none';
			_id('add_snippet').style.display = 'block';
			_id('snippet_container').style.display = 'none';
	
			for (var temp in colors) {
				data=data.replace(new RegExp(temp,'g'),'<span class="tag_'+colors[temp]+'">'+temp.replace('\\','')+'</span>')
			}
	  		_id('snippet_container').innerHTML = data;
	  		$('#snippet_container').fadeIn('slow');
		});
	

}

function clipBoard(theSel) {
	$.copy(_id(theSel).innerHTML);
	
}

function switchPage(str) {
	_id('loader').style.display = 'none';
	_id('query').value = '';
	_id('snippet_container').innerHTML = '<div class="loader"></div>';
	$.get(str+'.php', function(data) {
		_id('snippet_container').style.display = 'none';
  		_id('snippet_container').innerHTML = data;
  		$('#snippet_container').fadeIn('slow');
	});
	
}

function submitSnippetNew() {
	_id('loader').style.display = 'none';
	
	$.post('add.php', {
		authCode: _id('authCodex').value,
		snipTitle: _id('snipTitle').value,
		snipKey: _id('snipKey').value,
		snipCode: _id('snipCode').value
	}, function(data){
		_id('snippet_container').style.display = 'none';
  		_id('snippet_container').innerHTML = data;
  		$('#snippet_container').fadeIn('slow');
	});
	
}

function goTop() {
	$('html,body').animate({scrollTop: $('#topsite').offset().top}, 800);
}

function stripslashes(str) {
str=str.replace('\\','')
return str;
}

