$(function(){
	$('#hardcore_version').click(function(){
		$this = $(this);
		html = $this.html();
		if($this.css('position')!='absolute') {
			w = $this.css('width');
			h = $this.css('height');
			mv = $this.css('max-width');
			$this.css('position','fixed').css('max-width',5000).animate({'top':0,'left':0,'width':'100%','height':'100%'},200).html($this.html().replace(' hardcore version ▉',''));
			$('div textarea',$this).show().focus();
		}
		hc();
		pages = callPages();
	});

var isCtrl = false;

$(document).keyup(function (e) {
	if(e.which == 17) isCtrl=false;
}).keydown(function (e) {
	if(e.which == 17) isCtrl=true;
	if(e.which == 68 && isCtrl == true) {
		exit();
		return false;
	}
	else if(e.which == 67 && isCtrl == true) {
		$('#prog').val('');
		return false;
	}
});

});

var callPages =  function () {
    pages = [];
    $('.menu a').each(function(){
	url = $(this).attr('href').split('/');
	pages.push(url[url.length-1]);
    });
    return pages;
}

function function_exists( function_name ) {
    if (typeof function_name == 'string'){
        return (typeof window[function_name] == 'function');
    } else{
        return (function_name instanceof Function);
    }
}


Array.prototype.in_array = function(p_val) {
	for(var i = 0, l = this.length; i < l; i++) {
		if(this[i] == p_val) {
			return true;
		}
	}
	return false;
}

var hc = function () {
    $('#prog').keyup(function(k) {
	if(k.keyCode==13) {
	    v = $(this).val().replace("\n", '');
	    v = v.split(' ');
	    command = v[0];
	    subj = v[1];
	    $this = $(this);
	    if(function_exists(command) || command == '?') {
		if(pages.in_array(subj) || command == 'help' || command == '?' || command == 'exit' || (command == 'ls' && (typeof(subj) == 'undefined' || subj==''))) {
			$this.val('');
			if(command == '?') {
			    command = 'help';
			}
			eval(command+'("'+subj+'")');

		}
		else {
		    $this.val('Unknown resource');
		    a = setInterval(function() {
			$this.val('');
			clearTimeout(a);
		    }, 500);
		}
	    }
	    else if ($this.val()=="\n") {
		$this.before("<br />:~# ").val('');
	    }
	    else {
		$this.before($this.val().replace(/\n/g,"<br />"));
		$this = $(this);
		$this.before("-bash: "+$this.val()+": command not found<br />:~# ");
		$this.val('');
	    }


	    return false;
	}
    });
}

var cat = function(subj) {
    if(pages.in_array(subj)) {
	$.ajax({
	    url: '/'+subj+'/',
	    success: function (data) {
		text = $(data).text();
		$('#prog').before("cat "+subj+"<br />"+text.replace(/[(\n)|\r|\t]{2,}/g,"\1")+"<br />:~# ");
	    }
	});
    }
}

var exit = function () {
    $('#prog').val('');
    $('#hardcore_version').css('position','relative').css('max-width',mv).animate({'top':0,'left':0,'width':w,'height':h},200).html(html);
}

var ls = function() {
    $('#prog').before("ls<br />"+pages.join("<br />")+"<br />:~# ");
}

var help = function() {
    $('#prog').before("help<br />ls - list of the avialable pages<br />cat <page name> - show page<br />exit - exit hardcore vercion<br />:~# ");
}
$(document).ready(function() {
	$('.project a').piroBox({
			my_speed: 300, //animation speed
			bg_alpha: 0.5, //background opacity
			radius: 4, //caption rounded corner
			close_all : '.piro_close'// add class .piro_overlay(with comma)if you want overlay click close piroBox
	});
});

//
//var load_file = function(type) {
//	return function() {
//		$('#'+type+'_btn').click(function(){
//			$(this).after('<br /><span class="green">Размер файла не должен превышать 50 Кб.</span>');
//			$(this).hide();
//			$('#opapa'+type).val('tarampampam');
//			$('#'+type+',#'+type+'_sub').css('visibility','visible').css('position', 'relative');
//			$('#'+type+'_resume').show().css('width','100%');
//			return true;
//		});
//	}
//}
