jQuery(document).ready(function() { if($.browser.msie && parseFloat($.browser.version) < 7) { $(window).resize(function(){$('#nameselect').hide();setTimeout(function(){$('#nameselect').show()},50)}) }; $('#slideshow').cycle({fx:'fade',speed:2500,timeout:9000}); $('.rekmat').click(function(){if(!reklClick(this)) return false;}); mySearchh=new mySearch(); $('.imgslideshow').cycle({fx:'fade',speed:2500,timeout:9000}); var partCarousel=new myCarousel('carousel'); var popUp=new myPopUp(); var my_menu=new MMenu(); initDownload(); $('.slide-ten').css({ background:'#000', opacity:0.3 }); });

function reklClick(elem) {
	if(confirm(elem.title+'?')) return true;
	return false;
}

function loadSlideShow() {
	$.get('/scripts/slideshows.php',{},loadSlideShowComplete);
}

function loadSlideShowComplete(data) {
	$('#slideshow').html(data).cycle({fx:'fade',speed:2500,timeout:9000});
}

function myCarousel(sid){
	this.sid=sid;
	this.countLoadImage=0;
	this.totWidth=0;
	this.firstLeft=0;
	this.secondLeft=0;
	this.timeOut=10;
	this.change=1;
	this.init();
}

myCarousel.prototype.init=function() {
	$('#'+this.sid).css({ position:'relative' });
	this.images=$('#'+this.sid+' img').get();
	var cnt=this;
	this.countImg=this.images.length;
	for(i=0;i<this.countImg;i++) {
		if((this.images[i].width === undefined) || ((this.images[i].complete!==undefined) && (!this.images[i].complete))) {
			this.images[i].onload=function(){cnt.loadImageOK()};
		}
		else this.countLoadImage++;
	}
	if(this.countLoadImage==this.countImg) this.isLoadAllImages();
}

myCarousel.prototype.loadImageOK=function() {
	if(this.countLoadImage!=this.countImg) {
		this.countLoadImage++;
		if(this.countLoadImage==this.countImg) this.isLoadAllImages();
	}
}

myCarousel.prototype.isLoadAllImages=function() {
	for(i=0;i<this.countImg;i++) {
		if($.browser.msie && parseFloat($.browser.version)<7) {
			if(typeof(spacer)=='undefined') {
				var spacer=new Image();
				spacer.src='http://img.garantcom.ru/img/spacer.gif';
			}
			var old_src=$(this.images[i]).attr('src');
			$(this.images[i]).attr('src',spacer.src);
			this.images[i].style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+old_src+"', sizingMethod='crop')";
		}
		this.totWidth+=$(this.images[i]).outerWidth()+4;
	}
	this.totWidth-=115;
	this.firstLayer=$('<div></div>');
	this.firstLayer.css({ height:$('#'+this.sid).height()+'px', width:this.totWidth+'px', position:'absolute', top:0, left:0 }).html($('#'+this.sid).html());
	this.secondLayer=$('<div></div>');
	this.secondLayer.css({ height:$('#'+this.sid).height()+'px', width:this.totWidth+'px', position:'absolute', top:0, left:this.totWidth+'px' }).html($('#'+this.sid).html());
	$('#'+this.sid).html('');
	$('#'+this.sid).append(this.firstLayer).append(this.secondLayer);
	this.secondLeft=this.totWidth;
	this.start();
}

myCarousel.prototype.start=function() {
	var cnt=this;
	
	if(this.firstLeft!=-this.totWidth) {
		this.firstLeft=(this.firstLeft-this.change);
	}
	else{
		this.firstLeft=this.totWidth;
	}
	this.firstLayer.css({ left:this.firstLeft+'px' });
	
	if(this.secondLeft!=-this.totWidth) {
		this.secondLeft=(this.secondLeft-this.change);
	}
	else{
		this.secondLeft=this.totWidth;
	}
	this.secondLayer.css({ left:this.secondLeft+'px' });
	
	setTimeout( function(){cnt.start();},this.timeOut);
}

function mySearch() {
	this.a=1;
	var el=this;
	$('#nameselect').ajaxComboBox(
		'/scripts/ajaxComboBox.php',
		{
			'img_dir'	  : '/scripts/js/ajaxcombo/img/',
			'minchars'	  : 3,
			'navi_num'    : 10, 
			'mini'		  : true,
			'select_only' : true,
			'fnselectOK'  : function(a,b) { $('#submit').removeAttr('disabled','1'); $('#tovid').val(a); },
			'fnselectERR' : function(a) { el.nameSelectClick(); },
			'fnUnSelect'  : function() { if($.browser.msie && parseFloat($.browser.version) < 7) $('#poisksub1').show(); $('#poisksub1select').removeAttr('disabled','1'); $('#submit').attr('disabled','1');}
		}
	);
	$('#poisksub1select').change(function(){el.selectSub(this)});
	$('#poisksub2select').change(function(){el.selectSub(this)});
	$('#poisksub3select').change(function(){el.selectSub(this)});
	$('#poisksub4select').change(function(){el.selectSub(this)});
}

mySearch.prototype.nameSelectClick=function(par) {
	$('#poisksub2, #poisksub3, #poisksub4, #poiskpr').hide();
	$('#poisksub1select').attr('disabled','1');
	$('#submit').removeAttr('disabled');
	$('#tovid').val('');
	$('#tovname').val($('#nameselect_1').val());
	if($.browser.msie && parseFloat($.browser.version) < 7) $('#poisksub1').hide();
}

mySearch.prototype.selectSub=function(par) {
	var el=this;
	switch(par.id) {
		case 'poisksub1select':
			document.getElementById('poisksub2select').options.length=0;
			$('#poisksub4').hide();
			$('#poisksub2').show();
			$('#poisksub3').hide();
			$('#poiskpr').hide();
			$('#hidsub').val('');
			$('#submit').attr('disabled','1');
			$.get('/scripts/poisk.php',{sub:par.value},function(data){el.loadLevelComplete(data,2)});
			break;
			
		case 'poisksub2select':
			document.getElementById('poisksub3select').options.length=0;
			$('#poisksub4').hide();
			$('#poisksub3').hide();
			$('#poiskpr').hide();
			$('#submit').attr('disabled','1');
			$('#hidsub').val('');
			$.get('/scripts/poisk.php',{sub:par.value},function(data){el.loadLevelComplete(data,3)});
			break;
		
		case 'poisksub3select':
			document.getElementById('poisksub4select').options.length=0;
			$('#poisksub4').hide();
			$('#poiskpr').hide();
			$('#submit').attr('disabled','1');
			$('#hidsub').val('');
			$.get('/scripts/poisk.php',{sub:par.value},function(data){el.loadLevelComplete(data,4)});
			break;
			
		case 'poisksub4select':
			document.getElementById('poisksubpr').options.length=0;
			$('#submit').removeAttr('disabled');
			$('#hidsub').val(par.value);
			$.get('/scripts/poisk.php',{pr:par.value},function(data){el.loadLevelComplete(data,5)});
			break;
	}
}

mySearch.prototype.loadLevelComplete=function(data,level) {
	switch(level) {
		case 2:
			this.createSpisok(data,'poisksub2select');
			break;
		case 3:
			if(data) { $('#poisksub3').show(); this.createSpisok(data,'poisksub3select'); }
			else { $('#hidsub').val($('#poisksub2select').val()); $('#submit').removeAttr('disabled'); var el=this; document.getElementById('poisksubpr').options.length=0; $('#poiskpr').show(); $.get('/scripts/poisk.php',{pr:$('#poisksub2select').val()},function(data){el.loadLevelComplete(data,5)}); }
			break;
		case 4:
			if(data) { $('#poisksub4').show(); this.createSpisok(data,'poisksub4select'); }
			else { $('#hidsub').val($('#poisksub3select').val()); $('#submit').removeAttr('disabled'); var el=this; document.getElementById('poisksubpr').options.length=0; $('#poiskpr').show(); $.get('/scripts/poisk.php',{pr:$('#poisksub3select').val()},function(data){el.loadLevelComplete(data,5)}); }
			break;
		case 5:
			if(data) {this.createSpisok(data,'poisksubpr');$('#poiskpr').show();}
			else {document.getElementById('poisksubpr').options.length=0;}
			break;
	}
}

mySearch.prototype.createSpisok=function(data,sid) {
	var $new_element=jQuery("<option></option>");
	$new_element.val('').html("---неважно---");
	$('#'+sid).append($new_element);
	eval('var ar='+data);
	for(i=0;i<ar.length;i++) {
		$new_element=jQuery("<option></option>");
		$new_element.val(ar[i][0]).html(ar[i][1]);
		$('#'+sid).append($new_element);
	}
}

function myPopUp() {
	this.timer=0;
	this.div=$('<div></div>');
	this.div.css({background:'#fff',width:'140px',height:'50px',border:'1px solid #ccc',position:'absolute',color:'#004091'}).html('<p style="margin:5px 0 0 13px;"><img src="/img/phone-min.png" alt="" style="position:relative; top:2px;"> <span style="color:#0072d1">(812)</span> 448-16-16</p><p style="margin:5px 0 0 13px;"><img src="/img/phone-min.png" alt="" style="position:relative; top:2px;"> <span style="color:#0072d1">(812)</span> 600-20-60</p>').hide();
	$('body').append(this.div);
	var con=this;
	$('#dphone').mouseover(function(e){con.mover(e);}).mouseout(function(){con.mout()});
}

myPopUp.prototype.mover=function(e) {
	var ee=e;
	if(this.div.css('display')=='none'&&!this.timer) {
		var con=this;
		this.timer=setTimeout(function(){con.div.show().css({left:ee.pageX+'px',top:ee.pageY+'px'});},500);
	}	
}

myPopUp.prototype.mout=function(e) {
	clearInterval(this.timer);
	this.timer=0;
	this.div.hide();
}

function MMenu() {
	this.timeOut=0;
	this.lastLevel1=0;
	this.l2pref='a';
	this.trigger=0;
	var el=this;
	$('.submenu').mouseover(function(){ el.level1Over(this.id); });
	$('.submenu').mouseout(function(){ el.level1Out(this.id); });
	$('.menu-l2-cont').mouseover(function(){ el.level2Over(this.id); });
	$('.menu-l2-cont').mouseout(function(){ el.level2Out(this.id); });
}

MMenu.prototype.level1Over=function(sid) {
	if (this.timeOut) {
		clearTimeout(this.timeOut);
		this.timeOut=0;
		if(this.lastLevel1!=sid) this.level1Clear();
	}
	if (document.getElementById(this.l2pref+sid)) {
		this.lastLevel1=sid;
		document.getElementById(this.l2pref+sid).style.display='block';
		//var offset = $('#'+sid).position();
		//var loff=offset.left+275;
		//var wid=$('#'+sid).innerWidth();
		//$('#'+this.l2pref+sid).css({ left:loff+'px', width:wid<140?140:wid+'px' });
	}
}

MMenu.prototype.level1Out=function(sid) {
	if (!this.timeOut) {
		var el=this;
		this.timeOut=setTimeout(function() {el.level1Clear();}, 1000);
	}
}

MMenu.prototype.level2Over=function(sid) {
	if (this.timeOut) { clearTimeout(this.timeOut); this.timeOut=''; }
	document.getElementById(sid).style.display='block';
}

MMenu.prototype.level2Out=function(sid) {
	if (!this.timeOut) {
		var el=this;
		this.timeOut=setTimeout(function() {el.level1Clear();}, 1000);
	}
}

MMenu.prototype.level2linkOver=function (sid) {
	if (this.timeOut) { clearTimeout(this.timeOut); this.timeOut=0; }
}

MMenu.prototype.level1Clear=function() {
	if (document.getElementById(this.l2pref+this.lastLevel1)) {
		document.getElementById(this.l2pref+this.lastLevel1).style.display='none';
	}
}

function initDownload() {
	$('#price_list a').click(function(){$('#pr-download').show();return false;});
	var link=$('#price_list a').attr('href');
	$('#pr-down2000 a').attr('href',link);
	$('#pr-down97 a').attr('href',link.replace(/garantprice/,'garantpric97'));
	$('#pr-down97, #pr-down2000').mouseover(function(){$(this).removeClass('pr-down-but-off').addClass('pr-down-but-on')}).mouseout(function(){$(this).removeClass('pr-down-but-on').addClass('pr-down-but-off')});
	$('#pr-down97 a, #pr-down2000 a').click(function(){$('#pr-download').hide();})
}

function garmonSlider(params) {
	this.sid=params.container;
	this.timeout=params.timeout;
	this.animationSpeed=params.animationSpeed;
	this.shower=1;
	this.maxSlide=$('#'+this.sid+' div').get().length;
	this.minW=100;
	this.maxW=$('#sld-1').width();
	this.init();
}

garmonSlider.prototype.init=function() {
	el=this;
	$('#'+this.sid+' div').click(function(){ el.showNext(this.id,1) }).mouseover(function(){ el.divMover(this) }).mouseout(function(){ el.divMout(this) });
	$('#center-slide-button img').click(function(){ el.showNext(this.id,1) }).mouseover(function(){ el.divMover(this) }).mouseout(function(){ el.divMout(this) });
	this.startAnimation();
}

garmonSlider.prototype.divMover=function(cat) {
	var sid=Number(cat.id.split('-')[1]);
	if(sid==this.shower||$('#sld-'+sid+' .slide-image').is('.slide-no-click')) return;
	$('#sld-'+sid+' .slide-ten').hide();
	$('#slbut-'+sid).attr('src','/img/slide-button-on.jpg');
}

garmonSlider.prototype.divMout=function(cat) {
	var sid=Number(cat.id.split('-')[1]);
	if(sid==this.shower||$('#sld-'+sid+' .slide-image').is('.slide-no-click')) return;
	$('#sld-'+sid+' .slide-ten').show();
	$('#slbut-'+sid).attr('src','/img/slide-button-off.jpg');
}

garmonSlider.prototype.showNext=function(cat,isclick) {
	clearInterval(this.timer);
	var sid=Number(cat.split('-')[1]);
	if(sid>this.maxSlide) sid=1;
	if(sid==this.shower) {
		if(isclick&&$('#sld-'+this.shower+' .slide-image').attr('rel')) document.location=$('#sld-'+this.shower+' .slide-image').attr('rel');
		this.startAnimation();
		return;
	}
	if($('#sld-'+sid+' .slide-image').is('.slide-no-click')) {
		if(!isclick) this.showNext('a-'+(sid+1));
		return;
	}
	$('#sld-'+sid+' .slide-ten').hide();
	$('#slbut-'+sid).attr('src','/img/slide-button-on.jpg');
	$('#sld-'+this.shower+' .slide-ten').show();
	$('#slbut-'+this.shower).attr('src','/img/slide-button-off.jpg');
	if(sid!=1) {
		for(i=(sid-1);i>0;i--) {
			if($('#sld-'+i).width()!=this.minW) $('#sld-'+i).animate({width:this.minW+'px'},this.animationSpeed);
		}
	}
	if(sid!=this.maxSlide) {
		for(i=(sid+1);i<this.maxSlide;i++) {
			if($('#sld-'+i).width()!=this.maxW) $('#sld-'+i).animate({width:this.maxW+'px'},this.animationSpeed);
		}
	}
	$('#sld-'+sid).animate({width:this.maxW+'px'},this.animationSpeed);
	this.shower=sid;
	this.startAnimation();
}

garmonSlider.prototype.startAnimation=function() {
	var el=this;
	this.timer=setTimeout(function(){ this.timer=''; el.showNext('a-'+(el.shower+1)); },this.timeout);
}

$(document).ready(function(){var slider=new garmonSlider({container:'slider-mask',timeout:5000,animationSpeed:3000})});
