var dragObject;
var mouseOffset;
var ctrlPress = false;
var scroll_action = null;

var mouseX;
var mouseY;

var dlib="dlibs/dlib.lib.php";
var cache = {};

function isCached(parms) {
	var key="";
	for (var parm in parms) key+=parm+parms[parm];
	
	if(cache[key]) return cache[key];
	else return false;
}

function toCache(parms, data) {
	var key="";
	for (var parm in parms) key+=parm+parms[parm];
	cache[key]=data;
}

function $$(id) {
	return document.getElementById(id);
}

function get(id){return $$(id)}

function timeOut() {
	window.setTimeout("history.back(-1);", 2000);
}

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

function ScrollAction(action) {
	scroll_action=action;
}

function ScrollTo(obj) {
	el=$$(obj);
	el_pos=el.offsetTop;
	sc_pos=document.body.scrollTop;
	
	if(sc_pos > el_pos) {
		new_pos=sc_pos-scroll_step;
		if(new_pos < el_pos) {
			new_pos=el_pos;
			if(scroll_action != null) eval("setTimeout(\""+scroll_action+"\", 200);");
			scroll_action=null;
		}
		
		window.scroll(0, new_pos);
		
		setTimeout("ScrollTo('"+obj+"')", 10);
	} else {
		if(scroll_action != null) eval("setTimeout('"+scroll_action+"', 200);");
		scroll_action=null;
	}
}

function go(url) {
	location.href=url;
}

function progress() {
	return "<p align=center><img src='images/progress.gif' width=120 height=8></p>";
}

function keyUp(e) {
	var key = e? e.which: window.event.keyCode;
	if(key == 17) ctrlPress=false;
}

function keyDown(e) {
	e = fixEvent(e);
	
	var key = e? e.which: window.event.keyCode;
	if(key == 17) ctrlPress=true;

	if(ctrlPress && key == 69) {
		if (document.getSelection) {
			var str = document.getSelection();
		} else if (document.selection && document.selection.createRange) {
			var range = document.selection.createRange();
			var str = range.text;
		}
		showFloat(e,350,'div:errorReport','Сообщить об ошибке в тексте');
		$$('errorText').value=str;
	}
}

function mouseSpy(e){
    e = e || window.event 
  
    if (e.pageX == null && e.clientX != null ) {  
        var html = document.documentElement 
        var body = document.body 
      
        e.pageX = e.clientX + (html && html.scrollLeft || body && body.scrollLeft || 0) - (html.clientLeft || 0) 
        e.pageY = e.clientY + (html && html.scrollTop || body && body.scrollTop || 0) - (html.clientTop || 0) 
    }
    mouseX = e.pageX 
    mouseY = e.pageY 
}

function fixEvent(e) {
    e = e || window.event
 
    if ( e.pageX == null && e.clientX != null ) {
        var html = document.documentElement
        var body = document.body
        
        e.pageX = e.clientX + (html && html.scrollLeft || body && body.scrollLeft || 0) - (html.clientLeft || 0)
        e.pageY = e.clientY + (html && html.scrollTop || body && body.scrollTop || 0) - (html.clientTop || 0)
    }
 
    if (!e.which && e.button) {
        e.which = e.button & 1 ? 1 : ( e.button & 2 ? 3 : ( e.button & 4 ? 2 : 0 ) )
    }
    
    return e
}

function getMouseOffset(target, e) {
	var left = 0
    var top  = 0
 
    left += target.offsetLeft
    top  += target.offsetTop
    
	return {x:e.pageX - left, y:e.pageY - top}
}

function mouseUp(){
	dragObject = null

	$(document).unbind('mousemove');
	$(document).unbind('mouseup');
	$(document).mousemove(mouseSpy);
	
	document.ondragstart = null;
	document.body.onselectstart = null;
}

function mouseMove(e){
	e = fixEvent(e)

	with(dragObject.style) {
		position = 'absolute'
		top = e.pageY - mouseOffset.y + 'px'
		left = e.pageX - mouseOffset.x + 'px'
	}
	return false
}

function mouseDown(e) {
	mouseOffset = getMouseOffset(dragObject, e)

	$(document).mousemove(mouseMove);
	$(document).mouseup(mouseUp);
	
	if($.browser.opera) {
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
	
	return false
}

function showFloat(e, width, content, title) {
	if(!width) width=300;
	
	if(content) {
		$("#floatRes").empty();
		$("#floatRes").css({width:width});
		
		var tmp=content.split(":");
		if(tmp.length > 2) {
			var new_array=[];
			for(var i=1;i<tmp.length;i++) new_array[i-1]=tmp[i];
			tmp[1]=new_array.join(":");
		}
		if(tmp[0] == "ajax") {
			$("#floatRes").append(progress());
			
			var parms=tmp[1].split("&");
			var parmsObj={};
			
			for(var i=0;i<parms.length;i++) {
				var para=parms[i].split("=");
				parmsObj[para[0]]=para[1]
			}
			$("#floatRes").load(dlib, parmsObj);
		} else if(tmp[0] == "text") {
			$("#floatRes").append("<div style='margin: 10px; text-align: center' class='normal'>"+tmp[1]+"</div>");
			$("#floatRes").append("<div style='margin: 10 0 15 0; text-align: center'><input type='button' onclick='hideFloat()' value='&nbsp;&nbsp;OK&nbsp;&nbsp;' class='button'></div>");
		} else {
			var insText=$("#"+tmp[1]).html();
			insText=insText.replace("<!--", "");
			insText=insText.replace("-->", "");
			
			$("#floatRes").append(insText);
		}
	}
	
	if(!e) {
		$("#floatContainer").css({width: width});
		$("#floatContainer").alignCenter();
	} else {
		e = fixEvent(e);

		nX=e.pageX;
		nY=e.pageY;

		if(nX == 0 || nY == 0 || !nX || !nY) {
			nX=mouseX;
			nY=mouseY;
		}

		$("#floatContainer").css({width: width,top:nY+15,left:nX+15});
	}
	
	$("#floatTitle").empty();
	$("#floatTitle").append(title);
	$("#floatContainer").fadeIn("fast");
}

function showError(text) {
	showFloat(false, 350, "text:"+text, "Сообщение об ошибке");
}

function hideFloat() {
	$("#floatRes").empty();
	$("#floatContainer").hide();
}

$(document).ready(function() {
	$("#floatClose").click(function() {$("#floatContainer").fadeOut("slow")})
	
	$("#floatControler").mousedown(function(e){
		dragObject=$$("floatContainer");
		mouseDown(e);
	});
	
	$.fn.alignCenter = function() {
		var marginLeft = Math.max(40, parseInt($(window).width()/2 - $(this).width()/2));
		var marginTop = Math.max(40, parseInt($(window).height()/2 - $(this).height()/2));
		
		var html = document.documentElement 
        var body = document.body 
        
		marginTop += (html && html.scrollTop || body && body.scrollTop || 0) - (html.clientTop || 0);
		
		return $(this).css({'left':marginLeft, 'top':marginTop});
	};
});

function in_array(needle, haystack, strict) {
	var found = false, key, strict = !!strict;

	for (key in haystack) {
		if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
			found = true;
			break;
		}
	}

	return found;
}

function implode( glue, pieces ) {
	return ( ( pieces instanceof Array ) ? pieces.join ( glue ) : pieces );
}

$(document).mousemove(mouseSpy);
$(document).keyup(keyUp);
$(document).keydown(keyDown);

if (document.layers) {
	document.captureEvents(Event.KEYUP);
	document.captureEvents(Event.KEYDOWN);
}
