function addFav() {
	if (document.all){
		window.external.AddFavorite("http://www.avtopoisk.ua","АвтоПоиск.ua - поисковик объявлений");
	}else if (window.sidebar) { 
		window.sidebar.addPanel("АвтоПоиск.ua - поисковик объявлений", "http://www.avtopoisk.ua",""); 
	}else{
		alert('Чтобы добавить сайт в Избранное, нажмите Ctrl+D');
	}
	return false;
}
function setHomepage(){
	if (document.all){
		document.body.style.behavior='url(#default#homepage)';
		document.body.setHomePage('http://www.avtopoisk.ua/');
	}else{
		alert('Эта функция не работает в Вашем браузере. Постарайтесь сделать страницу стартовой используя настройки браузера');
	}
	return false;
}


function topNavShortcut(event)
{
	if (!document.getElementById) return;
	if (window.event) event = window.event;
	if (event.ctrlKey)
	{
		var code = event.keyCode ? event.keyCode : event.which ? event.which : null;
		var id = '';
		switch (code)
		{
			case 37:
				id = 'previous_page';
				break;
			case 39:
				id = 'next_page';
				break;
		}
		if (id)
		{
			var link = document.getElementById(id);
			if (link && link.href) document.location = link.href;
		}
	}			
}
document.onkeydown = topNavShortcut;


//Кукис
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 show_pics(obj){
	var toshowpics=readCookie('toshowpics');
	if(obj.checked==true){
		var sel = document.getElementsByTagName('var');
		for (var i = 0; i < sel.length; i++){
			sel[i].style.display = 'block';
		}
		createCookie('toshowpics',1,30);
	}else{
		var sel = document.getElementsByTagName('var');
		for (var i = 0; i < sel.length; i++){
			sel[i].style.display = 'none';
		}
		createCookie('toshowpics',2,30);
	}
}
function open_wind(link,w,h,imia,scrol,menu) {
	if(scrol==''){scrol='no';}
	if(menu==''){menu='no';}
	var win = "width="+w+",height="+h+",menubar="+menu+",location=no,resizable=no,scrollbars="+scrol;
	newWin = window.open(link,imia,win);
	newWin.focus();
}


window.addEvent('domready', function() {	
	var request = new Request.JSON({
		url: '/include/getmodels.php',
		onComplete: function(jsonObj) {
			$('s_mid').length = 0;
			$('s_mid').options[0] = new Option('-- любая --', '');
			var i=1;
			jsonObj.bazarmodels.each(function(model) {
				if(model.id==parseInt($('s_mark').getProperty('default'))){
					$('s_mid').options[i] = new Option(model.title, model.id, false,true);
				}else{
					$('s_mid').options[i] = new Option(model.title, model.id);
				}
				i++;
			});
			$('s_mid').disabled=false;
		}
	});									 
	$('s_mark').addEvent('change', function(){
		$('s_mid').length = 0;
		$('s_mid').options[0] = new Option('подождите ...', '');
		$('s_mid').disabled=true;
		request.get({'mark': this.value});
	});
	$$('.changablekurs').addEvent('change', function(){								  
		if($('cur1').value>0){
			//alert($('cur2').value)
			if($('cur1').value==$('cur2').value){
				$('curv2').value=$('curv1').value;
			}else{
				if($('cur1').value==1 && $('cur2').value==3){
					$('curv2').value=$('curv1').value*$('kurs0').innerHTML;
				}
				if($('cur1').value==1 && $('cur2').value==2){
					$('curv2').value=$('curv1').value*$('kurs0').innerHTML/$('kurs2').innerHTML;
				}
				if($('cur1').value==2 && $('cur2').value==3){
					$('curv2').value=$('curv1').value*$('kurs2').innerHTML;
				}
				if($('cur1').value==2 && $('cur2').value==1){
					$('curv2').value=$('curv1').value*$('kurs2').innerHTML/$('kurs1').innerHTML;
				}
				if($('cur1').value==3 && $('cur2').value==1){
					$('curv2').value=$('curv1').value/$('kurs1').innerHTML;
				}
				if($('cur1').value==3 && $('cur2').value==2){
					$('curv2').value=$('curv1').value/$('kurs3').innerHTML;
				}
				$('curv2').value=Math.round($('curv2').value*100)/100;
			}
		}
	});
	if($('curv2')){
		$('curv2').value=$('curv1').value*$('kurs0').innerHTML;
		$('curv2').value=Math.round($('curv2').value*100)/100;
	}
	if($('poleform')){
		$('poleform').addEvent('submit', function(e) {
			e.stop();
			$('polediv').setStyle('height',$('polediv').getHeight());
			$('poleform').setStyle('visibility','hidden');
			$('polediv').addClass('spinner2');
			var n=$('polediv').getProperty('n');
			var h=135+(n*21)+15;
			this.set('send', {onComplete: function(response) { 
				$('polediv').removeClass('spinner2');
				if(response){
					$('polediv').setStyle('height','');
					var so = new SWFObject("/ampie/ampie.swf", "ampie", "180", h, "8", "#FFE8E8");
					so.addVariable("path", "/ampie/");
					so.addVariable("chart_settings", encodeURIComponent("<settings><text_color>666666</text_color><pie><colors>FF0F00,FF9E01,F8FF01,B0DE09,04D215,0D8ECF,2A0CD0,8A0CCF,CD0D74,754DEB,DDDDDD,999999,333333,990000</colors><y>65</y><radius>50</radius><height>2</height></pie><add_time_stamp>1</add_time_stamp><thousands_separator>.</thousands_separator><precision>0</precision><balloon><max_width>160</max_width><corner_radius>5</corner_radius></balloon><legend><x>0</x><y>135</y><text_size>12</text_size><spacing>2</spacing><key><size>14</size><border_color>999999</border_color></key></legend><animation><start_time>0.3</start_time><start_effect>regular</start_effect><pull_out_time>0.2</pull_out_time><pull_out_effect>regular</pull_out_effect><pull_out_only_one>1</pull_out_only_one></animation><data_labels><show>{percents}%</show><radius>18%</radius><text_size>11</text_size><max_width>140</max_width><line_alpha>24</line_alpha><avoid_overlapping>0</avoid_overlapping></data_labels></settings>"));
					so.addVariable("chart_data", encodeURIComponent(response));
					so.write("polediv");
					$('allpoles').setStyle('display','block')
					
					//$('polediv').empty().set('html', response);
				}else{
					$('poleform').setStyle('visibility','visible');
				}
			}});
			this.send();

		});
	}
	$$('.dublink').addEvent('click', function(e){
		e.stop();
		id=this.getProperty('did');
		if($('dubs'+id).getStyle('display')=='none'){
			$('dubs'+id).setStyle('display','block');
		}else{
			$('dubs'+id).setStyle('display','none');
		}
	});
	var myTips = new Tips('.tips');
	myTips.addEvent('show', function(tip, el){
		tip.fade('in');
	});
	myTips.addEvent('hide', function(tip, el){
		tip.fade('out');
	});
	if($$('.b_wrong')){
		$$('.b_wrong').addEvent('click', function(e){
			e.stop();
			var id=this.getProperty('id');
			var site=this.getProperty('site');
			$('icon_wrong'+id).src='/images2/spinner.gif';
			new Request.JSONP({
			  url: 'http://img.avtopoisk.ua/sold.php',
				data: {
					 car: id,
					 site: site,
					 wrong: 1
				}
			}).send();
		});
	}
	

});
function apply_wrong(status,id){
	if(status==1){
		$('icon_wrong'+id).src='/icon/smiley_evil.png';
		//$('b_wrong').disabled=true;
	}else if(status==2){
		$('icon_wrong'+id).src='/icon/tick_circle.png';
		//$('b_wrong').disabled=true;
	}else{
		$('icon_wrong'+id).src='/icon/exclamation.png';
	}
}