// JavaScript Document
var lightboxImgPath='lightbox/';

function pageImg() { 	// Sticking "40years" label if the cap is more than 150 pixels high.
	return;
	if($('#pageimg').height() > 150) $('#pageimg').prepend('<img src="assets/images/pagetops/40years.png" alt="" width="115" height="105" style="position:absolute; right:23px; top:23px;" class="png" />');
}

function openVideo(vname) {
	window.open('/video.html?'+vname,'video','height=600,width=800');
	return false;
}

$(function() {
	//debug = window.open('','DEBUG');
	$('#mmenu > ul > li.active ul').parent().css({'background-image':'url(img/mmenu_open.gif)','background-position':'10px 8px'});
	$('#login img').click(function() {
		var div=$('#login .status');
		if(div.width() == 0) {
			div.animate({width:'195px'});
			$('#weblogin').slideDown();
		}
		else {
			div.animate({width:'0px'});
			$('#weblogin').slideUp();
		}
	});
	$('#login .status.no a').click(function(event){
		event.preventDefault();
		$('#weblogin').slideDown();
	});
	if($('#pagemidb').size()==1 && $('#pagemidb ul').size()==0) { // change styling if no level 3 submenu present
		$('#pagemidb').addClass('grey');
		$('#content').css({ backgroundImage:'none',backgroundColor:'#FFFFFF',zoom:'1'});
	}
	// Old 'dditem' behavior
	/*
	$('.dditem').addClass('closed');
	$('.dditem h5').click(function() {
		$(this).parent().toggleClass('open').toggleClass('closed');
	});
	*/
	// 'dditem' elements behavior
	
	$('.dditem').toggleClass('closed');
	$('.dditem .bullet').click(function() {$(this).parent().toggleClass('closed');});
	$('.dditem h5').click(function() {$(this).parent().toggleClass('closed');});
	
	$('.thumbs165 a').lightBox({
		imageLoading:lightboxImgPath+'lightbox-ico-loading.gif',		
		imageBtnPrev:lightboxImgPath+'lightbox-btn-prev.gif',			
		imageBtnNext:lightboxImgPath+'lightbox-btn-next.gif',
		imageBtnClose:lightboxImgPath+'lightbox-btn-close.gif',
		imageBlank:lightboxImgPath+'lightbox-blank.gif',
		txtImage:'Фото',
		txtOf:'из'
	});
	
	$('.lightbox').lightBox({
		imageLoading:lightboxImgPath+'lightbox-ico-loading.gif',		
		imageBtnPrev:lightboxImgPath+'lightbox-btn-prev.gif',			
		imageBtnNext:lightboxImgPath+'lightbox-btn-next.gif',
		imageBtnClose:lightboxImgPath+'lightbox-btn-close.gif',
		imageBlank:lightboxImgPath+'lightbox-blank.gif',
		txtImage:'Фото',
		txtOf:'из'
	});

// Transforming thumbnail paragraphs into thumbnail gallery widgets
	$('.thumbs165').each( function() {
		if($(this).find('img').size()<=4) return;
		$(this).removeClass().wrap('<div class="gallerywidget"><div class="thumbs"></div><div class="controls"><div></div><img src="img/galw_btn_up.gif" alt="Предыдущие 4" title="Предыдущие 4" class="up" /><img src="img/galw_btn_down.gif" alt="Следующие 4" title="Следующие 4" class="dn" /><img src="img/galw_btn_all.gif" alt="Показать все" title="Показать все" class="all" /></div></div>');
		
	});
	// Thumbnail gallery widget preparations
	$('.gallerywidget').each( function() {
		var numPhotos=$(this).find('.thumbs img').size();
		var numLines=Math.ceil(numPhotos/4);
		if(numLines>1)	{ // Multiple lines: set values and activate controls
			$(this).data('numLines',numLines);
			$(this).data('currLine',0);
			$(this).data('numPhotos',numPhotos);
			$(this).data('expanded',false);
			var controls=$(this).find('.controls');
			controls.find('.dn').click( function() {
				var widget = $(this).parents('.gallerywidget');
				if(widget.data('expanded')) return;
				var thumbs = widget.find('.thumbs');
				var currLine=widget.data('currLine');
				if(currLine < widget.data('numLines')-1)	{
					widget.data('currLine',++currLine);
					thumbs.animate({scrollTop : "+=114px"});
					var photoFirst = currLine*4+1;
					var photoLast = photoFirst+3;
					if(photoLast > widget.data('numPhotos')) photoLast=widget.data('numPhotos');
					widget.find('.controls div').text(photoFirst+'-'+photoLast+' / '+widget.data('numPhotos'));
				}
			});
			controls.find('.up').click( function() {
				var widget = $(this).parents('.gallerywidget');
				if(widget.data('expanded')) return;
				var thumbs = widget.find('.thumbs');
				var currLine=widget.data('currLine');
				if(currLine > 0)	{
					widget.data('currLine',--currLine);
					thumbs.animate({scrollTop : "-=114px"});
					var photoFirst = currLine*4+1;
					var photoLast = photoFirst+3;
					widget.find('.controls div').text(photoFirst+'-'+photoLast+' / '+widget.data('numPhotos'));
				}
			});
			controls.find('.all').click( function() {
				var widget = $(this).parents('.gallerywidget');
				var thumbs = widget.find('.thumbs');
				var numPhotos = widget.data('numPhotos');
				if(widget.data('currLine')>0) thumbs.animate({scrollTop : "0px"},'fast');
				widget.data('expanded',true);
				thumbs.animate({height : thumbs.find('p').height()});
				widget.find('.controls div').text('1-'+numPhotos+' / '+numPhotos);
			});
			$(this).find('.thumbs').scrollTop('0px'); // Fucking FF
			$(this).find('.controls div').text('1-4 / '+numPhotos);
			controls.slideDown();
		}
	});
	// Product medals
	$('#content #intro .note .medals div').hover(
		function() {
			$(this).find('.large').fadeIn('fast','linear');//css('display','block');
		},
		function() {
			$(this).find('.large').fadeOut('fast','linear');
		}
	);
});


function fixPNG(element) {
  if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent))  {
    var src;
    if (element.tagName=='IMG') {
      if (/\.png$/.test(element.src))  {
        src = element.src;
        element.src = "/spacer.gif";
      }
    }
    else {
      src = element.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i);
      if (src) {
        src = src[1];
        element.runtimeStyle.backgroundImage="none";
      }
    }
    if (src) element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='crop')";
  }
}

