var d = document;
var w = window;

w.onload	= function() {
	textareas_set_autoheight();
};

function textareas_set_autoheight(obj) {
	if( obj === undefined ) {
		var areas	= d.getElementsByTagName("TEXTAREA");
		for(var i=0; i<areas.length; i++) {
			if( areas[i].getAttribute("autoheight") != "true" ) { continue; }
			textareas_set_autoheight(areas[i]);
		}
		return;
	}
	if( !obj || !obj.nodeName || obj.nodeName!="TEXTAREA" ) { return; }
	if( obj.getAttribute("autoheight") != "true" ) { return; }
	if( obj.getAttribute("autoheight_is_set") == "true" ) { return; }
	if( !obj.id ) {
		obj.id	= "tmptxtarea_"+Math.round(Math.random()*10000);
	}
	var mn = 18, mx = 500;
	if( obj.style.height ) {
		mn	= Math.max(mn, parseInt(obj.style.height,10));
	}
	var func	= function(addnl) {
		setTimeout( function() {
			var dv	= d.createElement("DIV");
			dv.className	= obj.className;
			dv.style.fontSize		= "9pt";
			dv.style.padding		= "3px";
			dv.style.lineHeight	= "1.3";
			dv.style.width		= obj.clientWidth + "px";
			dv.style.overflow		= "auto";
			dv.style.whiteSpace	= "pre-wrap";
			dv.style.visibility	= "hidden";
			dv.style.display		= "block";
			dv.style.position		= "absolute";
			obj.parentNode.appendChild(dv);
			dv.innerHTML	= "";
			dv.appendChild(d.createTextNode(obj.value));
			if( addnl ) {
				if( obj.value=="" || obj.value=="\n" ) {
					dv.appendChild(d.createTextNode("\n"));
				}
				dv.appendChild(d.createTextNode("\n"));
			}
			var	h = parseInt(dv.clientHeight, 10);
			if( isNaN(h) ) { return; }
			if( h <= mn ) {
				h	= mn;
				obj.style.overflow	= "hidden";
			}
			else if( h >= mx ) {
				h	= mx;
				obj.style.overflow	= "auto";
			}
			else {
				obj.style.overflow	= "hidden";
			}
			obj.style.height	= (h - 6) + "px";
			dv.parentNode.removeChild(dv);
		}, 20);
	};
	obj.addEventListener("keyup", function() { func(false); }, false);
	obj.addEventListener("focus", function() { this.value = ltrim(trim(this.value)+"\n"); func(true); }, false);
	obj.addEventListener("blur", function() { this.value = trim(this.value); func(false); }, false);
	obj.setAttribute("autoheight_is_set", "true");
	setTimeout( function(){ func(false); }, 20 );
}

function get_nav()
{
	var nav	= {};
	nav.is_firefox	= false;
	nav.is_chrome	= false;
	nav.is_safari	= false;
	nav.is_opera	= false;
	nav.is_msie		= false;
	nav.ver	= 0;
	var ua	= navigator.userAgent.toLowerCase();
	var tmp;
	tmp	= ua.match(/firefox\/([0-9]+\.[0-9]+)/);
	if( tmp ) {
		nav.is_firefox	= true;
		nav.ver	= parseFloat(tmp[1]);
		return nav;
	}
	tmp	= ua.match(/chrome\/([0-9]+\.[0-9]+)/);
	if( tmp ) {
		nav.is_chrome	= true;
		nav.ver	= parseFloat(tmp[1]);
		return nav;
	}
	tmp	= ua.match(/msie\s([0-9]+\.[0-9]+)/);
	if( tmp ) {
		nav.is_msie	= true;
		nav.ver	= parseFloat(tmp[1]);
		return nav;
	}
	tmp	= ua.match(/opera/);
	if( tmp ) {
		nav.is_opera	= true;
		tmp	= ua.match(/version\/([0-9]+\.[0-9]+)/);
		if( tmp ) {
			nav.ver	= parseFloat(tmp[1]);
		}
		return nav;
	}
	tmp	= ua.match(/safari/);
	if( tmp ) {
		nav.is_safari	= true;
		tmp	= ua.match(/version\/([0-9]+\.[0-9]+)/);
		if( tmp ) {
			nav.ver	= parseFloat(tmp[1]);
		}
		return nav;
	}
	return nav;
}

function get_time()
{
	return parseInt( new Date().getTime().toString().substr(0, 10), 10 );
}

function get_time_ms()
{
	return new Date().getTime();
}

function confirmAndRedir(message, location)
{
	if( ! confirm(message) ) {
		return false;
	}
	w.location.href	= location;
}

function get_screen_scroll()
{
	var x=0, y=0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		x	= window.pageXOffset;
		y	= window.pageYOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		x	= document.body.scrollLeft;
		y	= document.body.scrollTop;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		y	= document.documentElement.scrollTop;
		x	= document.documentElement.scrollLeft;
	}
	return [x, y];
}

function get_screen_preview_size()
{
	var w=0, h=0;
	if( typeof( window.innerWidth ) == 'number' ) {
		w	= window.innerWidth;
		h	= window.innerHeight;
	}
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		w	= document.documentElement.clientWidth;
		h	= document.documentElement.clientHeight;
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		w	= document.body.clientWidth;
		h	= document.body.clientHeight;
	}
	return [w, h];
}

function find_coordinates(obj)
{
	var X=0, Y=0;
	if( obj.offsetParent ) {
		X =	obj.offsetLeft;
		Y =	obj.offsetTop;
		if( obj.offsetParent ) {
			do {
				obj = obj.offsetParent;
				X +=	obj.offsetLeft;
				Y +=	obj.offsetTop;
			}
			while( obj.offsetParent );
		}
	}
	return [X,Y];
}

function trim(txt)
{
	if( typeof(txt) != "string" ) { return txt; }
	txt	= txt.replace(/^\s+/, "");
	txt	= txt.replace(/\s+$/, "");
	return txt;
}

function ltrim(txt)
{
	if( typeof(txt) != "string" ) { return txt; }
	txt	= txt.replace(/^\s+/, "");
	return txt;
}

function rtrim(txt)
{
	if( typeof(txt) != "string" ) { return txt; }
	txt	= txt.replace(/\s+$/, "");
	return txt;
}

function obj_class_add(obj, cl)
{
	if( !obj ) { return false; }
	if( !obj.className ) { obj.className = ""; }
	var tmp	= obj.className.split(" ");
	if(cl in tmp) { return true; }
	tmp[tmp.length]	= cl;
	obj.className	= tmp.join(" ");
}

function obj_class_del(obj, cl)
{
	if( !obj ) { return false; }
	if( !obj.className ) { obj.className = ""; }
	var tmp	= obj.className.split(" ");
	for(var i=0; i<tmp.length; i++) {
		if(tmp[i]==cl || tmp[i]==="") { delete tmp[i]; }
	}
	obj.className	= tmp.join(" ");
}

function ajax_init(is_xml)
{
	var req = false;
	if (w.XMLHttpRequest) {
		req = new XMLHttpRequest();
		if (req.overrideMimeType) { req.overrideMimeType( is_xml ? "application/xml" : "text/plain" ); }
	} else if (w.ActiveXObject) {
		try { req = new w.ActiveXObject("MSXML3.XMLHTTP"); } catch(e) {
		try { req = new w.ActiveXObject("MSXML2.XMLHTTP.3.0"); } catch(e) {
		try { req = new w.ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {
		try { req = new w.ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {
		}}}}
	}
	if ( ! req ) {
		return false;
	}
	return req;
}
	
function in_array(sth, arr)
{
	if( sth===undefined || arr.constructor!=Array ) {
		return false;
	}
	for(var i=0; i<arr.length; i++) {
		if( arr[i] == sth ) {
			return true;
		}
	}
	return false;
}

function array_unique(arr)
{
	var tmp	= [];
	for(var i=0; i<arr.length; i++) {
		if( in_array(arr[i], tmp) ) {
			continue;
		}
		tmp[tmp.length]	= arr[i];
	}
	return tmp;
}

function array_shuffle(o)
{
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
}


function todo_add_task(get_params)
{
	var html	= '';
	var more_params = '';
	if(get_params) {
		more_params = get_params;
	}
	html	+= '<div style="background-color:#f2f2f2;">';
	html	+= '<iframe src="'+siteurl+'admin/tasks/add/?scr='+screen.width+'x'+screen.height+'&ref='+escape(w.location.href)+''+more_params+'" style="width:600px; height:460px; overflow-x: hidden;" border="0" frameborder="0" scrolling="auto"></iframe>';
	html	+= '</div>';
	flybox_open('"обави задача', html, false, 50);
	$(".flybox").draggable();
}
function createFlyBox(title, box_path, arr_vars, height) {
	if (height == undefined) height = '400';
	if (box_path) {
		if(!arr_vars) { arr_vars='' }
		var html = '';
		html += '<div style="background-color:#f2f2f2;">';
		html += '<iframe src="' + siteurl + '' + box_path + '?scr=' + screen.width + 'x' + screen.height + '&ref=' + escape(w.location.href) + ' ' + arr_vars + '" style="width:600px; height:' + height + 'px; overflow-x: hidden;" border="0" frameborder="0" scrolling="auto"></iframe>';
		html += '</div>';
		flybox_open(title, html, false, 50);
	} else {
		alert('Моля въведете път на flybox-a');
	}
}

function send_deal_link(deal_id){
	if(deal_id){
		var html = '';
		html	+= '<div style="background-color:#f2f2f2;">';
		html	+= '<iframe src="'+siteurl+'admin/deals/send/id:'+deal_id+'" style="width:600px; height:500px; overflow-x: hidden;" border="0" frameborder="0" scrolling="auto"></iframe>';
		html	+= '</div>';
		
		flybox_open('?зпрати линк към о"ертата', html, false, 50);
		$(".flybox").draggable();
	}
}




