// [d] переключатель блока товаров

$(document).ready(function(){
	// [d] показать первую картинку в блоке
	$('.product_item').eq(0).css("display",'block');
	
	// [d] пагинатор
    $('.listing a').click(function(){
    	switch($(this).attr('class')) {
    		// [d] вперед
    		case 'forward':
 		     	var current = 1;
		    	$('.listing a').each(function(i, val) {
					if (i != 0  &&  i < $('.listing a').length-2) {
						if ($(this).attr('class')=='cur') {
							$(this).attr('class','');	
							current = i+1;	
					    	$('.product_item').each(function(i, val) {
								$(this).css('display','none');
							});
							$('.listing a').eq(current).attr('class','cur');
							$('.product_item').eq(current-1).css('display','block');  
							return false;					
						}
					}
				});
				break;
			// [d] назад
    		case 'back':
  		     	var current = 1;
		    	$('.listing a').each(function(i, val) {
					if (i > 1  &&  i < $('.listing a').length-1) {
						if ($(this).attr('class')=='cur') {
							$(this).attr('class','');	
							current = i-1;	
					    	$('.product_item').each(function(i, val) {
								$(this).css('display','none');
							});
							$('.listing a').eq(current).attr('class','cur');
							$('.product_item').eq(current-1).css('display','block');  
							return false;					
						}
					}
				});
    			break;
    		// [d] переключение по картинке
    		default:
		     	var current = $(this).attr("rel");
		    	$('.listing a').each(function(i, val) {
					if (i != 0  &&  i!= $('.listing a').length-1) {
						if ($(this).attr('class')=='cur') {
							$(this).attr('class','');				
						}
					}
				});
		    	$('.product_item').each(function(i, val) {
					$(this).css('display','none');
				});
				$('.listing a').eq(current).attr('class','cur');
				$('.product_item').eq(current-1).css('display','block');  
				break; 			
    	}
        return false;
    })

});


$(function() {

	// Darkbox
	
	( function ( $ ) {

		$.fn.darkbox = function (mode) {
			var mode = mode || 'multi';

			var shadowFadeInTime  = 200,
				shadowFadeOutTime = 100,

				imageFadeInTime       = 400,
				imageErrorFadeOutTime = 800,

				darkboxStateClasses =
					'darkbox-canvas-done darkbox-canvas-load darkbox-canvas-error',

				boxMargin        = 50,
				buttonPlaceClass = /mac/i.test( navigator.platform ) ?
					'darkbox-button-left' :
					'darkbox-button-right';

			function openBox ( e ) {
				e.preventDefault();
				
				var link = $( this ),
					frame = $( 'div.darkbox-frame' );
				
				$( 'div.darkbox-canvas' ).removeClass( darkboxStateClasses );
				index = $('.scroller a').index($('a.selected'));

				if (mode == 'one') window.darkbox_arrows = $('.darkbox-next, .darkbox-prev').detach();
				else if (window.darkbox_arrows != undefined)
					window.darkbox_arrows.appendTo('.darkbox-canvas');
				
				if (index < ($('.scroller a').length -1)) {
					$('.darkbox-next').show();
				} else {
					$('.darkbox-next').hide();
				}
				var x = $('.make_live').length;
				
				if (index > x) {
					$('.darkbox-prev').show();
				} else {
					$('.darkbox-prev').hide();
				}
				frame.children( 'div.darkbox-canvas' ).
						css( { width: '', marginLeft: '', height: '', marginTop: '' } ).
						children( 'img' ).
							one( 'error', handleImageLoadError ).
							css( { width: '', height: '' } ).
							attr( 'src', link.attr( 'href' ) ). // + '?_=' + ( +new Date() )
							attr( 'alt', link.attr( 'title' ) ).
							end().
						end().

					show().
					children( 'div.darkbox-shadow' ).
						animate( { opacity: 0.6 }, shadowFadeInTime );
				$( '.darkbox-title', frame ).html(link.attr( 'title' ));
			}

			function closeBox() {
				$( 'div.darkbox-shadow' ).animate(
					{ opacity: 0 },
					shadowFadeOutTime,
					function () {
						$( 'div.darkbox-frame' ).
							hide().
							children( 'div.darkbox-canvas' ).
								removeClass( darkboxStateClasses ).
								children( 'img' ).
									unbind( 'error', handleImageLoadError ).
									attr( 'src', '' );
					}
				);
			}

			function handleKey( e ) {
				if ( 27 === e.which || 32 === e.which ) {
					// If darkbox is shown
					if ( 0 === $(".darkbox-frame:hidden").length ) {
						e.preventDefault();
						closeBox();
					}
				}
			}

			function handleImageLoadError() {
				$( 'div.darkbox-canvas' ).addClass( 'darkbox-canvas-error' );
				setTimeout( closeBox, imageErrorFadeOutTime );
			}

			function handleImageLoad() {
				var img = $( this ),
					frame = img.parents( 'div.darkbox-frame' ),
					canvas = img.parents( 'div.darkbox-canvas' ),
					ratio = 1,
					img_width = img.width(), img_height = img.height(),
					frame_width = frame.width(), frame_height = frame.height();

				if ( 0 === img_width && 0 === img_height ) {
					setTimeout( function (){ img.load(); }, 10 );
					return;
				}

				if (
					( img_width > frame_width - boxMargin ) ||
					( img_height > frame_height - boxMargin )
				) {
					ratio = Math.min(
						( frame_width - boxMargin ) / img_width,
						( frame_height - boxMargin ) / img_height
					);

					img_width = Math.round( img_width * ratio );
					img_height = Math.round( img_height * ratio );

					img.css( { width: img_width, height: img_height } );
				}

				canvas.
					addClass( 'darkbox-canvas-load' ).
					animate( {
						width:      img_width,
						marginLeft: -img_width / 2,
						height:     img_height,
						marginTop:  -img_height / 2
						}, imageFadeInTime,
						function () {
							$( this ).
								removeClass( 'darkbox-canvas-load' ).
								addClass( 'darkbox-canvas-done' ).
								children( 'img' ).
									show();
						}
					);
				$( '.darkbox-title', frame ).css({width:img_width -10});
					
			}
			function imageNext(e){
				e.preventDefault();
				
				var link = $( 'a.selected' ).next(),
					frame = $( 'div.darkbox-frame' ),
					index = $('.scroller a').index(link);
				$( 'a.selected' ).removeClass('selected');
				link.addClass('selected');
				$('.bar').scrollLeft(120*(index-1));
				$( 'div.darkbox-canvas' ).removeClass( darkboxStateClasses );
				if (index < ($('.scroller a').length -1)) {
					$('.darkbox-next').show();
				} else {
					$('.darkbox-next').hide();
				}
				var x = $('.make_live').length;
				
				if (index > x) {
					$('.darkbox-prev').show();
				} else {
					$('.darkbox-prev').hide();
				}
				frame.
					children( 'div.darkbox-canvas' ).
						css( { width: '', marginLeft: '', height: '', marginTop: '' } ).
						children( 'img' ).
							one( 'error', handleImageLoadError ).
							css( { width: '', height: '' } ).
							attr( 'src', link.attr( 'href' ) ). // + '?_=' + ( +new Date() )
							attr( 'alt', link.attr( 'title' ) ).
							end().
						end().

					show().
					children( 'div.darkbox-shadow' ).
						animate( { opacity: 0.6 }, shadowFadeInTime );
				$( '.darkbox-title', frame ).html(link.attr( 'title' ));
			}
			function imagePrev(e){
				e.preventDefault();
				
				var link = $( 'a.selected' ).prev(),
					frame = $( 'div.darkbox-frame' ),
					index = $('.scroller a').index(link);
				$( 'a.selected' ).removeClass('selected');
				link.addClass('selected');
				$('.bar').scrollLeft(120*(index-1));
				$( 'div.darkbox-canvas' ).removeClass( darkboxStateClasses );
				if (index < ($('.scroller a').length -1)) {
					$('.darkbox-next').show();
				} else {
					$('.darkbox-next').hide();
				}
				var x = $('.make_live').length;
				
				if (index > x) {
					$('.darkbox-prev').show();
				} else {
					$('.darkbox-prev').hide();
				}
				frame.
					children( 'div.darkbox-canvas' ).
						css( { width: '', marginLeft: '', height: '', marginTop: '' } ).
						children( 'img' ).
							one( 'error', handleImageLoadError ).
							css( { width: '', height: '' } ).
							attr( 'src', link.attr( 'href' ) ). // + '?_=' + ( +new Date() )
							attr( 'alt', link.attr( 'title' ) ).
							end().
						end().

					show().
					children( 'div.darkbox-shadow' ).
						animate( { opacity: 0.6 }, shadowFadeInTime );
				$( '.darkbox-title', frame ).html(link.attr( 'title' ));
			}
			
			if($('.darkbox-frame').length == 0){
				$( '<div class="darkbox-frame"><div class="darkbox-shadow"></div><div class="darkbox-canvas"><img><div class="darkbox-title"></div><div class="darkbox-button" title="Закрыть"></div><div class="darkbox-next" title="Следующая"></div><div class="darkbox-prev" title="Предидущая"></div></div></div>' ).
					children( 'div.darkbox-shadow' ).
						click( closeBox ).
						end().
					find( 'div.darkbox-button' ).
						addClass( buttonPlaceClass ).
						click( closeBox ).
						end().
					find( 'img' ).
						load( handleImageLoad ).
						end().
					find( 'div.darkbox-next' ).
						click( imageNext ).
						end().
					find( 'div.darkbox-prev' ).
						click( imagePrev ).
						end().
					appendTo( 'body' ).
					end();				
			}


			$( document ).
				keypress( handleKey ).
				keydown( handleKey );
			this.live('click', openBox );

			return this;
		};
	} ( jQuery ) );
	
	$( '.thumbs .overflow a.make_big' ).darkbox();
	$( 'a.big' ).darkbox();
	$( '#downloads .make_big' ).darkbox('one');
	
	$('.pages a').click(function(evt){
		evt.preventDefault();
		if ($(this).attr('rel') == 'next') {
			var index = $('.scroller a').index($('.selected'));
			if(index < ($('.scroller a').length -1)){
				$('.bar').scrollLeft(120*(index));
				$( '.thumbs .overflow a' ).removeClass('selected');
				$( '.thumbs .overflow a' ).eq(index+1).addClass('selected');				
			} else {
				return false;
			}
		} else if($(this).attr('rel') == 'prev'){
			var index = $('.scroller a').index($('.selected'));
			if(index > 0){
				$('.bar').scrollLeft(120*(index-2));
				$( '.thumbs .overflow a' ).removeClass('selected');
				$( '.thumbs .overflow a' ).eq(index-1).addClass('selected');				
			} else {
				return false;
			}
		} else {
			$('.bar').scrollLeft(120*($(this).attr('rel')-2));
			$( '.thumbs .overflow a' ).removeClass('selected');
			$( '.thumbs .overflow a' ).eq($(this).attr('rel')-1).addClass('selected');			
		}
		var link = $('.scroller a.selected');
		if (link.hasClass('make_big')) {
			var big = $('a.big').eq(0);
			if ($('a.big').eq(0).length == 0)
				big = $('a.live').eq(0);
	
			$('*',big).remove();
			big.removeClass('live').addClass('big');
			// Цепляем к попапу картинку и ждём её загрузки
			var image = $('<img src="'+ link.attr('href') +'.big_new" alt="'+ link.attr('title') +'"/>');
			image.css('opacity',0).appendTo(big);
			image.load(function(){
				if ( $.browser.msie && ($.browser.version < 7) ){
					image.css({
						opacity:1,
						'background-image': 'url('+link.attr('href')+'.big_new)'
					})
					image.attr('src','/templates/armed/i/zoom.png');
					big.attr('href',link.attr('href')).attr('title',link.attr('title'));
				} else {
					image.animate({opacity:1},500,function() {
							image.css({
								'background-image': 'url('+link.attr('href')+'.big_new)'
							}).attr('src','/templates/armed/i/zoom.png');
							big.attr('href',link.attr('href')).attr('title',link.attr('title'));
						});				
				}
			});
			
		} else {
			var big = $('a.big').eq(0);
			if ($('a.big').eq(0).length == 0)
				big = $('a.live').eq(0);
	
			$('*',big).remove();
			big.removeClass('big').addClass('live');
			// Цепляем к попапу картинку и ждём её загрузки
			var image = $('<img src="'+ link.attr('href') +'.big_new" alt="'+ link.attr('title') +'"/>');
			image.css('opacity',0).appendTo(big);
			image.load(function(){
				if ( $.browser.msie && ($.browser.version < 7) ){
					image.css({
						opacity:1,
						'background-image': 'url('+link.attr('href')+'.big_new)'
					})
					image.attr('src','/templates/armed/i/live.png');
					big.attr('href',link.attr('href')).attr('title',link.attr('title'));
				} else {
					image.animate({opacity:1},500,function() {
							image.css({
								'background-image': 'url('+link.attr('href')+'.big_new)'
							}).attr('src','/templates/armed/i/live.png');
							big.attr('href',link.attr('href')).attr('title',link.attr('title'));
						});				
				}
			});
			
		}
		
	})
	$( '.thumbs .overflow a' ).click(function(){
		$( '.thumbs .overflow a' ).removeClass('selected');
		$( this ).addClass('selected');
		
	})
	if($('.tech table').length){
		$('.tech table').wrap('<div class="ib">')
		$('.tech table tr').each(function(){
			$('td',this).eq(0).addClass('left').html('<span>'+$('td',this).eq(0).html()+'</span>');
			$('td',this).eq(1).addClass('right').html('<span>'+$('td',this).eq(1).html()+'</span>');
		})
	}
	
	$('.bar').scroll(function(evt){
		// console.log(evt);
	});
	
});
function printme(src) {
	link = "about:blank";
	var pw = window.open(link,"_new");
	pw.document.open();
	pw.document.write(makepage(src));
	pw.document.close();
}
function makepage(src) {
	return "<html>\n" +
	"<head>\n" +
	"<title>Распечатать</title>\n" +
	"<script>\n" +
	"function step1() {\n" +
	" setTimeout('step2()', 10);\n" +
	"}\n" +
	"function step2() {\n" +
	" window.print();\n" +
	" window.close();\n" +
	"}\n" +
	"</scr" + "ipt>\n" +
	"</head>\n" +
	"<body onLoad='step1()'>\n" +
	"<img src='" + src + "'/>\n" +
	"</body>\n" +
	"</html>\n";
}

