/*
* alert which print objects
* @require: jQuery
* 
* @param  mixed  mixed     value to print. can be value or object
* @param  bool   truncate  truncate long strings     (default = 100, 0 for unlimited)
* @param  bool   recursive use recursion for objects (default = false)
* @params int    depth     depth for recursion (default = 5)
* 
* $alert use 1 popup window and do not stop the javascript as usual alert() function. 
* if you want to stop javascript use $alert_stop_mode( true );  before $alert().
* this function will generate an javascript error and JS will be stoped.
*/
function $alert( mixed, truncate, recursive, depth ){
	if( truncate == null ) truncate = 200;
	if( depth == null ) depth = 5;

	if( !$('#customalertpopup').get(0) ){
		// append popup source
		var html = '';
		html += '<div id="customalertpopup"><div class="calpd_bg">';
		html += '<div class="calpd_header"><h2>alert</h2><a href="#close" class="close">close</a></div>';
		html += '<div class="calpd_content"></div>';
		html += '</div></div>';
		html += '<style type="text/css">#customalertpopup div.calpd_content p{margin:0;}</style>';
		$(document.body).append(html);
		//add styles
		var $popup = $('#customalertpopup');
		$popup.css({'border':'1px solid #000', 'background':'#fff', 'position':'absolute', 'top':20, 'left':30, 'display':'none','z-index':9999});
		$popup.find('div.calpd_bg').css({'background':'#ddd', 'border':'3px solid #fff', 'padding':'0 7px 7px'});
		$popup.find('div.calpd_header').css({'height':25, 'overflow':'hidden'});
		$popup.find('div.calpd_header h2').css({'color':'#000','float':'left','font-size':'12px','line-height':'25px','padding':'0 20px 0 0','margin':'0'});
		$popup.find('div.calpd_header a.close').css({'background':'url(http://brainfart.com.ua/bg-close.gif) no-repeat 0 0','text-indent':'-9999px','float':'right','display':'block','width':'14px','height':'14px','margin':'5px 2px 0 0','overflow':'hidden'});
		$popup.find('div.calpd_content').css({'background':'#fff','border':'1px solid #bbb','color':'#000','padding':'10px','font':'11px/14px "Courier New",Verdana,sans-serif', 'overflow':'auto'});
		
		// init events
		$popup.find('div.calpd_header a.close').click(function(){
			$('#customalertpopup div.calpd_content').html('');
			$('#customalertpopup').hide();
			return false;
		});
	}
	
	function $alertPopup( $html, lines ){
		var $popup = $('#customalertpopup');
		var $content = $('#customalertpopup div.calpd_content');

		$content.html( $html );
		if (lines == null || lines < 30){
			$content.css('height', 'auto');
		}else{
			$content.css('height', 420);
		}

		$popup.css('visibility', 'hidden');
		$popup.show();
		$popup.css({
			top: $(window).scrollTop() + ( Math.max($(window).height() - $popup.height(), 40) / 2),
			left: ($(document).width() - $popup.width()) / 2,
			widht: ($(window).width() - 200) / 2
		});
		$popup.css('visibility', 'visible');
		
		// kill javasctipt by producing an error if kill is enabled
		if($alertstopmode == 'kill') abc = abc;
	}
	
	var html = '';
	
	// get html for usual var (not object)
	if(!mixed || typeof(mixed) != 'object') {
		html += '<p>';
		var vartype = (mixed == 'null')? 'null' : typeof(mixed); 
		if( vartype == 'undefined' || vartype == 'null') {
			html += "["+vartype+"]";
		} else {
			html += "[" + vartype + "] => `" + mixed + "`";
		}
		html += '</p>';
		$alertPopup( html ); return;
	}
	
	// get object html
	var offset = 0; var lines = 1;
	function startline(ol){ return '<p style="padding-left:'+(ol*15)+'px;">'; };
	function endline(){return '</p>';}
	
	function objecthtml( mixed, offset, recursive, depth ){
		if(offset + 1 == depth) 
			return startline(offset+1) + 'max depth limit exsided' + endline();
			
		var html = '';
		if(offset == 0)
			html += startline(offset) + mixed + ' => ' + typeof(mixed) + ' {' + endline();
		
		offset++;
		try{
			for( var index in mixed ) {
				try{
				var vartype = typeof( mixed[index] );
				var value = mixed[index] + "";
				
				// get recursive properties
				if ( recursive && vartype == 'object' ){
					html += startline(offset) + '[' + index + '] => (' + vartype + ') { ' + endline();
					var recursivehtml = objecthtml(mixed[index], offset, recursive, depth);
					var matchlines = recursivehtml.match(/<p>/g);
					html += recursivehtml;
					html += startline(offset) + '}' + endline();
					lines += matchlines.length + 2; 
				}else{
					if ( index == 'innerHTML' || index == 'text' || index == 'textContent'){ 
						value = value.replace(/\</g, '&lt;').replace(/\>/g, '&gt;').replace(/\n/g, '<br />').replace(/\s\s/, '&nbsp;').replace(/\t/, '&nbsp;&nbsp;');
					}
					if ( truncate > 0 && value.length > truncate){
						value = value.substr(0, truncate) + ' ...';
					}
					html += startline(offset) + '[' + index + '] => (' + vartype + ') `' + value + '`' + endline();
				}
				lines ++;
				}catch(err){}
			}
		}
		catch(err){ 
			//html += startline(offset)+"can't get properties for " + mixed + " on index [" + index + "]" + endline();
		}
		
		if(offset == 1)
			html += startline(--offset) + "}" + endline();
			
		return html;
	}
	
	var html = objecthtml(mixed, 0, recursive, depth);
	
	$alertPopup( html, lines ); return;
}

var $alertstopmode = 'continue';
function $alert_stop_mode( mode ){
	if(mode == true)
		$alertstopmode = 'kill';
	else
		$alertstopmode = 'continue';
}
