﻿
/*
* Автор: Игнатьев И.И.
* E-mail: ignatiev.ivan@innoday.com
*/
var frameWidth = 0; //длинна списка изображений
var maxHeight = 0;
var scrollStop = 0;
var resizeOver = 0;
var wsize = 90;
/*
* Загрузка обложек категорий
*/
function loadminu(){
$.getJSON('/photoset/photoset_main.php', function(data) {
$.each(data.images, function(i, item) {
    $('#innerimg').append(
    $('<img/>')	.attr('src', item.path)
		.css({ 'width': 'auto', 'height': (parseInt(item.height) - resizeOver)})
        );})
        });
$.getJSON('/photoset/photoset_category.php', function(data) {
$.each(data.images, function(i, item) {
    $('span#minu').append($('<div style="display: inline-block;//display: inline; //zoom:1;">')
    .click(function() {$('#FramePage').html('');$('#FramePage').load( '/fotosety/', function(){loadSets( parseInt(item.id))} )} )
    .append('<a href="#" class="enter">'+item.description+'</a>&nbsp;|&nbsp;')
    )});
    });
}
function loadminu1(){
$.getJSON('/photoset/photoset_category.php', function(data) {
$.each(data.images, function(i, item) {
    $('span#minu').append($('<div style="display: inline-block;//display: inline; //zoom:1;">')
    .click(function() {loadSets( parseInt(item.id))} )
    .append('<a href="#" class="enter">'+item.description+'</a>&nbsp;|&nbsp;')
    );});
    })
}
function loadcategory() {
	$('#psSubframe').html('<br/><br/>');	// очищаем HTML-код в месте куда будем выводить обложки категорий
	$('#psSubframe').css( {'left': 0, 'width': 150 });
    $('#minu').html('');
	frameWidth = 0;
	//запрашиваем JSON список обложек
	$('#psNavigation').hide();
	$.getJSON('/photoset/photoset_category.php', function(data) {
		if (data.images != '')	$('#psSubframe').html('');
		// выводим обложки

		$.each(data.images, function(i, item) {
    $('#minu').append($('<a href="#" class="enter">')
    .click(function() {loadSets( parseInt(item.id) );} )
    .append(item.description+' | ')
    )
			$('#psSubframe').append(
				$('<div>').css( {'float':'left', 'padding': '0 1px'})	. append(
					$('<img/>')	.attr('src', item.path)
								.attr('id', 'image' + item.id)
								.css({ 'cursor':'pointer', 'width': 'auto', 'height': (parseInt(item.height) - resizeOver)})
								.mouseover(function() {
									$(this).css('height' , $(this).height() + resizeOver);
								})
								.mouseout(function() {
									$(this).css('height' , $(this).height() - resizeOver);
								})
								.click(function() {
									loadSets( parseInt(item.id) );
								})
				).append('<br/>'+item.description+'<br/>'+item.description1+'<br/>'+item.description2+'<br/>'+item.description3)
			);
			// собираем длинну списка
			frameWidth += parseInt(item.width);
			if (parseInt(item.height) > maxHeight) {
				maxHeight  =  parseInt(item.height);
			}
			$('#psSubframe').css('width', frameWidth);
		});

		setMainframeStyle();
	});
}

function loadSets(ctId) {
	$('#psSubframe').html('<br/><br/>');	// очищаем HTML-код в месте куда будем выводить обложки категорий
	$('#psSubframe').css( {'left': 0, 'width': 150 });
	frameWidth = 0;

	$('#psNavigation').show();

	$.getJSON('/photoset/photoset_sets.php', {'id': ctId}, function (data) {
		if (data.images != '')	$('#psSubframe').html('');
		$.each(data.images, function(i,item) {



			$('#psSubframe').append(
				$('<div>').css( {'float':'left', 'position': 'relative', 'padding': '0 1px', 'width': (parseInt(item.width)+30)+'px'})
.append($('<img/>').attr('src', '../photoset/r.gif').attr('title', 'открыть сет').attr('class','str1').attr('id', 'image' + item.id).attr('height','38').attr('width','25')
.css({'position': 'absolute', 'top': '195px', 'left':'10px', 'z-index': '4','cursor':'pointer'})
	.click(function() {
								$(this).unbind('mouseover').unbind('mouseout').unbind('click');

								$.each($("span[id^='content']"), function(i,item) {
									if ($(item).css('display') != 'none') {
										var id = parseInt($(item).attr('id').replace('content',''));
										closeSetContent(id);
									}
								});

								loadSetContent(item.id);
							})
).append(

				$('<img/>')	.attr('src', item.path)
							.attr('id', 'image' + item.id)
                            .attr('class', 'image1')
							.css({ 'cursor':'pointer', 'width': (parseInt(item.width) - resizeOver), 'height': (parseInt(item.height) - resizeOver)})
							.mouseover(function() {
									$(this).css('height' , $(this).height() + resizeOver);
							})
							.mouseout(function() {
									$(this).css('height' , $(this).height() - resizeOver);
							})
							.click(function() {
								$(this).css( {'height':item.height} ).unbind('mouseover').unbind('mouseout').unbind('click');

								$.each($("span[id^='content']"), function(i,item) {
									if ($(item).css('display') != 'none') {
										var id = parseInt($(item).attr('id').replace('content',''));
										closeSetContent(id);
									}
								});

								loadSetContent(item.id);
							})
).append( '<div style="margin-top: 5px; width:'+(parseInt(item.width)+25)+'px;"><b class="v1"></b><b class="v2"></b><b class="v3"></b><b class="v4"></b><b class="v5"></b><div class="inner-box"><table border="0" cellpadding="0" cellspacing="0"><tr><td>'+item.description+'</td></tr><tr><td>'+item.description1+'</td></tr><tr><td>'+item.description2+'</td></tr><tr><td>'+item.description3+'</td></tr></table></div><b class="v5"></b><b class="v4"></b><b class="v3"></b><b class="v2"></b><b class="v1"></b></div>')
			).append(
				$('<span>')	.attr('id','content'+item.id).hide()
			);
			// собираем длинну списка
			frameWidth += parseInt(item.width) +(resizeOver/parseInt(item.height))*parseInt(item.width)+40;

			if (parseInt(item.height) > maxHeight) {
				maxHeight  =  parseInt(item.height) + 20;
			}
			$('#psSubframe').css('width', frameWidth);

		});
        $('.image1').wrap('<div class="inner-box1"></div>');
        $('.inner-box1').wrap('<div style="margin:0px 6px 0px 0px;"></div>');
        $('.inner-box1').before('<b class="v1"></b><b class="v2"></b><b class="v3"></b><b class="v4"></b><b class="v5"></b>');
        $('.inner-box1').after('<b class="v5"></b><b class="v4"></b><b class="v3"></b><b class="v2"></b><b class="v1"></b>');
		setMainframeStyle();
	});

}

function loadSetContent(stId) {

	$.getJSON('/photoset/photoset_set.php', {'id': stId}, function (data) {
		$.each(data.images, function(i,item) {
			$('#content' + stId).append(
				$('<div>').css( {'float':'left', 'padding': '0 1px', 'width': (parseInt(item.width)+30)+'px'}).append('<a class="highslide" onclick="return hs.expand(this)" href="'+item.path+'.full"><img id="image4" class="image2" height="'+item.height+'" src="'+item.path+'"></a><br/>'+item.description)
);
			// собираем длинну списка
			frameWidth += parseInt(item.width) +40+ (resizeOver/item.height)*item.width;
			if (parseInt(item.height) > maxHeight) {
				maxHeight  =  parseInt(item.height);
			}
			$('#psSubframe').css('width', frameWidth);
		});

		$('#content' + stId).append(
			$('<img/>').attr('title', 'закрыть сет').attr('src', '../photoset/l.gif').attr('class', 'str2').attr('height','38').attr('width','25')
.click(function() {
				closeSetContent(stId);
			})
		).show();


		frameWidth += 40;

		$('#psSubframe').css('width', frameWidth);
        $('.image2').wrap('<div class="inner-box2"></div>');
        $('.inner-box2').wrap('<div style="margin:0 6px 0 0px;"></div>');
        $('.inner-box2').before('<b class="v1"></b><b class="v2"></b><b class="v3"></b><b class="v4"></b><b class="v5"></b>');
        $('.inner-box2').after('<b class="v5"></b><b class="v4"></b><b class="v3"></b><b class="v2"></b><b class="v1"></b>');
		setMainframeStyle();
	});
}

function closeSetContent(stId) {
	$.each( $('#content'+stId+' img'), function (i,item) {
			frameWidth -= ( $(item).width()+(resizeOver/$(item).height())*$(item).width() );
		}
	);
	frameWidth -= 50;

	var off = $('#image' + stId).offset();
	moveFrame( - off.left + 67 );

	$('#content'+stId).html('').hide();
    $('#m').css('width', parseInt(frameWidth)-100);
	$('#psSubframe').css('width', frameWidth);
	if ( $('#psSubframe').width() < $('#psMainframe').width() ) {
		$('#psSubframe').css('left', 0);
	}


	$('#image' + stId)	.mouseover(function() {
									$(this).css('height' , $(this).height() + resizeOver);
						})
						.mouseout(function() {
									$(this).css('height' , $(this).height() - resizeOver);
						})
						.click(function() {
							$(this).css('height' , $(this).height() + resizeOver).unbind('mouseover').unbind('mouseout').unbind('click');

							$.each($("span[id^='content']"), function(i,item) {
									if ($(item).css('display') != 'none') {
										var id = parseInt($(item).attr('id').replace('content',''));
										closeSetContent(id);
									}
							});

							loadSetContent(stId);
						})
						.css('height', ($('#image' + stId).height() - resizeOver) );

	setMainframeStyle();
}
//frameWidth заменить на $('#psInitWidth').width() при надоности
function setMainframeStyle() {
	$('#psMainframe').css({
		'width': frameWidth,
		'height': ( maxHeight + wsize )
	});

	$('#psSubframe').css({
		'height':( maxHeight + wsize )
	});

	$('#psLeftScroll')	.hide()
						.mousedown(function() {
							scrollStop = 0;
							scroll( 10 );
						})
						.mouseup(function() {
							scrollStop = 1;
						});
	$('#psRightScroll')	.hide()
						.mousedown(function() {
							scrollStop = 0;
							scroll( -10 );
						})
						.mouseup(function() {
							scrollStop = 1;
						});

	if ( $('#psSubframe').width() > $('#psMainframe').width() ) {
		$('#psRightScroll').show();
	}

	//добавляем обработчик  прокрутки колеса мыши
	if (document.getElementById('psSubframe').addEventListener) {
		document.getElementById('psSubframe').addEventListener('DOMMouseScroll', wheel, false);
	}
	document.getElementById('psSubframe').onmousewheel = wheel;

	// добавляем обработчик для кнопок
	$(document)	.keydown(function(event){

					switch (event.keyCode ? event.keyCode : event.which ? event.which : null) {
						case 0x25:
							scrollStop = 0;
							scroll( 10 );
						break;
						case 0x27:
							scrollStop = 0;
							scroll( -10 );
						break;
					}
				})
				.keyup(function(event){
						scrollStop = 1;
				});;
}

function wheel(event) {
	var wheelDelta = 0;
	if (!event) event = window.event;
	if (event.wheelDelta)
    {
        wheelDelta = event.wheelDelta/120;
    }
    else if (event.detail)
    {
        wheelDelta = -event.detail/3;
    }

	moveFrame(wheelDelta * 30);

   /* if (event.preventDefault)
    {
        event.preventDefault();
    }*/
    event.returnValue = false;
}

function scroll(step) {
	moveFrame(step);
	if (!scrollStop) {
		setTimeout("scroll("+step+");",5);
	}
}

function moveFrame(step) {
	if ( $('#psSubframe').width() < $('#psMainframe').width() ) {
		return;
	}

	var  marginLeft = parseInt($('#psSubframe').css('left')) + step;


	if (step < 0 && frameWidth + marginLeft < $('#psMainframe').width() ) {
		$('#psSubframe').css('left', - (frameWidth - $('#psMainframe').width() ) );
		$('#psRightScroll').hide();
		//scrollStop = 1;
	} else if(step >0 && marginLeft > 0) {
		$('#psSubframe').css('left',0);
		$('#psLeftScroll').hide();
		//scrollStop = 1;
	}	else {
		$('#psSubframe').css('left', marginLeft );
		$('#psRightScroll').show();
		$('#psLeftScroll').show();
	}
}