String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

function getModifications(cmark, cmodel, cbody){
	new Ajax.Request('/data/tpl-ajax-modifications/', {
        parameters : {mark:cmark, model:cmodel, body:cbody},
        onSuccess  : function(transport, json) {
            if (json.success){
                $('modifications-div').innerHTML=json.output;
				$('modifications').disabled=false;
			} else {
               
			}
        }
    });
	$('modifications').disabled=true;
}

function getBodies(cmark, cmodel){
	new Ajax.Request('/data/tpl-ajax-bodies/', {
        parameters : {mark:cmark, model:cmodel},
        onSuccess  : function(transport, json) {
            if (json.success){
                $('bodies-div').innerHTML=json.output;
				$('bodies').disabled=false;
			} else {
               
			}
        }
    });
	$('bodies').disabled=true;
}

function getModels(cmark, def){
	if(!cmark){
		clearModellist();
		return false;
	}
	if (def){
		var params={mark: cmark, select: def};
	}else{ var params={mark: cmark}; };
	var url =  $('bodies-div') ? '/data/tpl-ajax-models-ext/' : '/data/tpl-ajax-models/';
	new Ajax.Request(url, {
        parameters : params,
        onSuccess  : function(transport, json) {
            if (json.success){
                $('models-div').innerHTML=json.output;
				enableModellist();
			} else {
			}
        }
    });
	if ($('models')) {
		$('models').disabled=true;
	}
}

function enableModellist(o){
	if ($('models')) {
		$('models').removeAttribute('disabled');
	}
}

function clearModellist(){	
	$('models-div').innerHTML = '<select name="models" id="models"><option value="" selected="selected">Выберите модель</option></select>';
}

function openPopup(id){
	window.open(window.location.href + 'data/tpl-popup_photo/?imgid='+id, "popupwindow","scrollbars=0,resizable=1,width=800,height=600");
}

function openPopup_ingalery(id){
	window.open(window.location.href + 'data/tpl-popup_photo_ingalery/?imgid='+id, "popupwindow","scrollbars=0,resizable=1,width=800,height=600");
}

function modelGo(){
	if ($('models').value!=0) window.location.href=$('models').value;
	else if ($('mark').value!=0) window.location.href=$('mark').value;
}

//function showBigFigure(fileName,alt) {
//	var regExpFileName = /[^\/]+$/ig;
//	var oImg = $('figureSrc');
//	oImg.src = oImg.src.replace(regExpFileName,fileName);
//}

function showBigFigure(fileName,alt, hugeLink) {
	$('figureLink').href=hugeLink;
	$('figureSrc').src=fileName;
	$('figureSrc').alt=alt;
	$('figureSrc').title=alt;
	$('popup-info').innerHTML=alt;
}

function showComparePhotos(o, photo1, photo2) {
    var oImg1 = $('idCompareFotoCar1');
    var oImg2 = $('idCompareFotoCar2'); 
    oLinks = o.parentNode.parentNode.getElementsByTagName("A");
    for(var i=0; oLinks.length; i++){
        if(oLinks[i].className == "act") {
            oLinks[i].className = "";
            break;
        }
    }
    o.className = "act";
    oImg1.src = photo1;
    oImg2.src = photo2;
}

function showComparePhoto(photo1, photo2) {
    var oImg1 = $('idCompareFotoCar1');
    var oImg2 = $('idCompareFotoCar2'); 
    /*
    oLinks = o.parentNode.parentNode.getElementsByTagName("A");
    for(var i=0; oLinks.length; i++){
        if(oLinks[i].className == "act") {
            oLinks[i].className = "";
            break;
        }
    }
    o.className = "act";
    */
    oImg1.src = photo1;
    oImg2.src = photo2;
}

function showCompareFoto(o,fileName, n) {
	var regExpFileName = /[^\/]+$/ig;
	var oImg = $('idCompareFotoCar'+n);
	oLinks = o.parentNode.parentNode.getElementsByTagName("A");
	for(var i=0; oLinks.length; i++){
		if(oLinks[i].className == "act") {
			oLinks[i].className = "";
			break;
		}
	}
	o.className = "act";
	//oImg.src = oImg.src.replace(regExpFileName,fileName);
    oImg.src = fileName;
}

function changeCar(n) {
	for(var i=1; $('idCompareIcons'+i); i++){
		$('idCompareIcons'+i).style.display = "none";
	}
	$('idCompareIcons'+n).style.display = "block";

}


function bookmark(title, url) {
	if (window.sidebar) {
		// Firefox
		window.sidebar.addPanel(title, url, '');
	} else if (window.opera && window.print) {
		// Opera
		var t = document.createElement('a');
		t.setAttribute('rel', 'sidebar');
		t.setAttribute('href', url);
		t.setAttribute('title', title);
		t.click();
	} else {
		// IE
		window.external.AddFavorite(url, title);
	}
	return false;
}


function bookmarksite() {
	var title='Автомания.ру';
	var url = 'http://www.automania.ru';
	if (window.sidebar) // firefox
	window.sidebar.addPanel(title, url, "");
	else if (window.opera && window.print) { // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	} else if (document.all)// ie
	window.external.AddFavorite(url, title);
	return false;
}



/* -----for select in IE----- */

function foo(px,py,pw,ph,baseElement,fid)
{
		var win = document.getElementById(this.fid);
}


function dropdown_menu_hack(el)
{
	if(el.runtimeStyle.behavior.toLowerCase()=="none"){return;}
	el.runtimeStyle.behavior="none";

	var ie5 = (document.namespaces==null);
	el.ondblclick = function(e)
	{
		window.event.returnValue=false;
		return false;
	}
	
	if(window.createPopup==null)
	{
		
		var fid = "dropdown_menu_hack_" + Date.parse(new Date());
	
		window.createPopup = function()
		{
			if(window.createPopup.frameWindow==null)
			{
					el.insertAdjacentHTML("AfterEnd","<iframe   id='"+fid+"' name='"+fid+"' src='about:blank'  frameborder='1' scrolling='no'></></iframe>");
					var f = document.frames[fid];
					f.document.open();
					f.document.write("<html><body></body></html>");
					f.document.close();
					f.fid = fid; 
					

					var fwin = document.getElementById(fid);
					fwin.style.cssText="position:absolute;top:0;left:0;display:none;z-index:99999;";
				
					
					f.show = function(px,py,pw,ph,baseElement)
					{							
						py = py + baseElement.getBoundingClientRect().top + Math.max( document.body.scrollTop, document.documentElement.scrollTop) ;
						px = px + baseElement.getBoundingClientRect().left + Math.max( document.body.scrollLeft, document.documentElement.scrollLeft) ;
						fwin.style.width = pw + "px";
						fwin.style.height = ph + "px";						
						fwin.style.posLeft =px ;
						fwin.style.posTop = py ;		
						fwin.style.display="block";						
					}

					
					f_hide = function(e)
					{ 
						if(window.event && window.event.srcElement	&& window.event.srcElement.tagName && window.event.srcElement.tagName.toLowerCase()=="select"){return true;}
						fwin.style.display="none";
					} 
					f.hide = f_hide;
					document.attachEvent("onclick",f_hide);		
					document.attachEvent("onkeydown",f_hide);		
					
			}
			return f;
		}
	}

	function showMenu()
	{
		
		function selectMenu(obj)
			{				
				var o = document.createElement("option");
				o.value = obj.value;
				o.innerHTML = obj.innerHTML;			
				while(el.options.length>0){el.options[0].removeNode(true);}
				el.appendChild(o);
				el.title =  o.innerHTML; 
				el.contentIndex = obj.selectedIndex  ;
				el.menu.hide(); 				
			}		
		
		
		el.menu.show(0 , el.offsetHeight , 10,  10, el); 
		var mb = el.menu.document.body;
		
		mb.style.cssText ="border:solid 1px #7f9db9; _border:solid 1px #000000; margin:0;padding:0;overflow-y:auto;overflow-x:auto;background:white;text-aligbn:center;font-family:Tahoma,Arial,Helvetica,sans-serif;font-size:8.5pt;";
		var t = el.contentHTML;
		t = t.replace(/<select/gi,'<ul');
		t = t.replace(/<option/gi,'<li');
		t = t.replace(/<\/option/gi,'</li');
		t = t.replace(/<\/select/gi,'</ul');
		mb.innerHTML = t;	
	
		
		el.select = mb.all.tags("ul")[0];
		el.select.style.cssText="list-style:none;margin:0;padding:0;";
		mb.options = el.select.getElementsByTagName("li");
		
		for(var i=0;i<mb.options.length;i++)
		{
			mb.options[i].selectedIndex = i;
			mb.options[i].style.cssText = "list-style:none;margin:0;padding:1px 4px;width/**/:100%;cursor:hand;cursor:pointer;white-space:nowrap;"
			mb.options[i].title =mb.options[i].innerHTML;
			mb.options[i].innerHTML ="<nobr>" + mb.options[i].innerHTML + "</nobr>";
			mb.options[i].onmouseover = function()
				{
					if( mb.options.selected ){mb.options.selected.style.background="white";mb.options.selected.style.color="black";}
					mb.options.selected = this;
					this.style.background="#316ac5";this.style.color="white";
				}
			
			mb.options[i].onmouseout = function(){this.style.background="white";this.style.color="black";}
			mb.options[i].onmousedown = function(){selectMenu(this);	}
			mb.options[i].onkeydown = function(){selectMenu(this);	}
				

			if(i == el.contentIndex)
			{
				mb.options[i].style.background="#316ac5";
				mb.options[i].style.color="white";	
				mb.options.selected = mb.options[i];
			}
		}
	
		
		var mw = Math.max(   ( el.select.offsetWidth + 22 ), el.offsetWidth + 22  );
			 mw = Math.max(  mw, ( mb.scrollWidth+22) );
		var mh =  mb.options.length * 15  + 8 ; 
			 
		var mx = (ie5)?-3:0;
		var my = el.offsetHeight -2;
		var docH =   document.documentElement.offsetHeight ;
		var bottomH = docH  - el.getBoundingClientRect().bottom ; 

		mh = Math.min(mh, Math.max(( docH - el.getBoundingClientRect().top - 50),100)		);
		
		if(( bottomH < mh) )
		{
			
			mh = Math.max( (bottomH - 12),10);
			if( mh <100 ) 
			{
				my = -100 ;

			}
			mh = Math.max(mh,100);			
		}

		
		self.focus(); 
		
		el.menu.show( mx , my ,  mw, mh , el); 
		sync=null;
		if(mb.options.selected)
		{
			mb.scrollTop = mb.options.selected.offsetTop;
		}
	

		
		
		window.onresize = function(){el.menu.hide()};		
	}

	function switchMenu()
	{
		if(event.keyCode)
		{
			if(event.keyCode==40){ el.contentIndex++ ;}
			else if(event.keyCode==38){ el.contentIndex--; }
		}
		else if(event.wheelDelta )
		{
			if (event.wheelDelta >= 120)
			el.contentIndex++ ;
			else if (event.wheelDelta <= -120)
			el.contentIndex-- ;
		}else{return true;}




		if( el.contentIndex > (el.contentOptions.length-1) ){ el.contentIndex =0;}
		else if (el.contentIndex<0){el.contentIndex = el.contentOptions.length-1 ;}

		var o = document.createElement("option");
			 o.value = el.contentOptions[el.contentIndex].value;
			 o.innerHTML = el.contentOptions[el.contentIndex].text;
			 while(el.options.length>0){el.options[0].removeNode(true);}
			 el.appendChild(o);
			 el.title =  o.innerHTML; 
	}
	
	if(dropdown_menu_hack.menu ==null)
	{
		dropdown_menu_hack.menu =  window.createPopup();
		document.attachEvent("onkeydown",dropdown_menu_hack.menu.hide);
	}
	el.menu = dropdown_menu_hack.menu ;
	el.contentOptions = new Array();
	el.contentIndex = el.selectedIndex;
	el.contentHTML = el.outerHTML;

	for(var i=0;i<el.options.length;i++)
	{	
		el.contentOptions [el.contentOptions.length] = 
		{
			"value": el.options[i].value,
			"text": el.options[i].innerHTML
		}

		if(!el.options[i].selected){el.options[i].removeNode(true);i--;};
	}

	
	el.onkeydown = switchMenu;
	el.onclick = showMenu;
	el.onmousewheel= switchMenu;

}
/* -----/for select in IE----- */