function Rozbalit(id, display)
{
	if(display == undefined) display = 'block';
	if(document.getElementById(id).style.display == 'none')
	{
		document.getElementById(id).style.display = display;
		document.getElementById(id).style.overflow = 'hidden';
		document.getElementById(id).style.height = '1%';
		return false;
		DownInterval(id);
	}
	else
	{
		document.getElementById(id).style.display = 'none';
	}
	document.getElementById(id).style.overflow = 'none';
}

var down_interval;
var down_percent = 0;
function DownInterval(id)
{
	if(self.down_interval == undefined) self.down_interval = window.setInterval('DownInterval()', 1000);
	if(self.down_percent == 100)
	{
		window.clearInterval(self.down_interval);
		self.down_interval = undefined;
		self.down_percent = 0;
	}
	else
	{
		self.down_percent++;
		document.getElementById(id).style.height = self.down_percent + '%';
	}
}

function IE6HoverIn(o, classa)
{
	if (!window.XMLHttpRequest)	o.className=classa;
}
function IE6HoverOut(o)
{
	if (!window.XMLHttpRequest)	o.className="";
}

function SetIdes(id, key)
{
	ides = Explode('-', readCookie('ides['+key+']'));
	if (document.getElementById('multi-'+id).checked)
	{
		if (!InArray(id, ides)) ides[ides.length] = id;
	}
	else
	{
		if (InArray(id, ides))
		{
			for(i in ides)
		 	{
		 		if (ides[i] == id) ides[i] = undefined;
		 	}
		}
	}

	eraseCookie('ides['+key+']');
	createCookie('ides['+key+']', Implode('-', ides), 1);
}

function Explode(cchar, str)
{
	var new_arr = new Array();
	if(str != undefined)
	{
		var key = 0;
		for (i = 0; i < str.length; i++)
		{
			s = str.substr(i, cchar.length);
			if (s == cchar)
			{
				key++;
			}
			else
			{
				if (new_arr[key] == undefined) new_arr[key] = '';
				new_arr[key] += s.substr(0, 1);
			}
		}
	}
	return new_arr;
}

function Implode(cchar, arr)
{
	var str = '';
	var first_cycle = true;
	for(i in arr)
	{
		if(arr[i] != undefined)
		{
			if(first_cycle)
			{
				first_cycle = false;
			}
			else
			{
				str += cchar;
			}
			str += arr[i];
		}
	}
	return str;
}

function InArray(value, arr)
{
	for(i in arr)
	{
		if (arr[i] == value) return true;
	}
	return false;
}

function RemoveFromArray(arr_remove, arr)
{
	for(i in arr_remove)
	{
		if(!InArray(arr_remove[i], arr))
		{
			for(j in arr)
			{
				if(arr[j] == arr_remove[i]) arr[j] = undefined;
			}
		}
	}
	var new_arr = new Array();
	var j = 0;
	for(i in arr)
	{
		if(arr[i] != undefined) new_arr[j++] = arr[i];
	}
	return new_arr;
}

function FirstToSecond(arr1, arr2)
{
	var c = arr2.length;
	for(i in arr1)
	{
		if(InArray(arr1[i], arr2))
		{
			for(j in arr2)
			{
				if(arr1[i] == arr2[j]) arr2[j] = arr1[i];
			}
		}
		else
		{
			arr2[c++] = arr1[i];
		}
	}
	return arr2;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function InitSlider(id)
{
	$(id + " .SlideHeading").click(function(){
		$(id + " .SlideHeading a").click(function(){
			location.replace(this.href);
			return false;
		});
		$(id + " .SlidePanel").slideToggle("fast", function(){
			$($(id + " .SlideHeading")).toggleClass("Active");
		});
		return false;
	});
};

var Slided = new Array();
function InitHSlider(id)
{
	$(id + " .SlidePanel").hover(function(){HSliderShow(id);}, function(){HSliderHide(id);});
}

function HSliderShow(id)
{
	if (!Slided[id] && !readCookie('SidePanel'))
	{
		$(id + " .SlidePanel").animate({
			left: 0
		}, 500, function(){Slided[id] = true;});
	}
}

function HSliderHide(id)
{
	if (Slided[id] && !readCookie('SidePanel'))
	{
		$(id + " .SlidePanel").animate({
			left: -$(id + " .SlidePanel").outerWidth() + 14
		}, 500, function(){Slided[id] = false;});
	}
}

$(document).ready(function(){
	$(".Pin").click(function(){
		if ($("#Slider .SlidePanel").css('position') == 'absolute')
		{
			createCookie('SidePanel', 1, 100);
			$("#Slider .Pin div").attr('class', 'Dock');
			$("#Slider .SlidePanel").css('position', 'static');
			$("#Page").css('margin-left', '280px');
		}
		else
		{
			eraseCookie('SidePanel');
			$("#Slider .SlidePanel").css('left', 0);
			$("#Slider .Pin div").attr('class', 'Float');
			$("#Slider .SlidePanel").css('position', 'absolute');
			$("#Page").css('margin-left', '14px');
		}
	});
});

var JavaScripts = new Array();
function IncludeJs(file, forced)
{
	if (forced == undefined) forced = false;
	if (!InArray(file, JavaScripts) || forced)
	{
		$('head').append('<script type="text/javascript" src="' + webroot + file + '"></script>');
		JavaScripts[JavaScripts.length] = file;
	}
}

var CSSs = new Array();
function IncludeCSS(file)
{
	if (!InArray(file, CSSs))
	{
		$('head').append('<link rel="stylesheet" type="text/css" href="' + webroot + file + '" />');
		CSSs[CSSs.length] = file;
	}
}

function RemoveJs(file)
{
	$('script[src]').each(function(){
		console.log($(this).attr('src'));
	});
	$('script[src="' + webroot + file + '"]').remove();
}

function InArray(value, array)
{
	for (i in array)
	{
		if (array[i] == value) return true;
	}

	return false;
}

function GetKeyArray(value, array)
{
	for (i in array)
	{
		if (array[i] == value) return i;
	}

	return false;
}

function CloneJson(orig_json)
{
	var json = {};

	$.each(orig_json, function(key, value){
		json[key] = value;
	});

	return json;
}

function OneNumberToTwoNumbers(number)
{
	if (number < 10) return '0' + number.toString();
	return number;
}

$(document).ready(function(){
	$('a.ToNewWindow').click(function(){
		location.replace($(this).attr('href'));
		return false;
	});

	$('table th:first').addClass('First');
	$('table td:first').addClass('First');
	$('table th:last').addClass('Last');
	$('table td:last').addClass('Last');
	$('table th:even').addClass('Even');
	$('table tr:first').addClass('First');
	$('table tr:last').addClass('Last');
});

function ShowDialog(element)
{
	var div = $('<div class="Dialog"></div>');
	var outdiv = $('<div class="Out"></div>');
	var indiv = $('<div class="In"></div>');
	indiv.html(element);
	indiv.children().show();
	outdiv.html(indiv);
	div.html('<div class="Background"></div>');
	div.append(outdiv);
	$('body').append(div);

	SetDialogDimensions(div);

	div.children('.Background').click(function(){div.remove();});

	return div;
}

function SetDialogDimensions(dialog)
{
	var outdiv = dialog.children('.Out');
	//var el = outdiv = dialog.children('.Out').children('.In').children('div:first');

	if (outdiv.width() > GetWindowWidth())
	{
		outdiv.width(GetWindowWidth() - 50);
	}
	if (outdiv.height() > GetWindowHeight())
	{
		outdiv.height(GetWindowHeight() - 50);
	}

	outdiv.css('top', ((GetWindowHeight() - outdiv.height()) / 2) + 'px');
	outdiv.css('left', ((GetWindowWidth() - outdiv.width()) / 2) + 'px');
}

function GetWindowHeight()
{
	if (window.innerHeight == undefined || window.innerHeight == null || !window.innerHeight) return $(window).height();
	return window.innerHeight;
}

function GetWindowWidth()
{
	if (window.innerWidth == undefined || window.innerWidth == null || !window.innerWidth) return $(window).width();
	return window.innerWidth;
}

function GetInputsValuesByClass(cl)
{
	var values = {};
	$('.' + cl).each(function(){
		if ($(this).attr('type') == 'checkbox')
		{
			var val = $(this).attr('checked') ? '1' : '0';
		}
		else
		{
			var val = $(this).val();
		}
		values[$(this).attr('name')] = val;
	});
	return values;
}
