var tabsort = {};
var fkill = true;
var scroll_in_progress = false;
var resort_prv = 0;

function resort(el,blk,nums,arrs)
{
	var x = el;
	if (!blk) blk = 1;
	while(x.tagName != "TD" && x.tagName != "TH" && x.parentNode) x = x.parentNode;
	if (x.tagName == "TD" || x.tagName == "TH") var sort_cell = x.cellIndex;
	else return false;
	while(x.tagName != "TABLE" && x.parentNode) x = x.parentNode;
	if (x.tagName != "TABLE") return false;

	if (!x.id) x.id = "_rand" + Math.random();

	if (!tabsort[x.id]) tabsort[x.id] = isNaN(parseInt(x.getAttribute("defsort"))) ? 1 : parseInt(x.getAttribute("defsort"));

	if (tabsort[x.id] == sort_cell + 1)
	{
		var sort_asc = false;
		tabsort[x.id] = -1 - sort_cell;
	}
	else
	{
		var sort_asc = true;
		tabsort[x.id] = sort_cell + 1;
	}

var idx = 1;
var i = 1 + blk;
while(i < x.rows.length)
{
	if (i > 1 + blk) idx = i;
	if (x.rows[i].getAttribute("sect")) i += blk;

	for ( ; i < x.rows.length; i += blk)
	{
		if (x.rows[i].getAttribute("sect")) break;
		if (x.rows[i].getAttribute("skip")) continue;
		for (var j = i; j > idx; j -= blk)
		{
			if (x.rows[j].getAttribute("skip")) continue;
			if (x.rows[j - blk].getAttribute("skip")) continue;
			var a = x.rows[j].cells[sort_cell].innerText ? x.rows[j].cells[sort_cell].innerText : x.rows[j].cells[sort_cell].textContent;
			var b = x.rows[j - blk].cells[sort_cell].innerText ? x.rows[j - blk].cells[sort_cell].innerText : x.rows[j - blk].cells[sort_cell].textContent;
			if (nums)
			{
				a = parseInt(a);
				b = parseInt(b);
				if (isNaN(a)) a = 0;
				if (isNaN(b)) b = 0;
			}
			if ((sort_asc && a < b) || (!sort_asc && a > b))
			{
				for (k = 0; k < blk; k++)
				{
					for (var l = 0; l < x.rows[j].cells.length; l++)
					{
						var tmp = x.rows[j + k].cells[l];
						if (tmp)
						{
							tmp = tmp.innerHTML;
							x.rows[j + k].cells[l].innerHTML = x.rows[j + k - blk].cells[l].innerHTML;
							x.rows[j + k - blk].cells[l].innerHTML = tmp;
						}
					}
				}
			}
			else break;
		}
	}
}

	x.rows[0].cells[resort_prv].childNodes[1].className = "";
	x.rows[0].cells[sort_cell].childNodes[1].className = "a";
	x.rows[0].cells[sort_cell].firstChild.src = (sort_asc ? arrs && arrs[0] ? arrs[0] : "/i/ad.gif" : arrs && arrs[1] ? arrs[1] : "/i/ac.gif");
	resort_prv = sort_cell;

	return false;
}

$(document).ready(function(){
	checkWideScreen();
	bind_events();
	$(window).resize(function(){
		checkWideScreen();
	});
});

function bind_events()
{
	$("#button_n > a").bind("click", function(){
		
//		unbind_events();
		if (scroll_in_progress) return false;
		scroll_in_progress = true;
		
		tmp = $('#scroll_wrapper').find('.pic_block');
		ws = ifScreenIsWide();
//		if (tmp.get(ws ? 4 : 3).getAttribute("is_first") != '1')
		{
			hide = $('#scroll_wrapper > .pic_block:first');
			hide.clone(true).appendTo("#scroll_wrapper");
			hide = $('#scroll_wrapper > .pic_block:first');
			hide.animate({
				marginLeft: '-124px'
			},	function(){
				hide.remove();
				scroll_in_progress = false;
//				bind_events()
			}
			);

//			hide.remove();
//alert($("#scroll_wrapper").html());
		}
//		else
		{
//			bind_events();
		}
	
		return false;
	});
	$("#button_p > a").bind("click", function(){
		
//		unbind_events();
		if (scroll_in_progress) return false;
		scroll_in_progress = true;
		
		hide = $('#scroll_wrapper > .pic_block:last');
		tmp = $('#scroll_wrapper > .pic_block:first');
//		if (tmp.attr("is_first") != '1')
		{
			hide.clone(true).css({marginLeft: '-123px'}).prependTo("#scroll_wrapper");
			show = $('#scroll_wrapper > .pic_block:first');		
				
			show.animate({
				marginLeft: '0px'
			},function(){
				hide.remove();
				scroll_in_progress = false;
//				bind_events()
			}
			);
		}
//		else
		{
//			bind_events();
		}
	
		return false;
	});
}

function unbind_events()
{
	$("#button_n > a").unbind('click');
	$("#button_p > a").unbind('click');
	
	$("#button_p > a").bind('click',function(){

		return false;
	});
	
	$("#button_n > a").bind('click',function(){
		return false;
	});
}

function checkWideScreen()
{
/*	if (ifScreenIsWide())
	{
		wideScreen();
	}
	else
	{
		narrowScreen();
	}
*/
	sizeScreen();
}

function wideScreen()
{
	var ws = $('#scroll_wrapper').find('.pic_block').length;
	if (1 || ws > 3)
	{
		$("#scroll").removeClass('scroll');
		$("#scroll").addClass('scroll2');

		$("#scroll_wrapper").removeClass('scroll_wrapper');
		$("#scroll_wrapper").addClass('scroll_wrapper2');

		$("#scroll_bg").removeClass('scroll_bg_class');
		$("#scroll_bg").addClass('scroll_bg_class2');
	}
	if (ws > 4)
	{
		$("#button_p").removeClass('button_prev2');
		$("#button_p").addClass('button_prev');

		$("#button_n").removeClass('button_next2');
		$("#button_n").addClass('button_next');

		$("#button_p a:first").css({display: "block"});
		$("#button_n a:first").css({display: "block"});
	}
	else
	{
		$("#button_p").removeClass('button_prev');
		$("#button_p").addClass('button_prev2');

		$("#button_n").removeClass('button_next');
		$("#button_n").addClass('button_next2');

		$("#button_p a:first").css({display: "none"});
		$("#button_n a:first").css({display: "none"});
	}
}

function narrowScreen()
{
	var ws = $('#scroll_wrapper').find('.pic_block').length;

	$("#scroll").removeClass('scroll2');
	$("#scroll").addClass('scroll');

	$("#scroll_wrapper").removeClass('scroll_wrapper2');
	$("#scroll_wrapper").addClass('scroll_wrapper');

	$("#scroll_bg").removeClass('scroll_bg_class2');
	$("#scroll_bg").addClass('scroll_bg_class');

	if (ws > 3)
	{
		$("#button_p").removeClass('button_prev2');
		$("#button_p").addClass('button_prev');

		$("#button_n").removeClass('button_next2');
		$("#button_n").addClass('button_next');

		$("#button_p a:first").css({display: "block"});
		$("#button_n a:first").css({display: "block"});
	}
	else
	{
		$("#button_p").removeClass('button_prev');
		$("#button_p").addClass('button_prev2');

		$("#button_n").removeClass('button_next');
		$("#button_n").addClass('button_next2');

		$("#button_p a:first").css({display: "none"});
		$("#button_n a:first").css({display: "none"});
	}
}

function sizeScreen()
{
	var ws = $("#scroll_bg").get(0);
	if (!ws) return;

	if ($('#scroll_wrapper').find('.pic_block').length * 123 > ws.parentNode.offsetWidth + 1)
	{
		$("#button_p").removeClass('button_prev2');
		$("#button_p").addClass('button_prev');

		$("#button_n").removeClass('button_next2');
		$("#button_n").addClass('button_next');

		$("#button_p a:first").css({display: "block"});
		$("#button_n a:first").css({display: "block"});
	}
	else
	{
		$("#button_p").removeClass('button_prev');
		$("#button_p").addClass('button_prev2');

		$("#button_n").removeClass('button_next');
		$("#button_n").addClass('button_next2');

		$("#button_p a:first").css({display: "none"});
		$("#button_n a:first").css({display: "none"});
	}

	ws.style.width = (ws.parentNode.offsetWidth + 123) + "px";
	ws.style.clip = "rect(0 " + (ws.parentNode.offsetWidth - 2) + "px 100px 0)";
}

// imported part from xata33.com

/* get real widnow size */
function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return myWidth
}

function ifScreenIsWide()
{
	return getWindowSize() >= 1257;
}

function ScreenSize(){
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
    myTop = window.pageYOffset;
    myLeft = window.pageXOffset;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
    myTop = document.documentElement.scrollTop;
    myLeft = document.documentElement.scrollLeft;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
    myTop = document.body.scrollTop;
    myLeft = document.body.scrollLeft;
  }
}

function op_pha(id)
{
	var xScroll, yScroll, leftScroll, topScroll;
	
	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;
	}

	if (document.documentElement)
	{
		leftScroll = document.documentElement.scrollLeft;
		topScroll = document.documentElement.scrollTop;
	}
	else
	{
		leftScroll = document.body.scrollLeft;
		topScroll = document.body.scrollTop;
	}
	
	var windowWidth, windowHeight;
	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;
	}
	if (self.innerHeight) {	// all except Explorer
		if (!windowWidth || windowWidth > self.innerWidth)
			windowWidth = self.innerWidth;
		if (!windowHeight || windowHeight > self.innerHeight)
			windowHeight = self.innerHeight;
	}
	
	// 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;
	}

	sc = document.createElement("div");
	sc.style.position = "absolute";
	sc.style.left = "0";
	sc.style.top = "0";
	sc.style.width = pageWidth + "px";
	sc.style.height = pageHeight + "px";
	sc.style.background = "#444";
	if (typeof(sc.style.filter) != "undefined") sc.style.filter = "progid:DXImageTransform.Microsoft.Alpha(Opacity=50)";
	else sc.style.opacity = "0.5";
	sc.id = "pha";
	sc.onclick = killview;

	sct = document.createElement("table");
	sct.style.position = "absolute";
	sct.style.left = leftScroll + "px";
	sct.style.top = topScroll + "px";
	sct.style.width = windowWidth + "px";
	sct.style.height = windowHeight + "px";
	sct.id = "t_pha";
	sct.style.zIndex = "20";
	sct.onclick = killview;

	document.body.appendChild(sc);
	document.body.appendChild(sct);

	sctd = sct.insertRow(0);
	sctd = sctd.insertCell(0);
	sctd.align = "center";
	sctd.vAlign = "middle";

	elem = document.getElementById("pha_cont").cloneNode(true);
	elem.id = "";

	elem.onclick = nobubble;
	elem.ondblclick = nobubble;
	sctd.appendChild(elem);

	set_pha(id);

	elem.style.display = "block";
	elem.style.visibility = "visible";

	nobubble();

	return false;
}

function nobubble(e)
{
	if (!e) e = window.event;
	if (e)
	{
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
	}
}

function enkill()
{
	fkill = true;
}

function killview()
{
	if (fkill)
	{
		var nn = document.getElementById("pha");
		if (nn)
		{
			nn.parentNode.removeChild(nn.nextSibling);
			nn.parentNode.removeChild(nn);
		}
	}
	return false;
}

function set_pha(id)
{
	var tmp = $("#t_pha img.pha_img").get(0);
	tmp.src = pharr[id][0];
	tmp.alt = pharr[id][1];
	$("#t_pha div.img_about").html(pharr[id][1]);
	var cnt = 0;
	for (var x in pharr) if (x == id) break;
	else cnt++;
	tmp = $("#t_pha span.ph_num").get(0);
	if (tmp) tmp.innerHTML = (cnt + 1);
}

function prev_pha()
{
	var p = parseInt($("#t_pha span.ph_num").get(0).innerHTML);

	if (p == 1) p = parseInt($("#t_pha span.ph_num").get(1).innerHTML);
	else p--;

	var cnt = 1;
	for (var x in pharr) if (cnt == p) break;
	else cnt++;
	set_pha(x);

	return false;
}

function next_pha()
{
	var p = parseInt($("#t_pha span.ph_num").get(0).innerHTML);

	if (p == parseInt($("#t_pha span.ph_num").get(1).innerHTML)) p = 1;
	else p++;

	var cnt = 1;
	for (var x in pharr) if (cnt == p) break;
	else cnt++;
	set_pha(x);

	return false;
}

function openHTG(id,comm)
{
	window.open("/ag_map.php?id=" + escape(id),"_form","toolbar=no,bar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=445,height=" + (comm ? "535" : "370")).focus();
	return false;
}

function CurSw(tab)
{
	var els = document.getElementsByTagName("div");

	if (els) for (var x = 0; x < els.length; x++)
	{
		var attr = els[x].getAttribute("curr");
		if (attr && attr.charAt(0) == "c")
		{
			els[x].style.display = (attr == "c" + tab ? "" : "none");
		}
		if (attr && attr.charAt(0) == "z")
		{
			els[x].parentNode.className = (attr == "z" + tab ? "e8" : "gg");
			els[x].parentNode.parentNode.className = (attr == "z" + tab ? "e7" : "g3");
			els[x].parentNode.parentNode.parentNode.className = (attr == "z" + tab ? "e6" : "g2");
			els[x].parentNode.parentNode.parentNode.parentNode.className = (attr == "z" + tab ? "e5" : "g1");
		}
	}
	return false;
}

function ffSubmit(frm,what)
{
	if (!what) what = "отзыва";
	if (!frm.elements["fio"].value)
	{
		alert("Введите имя отправителя");
		frm.elements["fio"].focus();
		return false;
	}
	if (!frm.elements["email"].value)
	{
		alert("Введите e-mail отправителя");
		frm.elements["email"].focus();
		return false;
	}
	if (!frm.elements["phone"].value)
	{
		alert("Введите телефон отправителя");
		frm.elements["phone"].focus();
		return false;
	}
	if (!frm.elements["feedback"].value)
	{
		alert("Введите текст " + what);
		frm.elements["feedback"].focus();
		return false;
	}
	frm.submit();
	return false;
}

function rfSubmit(frm)
{
/*	if (!frm.elements["seminar"].value)
	{
		alert("Выберите семинар");
		frm.elements["seminar"].focus();
		return false;
	}
*/	if (!frm.elements["fio"].value)
	{
		alert("Введите ФИО участника");
		frm.elements["fio"].focus();
		return false;
	}
	if (!frm.elements["company"].value)
	{
		alert("Введите компанию");
		frm.elements["company"].focus();
		return false;
	}
	if (!frm.elements["post"].value)
	{
		alert("Введите должность");
		frm.elements["post"].focus();
		return false;
	}
	if (!frm.elements["city"].value)
	{
		alert("Введите город");
		frm.elements["city"].focus();
		return false;
	}
	if (!frm.elements["phone"].value)
	{
		alert("Введите телефон");
		frm.elements["phone"].focus();
		return false;
	}
	if (!frm.elements["email"].value)
	{
		alert("Введите e-mail");
		frm.elements["email"].focus();
		return false;
	}
	frm.submit();
	return false;
}

function openSearch(frm)
{
	if (frm.elements["q"].value)
	{
		return true;
//		window.open("http://www.google.com/search?hl=ru&q=site%3Aexotictime.ru+" + frm.elements["q"].value,"_exotic_search").focus();
	}
	else alert("Введите поисковую фразу");
	return false;
}

function openPrint()
{
	window.print();
	return false;
}

function Open(a) {
// открытие ссылки в новом окне браузера. Открывается посередине экрана.
// учитывается ширина и высота окна. Все остальные значения расчитываются автоматически
	var new_window = {
		width: 800,
		height: 650,			
		left: function() {return Math.ceil((window.screen.width - this.width)/2)},
		top: function() {return Math.ceil((window.screen.height - this.height)/2)},
		open: function(href) {
			window.open(href, 'new_window', 'scrollbars=1,width='+this.width+',height='+this.height+',left='+this.left()+',top='+this.top()+',resizable=1,toolbar=0,location=0,status=0,menubar=0,directories=0').focus();
		}
	};
	
	new_window.open(a.href);
	
	return false;
}

function swTr(el,coll)
{
	var tbl = el.parentNode;

	while(tbl.parentNode && tbl.tagName && tbl.tagName != "BODY" && tbl.tagName != "TABLE") tbl = tbl.parentNode;
	if (tbl.tagName == "TABLE")
	{
		for (var x = 0; x < tbl.rows.length; x++) if (tbl.rows[x].getAttribute("coll") == coll)
		{
			tbl.rows[x].style.display = (tbl.rows[x].style.display == "none" ? "" : "none");
		}
	}
	return false;
}


