var Across = {

	definedURL : false,
	working      : true,
	gallery      : {imgs : false, actual : false},
	savedGallery : false,

	popupImageWidth : false,
	popupImageHeight : false,


	urlwrite :function(url)
	{
		window.location.hash = url;
		
	},

	getsociallink : function (item)
	{
		var tmp = window.location.href.replace("#",'?');
		var linkref = $(item).href;
		//$(item).href= $(item).href.replace("XXX", tmp);
		//window.location.href = linkref.replace("XXX", tmp);

		window.open(linkref.replace("XXX", tmp));
		
		return false;
	},

	checkDefinedUrl : function ()
	{
		var loc = window.location.href.split('#', 2);
		
		
		
		if (loc[1])
		{

			
			if (loc[1].search("gallery=") != -1)
			{
				var tmp = loc[1].replace("gallery=", "");
				tmp = tmp.split('-', 3);
				
				if(tmp[2] == undefined)
				{
					tmp[2] = 'normal';
				}
				
				Across.opendefinedGallery(tmp[0],tmp[1],tmp[2]);


			}
		}
	},

	opendefinedGallery : function (gallery,actual,option)
	{
		
		
		var loc = window.location.href.split('?', 2);

		if (loc[1])
		{

			
			if (loc[1].search("gallery=") != -1)
			{
				var tmp = loc[1].replace("gallery=", "");
				tmp = tmp.split('-', 3);
				
				if(tmp[2] == undefined)
				{
					tmp[2] = 'normal';
				}
				else
				{
					option = 'large';
				}
				


			}
		}
		
		        new Ajax.Request(
		        '/', {
                method: "post",
                parameters: {
                    Func : 'gallery.getimages',
					id : gallery
                },
                onComplete: function (request){
                	
                    eval("var Result = " + request.responseText);
					
					Across.gallery.id      = gallery;			
					Across.gallery.imgs    = Result.images;
					Across.gallery.actual  = actual-1;
					Across.popupImageWidth = Result.width;
					
					dialogHandler.popup({
						Body  : Result.text
					});
					
					Across.nextImage();
					
					if(option == 'large')
					{
						Across.socialPop();
					}

					
					
                }
            }
        ); 
		
	},
	socialPop : function()
	{
		
		var imgsrc = $('large-image').readAttribute('src').replace('/mid','');
		
		$('popup_content').style.display = 'none';
		$('popup_large_image').style.display = 'none';
		$('popup_large_image').innerHTML = '<div id="large_holder"><img src="/layout/gfx/popup/close.gif" onclick="Across.CloseImage();" id="large_close_ize" alt="bezárás" /><img id="large_popped_image" src="' + imgsrc + '" alt="" style="cursor: pointer; margin-bottom: 20px;" onclick="Across.nextLargeImage()" /></div>';

		$('overlay').removeClassName('opacity');

		Across.AdjustLargeImage();

		$('popup_large_image').style.display = 'block';

        dialogHandler.setPT();

		dialogHandler.setOverlay();

		return false;
		
	},
	Toggle : function (id)
	{
		$(id).style.display = ($(id).style.display == 'block') ? 'none' : 'block';
	},
	
	clearField : function (item)
	{
        $(item).value = '';
	},

	sendM : function (thisFom)
    {
        Across.actualForm = thisFom;
        new Ajax.Request(
            '/', {
                method: "post",
                parameters: 'Func=contacts.sendMessage&'+$(thisFom).serialize(),
                onComplete: function (request){
                    eval("var Result = " + request.responseText);

                    if (Result.error)
                    {
                        Object.keys(Result.items).each(function(s, index) {
                            $(s).value = Result.items[s];
                        });
                    }
                    else
                    {
                        alert(Result.message);
                        $(Across.actualForm).reset();
                    }
                }
            }
        ); 
    },

	getGalleryPopup : function (id)
	{
    	
    	var loc = window.location.href.split('#', 2);

		

		if (loc[1] == undefined)
		{
			window.location.href = '#gallery='+id+'-0';
		}
    	
        new Ajax.Request(
            '/', {
                method: "post",
                parameters: {
                    Func : 'gallery.getimages',
					id : id
                },
                onComplete: function (request){

                    eval("var Result = " + request.responseText);
					
					Across.gallery.id      = id;			
					Across.gallery.imgs    = Result.images;
					Across.gallery.actual  = Result.actual;
					Across.popupImageWidth = Result.width;

					dialogHandler.popup({
						Body  : Result.text
					});

					Across.sortImages();
                }
            }
        ); 

	},

	nextImage : function ()
	{
		if (arguments[0])
		{
			Across.gallery.actual =( arguments[0] -1 );
		}
		if (Across.gallery.actual < (Across.gallery.imgs.length -1 ))
		{
			Across.gallery.actual++;
		}
		else
		{
			Across.gallery.actual = 0;
		}

		img = Across.gallery.imgs[ Across.gallery.actual ];

		$('large-name').innerHTML  = img['title'] + '<br/>' + img['description'];
		$('large-id').innerHTML    = img['id'];
		$('large-image').src       = '/uploads/gallery/mid/' + img['img'];
		$('large-link').href       = '/uploads/gallery/' + img['img'];

		Across.popupImageWidth = img['width'];
		Across.popupImageHeight = img['height'];


		window.location = '#gallery='+Across.gallery.id+'-'+Across.gallery.actual;


		Across.sortImages();
	},

	createImage : function (stack)
	{
		img = document.createElement('img');
		img.src = '/uploads/gallery/tn/' + Across.gallery.imgs[stack].img;

		$('thumbnails').appendChild(img);

		img.onclick = function () {
			Across.nextImage(stack);
		}
	},

	scrollTop : function ()
	{
		
		
		
		if (Across.gallery.actual > 0)
		{
			Across.gallery.actual--;
		}
		else
		{
			Across.gallery.actual = Across.gallery.imgs.length -1;
		}

		Across.sortImages();
	},

	scrollBottom : function ()
	{
		if (Across.gallery.actual >= Across.gallery.imgs.length)
		{
			Across.gallery.actual = 0;
		}
		else
		{
			Across.gallery.actual++
		}

		Across.sortImages();
	},

	sortImages : function ()
	{
		
		var actual    = arguments[0] ? arguments[0] : Across.gallery.actual;

		

		$('thumbnails').innerHTML = '';

		if (Across.gallery.imgs.length <= 4)
		{
			for (j=0;j<Across.gallery.imgs.length;j++)
			{
				Across.createImage(j);
			}
		}
		else
		{

			var displayed = 0;

			while ( displayed < 4)
			{
				if (Across.gallery.imgs[actual])
				{
					displayed++;
					Across.createImage(actual);
				}

				actual++;

				if (actual > Across.gallery.imgs.length)
				{
					actual = 0;
				}
			}
		}
	},
	
	OpenImage : function (item)
	{
		
		$$('.normal').each(function(i,v){
			$(i).hide();
		});
		$$('.large').each(function(i,v){
			$(i).show();
		})
		
		var imgsrc = $('large-image').readAttribute('src').replace('/mid','');
		
		$('popup_content').style.display = 'none';
		$('popup_large_image').style.display = 'none';
		//$('popup_large_image').innerHTML = '<div id="large_holder"><img src="/layout/gfx/popup/close.gif" onclick="Across.CloseImage();" id="large_close_ize" alt="bezárás" /><img id="large_popped_image" src="' + $(item).parentNode.parentNode.href + '" alt="" style="cursor: pointer; margin-bottom: 20px;" onclick="Across.nextLargeImage()" /></div>';
		$('popup_large_image').innerHTML = '<div id="large_holder"><img src="/layout/gfx/popup/close.gif" onclick="Across.CloseImage();" id="large_close_ize" alt="bezárás" /><img id="large_popped_image" src="' + imgsrc + '" alt="" style="cursor: pointer; margin-bottom: 20px;" onclick="Across.nextLargeImage()" /></div>';
		
		
		$('overlay').removeClassName('opacity');

		Across.AdjustLargeImage();

		$('popup_large_image').style.display = 'block';

        dialogHandler.setPT();

		dialogHandler.setOverlay();

		return false;
	},

	setPopups : function()
	{
		
		var items = $$('.zoomer');

		if (items) {
			for (var i = 0; i < items.length; i++) {
				Event.observe($(items[i]), 'click', Across.popupImage);
			}
		}
		
	},

	popupImage : function (event)
	{
		
		
				
		var item = Event.element(event);

		dialogHandler.popup({
			Body  : '<div id="large_holder"><img src="/layout/gfx/popup/close.gif" onclick="dialogHandler.dialogClose();" id="large_close_ize" alt="bezárás" /><img id="large_popped_image" src="' + $(item).parentNode.href + '" alt="" style="cursor: pointer; margin-bottom: 20px;" onclick="dialogHandler.dialogClose();" /></div>'
		});


		Event.stop(event)

//		Across.AdjustLargeImage();

//        dialogHandler.setPT();


		$('large_popped_image').onload = function () 
		{
			var width = $('large_popped_image').getWidth();
			var height = $('large_popped_image').getHeight();

			$('large_holder').setStyle({
				width   : width +'px',
				height  : height +'px',
				display : 'block'
			});

			dialogHandler.popupItem.setStyle({
				width   : width +'px',
				height  : height +'px',
				display : 'block'
			});
			$('large_close_ize').setStyle({
				left   : (width-20) +'px'
			});

		dialogHandler.setOverlay();

		dialogHandler.setPT();

		}


		dialogHandler.setPT();
		return false;
	},

	AdjustLargeImage : function ()
	{
		$('large_holder').setStyle({
            width   : (Across.popupImageWidth) +'px',
            height  : (Across.popupImageHeight) +'px',
            display : 'block'
        });

		dialogHandler.popupItem.setStyle({
            width   : (Across.popupImageWidth) +'px',
            height  : (Across.popupImageHeight) +'px',
            display : 'block'
        });

		$('large_close_ize').setStyle({
            left   : (Across.popupImageWidth-20) +'px'
        });

		dialogHandler.setPT();
	},

	nextLargeImage : function ()
	{
		if (arguments[0])
		{
			Across.gallery.actual =( arguments[0] -1 );
		}
		if (Across.gallery.actual < (Across.gallery.imgs.length -1 ))
		{
			Across.gallery.actual++;
		}
		else
		{
			Across.gallery.actual = 0;
		}

		img = Across.gallery.imgs[ Across.gallery.actual ];

		$('large_popped_image').src       = '/uploads/gallery/' + img['img'];

		Across.popupImageWidth = img['width'];
		Across.popupImageHeight = img['height'];

		Across.AdjustLargeImage();

		dialogHandler.setOverlay();

		Across.sortImages();
	},

	CloseImage : function ()
	{
		
		$$('.normal').each(function(i,v){
			$(i).show();
		});
		$$('.large').each(function(i,v){
			$(i).hide();
		})
		
		$('popup_large_image').innerHTML = '';

		dialogHandler.popupItem.setStyle({
            width   : dialogHandler.defaultWidth+'px',
            display : 'block'
        });

		dialogHandler.setPT();

		$('overlay').addClassName('opacity');
		$('popup_content').style.display = 'block';
	},

	popwin : function (url,width,height)
	{
		if ( !width ) width = '1024';
		if ( !height ) height = '768';

	    var popupWin = window.open(url,'popupWin','width='+width+'px,height='+height+'px,left=100,top=100,menubar=no,status=no,toolbar=no,scrollbars=yes,resizable=no,screenX=100,screenY=100,directories=no,location=no');

		popupWin.focus();
        return false;
	},
	
	showHide : function (id, parent)
	{
		
		
        var items = $$('.column2-item');
        
        for (i=0;i<items.length;i++)
        {
            if($(items[i]).id == id)
            {
               $(items[i]).style.display = 'block'; 
            }
            else
            {
               $(items[i]).style.display = 'none';  
            }
        }
	},

	setVideo : function (video)
	{
		var autostart      = arguments[1] ? arguments[1] : false;
		var flashfile      = '/layout/swf/bodogan_player.swf';
		var expressInstall = '/layout/swf/express_install.swf';

		var flashvars = {
			videoUrl : '/uploads/diaporamas/videos/' + video,
			autoStart   : autostart
		}

		var params = {
			scale: "noBorder",
			allowFullscreen:"true", 
			allowScriptAccess:"always",
			wmode : 'opaque'
		}
		var attributes = {
			id : 'player_' + video,
			name : 'player_' + video
		}

		dialogHandler.popup({
			Body  : '<div style="width: 640px; height: 530px;" class="video-frame"><img src="/layout/gfx/popup/close.gif" onclick="dialogHandler.dialogClose();" id="video-close" alt="bezárás" /><div id="video-holder"></div>',
			Width: 640
		});
		swfobject.embedSWF(flashfile, 'video-holder', "640", "530", '9.0.0', expressInstall, flashvars, params);
	},
	
	setEmbedPopup : function (id)
	{
		 new Ajax.Request(
            '/', {
                method: "post",
                parameters: {
                    Func : 'diaporamas.getembed',
					id : id
                },
                onComplete: function (request){
					
					eval ("var Result = " + request.responseText)
					
					dialogHandler.popup({
						Body  : Result.text,
						Width: 640
					});

					
                }
            }
        ); 
	},
	autoShowHide : function()
	{
		
		var locat = window.location.href.split('/szolgaltatasok/', 2);
		
		if(locat[1] != undefined)
		{
		
			var items = $$('.column2-item');
		    
		    
		    for (i=0;i<items.length;i++)
		    {
		        
		       $(items[i]).style.display = 'none';  
		        
		    }
			
		    $(locat[1]).style.display = 'block';
		}
	}
	
};

Event.observe(window, 'load', function() {
	Across.setPopups();
	Across.autoShowHide();
	
});
