var request = false;
try {
     request = new XMLHttpRequest(); 
   } catch (trymicrosoft) {
     try {
       request = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (othermicrosoft) {
       try {
         request = new ActiveXObject("Microsoft.XMLHTTP");
       } catch (failed) {
         request = false;
       }  
     }
}

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

function terminateForm (elem) {
	var form = elem.parentNode;
	
	var inputs = form.getElementsByTagName('input');
	for (var z in inputs) {
		inputs[z].value='';
	}
	
	if (form.id!='copyMe') {
		form.parentNode.removeChild(form);
	}
}

function showLogin () {
	$('loginScreen').style.display='block';
}


	function copyHtml (srcId, targetid) {
	
		var wrapper = document.createElement('div');	
		wrapper.innerHTML = '<div class="wrap"></div><div style="border-top:1px solid #aaa"></div><div style="border-top:1px solid #fff"></div><div class="doubleWrap"></div>';
	
		var div = document.createElement('div');	
		
		div.appendChild(wrapper);
		div.innerHTML += document.getElementById(srcId).innerHTML;
		div.className='rel';
		var inputs = div.getElementsByTagName('input');
		for (var z in inputs) {
			inputs[z].value='';
		}
		
		document.getElementById(targetid).appendChild(div);
	}
	
	
	

function set_opacity(el, o) {

	var opacityProp = getOpacityProperty();
	if (opacityProp=="filter")  // Internet Exploder 5.5+ 
	{
		o*=100;
		el.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+o+")";
	}
	else el.style.opacity=o;
}

function chkChk(id) {
	if ($(id).style.display=='block') {
		$(id).style.display='none';
	} else {
		$(id).style.display='block';
	}
}

function getOpacityProperty()
{
  if (typeof document.body.style.opacity == 'string') // CSS3 compliant (Moz 1.7+, Safari 1.2+, Opera 9)
    return 'opacity';
  else if (typeof document.body.style.MozOpacity == 'string') // Mozilla 1.6 и младше, Firefox 0.8 
    return 'MozOpacity';
  else if (typeof document.body.style.KhtmlOpacity == 'string') // Konqueror 3.1, Safari 1.1
    return 'KhtmlOpacity';
  else if (document.body.filters && navigator.appVersion.match(/MSIE ([\d.]+);/)[1]>=5.5) // Internet Exploder 5.5+
    return 'filter';

  return false; //нет прозрачности
}


var timer;
var initValue=0;
function animateOp(elem, maxValue, inc, pause){
	clearInterval(timer);
	if ((parseInt(elem.style.opacity) < maxValue) || (isNaN(parseInt(elem.style.opacity)))) {
		set_opacity(elem, initValue + inc);
		initValue+=inc;
		timer = setTimeout(function(){animateOp(elem, maxValue, inc, pause);}, pause);
	} else initValue=0;
}

var myTimer = '';
function fadeOut (elem, opValue, currOp, inc, pause) {
	clearInterval(myTimer);
	if (currOp >= opValue) {
		currOp+=inc;
		set_opacity(elem, currOp);
		myTimer = setTimeout(function(){fadeOut(elem, opValue, currOp, inc, pause);}, pause);
	}
}


var downInt;
function animateUp(elem, prop, limit, inc, pause, action, pos)
{
	if (action=='up') {
		clearInterval(downInt);
		if (limit>pos) {
			pos+=inc;
			eval('elem.style.'+prop+'=(pos)+\'px\';');
			upInt = setTimeout(function(){animateUp(elem, prop, limit, inc, pause, action, pos)}, pause);
		}else upInt=0;
	}
	else if  (action=='down')
	{
		clearInterval(upInt);
		if (limit<pos)
		{
			pos-=inc;
			eval('elem.style.'+prop+'=(pos)+\'px\';');
			downInt = setTimeout(function(){animateUp(elem, prop, limit, inc, pause, action, pos)}, pause);
		}else downInt=0;
	}
}

function getStyle(elem, prop)
{
  if (typeof elem!="object") elem = document.getElementById(elem);
  
  // external stylesheet for Mozilla, Opera 7+ and Safari 1.3+
  if (document.defaultView && document.defaultView.getComputedStyle)
  {
    if (prop.match(/[A-Z]/)) prop = prop.replace(/([A-Z])/g, "-$1").toLowerCase();
    return document.defaultView.getComputedStyle(elem, "").getPropertyValue(prop);
  }
  
  // external stylesheet for Explorer and Opera 9
  if (elem.currentStyle)
  {
    var i;
    while ((i=prop.indexOf("-"))!=-1) prop = prop.substr(0, i) + prop.substr(i+1,1).toUpperCase() + prop.substr(i+2);
    return elem.currentStyle[prop];
  }
  
  return "";
}

function byClass (classList, node) {            
	var node = node || document,
	list = node.getElementsByTagName('*'), 
	length = list.length,  
	classArray = classList.split(/\s+/), 
	classes = classArray.length, 
	result = [], i,j, key
	for(i = 0; i < length; i++) {
		for(j = 0; j < classes; j++)  {
			if(list[i].className.search('\\b' + classArray[j] + '\\b') != -1) {
				result.push(list[i])
				break
			}
		}
	}     
	return result
}


window.onload = function () {
	if ($('showSearchFields')!=undefined) {
	$('showSearchFields').onclick = function () {
		var div = $('searchWhereBlock');
		if (parseInt(getStyle(div, 'height'))==0) {
			animateUp(div, 'height', 85, 3, 10, 'up', 0);
		} else {
			animateUp(div, 'height', 0, 3, 10, 'down', parseInt(getStyle(div, 'height')));
		}
		return false;
	}}
}

function paginate(page, pages) {
	var htFuckingMl = '<div class="right">';
	if (page>1){
		htFuckingMl +='<a href="#top" onclick="switchPage(\''+(parseInt(page)-1)+'\', '+pages+');"  class="pageInt tmr">&#8592;</a>';
	}
					
	if ((page>=6)&&(pages - page >= 5)) var pagePos=6; 
	else if (page<6) var pagePos=page; 
	else if (pages - page < 6) {
		var pagePos = pages - page;
	}
					
	for (var z = page - 5; z<=page - pagePos+11; z++) {
		if ((z>pages)||(z<=0)) continue;
		htFuckingMl += '<a href="#top" onclick="switchPage(\''+parseInt(z)+'\', '+pages+');"  class="pageInt ';
		if (page==z){ htFuckingMl +=' bold'; }
		htFuckingMl +='">'+z+'</a>';	
	}
					
	if (page < pages) {
		htFuckingMl +='<a href="#top" onclick="switchPage(\''+(parseInt(page)+1)+'\', '+pages+');" class="pageInt tmr">&#8594;</a>';
	}					
	htFuckingMl +='</div><div class="wrap"></div>';
	$('pagesHere').innerHTML = htFuckingMl;
	$('pagesHere2').innerHTML = htFuckingMl;
}

function switchPage(page, pages) {
	for (var z = 1; z <= pages; z++) {
		if ($('page_'+z)) {$('page_'+z).style.display = 'none';}
	}
	$('page_'+page).style.display = 'block';
	paginate(page, pages);
}
