if (typeof $$ == "undefined") var $$ = new Object();
$$ = {
	windowLoad: []
	,documentReady: []
	,registerWindowLoad: function(func){this.windowLoad.push(func);}
	,executeWindowLoad: function(){for(var i = 0; i < this.windowLoad.length; i++)if(typeof this.windowLoad[i] == 'function')this.windowLoad[i]();}
	,registerDocumentReady: function(func){this.documentReady.push(func);}
	,executeDocumentReady: function(){for(var i = 0; i < this.documentReady.length; i++)if(typeof this.documentReady[i] == 'function')this.documentReady[i]();}
	,setDestination:function(url){if(typeof url != 'undefined')document.location.href = url;else document.location.href = '/';}
	,go:function(url){this.setDestination(url);}
	,message: function(messages, title, callback, type){
		var items = [];
		if(typeof messages == 'string') items = [messages];
		else if(typeof messages != 'undefined') items = messages;


		if(typeof title == 'function'){
			callback = title;
			title = null;
		}else if(typeof title == 'undefined'){
			title = null;
		}

		if(title === null){
			if(type == 'alert'){title = 'Üzenet';}
			else{title = 'Figyelmeztetés';}
		}

		var html = '';
		for(var i=0;i<items.length;i++){
			if(type == 'alert'){
				html += '<p><span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span><strong>' + items[i] + '</strong></p>';
			}else{
				if(typeof items[i] == 'array'){
					$('#' + items[i][0]).addClass('error');
					items[i] = items[i][1];
				}
				html += '<div class="ui-widget"><div style="padding: 0pt 0.7em;" class="ui-state-error ui-corner-all"><span style="float: left; margin-right: 0.3em;" class="ui-icon ui-icon-alert"></span><strong>' + items[i] + '</strong></div></div>';
			}
		}
		$("#dialog").html(html);
		$("#dialog").dialog({
			modal: true,
			title: title,
			buttons: {
				Rendben: function() {
					$(this).dialog('close');
					if(typeof callback == 'function')callback();
				}
			}
		});
	}
	,alert: function(messages, title, callback){$$.message(messages, title, callback, 'alert')}
	,warning: function(messages, title, callback){$$.message(messages, title, callback, 'warning')}
	,confirm: function(message, title, callbackOK, callbackCancel){
		if(typeof title == 'function'){
			callbackCancel = callbackOK;
			callbackOK = title;
			title = 'Megerősítés';
		}
		$$.choose(message, title, {
			'Rendben':callbackOK ,
			'Mégsem': callbackCancel
		});
	}

	,choose: function(message, title, options){
		var createCallback = function(func){
			return function(){
				if(typeof func == 'function')func();
				$(this).dialog('close');
			}
		}
		if(typeof title == 'object'){
			callback = title;
			title = 'Válassz!';
		}
		var buttons = {};
		for(var i in options){
			buttons[i] = createCallback(options[i]);
		}
		html = '<p><span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span><strong>' + message + '</strong></p>';
		$("#dialog").html(html);
		$("#dialog").dialog({
			modal: true,
			title: title,
			buttons: buttons
		});
	}
	,loadingStart: function(){$('#loading_div').fadeIn();}
	,loadingEnd: function(){$('#loading_div').fadeOut();}
	,initCtGalleries: function(){
		$('div.ct_gallery').each(function(){
			var imageTitle = $(this).attr('title');
			var rel = $(this).attr('rel');
			$('a[rel='+rel+']').fancybox({
				'titlePosition' 	: 'over',
				'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
					var t;
					if($(this).attr('title')) t = $(this).attr('title');
					else t = imageTitle;
					return '<span id="fancybox-title-over">' + t + ' ' +  (currentIndex + 1) + ' / ' + currentArray.length + '</span>';
				}
			});
		});
	}
}
$$.registerDocumentReady(function(){
	$('body').append('<div id="dialog" style="display:none;"></div>');
	$('body').append('<div id="loading_div" style="display:none;">'+
		'<div class="loading"></div>'+
		'<div class="loading_image"><img src="/img/loader.gif" /></div>'+
	'</div>');
});
