﻿
/* определение браузера */

function $Browser()
{
    this.isOpera = false;
    this.isFirefox = false;
    this.isIE = false;
    this.isMozilla = false;
    this.version = '';
}
var ua = navigator.userAgent.toLowerCase();
if (ua.indexOf('opera') != -1) { $Browser.isOpera = true; } 
else if (ua.indexOf('firefox') != -1) { $Browser.isFirefox = true; } 
else if (ua.indexOf('msie') != -1) { $Browser.isIE = true; } 
else if (ua.indexOf('mozilla') != -1) { $Browser.isMozilla = true; }

/* бирки */
function lmLabelHandler(i,l) {
	i.onfocus = function() {
		l.className='lmLabelHide';
	}
	i.onblur = function() {
		if(this.value == '') {
			l.className='lmLabelShow';
		}
	}
}
function lmLabel(id) {
	if(document.getElementById && document.getElementById(id)) {
		var obj=document.getElementById(id);
		var newlabel=document.createElement("label");
		newlabel.className = 'lmLabelShow';
		newlabel.htmlFor = id;
		obj.parentNode.insertBefore(newlabel,obj);
		newlabel.innerHTML=obj.getAttribute("label");
		newlabel.style.lineHeight = ((obj.offsetHeight))+"px";
		lmLabelHandler (obj,newlabel);
	}
}

/* Options */
var lmoptions = {}
lmoptions.animation = {
	speed: 40, // скорость анимации
	duration: 600 //длительность анимации
}

/* Core */

function $(element)
{
    return typeof element == 'object' ? element : document.getElementById(element);
}

function $$(element)
{
    return new $Element(element);
}

function $Element (element)
{
    element = $(element);
    for (var property in this) {
        element[property] = this[property];
    }
    return element;
}

/* изменение видимости элемента */

$Element.prototype.hide = function ()
{
    this.style.display = 'none';
}
$Element.prototype.show = function ()
{
    switch (this.tagName) {
        case 'TR': this.style.display = $Browser.isIE ? 'block' : 'table-row'; break;
        case 'TD': this.style.display = $Browser.isIE ? 'block' : 'table-cell'; break;
        case 'SAPN' : this.style.display = 'inline'; break;
        default: this.style.display = 'block'; break;
    }
}
$Element.prototype.toggle = function ()
{
    if (this.style.display != 'block') {
        this.show();
    } else {
        this.style.display = 'none';
    }
}

/* эффекты  */
/* slide */
$Element.prototype.slideIn = function (afterfunction)
{
	var obj=this;
	if(obj.offsetHeight) {
		var speed=lmoptions.animation.speed;
		var duration = lmoptions.animation.duration;
		var start = obj.offsetHeight;
		var process  = start;
		var end = 0;
		var step = Math.round((end-start)/(duration/speed));
		obj.style.overflow = "hidden";
		$animate(function(){
			process += step;
			if(process>0){
				obj.style.height = process;
			} else {
				obj.style.height = 0;
				obj.style.display = "none";
				obj.style.overflow = "";
				if(afterfunction && typeof afterfunction == 'function') { afterfunction(); }
				return true; //stop;
			}
		},speed,duration);
	}
}
$Element.prototype.slideOut = function (afterfunction)
{
	var obj=this;
	if(!obj.offsetHeight){
		var speed=lmoptions.animation.speed;
		var duration = lmoptions.animation.duration;
		var process = 0;
		var start = 0;
		var tmp_position = obj.style.position||'';
		var tmp_left = obj.style.left||'';
		obj.style.position = 'absolute';
		obj.style.left = -9999;
		obj.style.height="";
		obj.style.display="block";
		end = obj.offsetHeight;
		obj.style.overflow = "hidden";
		obj.style.height=1;
		obj.style.position = tmp_position;
		obj.style.left = tmp_left;
		step = Math.round((end-start)/(duration/speed));
		$animate(function(){
			process += step;
			if(process>0 && process<end) {
				obj.style.height = process;
			} else {
				obj.style.height = "";
				obj.style.display = "block";
				obj.style.overflow = "";
				if(afterfunction && typeof afterfunction == 'function') { afterfunction(); }
				return true; //stop;
			}
		},speed,duration);
	}
}
$Element.prototype.slideToggle = function (afterfunction)
{
	if(this.offsetHeight){
		this.slideIn(afterfunction);
	} else {
		this.slideOut(afterfunction);
	}
}
/* fade */
$Element.prototype.fadeIn = function (afterfunction)
{
	var obj=this;
	var dothis = false;
	if($Browser.isIE) {
		if(!obj.filters.alpha || (obj.filters.alpha!=null && obj.filters.alpha.opacity>0))
			dothis=true;
	} else {
		if(!obj.style.opacity || (obj.style.opacity && obj.style.opacity>0))
		dothis=true;
	}
	if(dothis) {
		var speed=lmoptions.animation.speed;
		var duration = lmoptions.animation.duration;
		var start = 100;
		var process  = start;
		var end = 0;
		var step = Math.round((end-start)/(duration/speed));
		$animate(function(){
			process += step;
			if(process>0){
				obj.style.filter = 'alpha(opacity:'+process+')';
				obj.style.opacity = process/100;
			} else {
				obj.style.filter = 'alpha(opacity:0)';
				obj.style.opacity = 0;
				if(typeof afterfunction == 'function') { afterfunction(); }
				return true; //stop;
			}
		},speed,duration);
	}
}
$Element.prototype.fadeOut = function (afterfunction)
{
	var obj=this;
	var dothis = false;
	if($Browser.isIE) {
		if(obj.filters.alpha && obj.filters.alpha.opacity!=null && obj.filters.alpha.opacity<100)
			dothis=true;
	} else {
		if((obj.style.opacity && obj.style.opacity<100))
			dothis=true;
	}
	if(dothis) {
		var speed=lmoptions.animation.speed;
		var duration = lmoptions.animation.duration;
		var start = ($Browser.isIE)?obj.filters.alpha.opacity:obj.style.opacity;
		var process  = start;
		var end = 100;
		var step = Math.round((end-start)/(duration/speed));
		$animate(function(){
			process += step;
			if(process>0 && process<end){
				obj.style.filter = 'alpha(opacity:'+process+')';
				obj.style.opacity = process/100;
			} else {
				obj.style.filter = 'alpha(opacity:100)';
				obj.style.opacity = 100;
				if(afterfunction && typeof afterfunction == 'function') { afterfunction(); }
				return true; //stop;
			}
		},speed,duration);
	}
}
$Element.prototype.fadeToggle = function (afterfunction){
	if(($Browser.isIE && this.filters.alpha && this.filters.alpha.opacity!=null && this.filters.alpha.opacity<100) || (!$Browser.isIE && this.style.opacity && this.style.opacity<100)) {
		this.fadeOut(afterfunction);
	} else {
		this.fadeIn(afterfunction);
	}

}

/**/
$Element.prototype.explain = function ()
{
    var debugInfo = '';
    for (var property in this) {
        debugInfo += property + ' = ' + this[property] + '<br/>';
    }
    document.write(debugInfo);
}

/* переключение значения поля ввода */

$Element.prototype.toggleValue = function (event, str)
{
    var element = event.target ? event.target : event.srcElement;
    if (element.value == str && event.type == 'focus') {
        element.value = '';
    }
    if (element.value == '' && event.type == 'blur') {
        element.value = str;
    }
}

/* переход между элементами */

$Element.prototype.next = function() {
    var n = this;
    do n = n.nextSibling;
    while (n && n.nodeType != 1);
    return $$(n);
}
$Element.prototype.prev = function () {
    var p = this;
    do p = p.previousSibling;
    while (p && p.nodeType != 1);
    return $$(p);
}

/* операции над обработчиками событий */

$Element.prototype.addEvent = function (eventType, eventHandler)
{
    if ($Browser.isIE) {
        this.attachEvent('on' + eventType, eventHandler);
        return true;
    } else {
        this.addEventListener(eventType, eventHandler, false);
        return true;
    }
}
$Element.prototype.removeEvent = function (eventType, eventHandler)
{
    if ($Browser.isIE) {
		this.detachEvent('on' + eventType, eventHandler);
        return true;
    } else {
		this.removeEventListener(eventType, eventHandler, false);
        return true;
    }
}

/* документ внутри iframe */

$Element.prototype.getIFrameDocument = function ()
{
    if (this.tagName == "IFRAME") {
        return (this.document) ? this.document : this.contentDocument;
    }
}

/* определение положения элемента */

$Element.prototype.getX = function ()
{
	var x = 0;
    var obj = this;
	while(obj){
		x += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return x;
}
$Element.prototype.getY = function ()
{
	var y = 0;
    var obj = this;
	while(obj){
		y += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return y;
}

/* операции над css классами */

$Element.prototype.hasClass = function (c2)
{
    var c1 = this.className.toLowerCase() || '';
    c2 = c2.toLowerCase();
    if (c1 == c2) { return true; } //один класс
    else if(c1.indexOf(c2+' ') == 0) { return true; } //в начале
    else if(c1.indexOf(' '+c2+' ') != -1) { return true; } //в середине
    else if(c1.indexOf(' '+c2) == (c1.length - c2.length-1)) { return true; } //в конце
    return false;
}
$Element.prototype.addClass = function (c2)
{
    if (!this.hasClass(c2)) {
        this.className = this.className == '' ? c2 : this.className + ' ' + c2;
    }
}
$Element.prototype.removeClass = function (c2)
{
    if (this.hasClass(c2)) {
        var r1 = new RegExp("(^|\\s+)("+c2+")(\\s+|$)", "gi");
        this.className = this.className == c2 ? '' : this.className.replace(r1, "$1"+"$3");
    }
}
$Element.prototype.toggleClass = function (c1, c2)
{
    if (this.hasClass(c1)) {
        this.removeClass(c1);
        this.addClass(c2);
    } else {
        this.addClass(c1);
        this.removeClass(c2);
    }
}

/* AJAX */

function $AjaxGetObject ()
{
    var httpRequestObject = false;
    if (!httpRequestObject) try { httpRequestObject = new ActiveXObject("MSXML2.XMLHTTP.3.0"); } catch (e) {}
    if (!httpRequestObject) try { httpRequestObject = new ActiveXObject('Msxml2.XMLHTTP'); } catch (e){}
    if (!httpRequestObject) try { httpRequestObject = new ActiveXObject('Microsoft.XMLHTTP'); } catch (e){}
    if (!httpRequestObject) try { httpRequestObject = new XMLHttpRequest(); } catch (e){}
    if (!httpRequestObject)
        return false;
    else
        return httpRequestObject;
}
function $AjaxGetUrl (url)
{
    return $AjaxSyncQuery(url);
}

function $AjaxSyncQuery (url)
{
    var obj = $AjaxGetObject();
    obj.open('GET', url, false);
    obj.send(null);
    return obj.responseText;
}

function $AjaxAsyncQuery (url, onLoading, onLoaded, onError)
{
    var obj = $AjaxGetObject();
    obj.onreadystatechange = function () { $AjaxAsyncLoaded (obj, onLoaded, onError) };
    obj.open('GET', url, true);
    onLoading();
    obj.send(null);
}

function $AjaxAsyncLoaded (AjaxObject, onLoaded, onError)
{
    if (AjaxObject.readyState == 4) {
        if (AjaxObject.status == 200) {
			onLoaded(AjaxObject.responseText);
        } else {
            onError();
        }
    }
}

/* редирект */

function $go (url)
{
    document.location.href = url;
}

function $animate(f,s,d){
	var iterations = Math.ceil(d/s)+1;
	var iteration = 0;
	var t = setInterval(function(){
		var stop = f();
		iteration++;
		if(stop || iteration==iterations){
			clearInterval(t);
			t = null;
		}
	},s);
}

/* Drag & Drop */

var $DragObject = function ()
{
    this.startX = 0;
    this.startY = 0;
    this.elementX = 0;
    this.elementY = 0;
    this.element = '';
}
function $DoDrag (event)
{
    element = $($DragObject.element);
    var pageSize = $getPageSize();
    newX = $DragObject.elementX + (event.clientX - $DragObject.startX);
    if (newX  > pageSize.pageWidth - $DragObject.element.offsetWidth) {
        newX = pageSize.pageWidth - $DragObject.element.offsetWidth;
    }
    if (newX < 0) {
        newX = 0;
    }
    newX += 'px';
    newY = $DragObject.elementY + (event.clientY - $DragObject.startY);
    if (newY  > pageSize.pageHeight - $DragObject.element.offsetHeight) {
        newY = pageSize.pageHeight - $DragObject.element.offsetHeight;
    }
    if (newY < 0) {
        newY = 0;
    }
    newY += 'px';
    element.style.left = newX;
    element.style.top = newY;
    if ($Browser.isIE) {
        if ($($DragObject.element.id + 'IeSelectFix')) {
            var IeSelectFixIframe = $($DragObject.element.id + 'IeSelectFix');
            IeSelectFixIframe.style.left = newX;
            IeSelectFixIframe.style.top = newY;
        }
    }
}
function $StopDrag ()
{
    $$(document.body).removeEvent('mousemove', $DoDrag);
    $$(document.body).removeEvent('mouseup', $StopDrag);
    element = $($DragObject.element);
    $DragObject.element = '';
    //control.style.cursor = 'default';
    document.body.onselectstart = '';
}
function $StartDrag (event)
{
    var element = event.srcElement ? event.srcElement : event.target;
    //element = element.parentNode;
    $DragObject.element = element;
    element = $(element);
    $DragObject.startX = event.clientX;
    $DragObject.startY = event.clientY;
    //control.style.position = 'absolute';
    //control.style.cursor = 'move';
    var curX = $$(element).getX();
    var curY = $$(element).getY();
    element.style.left = curX+'px';
    element.style.top = curY+'px';
    $DragObject.elementX = curX;
    $DragObject.elementY = curY;
    $$(document.body).addEvent('mousemove', $DoDrag);
    $$(document.body).addEvent('mouseup', $StopDrag);
    document.body.onselectstart = function () { return false; }
}

/* определение размера страницы */

function $getPageScroll ()
{
	var yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	// Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {	// all other Explorers
		yScroll = document.body.scrollTop;
	}
	arrayPageScroll = new Array('',yScroll)
	return arrayPageScroll;
}

function $getPageSize ()
{
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight-4;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	//arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	//return arrayPageSize;
    return {"pageWidth":pageWidth,"pageHeight":pageHeight,"windowWidth":windowWidth,"windowHeight":windowHeight};
}