var hlpData=[];
var hlpId=[];
function clearhlps() {
	for (var i=0;i<hlpData.length;i++) {
		if (hlpData[i]=='') {
			var clrer=document.getElementById(hlpId[i]);
			var parent=clrer.parentNode;
			parent.removeChild(clrer);
		} else {
			var z=document.createElement('div');
			z.className='hidden-stuck help-desk';
			z.innerHTML='<div class="hlp-top"></div><div class="hlp-txt">'+hlpData[i]+'</div><div class="hlp-bot"></div>';
			z.id=hlpId[i]+'h';
			var clrer=document.getElementById(hlpId[i]);
			clrer.parentNode.parentNode.appendChild(z);
		}
	}
}
function showhlp(shid,iks,ygrek) {
	addr=document.getElementById(shid);
	addr.className='help-desk';
	addr.style.left=iks-385+"px";
	addr.style.top=ygrek+"px";
}
function hidehlp(shid) {
	addr=document.getElementById(shid);
	addr.className='hidden-stuck help-desk';
}
function showPopupPodskaz() {
	var iks=document.getElementById('dostav-example');
	iks.className='';
}
function hidePopupPodskaz() {
	var iks=document.getElementById('dostav-example');
	iks.className='hidden-stuck';
}
function catchhref() {
	var hlpvar='mainpage';
	var levelmark1;
	var levelmark2;
	var lvl1flag=false;
	var lvl2flag=false;
	var aimhlp=location.pathname;
	var zu=aimhlp.slice(-4);
	if (zu=='html') {
		var qu=aimhlp.lastIndexOf('/');
		aimhlp=aimhlp.slice(0,qu);
	}
	var aim=location.protocol+'//'+location.host+aimhlp;
	var checker=document.getElementById('checked-menu').getElementsByTagName('li');
	for (var i=0; i<checker.length; i++) {
		if (checker[i].getElementsByTagName('a')[0].href==aim) {hlpvar=i}
	}
	if (hlpvar=='mainpage') {
		levelmark1='level1';
		levelmark2='level1';
	} else {
		levelmark1=checker[hlpvar].className;
		levelmark2=checker[hlpvar].className;
	}
	if (levelmark2=='level1'&&hlpvar!='mainpage') {
		lvl1flag=true;
		checker[hlpvar].className+=' open';
	}
	if (levelmark2=='level2') {lvl2flag=true}
	if (hlpvar=='mainpage') {hlpvar=0}
	for (var iks=hlpvar-1;iks>=0;iks--) {
		switch (checker[iks].className) {
			case 'level3':
				if (levelmark1!='level3') {checker[iks].className+=' hidden-stuck'}
				break;
			case 'level2':
				switch (levelmark1) {
					case 'level3':
						levelmark1='level2';
						break;
					case 'level1':
						checker[iks].className+=' hidden-stuck';
						break;
				}
				break;
			case 'level1':
				if (levelmark1!='level1') {
					levelmark1='level1';
					checker[iks].className+=' open';
				}
				break;
		}
	}
	for (var iks=hlpvar+1; iks<checker.length; iks++) {
		switch (checker[iks].className) {
			case 'level3':
				switch (levelmark2) {
					case 'level2':
						if (lvl2flag==false) {checker[iks].className+=' hidden-stuck'}
						break;
					case 'level1':
						checker[iks].className+=' hidden-stuck';
						break;
				}
				break;
			case 'level2':
				switch (levelmark2) {
					case 'level3':
						levelmark2='level2';
						break;
					case 'level2':
						if (lvl2flag==true) {lvl2flag=false}
						break;
					case 'level1':
						if (lvl1flag==false) {checker[iks].className+=' hidden-stuck'}
						break;
				}
				break;
			case 'level1':
				if (lvl1flag==true) {lvl1flag=false}
				if (levelmark2!='level1') {levelmark2='level1'}
				break;
		}
	}
}
function menuswitcher(rpoint) {
	rpoint.parentNode.id='lipoint';
	if (rpoint.parentNode.className=='level1') {
		rpoint.parentNode.className='level1 open';
	} else {
		rpoint.parentNode.className='level1';
	}
	var hlpvar=0;
	var rearflag=true;
	var checker=document.getElementById('checked-menu').getElementsByTagName('li');
	for (var i=0; i<checker.length; i++) {
		if (checker[i].id=='lipoint') {hlpvar=i+1}
	}
	for (var y=hlpvar; y<checker.length; y++) {
		if (rearflag) {
			switch (checker[y].className) {
				case 'level1':
					rearflag=false;
					break;
				case 'level1 open':
					rearflag=false;
					break;
				case 'level2':
					checker[y].className+=' hidden-stuck';
					break;
				case 'level2 hidden-stuck':
					checker[y].className='level2';
					break;
				case 'level3':
					checker[y].className+=' hidden-stuck';
					break;
			}
		}
	}
	rpoint.parentNode.id='';
}

$(document).ready( function() {
	$('.search-form .search-field').formclear();
	$('.l-search-form .search-field').formclear();
	
	// Manufacturers list
	manufacturers('.col-sidebar .mfgrs-list');

	if ( $('#thumbs li').length > 1 ) {
		// Banner slideshow
		$('#slideshow > span').remove();

		var gallery = $('#thumbs').galleriffic({
			delay:                     8000, // in milliseconds
			preloadAhead:              2, // Set to -1 to preload all images
			enableTopPager:            false,
			enableBottomPager:         false,
			imageContainerSel:         '#slideshow', // The CSS selector for the element within which the main slideshow image should be rendered
			controlsContainerSel:      '#ss-btns', // The CSS selector for the element within which the slideshow controls should be rendered
			captionContainerSel:       '', // The CSS selector for the element within which the captions should be rendered
			loadingContainerSel:       '', // The CSS selector for the element within which should be shown when an image is loading
			renderSSControls:          true, // Specifies whether the slideshow's Play and Pause links should be rendered
			renderNavControls:         false, // Specifies whether the slideshow's Next and Previous links should be rendered
			playLinkText:              'Запустить слайдшоу',
			pauseLinkText:             'Остановить слайдшоу',
			prevLinkText:              'Previous',
			nextLinkText:              'Next',
			nextPageLinkText:          'Next &rsaquo;',
			prevPageLinkText:          '&lsaquo; Prev',
			enableHistory:             false, // Specifies whether the url's hash and the browser's history cache should update when the current slideshow image changes
			enableKeyboardNavigation:  false, // Specifies whether keyboard navigation is enabled
			autoStart:                 true // Specifies whether the slideshow should be playing or paused when the page first loads
		});
	
		$('#btns').show();
	
		$('#btns .next').click( function(e) {
			gallery.next();
			e.preventDefault;
			return false;
		});
	
		$('#btns .prev').click( function(e) {
			gallery.previous();
			e.preventDefault;
			return false;
		});
	
		$('#ss-btns').show();
	}
	
	testCode('#test-code');
	
	// add to favourites
	if ( !$.browser.safari ) {
		$('#favourite').show();
		
		$('#favourite a').attr('rel', 'sidebar').click(function(e) {
			var title = window.document.title;
			var url = window.location.href;
	
			try { 
				// Internet Explorer 
				window.external.AddFavorite(url, title); 
			} 
			catch (e) { 
				try { 
				// Mozilla 
					window.sidebar.addPanel(title, url, ""); 
				} 
				catch (e) { 
					// Opera 
					if (typeof(opera)=="object") { 
						a.rel="sidebar"; 
						a.title=title; 
						a.url=url; 
					} 
				} 
			}
				
			e.preventDefault();
		});
	}
});

function manufacturers(s) {
	var alwaysVisible = 5;
	var showText = 'показать все', hideText = 'только первые';
	
	var $mfrs = $('li', s);
	var $link = $(s).find('a.all');
	$link = $link.add('#js-manHeader');
	
	if ( $mfrs.length < alwaysVisible ) {
		$link.hide();
	} else {
		$link.show();	

		hideManufacturers($mfrs, alwaysVisible);
		$link.click( function(e) {
			var $t = $mfrs.filter('li.js-hidden');
			$t.toggle();

			if ( $t.is( ':visible' ) ) {
				$('span', this).html(hideText);
			} else {
				$('span', this).html(showText);
			}
		
			e.preventDefault();
		});
	}
}

function hideManufacturers(s, visible) {
	$(s).filter('li:not(:lt(' + visible + '))').addClass('js-hidden').hide();
};

function numberFormat(a, b, c, d) {
 a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
 e = a + '';
 f = e.split('.');
 if (!f[0]) {
  f[0] = '0';
 }
 if (!f[1]) {
  f[1] = '';
 }
 if (f[1].length < b) {
  g = f[1];
  for (i=f[1].length + 1; i <= b; i++) {
   g += '0';
  }
  f[1] = g;
 }
 if(d != '' && f[0].length > 3) {
  h = f[0];
  f[0] = '';
  for(j = 3; j < h.length; j+=3) {
   i = h.slice(h.length - j, h.length - j + 3);
   f[0] = d + i +  f[0] + '';
  }
  j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
  f[0] = j + f[0];
 }
 c = (b <= 0) ? '' : c;
 return f[0] + c + f[1];
}


function testCode(s) {
	var $code = $(s);
	
	if ( $code.length ) {
		var $val = $code.find('.value');
		var code = new Number( $val.text() );
		
		$val.text( numberFormat(code, 0, ',', ' ') );
	}
}

function scriptloader() {
	catchhref();
	clearhlps();
}
