﻿var boxwrap = '<div id="boxwrap"><div id="boxwrap-bg"></div><div id="boxwrap-content"></div></div>';
var curr_img;

function returnshowboxhandler(i)
{
	var drdre = $('#showbox-content > div')[i];
	return function () {
		showbox(drdre);
		return false;
	}
}

$(document).ready(function () {
	links = $('.showbox-link');
	for (i = 0; i < links.length; i++)
	{
		links.eq(i).click(returnshowboxhandler(i));
	}
	$('ul.folders > li > span').click(function () {
		$(this).parent().find('ul').toggle();
	});
	
});

function next_img()
{
	imgs = $('#boxwrap-content img');
	if (curr_img < imgs.length-1)
	{
		$('#prev-img').show();
		imgs.eq(curr_img).hide();
		curr_img++;
		imgs.eq(curr_img).show();
		if (curr_img == imgs.length-1)
		{
			$('#next-img').hide();
		}
	}
}

function prev_img()
{
	imgs = $('#boxwrap-content img');
	if (curr_img > 0)
	{
		$('#next-img').show();
		imgs.eq(curr_img).hide();
		curr_img--;
		imgs.eq(curr_img).show();
		if (curr_img == 0)
		{
			$('#prev-img').hide();
		}
	}
}


function showbox(eminem) {
	wutang = $('#boxwrap');
	if (wutang.length) 
	{
		wutang.remove();
	}
	var box = $(boxwrap).hide().appendTo('body');
	$('#boxwrap-bg').height('100%').width('100%');
	hx = $(window).width()/2 - 250;
	hy = $(window).height()/2 - 400;
	if (hy < 0) {
		hy = 20;
	}
	$('#boxwrap-content').css({'left': hx+ 'px', 'top': hy + 'px'});
	$('#boxwrap').show();
	$('#boxwrap-bg').click(function () {
		$('#boxwrap').remove();
	});
	$('<div id="boxwrap-close">x</div>').appendTo('#boxwrap-content').click(function () {
		$('#boxwrap').remove();
	});
	html = $(eminem).html();
	$(html).appendTo('#boxwrap-content');
	$('#boxwrap-content img').eq(0).show();
	curr_img = 0;
	if ($('#boxwrap-content img').length > 1) 
	{
		$('#boxwrap-content').append('<br/><div style="float:right; padding-top:10px; cursor:pointer;" id="next-img">следующая страница &gt;</div><div style="float:left; padding-top:10px; display:none; cursor:pointer;" id="prev-img">&lt; предыдущая страница </div>');
		$('#next-img').click(function () {
			next_img();
		});
		$('#boxwrap-content img').click(function () {
			next_img();
		});

		$('#prev-img').click(function () {
			prev_img();
		});
		
	}
}
