// JavaScript Document
var MyBox = function()
{
	var _images = null;
	
	var _screen_bg = null;
	var _image_bg = null;
	
	var _arrow_left = null;
	var _arrow_right = null;
	var _btn_close = null;
	
	var _image = null;
	
	var _menu_position = null;
	var _screen_bg_padding = 24;
	
	var _current_nr = null;
	
	var _timeout = null;
	
	var _wallpapers_table = null;

	

	
	return{
				Init:function()
				{
					_screen_bg = $('<div id="__MyBoxBg" style="background-color:#FF0000; position:absolute; opacity:0; filter: alpha(opacity = 0); visibility:hidden; top:0; left:0;"></div>');
					_image_bg = $('<div id="__MyBox;" style="position:absolute; visibility:hidden; top:0; left:0; padding:'+_screen_bg_padding+'px; background:url(images/ajax-loader.gif) #000000 no-repeat 50% 50%; opacity:0;"></div>');
					_arrow_left = $('<div style="position:absolute; top:0; left:0; visibility:hidden; cursor:pointer;"><img src="images/goleft.gif" onmouseover="this.src=\'images/goleft_act.gif\'" onmouseout="this.src=\'images/goleft.gif\'"/></div>');
					_arrow_right = $('<div style="position:absolute; top:0; left:0; visibility:hidden; cursor:pointer;"><img src="images/goright.gif" onmouseover="this.src=\'images/goright_act.gif\'" onmouseout="this.src=\'images/goright.gif\'" /></div>');
					_btn_close = $('<div style="position:absolute; top:0; left:0; visibility:hidden; cursor:pointer;"><img src="images/close.gif" onmouseover="this.src=\'images/close_act.gif\'" onmouseout="this.src=\'images/close.gif\'"/></div>');

					
					//_image_bg = $('<div id="__MyBox;" style="position:absolute; visibility:hidden; top:0; left:0; padding:20px; background:url(images/ajax-loader.gif) #000000 no-repeat 50% 50%; opacity:0;"></div>');
					//_image_bg = $('<table border="0" cellspacing="0" cellpadding="0" style="position:absolute; top:0; left:0; visibility:hidden; background-color:#000000"><tr><td><img src="images/0.gif" /></td><td><img src="images/0.gif" /></td><td style="text-align:right; cursor:pointer; height:15px; padding-right:5px; padding-top:5px" id="__MyBox_BtnClose"><img src="images/btn_close.gif" /></td></tr><tr><td valign="middle" style="padding:0 5px"><img src="images/a_left.gif" style="cursor:pointer" id="__MyBox_BtnLeft" /></td><td style="background:url(images/ajax-loader.gif) no-repeat 50% 50%;"><div id="__MyBox_image_cell"></div></td><td valign="middle" align="right" style="padding:0 5px"><img src="images/a_right.gif" style="cursor:pointer" id="__MyBox_BtnRight" /></td></tr><tr><td><img src="images/0.gif" /></td><td><img src="images/0.gif" /></td><td style="height:15px"><img src="images/0.gif" /></td></tr></table>');
					_menu_position = $("#submeniu").position();					
					
					$('body').append(_screen_bg);
					$('body').append(_image_bg);
					$('body').append(_arrow_left);
					$('body').append(_arrow_right);
					$('body').append(_btn_close);
					
					_screen_bg.click(function(){MyBox.Close()});
					_btn_close.click(function(){MyBox.Close()});
					_arrow_left.click(function(){MyBox.NavLeft()});
					_arrow_right.click(function(){MyBox.NavRight()});
					
					_images = $(".gallery img.full");
					_images.each(function(){MyBox.Image_init(this)});
					
					$("#__MyBoxBg").height($(window).height());
					$("#__MyBoxBg").width($(document).width());
				},
				
				Image_init:function(img)
				{
					$(img).parent("a").click(function(){return false;});
					var wallpapers = $(img).parents("li").children(".wallpaper");
					wallpapers.css('display', 'none');
					
					$(img).click(function(){MyBox.Image_OnClick(img)});
				},
				
				Image_OnClick:function(img)
				{
					var ScrollTop = document.body.scrollTop;
					if (ScrollTop == 0)
					{
						if (window.pageYOffset)
							ScrollTop = window.pageYOffset;
						else
							ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
					}
					_menu_position.top=ScrollTop+100;

					var nr = null;
					clearTimeout(_timeout);
					for(var i = 0 ; i < _images.length; i++)
					{
						if(_images[i] == img)
							nr = i;
					}
					
					_current_nr = nr;
					
					_screen_bg.css({'visibility':'visible'});
					
					_image_bg.width(200);
					_image_bg.height(200);
					
					_image_bg.css({'visibility':'visible', 'top':_menu_position.top +'px', 'left':_menu_position.left+'px'});
					_image_bg.fadeTo(300, 1);
					
					this.Image_Preload()
				},
				
				Image_Preload:function()
				{
					if(_image)
					{
						_image.remove();	
					}
					if(_wallpapers_table)
					{
						_wallpapers_table.remove();	
						_wallpapers_table = null;
					}
					
					var img = new Image();
					_image = $(img);
					_image_bg.append(_image);
					
					_image.css({'opacity':'0'});
					//_image.css({'display':'none'});
					_arrow_left.css({'visibility':'hidden'});
					_arrow_right.css({'visibility':'hidden'});
					_btn_close.css({'visibility':'hidden'});
					
					_image.attr("src", $(_images[_current_nr]).parent("a").attr("href"));
					img.onLoad = this.Image_OnLoad();
					//_image.load(MyBox.Image_OnLoad());
					//_time = new Date().getTime();
					
				},
				
				Image_OnLoad:function()
				{
					if(_image == null)
						return;
					_timeout = setTimeout("MyBox.Resize_Start()", 100);
				},
				
				Resize_Start:function()
				{
					
					if(_image.width() <= 50) //mol nesurenderino dar
					{
						clearTimeout(_timeout);
						_timeout = setTimeout("MyBox.Resize_Start()", 100);
						return;	
					}
					
					if(_image == null)
						return;
					
					_image_bg.animate({width:_image.width()+'px',
									   height:_image.height()+'px'}, 200, "linear", function(){MyBox.Resize_Finish()});
				},				
				
				Resize_Finish:function()
				{
					if(_image == null)
						return;
					_btn_close.css({'visibility':'visible', 'top':(_menu_position.top + 5) +'px', 'left':(_menu_position.left + _image_bg.width() + (_screen_bg_padding * 2) - 20) + 'px'});
					_arrow_left.css({'visibility':'visible', 'top':(_menu_position.top + _image_bg.height() / 2  + _screen_bg_padding - 10) +'px', 'left':(_menu_position.left + 5) + 'px'});
					_arrow_right.css({'visibility':'visible', 'top':(_menu_position.top + _image_bg.height() / 2 + _screen_bg_padding - 10) +'px', 'left':(_menu_position.left + _image_bg.width() + (parseInt(_image_bg.css("padding")) * 2) - 20) + 'px'});
					//_image.css({'opacity':'1'});
					_image.fadeTo(300, 1);
					
					/*mol pluginas, wallpaperiams atsisiųsti*/
					var wallpapers = $(_images[_current_nr]).parents("li").children('.wallpaper');
					
					if(wallpapers.length)
					{
						
						var _wallpapers_table_html = '<table class="__MyBoxBg_wallpapers_table"><tr>';
						for(var i = 0; i < wallpapers.length; i++)
						{
							_wallpapers_table_html += '<td style="padding-bottom:10px"><a href="'+wallpapers[i].href+'" target="_blank">'+wallpapers[i].innerHTML+'<img src="images/a_down.gif"  align="absmiddle" /></a></td>';
							/*td = $('<td><a href="'+wallpapers[i].href+'" target="_blank">'+wallpapers[i].innerHTML+'<img src="images/a_down.gif"  align="absmiddle" /></a></td>');
							_wallpapers_table.children("tr").append(td);*/
						}
						_wallpapers_table_html += "</tr></table>";
						_wallpapers_table = $(_wallpapers_table_html);
						//_wallpapers_table = $('<table class="__MyBoxBg_wallpapers_table"><tr></tr></table>');
						
						_wallpapers_table.css({'padding':'0', 'margin':'0', 'width':(_image_bg.width() + parseInt(_image_bg.css("padding")) * 2)+'px', 'left':_menu_position.left+'px', 'top':(_menu_position.top + _image_bg.height() + parseInt(_image_bg.css("padding")) + 10)});
						//var td = null;
						/*for(var i = 0; i < wallpapers.length; i++)
						{
							td = $('<td><a href="'+wallpapers[i].href+'" target="_blank">'+wallpapers[i].innerHTML+'<img src="images/a_down.gif"  align="absmiddle" /></a></td>');
							_wallpapers_table.children("tr").append(td);
						}*/
						$('body').append(_wallpapers_table);
						//_wallpapers_table.remove();
						
						//_wallpapers_table.show("slow");
					}
					/*//mol pluginas, wallpaperiams atsisiųsti*/					
					
					if(_current_nr <= 0)
					{
						_arrow_left.css({'visibility':'hidden'});
					}
					
					if(_current_nr >= _images.length  - 1)
					{
						_arrow_right.css({'visibility':'hidden'});
					}
					
					if(_images.length <= 1)
					{
						_arrow_right.css({'visibility':'hidden'});
						_arrow_left.css({'visibility':'hidden'});
					}
				},
				
				
				
				NavLeft:function()
				{
					_current_nr--;
					this.Image_Preload();
				},
				
				NavRight:function()
				{
					_current_nr++;
					this.Image_Preload();
				},
				
				Close:function()
				{
					
					if(_wallpapers_table != null)
					{
						_wallpapers_table.remove();
						_wallpapers_table = null;						
					}
					
					if(_image)
					{
						//_image.css({'visibility':'hidden'});
						_image.remove();
						_image = null;
					}
					_arrow_left.css({'visibility':'hidden'});
					_arrow_right.css({'visibility':'hidden'});
					_btn_close.css({'visibility':'hidden'});
					
					_image_bg.fadeTo(300, 0, this.Close_Finished);
				},
				
				Close_Finished:function()
				{
					_image_bg.css({'visibility':'hidden'});
					_screen_bg.css({'visibility':'hidden'});
					if(_image)
					{
						_image.remove();
						_image = null;
					}
				}
	}
}();

$(document).ready(MyBox.Init);
