var path_to_loader = "images/loading.gif";
var animation_speed = "normal";
var fade_speed = "slow";
	
var next_label_active = "<a href='' id='jb_next'>Вперед &gt;</a>";
var next_label_not_active = "<font>Вперед &gt;</font>";
var prev_label_active = "<a href='' id='jb_prev'>&lt; Назад</a>";
var prev_label_not_active = "<font>&lt; Назад</font>";
var beetween_labels_html = "&nbsp;&mdash;&nbsp;";
var close_html = "<span style='display:none' class='jb_close'><a href=''>закрыть</a></span>";


var top_indent = 100;
var DEFAULT_TYPE = "image";
var cont_types = new Array();
cont_types["image"] = "Фото";
cont_types["page"] = "Cтраница";
cont_types["video"] = "Видео";
var _PARAM = new Array();

var loader = new Image();
loader.src = path_to_loader;
function jqbox()
{
	$("a[rel^=jqbox]").unbind("click").click(function(){
									  jqbox_show($(this).attr("title"), this.href, $(this).attr("rel"), $(this).attr("rev"));
									  return false;
										})
}
function jqbox_show(title, url, group, params)
{	
	function jqbox_animate(content_html, width, height)
	{	
			function jqbox_postinit()
			{
				var $bottom = $("\n\t<div class='jqbox_inbottom'>"+title_html+"\n\t\t<div><span>"+count_html+prev_html+beetween_html+next_html+"</span>\n\t\t</div>\n\t</div>");
			
				if($('#jqbox_bottom').is(':hidden'))
					$bottom.css('visibility', 'hidden');
				$("#jqbox_bottom").html($bottom)
					.width($('#jqbox_image').width()+parseInt($('#jqbox_image').css('border-left-width'))+parseInt($('#jqbox_image').css('border-right-width')));

				var close_bottom = parseInt($("div.jqbox_inbottom").css("padding-bottom")) + "px";			
				$(".jb_close").css({right: $("div.jqbox_inbottom").css("padding-right"), bottom: close_bottom});
				
				if($('#jqbox_bottom').is(':hidden'))
					$('#jqbox_bottom').slideDown('fast', function(){$bottom.css('visibility', 'visible'); $(".jb_close").show();});

				if(_PARAM["scroll"] && _PARAM["scroll"].indexOf("yes") != -1) $("#jqbox_image").css("overflow-y", "auto");
				$("#jb_next").attr("href", next_url).click(function(){ 
										 //$("div#jqbox_bottom, .jb_close").hide();
										 $("div#jqbox_window img").fadeOut(fade_speed, function(){jqbox_show(next_title, next_url, group, next_params);});
										 if(_PARAM["type"].indexOf("image") == -1) jqbox_show(next_title, next_url, group, next_params);
										 
										 return false;
										 });
				$("#jb_prev").attr("href", prev_url).click(function(){ 
										 //$("div#jqbox_bottom, .jb_close").hide();
										 $("div#jqbox_window img").fadeOut(fade_speed, function(){jqbox_show(prev_title, prev_url, group, prev_params);});
										 if(_PARAM["type"].indexOf("image") == -1) jqbox_show(prev_title, prev_url, group, prev_params);
										 
										 return false;											
										 });
				$("#jqbox_over, .jb_close").click(function(){ 
										$("div#jqbox_window, div#jqbox_over").css("display", "none").remove();
										$(document).unbind('keypress');
										return false;
										});				
				}
				
				$("div#jqbox_image img").remove();
				JB_WIDTH = width;
				JB_HEIGHT = height;
				$("div#jqbox_image").animate({"height": JB_HEIGHT+"px"}, animation_speed, function(){
																						 $("div#jqbox_image").animate({"width": JB_WIDTH+"px"}, animation_speed);
																						 $("div#jqbox_window").animate({"left": parseInt($(window).width()/2)-parseInt(JB_WIDTH/2)+'px'}, animation_speed, function(){
				$("div#jqbox_image").html(content_html);
				if(_PARAM["type"].indexOf("video") != -1) jqbox_postinit();	
				else $("div#jqbox_image").children().hide().fadeIn(fade_speed, function(){jqbox_postinit();});
				});
																						 });
		}

		_PARAM["type"] = DEFAULT_TYPE;
		if(params) 
		{
			var param = params.split("~");
			for(i = 0; i<param.length; i++)
			{
				if(param[i].indexOf("width") != -1){
					var tmp = param[i].replace('width:', '');
					_PARAM["width"] = parseInt(tmp);
				}else if(param[i].indexOf("height") != -1){
					var tmp = param[i].replace('height:', '');
					_PARAM["height"] = parseInt(tmp);
				}else if(param[i].indexOf("scroll") != -1){
					var tmp = param[i].replace('scroll:', '');
					_PARAM["scroll"] = tmp;
				}else if(param[i].indexOf("type") != -1){
					var tmp = param[i].replace('type:', '');
					_PARAM["type"] = tmp.replace(/\s/, '');
				}else if(param[i].indexOf("add") != -1){
					var tmp = param[i].replace('add:', '');
					_PARAM["add"] = tmp;
				}
			}
		}

		loading_image = new Image();
		loading_image.src = path_to_loader;
		if(document.getElementById("jqbox_over") === null){
			$("body").append("\n<div id='jqbox_over' class='video'></div>\n<div id='jqbox_window'>\n"+close_html+"\n<div id='jqbox_image'>\n</div>\n<div id='jqbox_bottom'>\n</div>\n</div>");
			$("div#jqbox_window").css("left", (parseInt($(window).width()/2-$("#jqbox_image").width()/2))+"px").css("top", $(window).scrollTop()+top_indent);
			$("#jqbox_over").css({width: $(document).width(), height: $(document).height()});
			$(document)
				.keypress(function(e){
					if(e.keyCode == 27)
					{
						$("div#jqbox_window, div#jqbox_over").css("display", "none").remove();
						$(document).unbind('keypress');
					}
				});
		}

		var base_url = url;
		var next_html = prev_html = "";
		var next_url = prev_url = "";
		var next_title = prev_title = "";
		var count_html = beetween_html =  title_html = "";
		if(title != "") title_html = "<h4>"+title+', '+_PARAM['add']+"</h4>";
		else beetween_html = "&nbsp;";

		if(group.search(/.*\[.*\]/) != -1)
		{	
			JB_Array = $("a[rel='"+group+"']").get();
			found = false;
			
			for(i = 0; ((i<JB_Array.length) && (next_url === "")); i++)
			{
				if(!(JB_Array[i].href == url))
				{
					if(found){
						next_url = JB_Array[i].href;
						next_title = JB_Array[i].title;
						next_params = JB_Array[i].rev;
					}
					else {
						prev_url = JB_Array[i].href;
						prev_title = JB_Array[i].title;
						prev_params = JB_Array[i].rev;
					}
				}
				else
				{
					found = true;
					count_html = cont_types[_PARAM["type"]]+" "+(i+1)+" из "+JB_Array.length+"&nbsp;&nbsp;&nbsp;";
				}
			}
			
			if(next_url != "") next_html = next_label_active; else next_html = next_label_not_active;
			if(prev_url != "") prev_html = prev_label_active; else prev_html = prev_label_not_active;
			beetween_html = beetween_labels_html;
		}
		if(url.indexOf("?") !== -1) base_url = url.substr(0, url.indexOf("?"));
		var extension = base_url.match(/\.[a-z]+$/);
		
		$("div#jqbox_image").html("<img src='"+loading_image.src+"' />");
		$("#jqbox_image img").hide().css({left: parseInt($("#jqbox_image").width()/2-loading_image.width/2), top: parseInt($("#jqbox_image").height()/2-loading_image.height/2)}).show();
		if(_PARAM["type"].indexOf("image") != -1)
		{
			img = new Image();
			img.onload = function(){
				jqbox_animate("<img src='"+img.src+"' />", img.width, img.height);
			};
			
			img.src = base_url;	
		}else
		{
			$.post(url, {isAjax: "1"}, function(data){
									 $("#jqbox_bottom").html(data);
									 var width = (_PARAM["width"] != null ? (_PARAM["width"] > $("#jqbox_bottom").width() ? $("#jqbox_bottom").width() : _PARAM["width"]) : $("#jqbox_bottom").width());
									 var height = (_PARAM["height"] != null ? (_PARAM["height"] > $("#jqbox_bottom").height() ? $("#jqbox_bottom").height() : _PARAM["height"]) : $("#jqbox_bottom").height());
									 $("#jqbox_bottom").html("");
									 var tmp_height = top_indent+height+$(window).scrollTop()+50;
									 if(tmp_height > $(document).height()) $("#jqbox_over").css("height", tmp_height);
									 jqbox_animate(data, width, height);									 
									 });
		}
	}
	$(document).ready(function(){jqbox();});