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

var photoGallery = function(id){
	if (id!=undefined)this.id = '_'+id;
	else this.id='';
	this.images = new Array();
	this.current = 0;
	this.position = 0;
	this.length = 0;
	this.frame = 5;
	this.ads = 0;
}
photoGallery.prototype.add = function (pathSmall,pathLarge){
		this.images[this.length] = new Array();
		if (this.length<9) {
			this.images[this.length]['small'] = new Image();
			this.images[this.length]['large'] = new Image();
		} else {
			this.images[this.length]['small'] = new Object();
			this.images[this.length]['large'] = new Object();
		}
		this.images[this.length]['small'].src = pathSmall;
		this.images[this.length]['large'].src = pathLarge;
		this.length++;
	}

photoGallery.prototype.print = function(){
		html = '<div class="photo-left">';
		html += '<div class="photo-big"><a href="#"><img id="photoGalleryImgLarge'+this.id+'" src="'+this.images[0]['large'].src+'" width="480" height="320" alt=""></a></div>';
		html += '<div class="photo-other ph-img"><div class="fotoGalery">';
		html += '<div class="btn_l"><a onclick="photoGallery'+this.id+'.prev(); return false" href="#"><div id="photoGalleryLinkPrev'+this.id+'" class="no_act"></div></a></div>';
		html += '<div class="center"><ul class="cfix">';
		for (i = 0; i<this.length && i<this.frame; i++) {
			html += '<li><a class="pic" onclick="photoGallery'+this.id+'.setActPosition('+i+'); return false" style="cursor:pointer;"><img '+((i==0)?'class="act"':'')+' id="photoGalleryImgSmall'+this.id+'_'+i+'" src="'+this.images[i]['small'].src+'" width="95" height="63" alt=""></a></li>';
		}
		html += '</ul></div>';
		html += '<div class="btn_r"><a onclick="photoGallery'+this.id+'.next(); return false" href="#" >'+((this.length>6)?'<div id="photoGalleryLinkNext'+this.id+'"></div>':'')+'</a></div>';
		html += '</div><br clear="all"/>' +
				'<div class="show-zoom"><span class="ico-set"></span>Кликните на картинку для увеличения</div>' +
				'</div>';
		html += '</div>';
		document.write(html);
	}

photoGallery.prototype.next = function(){
		if (this.current<this.length-1){
			if (this.position<this.frame-1) {
				this.setActPosition(this.position+1);
			} else {
				this.current++;
				this.refreshFrame();
				this.showCurrentImgLarge();
			}
		}
	}

photoGallery.prototype.prev = function(){
		if (this.current>0){
			if (this.position>0) {
				this.setActPosition(this.position-1);
			} else {
				this.current--;
				this.refreshFrame();
				this.showCurrentImgLarge();
			}
		}

	}

photoGallery.prototype.setActPosition = function(position){
		$('photoGalleryImgSmall'+this.id+'_'+this.position).className='';
		oldPosition = this.position;
		this.position = position;
		$('photoGalleryImgSmall'+this.id+'_'+this.position).className='act';
		this.current=this.current-oldPosition+this.position;
		this.showCurrentImgLarge();
	}

photoGallery.prototype.refreshFrame = function(){
		startFramePos=this.current-this.position;
		for (i=0; i<this.frame && i<this.length; i++){
			$('photoGalleryImgSmall'+this.id+'_'+i).src=this.images[i+startFramePos]['small'].src;
		}
	}

photoGallery.prototype.correctArrows = function(){

		if (this.current==0) {
			$('photoGalleryLinkPrev'+this.id).className='no_act';
		} else {
			$('photoGalleryLinkPrev'+this.id).className='';
		}

		if ($('photoGalleryLinkNext'+this.id))
			if (this.current==this.length-1) {
				$('photoGalleryLinkNext'+this.id).className='no_act';
			} else {
				$('photoGalleryLinkNext'+this.id).className='';
			}

	}

photoGallery.prototype.showCurrentImgLarge = function(){
		$('photoGalleryImgLarge'+this.id).src=this.images[this.current]['large'].src;
		this.correctArrows();
	}

photoGallery.prototype.openPopup = function(){
		popup = window.open('/photos/?ads='+this.ads+'&n='+this.current,'popup', 'width=620,height=520');
		popup.focus();
	}

/// -----compare
function toggleCompare(obj){
	if (!obj) return false;

	var exist_compare = getCookie2('compare');
	if (exist_compare){
		exist_compare= exist_compare.replace(/^,+|,+$/g,"");
		if(exist_compare.split(',').length==4){
			alert('В списке сравнения может быть не более 4 автомобилей.');
			return false;
		}
	}

	img = Ext.get(obj).child('div');
	var id = obj.id.slice(3);
	img.setStyle('cursor', 'wait');

	if (!img.hasClass('act') ){
		Ext.Ajax.request({
        url: '/ajax/auto/compare.php?' + Math.random(),
        success: function(response) {
            response = Ext.decode(response.responseText);
            if(!response.success) {
                alert(response.msg);
            }
            else {
            	var ul_obj = Ext.get('compare_right_id').child('ul');
            	var li_node = '<li id="li_'+id+'"><div class="ico-set"></div><a href=" _url_ "> _title_ </a></li>';
            	li_node = li_node.replace(/_url_/g, response.data.url);
            	li_node = li_node.replace(/_title_/g, response.data.title);
            	ul_obj.createChild({html: li_node});
            }
        },
        failure: function() { },
        params: {id:id, op:'get_item'}
        });
		compareAdd(id);
		img.addClass('act');
		img.setStyle('cursor', 'pointer');
	}else if(img.hasClass('act')){
		Ext.get('li_'+id).remove();
		compareDel(id);
		img.removeClass('act');
		img.setStyle('cursor', 'pointer');
	}
	var all_compare = getCookie2('compare');

	if (all_compare==null || all_compare.trim()==',' || all_compare.trim()=='')
		$('compare_right_id').style.display='none';
	else
		$('compare_right_id').style.display='';
}

function compareAdd(id){
	var cur_cookie;
	cur_cookie = getCookie2('compare');
	if (!cur_cookie) cur_cookie = ',';
	cur_cookie = cur_cookie + id + ',';
	setCompare( cur_cookie,0);
}
function setCompare(items, remove){
	var modifer = (remove ? -1 : 1);
	var dd = new Date();
	var expire_days = ($('site_cookie_lifetime')) ? $('site_cookie_lifetime').value : 20;
	dd.setDate(dd.getDate() + modifer * expire_days);
	setCookie2('compare', items, dd.toGMTString(), '/', $('site_cookie_path').value);

}
function compareDel(id){
	var cur_cookie;
	cur_cookie = getCookie2('compare');
	if (!cur_cookie) return;
	cur_cookie = cur_cookie.replace(','+id, '');
	setCompare( cur_cookie,0);
}
function delAllCompare(){
	var cur_cookie = getCookie2('compare');
	setCompare('',-1);
	$('compare_right_id').style.display='none';
	Ext.each(Ext.query('div[class="ico-set ico-auto act"]'), function(el){el.className="ico-set ico-auto " ;});
	Ext.each(Ext.query('div[class="ico-set ico-auto  act"]'), function(el){el.className="ico-set ico-auto " ;});
	Ext.each(Ext.get('compare_right_id').query('li[id^=li_]'),function(el){Ext.removeNode(el) ;});
}

function comparePhotoChange(){
	goTo('/autocompare/photo/?mod='+$('select_1').value+','+$('select_2').value);
}
function deleteCar(id){
	compareDel(id);
	goTo('/autocompare/');
}
function resetCompareFields(id){
	$('markSelect_'+id).selectedIndex = '';
	$('car_img_'+id).src = '/site/pic/auto/autoirr_compare1_03.jpg';
	$('modelsSelect_'+id).innerHTML = '';
	$('bodiesSelect_'+id).innerHTML = '';
	$('modificationSelect_'+id).innerHTML = '';
}

/// -----/compare

function clearStars() {
	for(var i=1; i<=5; i++) {
		$('voting-'+i).className = 'ico-set ico-whiteStar';
	}
}

function showStars(starnum) {
	for(var i=1; i<=starnum; i++) {
		$('voting-'+i).className = 'ico-set ico-yellowStar';
	}
}

function vote(photoId, vote) {

	Ext.Ajax.request({
        url: '/ajax/auto/gallery_voting.php?' + Math.random(),
        success: function(response) {
            response = Ext.decode(response.responseText);
            if(!response.success) {
                alert(response.msg);
            }
            else {
				for(var i=1; i<=5; i++) {
					$('voting-'+i).setAttribute('onmouseover', '');
					$('voting-'+i).setAttribute('onmouseout', '');
					$('voting-'+i).setAttribute('onclick', '');

					if(i <= response.vote) {
						$('voting-'+i).className = 'ico-set ico-yellowStar';
					}
					else {
						$('voting-'+i).className = 'ico-set';
					}

					if(i <= response.average_int) {
						$('votes-'+i).className = 'ico-set ico-yellowStar';
					}
					else {
						$('votes-'+i).className = 'ico-set';
					}
				}

				$('average').innerHTML = response.average;
				$('votes-count').innerHTML = response.votes_count;
            }
        },
        failure: function() { },
        params: {photo:photoId, vote:vote}
    });
}
