var deltacrop_w=16;
var deltacrop_h=72;
/////////////////////drag
//window.onload=drag_draw;
var cont_h=0;
var cont_w=0;
var isdrag=false;
var isresize=false;

var drag_h=80;
var drag_w=80;
var drag_x=0;
var drag_y=0;

var start_x=0;
var start_y=0;
var ss_x=0;
var ss_y=0;

var prop_w=1;
var prop_h=1;

var minsel_h=40;
var minsel_w=40;


function select(ch, alpha){
	var archiv=document.getElementById('mp3archiv');
	var alphabet=document.getElementById('alphabet');
	var last=document.getElementById('mp3Last');
	last.style.display="none";	
	archiv.style.display="block";
	if(archiv.childNodes.length>0){
		for(i=0;i<archiv.childNodes.length;i++){
			if(archiv.childNodes[i].id){
				if(archiv.childNodes[i].id!=ch){
					archiv.childNodes[i].style.display="none";
				}
				else{
					archiv.childNodes[i].style.display="block";
				}
			}
		}
	}

	if(archiv.childNodes.length>0){
		for(i=0;i<alphabet.childNodes.length;i++){
			if(alphabet.childNodes[i].className){
				if(alphabet.childNodes[i].className){
					alphabet.childNodes[i].className="";
				}				
			}			
		}
	}
	alpha.className='sel';	
	document.getElementById("all").className='';
	document.getElementById("all").onclick=new Function('return showAll()');
	document.getElementById("last").className='';
	document.getElementById("last").onclick=new Function('return last()');
	return false;	
}

function showAll(){
	var alphabet=document.getElementById('alphabet');	
	var archiv=document.getElementById('mp3archiv');
	var last=document.getElementById('mp3Last');	
	document.getElementById("last").onclick=new Function('return last()');
	document.getElementById("last").className="";
	
	document.getElementById("all").className='sel';
	document.getElementById("all").onclick=new Function('');
	
	archiv.style.display="block";
	last.style.display='none';
	if(archiv.childNodes.length>0){
		for(i=0;i<archiv.childNodes.length;i++){
			if(archiv.childNodes[i].id){
				archiv.childNodes[i].style.display="block";
			}
		}
	}
		
	if(archiv.childNodes.length>0){
		for(i=0;i<alphabet.childNodes.length;i++){
			if(alphabet.childNodes[i].className){
				if(alphabet.childNodes[i].className){
					alphabet.childNodes[i].className="";
				}				
			}			
		}
	}
	return false;
}

function last(){
	var alphabet=document.getElementById('alphabet');
	var archiv=document.getElementById('mp3archiv');
	var last=document.getElementById('mp3Last');
	
	document.getElementById("last").className='sel';
	document.getElementById("last").onclick=new Function('');
	
	document.getElementById("all").className='';
	document.getElementById("all").onclick=new Function('return showAll()');
	
	archiv.style.display='none';
	
	if(archiv.childNodes.length>0){
		for(i=0;i<alphabet.childNodes.length;i++){
			if(alphabet.childNodes[i].className){
				if(alphabet.childNodes[i].className){
					alphabet.childNodes[i].className="";
				}				
			}			
		}
	}
	
	last.style.display='block';	
	return false;
}



document.getElementsByClassName = function(clsName){
    var retVal = new Array();
    var elements = document.getElementsByTagName("*");
    for(var i = 0;i < elements.length;i++){
        if(elements[i].className.indexOf(" ") >= 0){
            var classes = elements[i].className.split(" ");
            for(var j = 0;j < classes.length;j++){
                if(classes[j] == clsName)
                    retVal.push(elements[i]);
            }
        }
        else if(elements[i].className == clsName)
            retVal.push(elements[i]);
    }
    return retVal;
}

function show_voting(){
	
}

function mp3Best(id,sign,sid){
	url=window.location.href;
	url=url.split('/');
	url = 'http://'+url[2]+'/ajax.php?type=mp3Best&id='+id+'&sign='+sign+'&sid='+sid;
	if(url!=lasturl){
		loadXMLDoc(url);
		lasturl=url;
	}
	document.getElementById("voit").style.display="none";
	return false;
}

function showRingtoner(a){
	if(document.getElementById('widget').style.display=='block'){
		a.innerHTML="Сделать рингтон!";
		document.getElementById('widget').style.display='none';		
	}
	else{
		a.innerHTML="Я передумал!";
		document.getElementById('widget').style.display='block';
	}
	 
	return false;
}

function showmp3s(a){
a.style.display="none";
	var mp3s = document.getElementsByClassName("hiddenmp3");
	for(i=0;i<mp3s.length;i++){
		mp3s[i].className="itemmp3";
	}
	return false;
}

function showMsg(id){
	document.getElementById("hidden"+id).style.display="block";
	document.getElementById("badmsg"+id).style.display="none";
	return false;
}
function drag_draw(){
	obj=document.getElementById('sel');
	obj.style.height=drag_h+'px';
	obj.style.width=drag_w+'px';
	obj.style.left=drag_x+'px';
	obj.style.top=drag_y+'px';
	
	document.getElementById('crop_x').value=drag_x;
	document.getElementById('crop_y').value=drag_y;
	document.getElementById('crop_h').value=drag_h;
	document.getElementById('crop_w').value=drag_w;
}

function smileAdd(smile){
    out=document.getElementById('text');
    out.value+=smile.alt;
}  

function startsel(e){
	if(!isresize){
		isdrag=true;
		start_x = (window.Event) ? e.pageX : event.clientX;
		start_y = (window.Event) ? e.pageY : event.clientY;
		ss_x=drag_x;
		ss_y=drag_y;
	}
}

function endsel(){
	isdrag=false;
	isresize=false;
}

function movesel(e){
	if(isdrag && !isresize){
		cx = (window.Event) ? e.pageX : event.clientX;
		cy = (window.Event) ? e.pageY : event.clientY;
		drag_x=ss_x+cx-start_x;
		drag_y=ss_y+cy-start_y;
		if(drag_x<0) drag_x=0;
		if(drag_y<0) drag_y=0;
		if((drag_x+drag_w)>cont_w){
			drag_x=cont_w-drag_w-2;
		}
		if((drag_y+drag_h)>cont_h){
			drag_y=cont_h-drag_h-2;
		}
		drag_draw();
	}
	else if(isresize){
		resizesel(e);
	}
}

function startresize(e){
	isdrag=false;
	isresize=true;
	start_x = (window.Event) ? e.pageX : event.clientX;
	start_y = (window.Event) ? e.pageY : event.clientY;
	ss_x=drag_w;
	ss_y=drag_h;
}

function endresize(){
	isresize=false;
}

function resizesel(e){
	if(isresize){
		cx = (window.Event) ? e.pageX : event.clientX;
		cy = (window.Event) ? e.pageY : event.clientY;
		drag_w=cx-start_x+ss_x;
		drag_h=cy-start_y+ss_y;
	
		if((drag_w/prop_w)>(drag_h/prop_h)){
			drag_h=drag_w/prop_w*prop_h;
		}
		else{
			drag_w=drag_h/prop_h*prop_w;
		}
		
		if(drag_w<minsel_w){
			drag_w=minsel_w;
		}
		if(drag_h<minsel_h){
			drag_h=minsel_h;
		}

		if((drag_w+drag_x)>cont_w){
			drag_w=cont_w-drag_x-2;
		}
		if((drag_h+drag_y)>cont_h){
			drag_h=cont_h-drag_y-2;
		}
		
		if((drag_w/prop_w)>(drag_h/prop_h)){
			drag_w=drag_h/prop_h*prop_w;
		}
		else{
			drag_h=drag_w/prop_w*prop_h;
		}
		drag_draw();
	}
}

/////////////////////end drag

function preparecroper(imgt,w,h){
	document.getElementById('cimg').style.width=w+'px';
	document.getElementById('cimg').style.height=h+'px';
	document.getElementById('cimg').style.backgroundImage='url('+imgt+')';
	document.getElementById('butsave').style.display='block';
	document.getElementById('sel').style.display='block';
	
	if((h/prop_h)>(w/prop_w)){
		if(h<100 || w<100){}
		else{
			drag_w=w-20;
			drag_x=10;
		}
		drag_h=drag_w/prop_w*prop_h;
		drag_y=(h-drag_h)/2;
	}
	else{
		if(h<100 || w<100){}
		else{
			drag_h=h-20;
			drag_y=10;
		}
		drag_w=drag_h/prop_h*prop_w;
		drag_x=(w-drag_w)/2;
	}
	drag_draw();
	cont_h=h;
	cont_w=w;
}

function preCroper(){
	showbg(2);
}
var stepi=1;
function preCroper2(){
	img=document.getElementById('temp_img').value;
	w=parseInt(document.getElementById('w_img').value);
	h=parseInt(document.getElementById('h_img').value);
	mw=parseInt(document.getElementById('mw_img').value);
	mh=parseInt(document.getElementById('mh_img').value);
  	prop_w=4;
	prop_h=5;
	
	
	if(mh<40) mh=40;
	if(mw<40) mw=40;
	minsel_h=parseInt(mh);
	minsel_w=parseInt(mw);
  	resizer('cropc',w+deltacrop_w,h+deltacrop_h,img);
  	document.getElementById('crop_img').value=img;
  	
  	document.getElementById('butsave').style.display='none';
  	document.getElementById('butsave2').style.display='block';
	if(stepi==1){
		document.getElementById('crptitle').innerHTML='Кадрирование изображения';
	}
}



function subimg(){
	cropImg=document.getElementById('crop_img').value;
	cropX=document.getElementById('crop_x').value;
	cropY=document.getElementById('crop_y').value;
	cropW=document.getElementById('crop_w').value;
	cropH=document.getElementById('crop_h').value;
	
  	dummyimage = new Image();
  	dummyimage.src = '/saveimg.php?crop_img='+cropImg+'&crop_x='+cropX+'&crop_y='+cropY+'&crop_h='+cropH+'&crop_w='+cropW+'&type='+stepi;
  	
  	if(stepi==1){
		if(stepi==1){
			document.getElementById('crptitle').innerHTML='Микрокартинка';
		}

  		stepi=2;
	  	prop_w=1;
		prop_h=1;
		if((h/prop_h)>(w/prop_w)){
			if(h<100 || w<100){}
			else{
				drag_w=w-20;
				drag_x=10;
			}
			drag_h=drag_w/prop_w*prop_h;
			drag_y=(h-drag_h)/2;
		}
		else{
			if(h<100 || w<100){}
			else{
				drag_h=h-20;
				drag_y=10;
			}
			drag_w=drag_h/prop_h*prop_w;
			drag_x=(w-drag_w)/2;
		}
		drag_draw();
		cont_h=h;
		cont_w=w;
  	}
  	else{
  		closew();
  		setTimeout('loadMobileImg()',200);
  		
  	}
	return false;
}


function loadMobileImg(){
	
	cropImg=document.getElementById('crop_img').value;
	
	cropImg=cropImg.split('/');
	cropImg=cropImg[4].split('_');
 	sampi = new Image();
  	sampi.src = "/res/mobile/samp_big/"+cropImg[0]+".jpg";
	sampi.width='176';
	sampi.height='216';
	
	if(brb1=document.getElementById('brb')){
		brb1.parentNode.appendChild(sampi);
		brb1.parentNode.removeChild(brb1);
	}
	document.getElementById('step2').className+=' step2h';
	document.getElementById('step3').style.display='block';
	setTimeout('loadMobileImg2()',4000);
}

function loadMobileImg2(){
	document.getElementById('imgi').style.display='block';
	
 	sampi2 = new Image();
  	sampi2.src = "/res/mobile/samp_small/"+cropImg[0]+".jpg";
	sampi2.width='72';
	sampi2.height='72';

	if(imgsl1=document.getElementById('imgsl')){
		//imgsl.parentNode.replaceChild(sampi2, imgsl);
		imgsl1.parentNode.appendChild(sampi2);
		imgsl1.parentNode.removeChild(imgsl1);
	}

}


function handleResponse(img,w,h,error,mh,mw,type) {
	if(error==0){
	  	document.getElementById('formaterror1').style.display='none';
	  	document.getElementById('formaterror1').innerHTML='';

		if(type==1){
			if(mh<40) mh=40;
			if(mw<40) mw=40;
			minsel_h=parseInt(mh);
			minsel_w=parseInt(mw);
		  	resizer('cropc',w+deltacrop_w,h+deltacrop_h,img);
		  	document.getElementById('crop_img').value=img;
		}
		else if(type==2){
			document.getElementById('imgpreload').style.display="none";
			document.getElementById('step1').className+=" step1h";
			
			document.getElementById('step2').style.display="block";
			
			document.getElementById('temp_img').value=img;
			document.getElementById('w_img').value=w;
			document.getElementById('h_img').value=h;
			document.getElementById('mw_img').value=mw;
			document.getElementById('mh_img').value=mh;
		}
	}
	else{
		if(type==1){
			closew();
		}
		else if(type==2){
			document.getElementById('imgpreload').style.display="none";
			document.getElementById('imgph').style.display="none";
		}
		
	  	document.getElementById('formaterror1').style.display='block';
	  	document.getElementById('formaterror1').innerHTML=error;
	}
}

function resizer(a,w,h,imgt){
	ws=w;
	stepsize=3;
	delay=15;
	obj=document.getElementById(a);
	
	w0=obj.clientWidth;
	h0=obj.clientHeight;
	dw=w-w0;
	dh=h-h0;

	
	m=Math.abs(dw);
	step=parseInt(m/stepsize);
	for(var i=0;i<=step;i++)	{
		
		if(step!=1){
			setTimeout('document.getElementById("'+a+'").style.width="'+(w0+(dw/(step-1))*i)+'px";',delay*i);			
		}
		else{
			setTimeout('document.getElementById("'+a+'").style.width="'+(w0+(dw)*i)+'px";',delay*i);			
		}
	}
	dtime=step*delay;
	m=Math.abs(dh);
	step=parseInt(m/stepsize);

	for (var i=0;i<=step;i++){
		if(i<step){
			if(step!=1){
				
				setTimeout('document.getElementById("'+a+'").style.height="'+(h0+(dh/(step-1))*i)+'px";document.getElementById("'+a+'").style.marginTop="-'+((h0+(dh/(step-1))*i)/2)+'px";',delay*i+dtime);
			}
			else{
				setTimeout('document.getElementById("'+a+'").style.height="'+(h0+(dh)*i)+'px";document.getElementById("'+a+'").style.marginTop="-'+((h0+(dh)*i)/2)+'px";',delay*i+dtime);
			}
		}
		else{
			setTimeout('preparecroper("'+imgt+'","'+(w-deltacrop_w)+'","'+(h-deltacrop_h)+'")',(delay*i+200)+dtime);
		}
	}
}



function closew(){
	sele=document.getElementById('sel');
	sele.style.width='80px';
	sele.style.height='80px';
	sele.style.left='0';
	sele.style.top='0';
	sele.style.display='none';
	
	drag_h=80;
	drag_w=80;
	drag_x=0;
	drag_y=0;


	document.getElementById('cimg').style.width='80px';
	document.getElementById('cimg').style.height='80px';
	document.getElementById('butsave').style.display='none';
	Obj=document.getElementById('cropc');
	Obj.style.width="250px";
	Obj.style.height="250px";
	Obj.style.marginLeft="-125px";
	Obj.style.marginTop="-125px";
	
	
	document.getElementById('cropimg').style.display="none";
	Obj=document.getElementById('shade');
	Obj.style.display="none";
	Obj.style.opacity = '0';
	Obj.style.MozOpacity = '0';
	Obj.style.filter = 'alpha(opacity=0)';
	
	
	document.getElementById('cimg').style.backgroundImage='none';
}



///////////--------///////////
function showbg(type){
	
	if (document.compatMode && document.compatMode != "BackCompat"){
		theHeight = document.documentElement.scrollHeight;
	}
	else{
		theHeight = document.body.scrollHeight;
	}
	document.getElementById('shade').style.display='block';
	document.getElementById('shade').style.height=theHeight+'px';
	
	var delay=100;
	var max=8;
	
	for(var i=0;i<=max;i++){
		if(i!=max){
			setTimeout('setOpacity('+i+')',delay*i);
		}
		else{
			dtop=document.documentElement.scrollTop;	
			htop=document.documentElement.clientHeight;
			prc=50;
			if(htop>0){
				prc=parseInt(dtop/htop*delay)+50;
			}
			document.getElementById('cropimg').style.top=prc+'%';
			document.getElementById('cropc').style.marginTop="-125px";
			setTimeout("document.getElementById('cropimg').style.display='block';",delay*i);
			if(type==2){
				setTimeout("preCroper2()",delay*i);
			}
		}
	}
}

function setOpacity(value) {
	Obj=document.getElementById('shade');
	Obj.style.opacity = value/10;
	Obj.style.MozOpacity= value/10; 
	Obj.style.filter = 'alpha(opacity=' + value*10 + ')';
}
//////////--------////////////

function cropimgshow(a, type){
	if(type==1){
		showbg();
	}
	if(type==2){
		document.getElementById('imgpreload').style.display="inline";
		document.getElementById('imgph').style.display="block";
	}
	setTimeout('document.'+a+'.submit()',900);
}

function nickcheck(e){
	var key;
	var keychar;
	if (window.event){
		key = window.event.keyCode;
	}
	else if (e){
		key = e.which;
	}
	else{
		return true;
	}
	keychar = String.fromCharCode(key);
	if((key>=65 && key<=97) || (key>=90 && key<=122)){
		document.getElementById('errornick').style.display='none';
		return true;
	}
	else if(key==8 || key==13 || key==45 || key==0){
		document.getElementById('errornick').style.display='none';
		return true;
	}
	else if ((("0123456789").indexOf(keychar) > -1)){
		return true;
	}
	else{
		document.getElementById('errornick').style.display='block';
		return false;
	}
}

////town 
var lasturl='';
var lastch='';

function getTown(){	
	var t=document.getElementById('city').value;
	var c=document.getElementById('country').value;
	
	if(t.length>0){
		url=window.location.href;
		url=url.split('/');
		url = 'http://'+url[2]+'/ajax.php?cid='+c+'&txt='+t+'&l='+t.length;
		if(url!=lasturl){
			loadXMLDoc(url);
			lasturl=url;
		}
	}
	else{
		lasturl='';
		hidf();
	}
}

function hid(){
	setTimeout('hidf()',300);
}

function hidf(){
	var obj=document.getElementById('seltown');
	obj.firstChild.firstChild.innerHTML='';	
	obj.style.display='none';
}

function odiv(a){
	for(count=0;document.getElementById('conttown').childNodes[count];count++){
		document.getElementById('conttown').childNodes[count].className="item";
	}
	a.className="itemsel";
}

function outdiv(a){
	
//	a.className="item";
}


function keycheck(e){
	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	var obj=document.getElementById('seltown');
	var contt=document.getElementById('conttown');
	var sel;
	if(obj.style.display=='block'){
		var count=contt.childNodes.length;
		
		if (code==13) {
			sel=-1;
			for(i=0;i<count;i++){
				if(contt.childNodes[i].className=='itemsel'){
					sel=i;
				}
			}
			if(sel!=-1){
				
				document.getElementById('city').value=contt.childNodes[sel].firstChild.nodeValue;
			}
			setTimeout('hidf()',500);
		}

		if(code==38||code==40){
			
			if(code==38||code==40){
				sel=-1;
				for(i=0;i<count;i++){
					if(contt.childNodes[i].className=='itemsel'){
						sel=i;
						contt.childNodes[i].className='item';
					}
				}
				if (code == 38) {
					sel--;
					if(sel<0){
						sel=count-1;
					}
				}
				if (code == 40) {
					sel++;
					if(sel>=count){
						sel=0;
					}
				}
				if(count>0){
					contt.childNodes[sel].className='itemsel';
				}
			}
		}
	}
}

function testp(){
	if(document.getElementById('seltown').style.display=='block'){
		return false;
	}
}

function rusonly(myfield, e){
	var key;
	if (window.event){
		key = window.event.keyCode;
	}
	else if (e){
		key = e.which;
	}
	else{
		return true;
	}
	if((key>=65 && key<=97) || (key>=90 && key<=122)){
		document.getElementById('error').style.display='block';
		return false;
	}
	else{
		document.getElementById('error').style.display='none';
		return true;
		
	}
}

function selTown(o){
	document.getElementById('city').value=o.firstChild.nodeValue;
}

var lasturl_mp3='';
function singsearch(a){
	var t=a.value;
	if(t.length>0){
		url=window.location.href;
		url=url.split('/');
		url = 'http://'+url[2]+'/ajax.php?type=mp3&txt='+t+'&l='+t.length;	
		
		if(url!=lasturl_mp3){
			loadXMLDoc(url);
			lasturl_mp3=url;
		}
	}
	else{
		lasturl_mp3='';
		hidmp3s(0)
	}
}

function hidmp3s(t){
	var cont=document.getElementById('searchmp3');
	var h=document.getElementById('searchmp3h');
	var mp3scanc=document.getElementById('mp3scanc');
	
	cont.style.display='none';
	mp3scanc.style.display='none';
	if(t && document.getElementById('q2').value!=''){
		h.style.display='block';
		h.lastChild.nodeValue='Ничего не найдено.';
	}
	else{
		h.style.display='none';
	}
}

var masksel=0;
var selsize=0;


function selmobile(id,date, name){
	if(masksel!=0){
		document.getElementById('mask'+masksel).className=document.getElementById('mask'+masksel).className.replace('sel','');
	}
	else{
		document.getElementById('imginf').style.display="block";
		selsize=retsizejs();
	}
	masksel=id;
	resizeimg();
	url=window.location.href;
	url=url.split('/');
	document.getElementById('imgbigm').src="/res/mobile/samp_big/"+id+".jpg";
	document.getElementById('imgia').href='/community/users/'+name;
	
	document.getElementById('imgia').innerHTML=name;
	document.getElementById('dateimg').innerHTML=date;
	if(page!=0){
		document.getElementById('imgplink').href='http://'+url[2]+'/community/mobile?page='+page+'&i='+id;
		document.getElementById('imgbad_link').href='?page='+page+'&badimg='+id;
	}
	else{
		document.getElementById('imgplink').href='http://'+url[2]+'/community/mobile?i='+id;
		document.getElementById('imgbad_link').href='?badimg='+id;
	}
	
	if(document.getElementById("hidevote"+id).value=='1'){
		document.getElementById("minuz").style.display="none";
	}
	else{
		document.getElementById("minuz").style.display="block";
	}
	
	
	var msk=document.getElementById('mask'+id);
	msk.className+='sel'
}

function resizeimg(){
	var s=document.getElementById('selsize'+selsize).innerHTML;
	document.getElementById('sizetxt').innerHTML=s;
	res='';
	if(selsize<10){
		res='00'+selsize;
	}
	else if(selsize<100){
		res='0'+selsize;
	}
	res=masksel+res;
	url=window.location.href;
	url=url.split('/');
	url = 'http://'+url[2]+'/m/'+res+'.jpg';
	document.getElementById('imglink').innerHTML=url;
}

var selcity='kirillovka';
var imgtype='selimg2';
var selsize='1';


function selectcity(a){
	if(selcity!=a){
		document.getElementById(a).className='city selected';
		document.getElementById(selcity).className='city' ;	
		selcity=a;
		reimgm();
	}
}

function changeimg(a){
	if(imgtype!=a){
		document.getElementById(a).className='selected';
		document.getElementById(imgtype).className='';
		imgtype=a;
		reimgm();
	}
}

function selisize(a){
	if(selsize!=a){
		document.getElementById('selsize'+a).className='sel';
		document.getElementById('selsize'+selsize).className='';
		document.getElementById('sizetxt').innerHTML=document.getElementById('selsize'+a).innerHTML;
		selsize=a;
		resizeimg();
	}
	
}

function reimgm(){
	var iname='';
	if(selcity=='kirillovka')	iname='1';
	if(selcity=='yalta')		iname='2';
	if(selcity=='odessa')		iname='3';
	
	if(imgtype=='selimg1')		iname+='1';
	if(imgtype=='selimg2')		iname+='2';
		
	if(selsize=='selsize1')		iname+='1';
	if(selsize=='selsize2')		iname+='2';
	if(selsize=='selsize3')		iname+='3';
	if(selsize=='selsize4')		iname+='4';
	if(selsize=='selsize5')		iname+='5';
	
	//document.getElementById('cur_img').src='/mz/'+iname+'.jpg';
	
	//document.getElementById('imglink').innerHTML='http://flint.ua/mz/'+iname+'.jpg';
	
}