var global_style = new Array();
var global_index = 0;

/**
 * 
 * @param {String} css_file
 * return {Boolean}
 */
function set_style(css_file)
{
    css_file = '/styles/geshi/'+css_file;
    
	if (in_array(css_file, global_style) == false)
    {
		global_style[global_index++] = css_file;
        return true;
    }
    else
    {
        return false;
    }
}

function get_styles()
{
 	var output = new String();
	
	/* global styles */
	for (var i = 0; i < global_index; i++)
	{
		output += '@import url("'+global_style[i]+'");'+"\n";
	}

	return output;
}

function in_array(needle, haystack)
{
	for (var i = 0; i < haystack.length; i++)
	{
		if (needle == haystack[i])
			return true;
	}
	return false;
}

/**
 * @desc implementing own trim() functin to Strnig object
 */
String.prototype.trim = function()
{
    return this.replace(/^\s+|\s+$/, '');
};

/**
 * @desc grab url and focus it
 */
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();
  }
}

/**
 * @desc email post to custom email adress
 */
function email_post(ID, LANG)
{
    var fcode  = document.getElementById(ID).innerHTML;
    var code   = ((document.getElementById('code')) ? document.getElementById('code').innerHTML : false);
    var desc   = ((document.getElementById('desc')) ? document.getElementById('desc').innerHTML : '');
    var styles = get_styles();
    var pastebin_header = document.getElementById('pastebin_header').innerHTML;
    var from   = getCookie('email');
    var URL    = document.getElementById('URL').innerHTML;
    var email_console;
    
    
    email_console = window.open("","email_post","width=750,height=600,resizable,scrollbars=yes");
    email_console.document.writeln('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
 	email_console.document.writeln('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">');
 		email_console.document.writeln('<head>');        
			email_console.document.writeln('<title>'+pastebin_header+' - sending post by email</title>');
		    email_console.document.writeln('<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />');
			email_console.document.writeln('<meta http-equiv="Content-Style-Type" content="text/css" />');
		    email_console.document.writeln('<meta name="author" content="char0n, lordProtector - member of binary groop" />');
			email_console.document.writeln('<meta name="keywords" content="print, mortality, pastebin, code, bin" />');
			email_console.document.writeln('<meta name="description" content="Sending post by email" />');
		    email_console.document.writeln('<meta name="generator" content="my brain, hands, ZEND IDE" />');
            // IE hack making browser not fall into recursive loop while loading external javascripts
            email_console.document.writeln('<script type="text/javascript" language="Javascript">');            
                email_console.document.writeln('window.onload = import_scripts;');
                email_console.document.writeln('function import_scripts()');
                email_console.document.writeln('{');
                    email_console.document.writeln('var mybic=document.createElement("script")');
                    email_console.document.writeln('var email=document.createElement("script")');
                    email_console.document.writeln('var common=document.createElement("script")');
                    email_console.document.writeln('document.getElementsByTagName("head")[0].appendChild(mybic)');
                    email_console.document.writeln('document.getElementsByTagName("head")[0].appendChild(email)');
                    email_console.document.writeln('document.getElementsByTagName("head")[0].appendChild(common)');
                    email_console.document.writeln('mybic.setAttribute("type", "text/javascript", 0);');
                    email_console.document.writeln('mybic.setAttribute("src", "/libs/javascript/my-bic/mybic.js", 0);'); 
                    email_console.document.writeln('mybic.setAttribute("language", "Javascript", 0);'); 
                    email_console.document.writeln('email.setAttribute("type", "text/javascript", 0);');
                    email_console.document.writeln('email.setAttribute("src", "/libs/javascript/pastebin_email.js", 0);'); 
                    email_console.document.writeln('email.setAttribute("language", "Javascript", 0);'); 
                    email_console.document.writeln('common.setAttribute("type", "text/javascript", 0);');
                    email_console.document.writeln('common.setAttribute("src", "/libs/javascript/common.js", 0);'); 
                    email_console.document.writeln('common.setAttribute("language", "Javascript", 0);'); 
                email_console.document.writeln('}');
            email_console.document.writeln('</script>');
            // end of IE hack            
            email_console.document.writeln('<style type="text/css" media="screen">');            
                email_console.document.writeln(styles);
                email_console.document.writeln('body, table { font-family: Arial, Helvetica, serif; font-size: 12px; background: white }');
                email_console.document.writeln('#fcode { background: black }');
                email_console.document.writeln('form { margin: 20px; text-align: center; }');
                email_console.document.writeln('.important { color: red; }');
                email_console.document.writeln('.red { color: red; background: rgb(255, 221, 221);}');
                email_console.document.writeln('.blue { color: blue; background: rgb(211, 231, 255);}');
            email_console.document.writeln('</style>');
        email_console.document.writeln('</head>');
        email_console.document.writeln('<body>');
   	  		email_console.document.writeln('<h2 style="text-align: center">'+pastebin_header+'</h2>');
            email_console.document.writeln('<form id="URL" name="URL" action="/">'+URL+'</form>');
            email_console.document.writeln('<div id="fcode">'+fcode+'</div>');
            email_console.document.writeln('<form id="email_post" name="email_post" action="post" name="add_child">');
            // geting styles as hidden form elements
            for (var i = 0; i < global_style.length; i++)
            {
                email_console.document.writeln('<input type="hidden" name="styles[]" value="'+global_style[i]+'" />');
            }
                if (code)
                {
                    email_console.document.writeln('<p><b>'+_general_email_serv_code+'</b></p>');
                    email_console.document.writeln('<textarea name="code" cols="80" rows="10">'+code+'</textarea>');
                }
                else
                {
                    email_console.document.writeln('<textarea style="display: none" name="code" cols="1" rows="1">inactvie</textarea>');
                }
                email_console.document.writeln('<p><b>'+_general_email_serv_desc+'</b></p>');
                email_console.document.writeln('<textarea name="desc" cols="65" rows="4">'+desc+'</textarea>');
                email_console.document.writeln('<p><u>'+_general_email_serv_send_as+'</u>');
                email_console.document.writeln('<input type="radio" name="format" value="html" checked="checked" /><b>'+_general_email_serv_html+'</b>');
                if (code)
                {
                    email_console.document.writeln('<input type="radio" name="format" value="text" /><b>'+_general_email_serv_text+'</b>');
                    email_console.document.writeln('<input type="radio" name="format" value="both" /><b>'+_general_email_serv_both+'</b></p>');
                }
                email_console.document.writeln('<table align="center">');
                    email_console.document.writeln('<tr><td></td><td><img id="status" style="display: none" src="/images/loading.gif" width="126" height="22" alt="Sending Post" /></td></tr>');
                    email_console.document.writeln('<tr><td><span class="important">*</span> <b>'+_general_email_serv_to+':</b></td><td><input type="text" name="to" maxlength="160" /></td></tr>');
                    email_console.document.writeln('<tr><td><b>'+_general_email_serv_from+':</b></td><td><input type="text" name="from" value="'+from+'" maxlength="160" /></td></tr>');
                email_console.document.writeln('</table>');
                email_console.document.writeln('<input type="hidden" name="LANG" value="'+LANG+'" />');
                email_console.document.writeln('<input type="button" value="'+_general_email_serv_button+'" onclick="email_send();" />');
            email_console.document.writeln('</form>');
        email_console.document.writeln('</body>');
    email_console.document.writeln('</html>');
    email_console.document.close();        
}

function getCookie(c_name)
{
    if (document.cookie.length>0)
    {
        c_start=document.cookie.indexOf(c_name + "=")
        if (c_start!=-1)
        { 
            c_start=c_start + c_name.length+1 
            c_end=document.cookie.indexOf(";",c_start)
            if (c_end==-1) c_end=document.cookie.length
                return unescape(document.cookie.substring(c_start,c_end))
        } 
    }
    return "";
}

function post_redirect(post_id)
{
	window.location.href = post_id;
}

/**
 * switching language routine
 */
function switch_lang()
{
	var language_switch = document.forms.language_switch.lang;
	
	
	
	var source_lang = window.location.href;
	source_lang = source_lang.match(/^http:\/\/[^\/]+\/([a-z]{2})\//, '');
	source_lang = '/'+source_lang[1]+'/';
	
	var target_lang = '/'+language_switch.options[language_switch.selectedIndex].value+'/';

	var target_location = window.location.href;
	target_location = target_location.replace(source_lang, target_lang);
	
	window.location.href = target_location;
}
function mark_active_language()
{
	var used_lang = window.location.href;
	used_lang = used_lang.match(/^http:\/\/[^\/]+\/([a-z]{2})\//, '');
	used_lang = used_lang[1];

	var language_switch = document.forms.language_switch.lang;
	for (var i = 0; i < language_switch.options.length; i++)
	{
		if (language_switch.options[i].value == used_lang)
		{
			language_switch.options[i].selected = true;
			return;
		}
	}
}