var flashinstalled = 0;
var flashversion = 0;
MSDetect = "false";
if (navigator.plugins && navigator.plugins.length)
{
    x = navigator.plugins["Shockwave Flash"];
    if (x)
    {
        flashinstalled = 2;
        if (x.description)
        {
            y = x.description;
            //flashversion = y.charAt(y.indexOf('.')-2);
			flashversion = parseInt(y.slice(16));

        }
    }
    else
        flashinstalled = 1;
    if (navigator.plugins["Shockwave Flash 2.0"])
    {
        flashinstalled = 2;
        flashversion = 2;
    }
}
else if (navigator.mimeTypes && navigator.mimeTypes.length)
{
    x = navigator.mimeTypes['application/x-shockwave-flash'];
    if (x && x.enabledPlugin)
        flashinstalled = 2;
    else
        flashinstalled = 1;
}
else
    MSDetect = "true";

if (MSDetect == "true") {
  	document.write('<SCRIPT LANGUAGE=VBScript\> \n');
  	document.write('on error resume next \n');
	document.write('For i = 4 to 9 \n');
	document.write('If Not(IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & i))) Then \n');
	document.write('Else \n');
	document.write('flashinstalled = 2 \n');
	document.write('flashversion = i \n');
	document.write('End If \n');
	document.write('Next \n');
	document.write('If flashinstalled = 0 Then \n');
	document.write('flashinstalled = 1 \n');
	document.write('End If \n');
	document.write('<\/SCRIPT\> \n');  
}

function insFl(v,fl,gif,lnk,w,h) {

	var d=' width='+w+' height='+h;
	if (flashinstalled > 0 && flashversion >= v) {
		document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'+d+'><param name=wmode value=transparent><param name=movie value='+fl+'><embed src='+fl+' quality=high'+d+' wmode=transparent></embed></object>')
	} else {
		if (gif != '') {
			document.write('<a href='+lnk+'><img src='+gif+d+' border=0></a>')
		}
	}
}


function ShowPhoto(url, width, height) {
	var scroll = (screen.width < 1000) ? "yes" : "no";
	
	var left = (self.screen.width >> 1) - (width >> 1);
	var top = (self.screen.height >> 1) - (height >> 1);
	var param = 'left='+left+',top='+top+',width='+width+',height='+height+',';
	
	window.open(url, '_blank', param+'menubar=no, scrollbars=' + scroll + ', status=no');
}

function emoticon(theSmilie)
{
	doInsert(" " + theSmilie + " ", "", false);
}

function doInsert(ibTag, ibClsTag, isSingle)
{
	var isClose = false;
	var obj_ta = document.post.message;
	
	//----------------------------------------
	// It's IE!
	//----------------------------------------
	if ( (ua_vers >= 4) && is_ie && is_win)
	{
		if (obj_ta.isTextEdit)
		{
			obj_ta.focus();
			var sel = document.selection;
			var rng = sel.createRange();
			rng.colapse;
			if((sel.type == "Text" || sel.type == "None") && rng != null)
			{
				if(ibClsTag != "" && rng.text.length > 0)
					ibTag += rng.text + ibClsTag;
				else if(isSingle)
					isClose = true;
	
				rng.text = ibTag;
			}
		}
		else
		{
			if(isSingle)
			{
				isClose = true;
			}
			
			obj_ta.value += ibTag;
		}
	}
	//----------------------------------------
	// It's MOZZY!
	//----------------------------------------
	
	else if ( obj_ta.selectionEnd )
	{ 
		var ss = obj_ta.selectionStart;
		var st = obj_ta.scrollTop;
		var es = obj_ta.selectionEnd;
		
		if (es <= 2)
		{
			es = obj_ta.textLength;
		}
		
		var start  = (obj_ta.value).substring(0, ss);
		var middle = (obj_ta.value).substring(ss, es);
		var end    = (obj_ta.value).substring(es, obj_ta.textLength);
		
		//-----------------------------------
		// text range?
		//-----------------------------------
		
		if (obj_ta.selectionEnd - obj_ta.selectionStart > 0)
		{
			middle = ibTag + middle + ibClsTag;
		}
		else
		{
			middle = ibTag + middle;
			
			if (isSingle)
			{
				isClose = true;
			}
		}
		
		obj_ta.value = start + middle + end;
		
		var cpos = ss + (middle.length);
		
		obj_ta.selectionStart = cpos;
		obj_ta.selectionEnd   = cpos;
		obj_ta.scrollTop      = st;


	}
	//----------------------------------------
	// It's CRAPPY!
	//----------------------------------------
	else
	{
		if (isSingle)
		{
			isClose = true;
		}
		
		obj_ta.value += ibTag;
	}
	
	obj_ta.focus();

	return isClose;
}	

function getBodyScrollTop() {
  return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}
function getBodyScrollTop() {
  return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}

var init_news = false;




function getlnsideWindowHeight() {
	if (window.innerHeight) {
		return window.innerHeight;
	} else {
		return document.body.parentElement.clientHeight;
	}
	return 0;
}
function getlnsideWindowWidth() {
	if (window.innerWidth) {
		return window.innerWidth;
	} else {
		return document.body.parentElement.clientWidth;
	}
	return 0;
}

function moving(url, alt, width, height, second_url){
	try{
	var tds = document.getElementsByTagName("center")[0].getElementsByTagName("td")
	if(alt!=''){
	    tds[tds.length - 1].innerHTML = alt;
	}
	else{
        tds[tds.length - 1].innerHTML = document.getElementsByTagName("h1")[0].innerHTML
	}
	}
	catch(e){}

	img = document.getElementById("gallery_id");
	
	if(second_url==null || second_url==""){
		second_url = url
	}
	if(img==null || img.src == "http://"+location.host+"/"+second_url){
		ShowPhotoHere(url, alt, width, height);
	}else{
		img.src = "/"+second_url;
		//img.parentNode.setAttribute('onclick',"moving('"+url+"', '"+alt+"', "+width+", "+height+", '"+second_url+"'); return false;");
		img.parentNode.onclick = function(){ moving(url, alt, width, height, second_url); return false; };
	   	img.parentNode.setAttribute('href',"/"+url);
	}
	return false;
}


function ShowPhotoHere(url, alt, width, height) {
	//if(init_news){
	//	document.getElementById("gallery_id").src = "/"+url;
	//	return;
	//}

	var ww = getlnsideWindowWidth();
	var hh = getlnsideWindowHeight() - 40;

	getCount(url);

    var scrolling = getBodyScrollTop(body)

	var body = document.getElementsByTagName("body")[0];
    var over = body.appendChild(document.createElement('div'));
 	over.id = "over2";
	var h = document.getElementById("conteiner").offsetHeight;
	if(h>(scrolling+height+40))
	over.style.height = h+"px"
	else
	over.style.height = scrolling+height+40+"px"
//	over.style.width = width+20+"px" 
 	var conteiner = body.appendChild(document.createElement('div'));
 	conteiner.id = "conteinerimg";
	var inside = conteiner.appendChild(document.createElement('div'));
	inside.id = "inside";

/*	var k1 = width/height;
	var k2 = ww/hh;

    var max = width;
	var min = height;
	if(max<height){
   		max = height;
		min = width;
	}

	if(ww>max && hh>max){
		inside.style.width = width+"px";
		inside.style.height = height+"px";
	}
	else{
	  	if(k1>=k2){
			inside.style.width = ww+"px";
			inside.style.height = ww/k1+"px";
		}
		else{
			inside.style.height = hh+"px";
			inside.style.width = hh*k1+"px";
		}
	}  */
    inside.style.height = height+"px";
	inside.style.width = width+"px";
	inside.style.marginLeft = "auto";
	inside.style.marginRight = "auto";
    inside.style.border = "1px solid red";
    inside.style.marginTop = scrolling+"px";
	var image = document.createElement('img');
	image.width = parseInt(inside.style.width)
	image.height = parseInt(inside.style.height)
	inside.appendChild(image);
	var descr = inside.appendChild(document.createElement('p'));

	//////////////////////////////////////////

 	image.setAttribute("src","/"+url);
	image.setAttribute("title","Для закрытия фотографии щелкните по ней левой клавишей мыши");
	descr.innerHTML = alt;  
	image.onclick = function() {
		over.parentNode.removeChild(over);
		conteiner.parentNode.removeChild(conteiner);
	}
}

var time;
var div;
var imgs;
var g=null;


function galleryInit(){
	div = document.getElementById("over");
	div1 = document.getElementById("photos");

//onclick="moving(\'files/photos/214751.jpg\', \'\', 467, 701, \'files/photos/214751s462.jpg\'); return false;"
	
	if(!(g == null)){
	    for(var i=0; i<g.length; i++){
	         var ff = '<div style="float: left; margin-right: 4px;"><a onclick=\'testImage('+i+'); return false\'  href="'+path+g[i]['medium_url']+'"><img hspace="0" height="'+small_h+'" width="'+small_w+'" align="center" style="" alt="" src="'+path+g[i]['small_url']+'"/></a><p style="padding-top: 2px; text-align: center;">'+(i+1)+'</p></div>';
	        div1.innerHTML += ff
	    }
	}

	imgs = div.getElementsByTagName("img");
	var length = 0;
	for(var i=0; i<imgs.length; i++){
		length = length + imgs[i].width+15;
	}
	document.getElementById("photos").style.width = length+"px"

    curImg = 0;
	try{
	if(g[0]["alt"]!='')
	document.getElementById("phName").innerHTML = " Фото: <b>"+g[0]["alt"]+"</b>";
}catch(e){}	
}



function testImage(n){
  n = (n<0)? n=g.length-1:n;
  n = (n>g.length-1)? 0:n;
  var mImg = document.getElementById("gallery_id");
  var mPath = "http://"+location.host+""+path+g[n]["medium_url"];
	try{
	if(g[n]["alt"]!='')
	document.getElementById("phName").innerHTML = " Фото: <b>"+g[n]["alt"]+"</b>";
}catch(e){}	

    if(mImg.src==mPath){
       ShowPhotoHere((path.slice(1)+g[n]["big_url"]), g[n]["alt"], g[n]["big_w"], g[n]["big_h"]);
    }
    else{
       mImg.src=mPath;
       mImg.parentNode.onclick = function(){ShowPhotoHere((path.slice(1)+g[n]["big_url"]), g[n]["alt"], g[n]["big_w"], g[n]["big_h"]); return false; };
       curImg = n;
  }
}

var speed = 1;
function scrollleft(){
  div.scrollLeft=div.scrollLeft+speed;
  }
function scrollright(){
  div.scrollLeft=div.scrollLeft-speed;
  }
function scroll(x,s){
  time = clearInterval(time);
  if(s==1){
  speed = 3}
  if(x=="l")
  time = setInterval("scrollleft()",5);
  else
  time = setInterval("scrollright()",5);
}

function stop(){
  time = clearInterval(time);
  speed = 1; 
}



//////////////////////years





function options2(){
		if (this.name=="is_milage"){
			if(this.value!=2){
	  			document.filtr.milage.disabled = true;
	  		  	document.filtr.milage.style.display="none";
	  			document.filtr.milage.parentNode.getElementsByTagName("span")[0].style.display= "none"
			}
			else{
	  			document.filtr.milage.disabled = false;
	  			document.filtr.milage.style.display="inline";
	  			document.filtr.milage.parentNode.getElementsByTagName("span")[0].style.display= "inline"
			}
       }

       if (this.name=="year_1"){
           var all = document.filtr.year_2.options;
  		   var stop = this.value;
		   getYear(stop,all,1)
		   y1 = stop;
	   }

       if (this.name=="year_2"){
           var all = document.filtr.year_1.options
  		   var stop = this.value;
		   getYear(stop,all,2)
		   y2 = stop;
	   }
       if (this.name=="id_car_man"){
           getModels(this.value, 1, "s2");
	   }
	}


function getYear(val,opts,lim){
    var sel = opts[0].parentNode;
	opts.length = 0;
	if(lim==1){
		for (var i=maxyear; i>=val; i--)
        if(i==y2)
		addOption(sel, i, i, true, true);
		else
		addOption(sel, i, i, false);
	}
	if(lim==2){
		for (var i=val; i>=minyear; i--)
		if(i==y1)
		addOption(sel, i, i, true, true);
        else
		addOption(sel, i, i, false);
	}
}


function addOption (oListbox, text, value, isDefaultSelected, isSelected)
{
  var oOption = document.createElement("option");
  oOption.appendChild(document.createTextNode(text));
  oOption.setAttribute("value", value);

  if (isDefaultSelected) oOption.defaultSelected = true;
  else if (isSelected) oOption.selected = true;

  oListbox.appendChild(oOption);
}

function gogo(){
  	if(document.anno.dsc_adv.disabled == true){
   		document.anno.dsc_adv.disabled = false;

   if(document.anno.id_car_man){
   var m = document.anno.id_car_man.value;
   for (var i=0; i<document.anno.id_car_man.options.length; i++){
       if(document.anno.id_car_man.options[i].value == m)
	   man = document.anno.id_car_man.options[i].innerHTML
   }
   } else{
   	  man = document.anno.mark.value;
   }

   var y = document.anno.year.value;
   for (var i=0; i<document.anno.year.options.length; i++){
       if(document.anno.year.options[i].value == y)
	   year = document.anno.year.options[i].innerHTML
   }

   if(document.anno.id_car_model){ 
   var mo = document.anno.id_car_model.value;
   for (var i=0; i<document.anno.id_car_model.options.length; i++){
       if(document.anno.id_car_model.options[i].value == mo)
	   model = document.anno.id_car_model.options[i].innerHTML
   }
   } else{
   	  model = document.anno.model.value;
   }

   var cu = document.anno.id_curr.value;
   for (var i=0; i<document.anno.id_curr.options.length; i++){
       if(document.anno.id_curr.options[i].value == cu)
	   cur = document.anno.id_curr.options[i].innerHTML;
		cur = (cur=="р.")?"руб":cur;
   }

   var col = document.anno.cv9.value;
   for (var i=0; i<document.anno.cv9.options.length; i++){
       if(document.anno.cv9.options[i].value == col)
	   col = document.anno.cv9.options[i].innerHTML;
   }
	   col =(col=="") ? "":col+", ";

   var gen = document.anno.id_car_gen.value;
   for (var i=0; i<document.anno.id_car_gen.options.length; i++){
       if(document.anno.id_car_gen.options[i].value == gen)
	   gen = document.anno.id_car_gen.options[i].innerHTML
   }

   year=(year=="") ? "":year+" г., ";
   milage=(document.anno.milage.value=="") ? "":document.anno.milage.value+" тыс. км, ";
   var eng =(document.anno.engine_v.value=="") ? "":document.anno.engine_v.value+" л, ";
   var hp =(document.anno.hp.value=="") ? "":document.anno.hp.value+" л.c., ";

   if(document.anno.is_new[0].checked==true)
   var text = /*man+" "+model+((gen!='')?" "+gen:'')+*/": "+ year + eng + hp + milage + col + ((document.anno.price.value!=''&&document.anno.price.value!='0')? document.anno.price.value+" "+cur+",": "")+" т.: "+document.anno.phone.value;
	else
   var text = /*man+" "+model+" "+((gen!='')?" "+gen:'')+*/": "+year+"без пробега, " + col + eng + hp +((document.anno.price.value!=''&&document.anno.price.value!='0')? document.anno.price.value+" "+cur+",": "")+" т. "+document.anno.phone.value;

/*if((man!="" && model!="" && year!="" && document.anno.milage.value!="" && document.anno.price.value!="" &&cur!="" && document.anno.phone.value!="" && document.anno.is_milage[0].value==1) || (man!="" && model!="" && year!="" && document.anno.price.value!="" &&cur!="" && document.anno.phone.value!="" && document.anno.is_milage[1].checked==true)){     */

if(man!="" && model!="" && document.anno.price.value!="" &&cur!="" && document.anno.phone.value!=""){

	document.anno.dsc_adv.value = text

	document.getElementById("sym").innerHTML = "Осталось "+(250 - document.anno.dsc_adv.value.length)

		document.anno.dsc_adv.onkeydown = function(e){
			  	var evt = e || window.event;
			  	if((250 - document.anno.dsc_adv.value.length)>=0){
					var txt = "Осталось "+(250 - document.anno.dsc_adv.value.length)
					document.getElementById("sym").innerHTML = txt
				}

			   	else if(evt.keyCode!=8 && evt.keyCode!=46)
					return false;
			}
		}
	else {
         document.getElementById("sym").innerHTML = "Заполните все поля отмеченные (*)"
		 document.anno.dsc_adv.disabled = true;
		 document.anno.print.checked = false;
	}
	}
    else{
		   document.anno.dsc_adv.value = "";
	       document.anno.dsc_adv.disabled = true;
		   document.getElementById("sym").innerHTML = "Ограничен 250 символами"
	 	   }
}

function nums(e,type,input){
			if (!e) var e = window.event
			if (e.keyCode) code = e.keyCode;
			else if (e.which) code = e.which;

			/*
			// TYPE description
			// 0 - text
			// 1 - int
			// 2 - float  (. ,)
			*/
                              //alert(code)
/* 			    switch(type){
	           case 1:
				   if (((code < 48) || (code > 57)) && (code!=8) && (code!=46) && (code!=44))
				   e.preventDefault? e.preventDefault() : e.returnValue = false;
			   break;
	           case 2:
*/				   if(input.value.indexOf(",")==-1 && input.value.indexOf(".")==-1){
				       if (input.value==''){
		 				   if (((code < 49) || (code > 57)) && (code!=8))
						   e.preventDefault? e.preventDefault() : e.returnValue = false;
				       }
					   else{
						   if (((code < 48) || (code > 57)) && (code!=8) && (code!=46) && (code!=44))
						   e.preventDefault? e.preventDefault() : e.returnValue = false;
						   }
					   }
	               else{
					   if (((code < 48) || (code > 57)) && (code!=8))
					   e.preventDefault? e.preventDefault() : e.returnValue = false;
					   }
/*			   break;
		   }
*/
}

function convert(inp,out,k,unit,ok){
//	if(!ok){
	// if (inp.value.indexOf(',')!=-1){
	// 	inp.value.replace(',','.');     alert("!.!"+inp.value)
	//	}
//   		if (inp.value.charAt(inp.value.length-1)!='.') {

			var str = inp.value;			
			if(str.indexOf(",")!=-1)
				str = str.replace(/,/,".")  
	 		var a = Math.floor(str*k)+unit;
			document.getElementById(out).innerHTML = a; 
	
//		}
//	}
//	else
//	document.getElementById(out).innerHTML = "";
}

function newsToogle(){
	var its = document.getElementById("e2e4").getElementsByTagName('a');

	if(readCookie('news')!==null){
		toogleLinks(its);
	}
	for (var i=0; i<its.length; i++){		
		if(readCookie('news')!==null && its[i].rel == readCookie('news')) {
				its[i].className = 'linkz act';
				document.getElementById(its[i].rel).style.display = 'block';
		}
		its[i].onclick = function(){
			toogleLinks(its);
			this.className = 'linkz act';
			document.getElementById(this.rel).style.display='block';
			setcookie('news',this.rel,7);				
			return false;	
		}
	}
}
function setcookie(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 toogleLinks(its){
	for (var i=0; i<its.length; i++){
		its[i].className = 'linkz';
		document.getElementById(its[i].rel).style.display='none';
	}	
}

function toLogin(){
	if (document.forms[0].login) {
		document.forms[0].login.focus();
		var t = document.forms[0].login.parentNode.parentNode.parentNode.parentNode;
	/*	t.style.backgroundColor = "#DB2E07";
		setTimeout(function(){
			t.style.backgroundColor = "#E47D1E"
		}, 500)*/
		flashMe(t, 6)
	}
}

function flashMe(obj, t){
	var colors = ["#DB2E07","#E47D1E"];
	obj.style.backgroundColor = colors[t%2];
	if(t>1)
		setTimeout(function(){flashMe(obj, t-1)}, 500)
}

