var Blog={
	id:0,
	url:'http://blog.hu',
	adocean:false,
	adrand:Math.floor(Math.random()*10000),
	sidebox:{
		width:160,
		height:290,
		container:null
	},
	billboard:{
		width:310,
		height:232,
		container:null
	},
	isIndex:function(){
		return ((Blog.adrand % 10)<5)
	},
	createAd:function(type,container_id){
		Blog[type].container=Ext.get(container_id);
		var iframe='<iframe width="'+Blog[type].width+'" height="'+Blog[type].height+'" src="'+Blog.url+'/ad/'+type+'"';
		iframe+='frameborder="0" scrolling="no" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true">';
		iframe+='</iframe>';
		Blog[type].container.insertHtml('afterBegin',iframe);
	},
	closeAd:function(type){
		Blog[type].container.setStyle('display','none');
	},
	shortcuts:{
		itemSelector:'a.post_anchor',
		items:[],
		currentItem:-1,
 		excludedElements:['INPUT','TEXTAREA'],
		isVisible:function(e){
			var itemY;
			var docScroll=Ext.get(document).getScroll().top;
			return Ext.get(e).getY()>=docScroll; 
		},
		scroll:function(e){
			window.scrollTo(Ext.get(document).getScroll().left,Ext.get(e).getY());
		},
		keyCodeFunctions:{
			74:function(e){
				var delta;
			    if(Blog.shortcuts.currentItem<Blog.shortcuts.items.length-1) {
			    	
			    	if(Blog.shortcuts.currentItem==-1 || Blog.shortcuts.isVisible(Blog.shortcuts.items[Blog.shortcuts.currentItem])) {
						Blog.shortcuts.currentItem++;
			    	}
			    	else
			    	{
				    	if(Ext.get(Blog.shortcuts.items[Blog.shortcuts.currentItem]).getY()<Ext.get(document).getScroll().top) {
				    		delta=1;
				    	}
				    	else
				    	{
				    		delta=-1;
				    	}
				    	while(!Blog.shortcuts.isVisible(Blog.shortcuts.items[Blog.shortcuts.currentItem]) && (Blog.shortcuts.currentItem<Blog.shortcuts.items.length-1) && Blog.shortcuts.currentItem>-1) {
				    		Blog.shortcuts.currentItem+=delta;
				    	}			    		
			    	}
					Blog.shortcuts.scroll(Blog.shortcuts.items[Blog.shortcuts.currentItem]);
			    }
			},
	    	75:function(e){
	    		if(Blog.shortcuts.currentItem>0){
			    	if(Blog.shortcuts.isVisible(Blog.shortcuts.items[Blog.shortcuts.currentItem])) {
						Blog.shortcuts.currentItem--;
			    	}
			    	else
			    	{
				    	if(Ext.get(Blog.shortcuts.items[Blog.shortcuts.currentItem]).getY()<Ext.get(document).getScroll().top) {
				    		delta=-1;
				    	}
				    	else
				    	{
				    		delta=1;
				    	}
				    	while(!Blog.shortcuts.isVisible(Blog.shortcuts.items[Blog.shortcuts.currentItem]) && (Blog.shortcuts.currentItem<Blog.shortcuts.items.length-1) && Blog.shortcuts.currentItem>-1) {
				    		Blog.shortcuts.currentItem+=delta;
				    	}			    		
			    	}
					Blog.shortcuts.scroll(Blog.shortcuts.items[Blog.shortcuts.currentItem]);
			    }
	    	}
		},
		addKey:function(keycode,func){
			Blog.shortcuts.keyCodeFunctions[keycode]=func;
		},
		loadItems:function(){
			var anchors=Ext.select(Blog.shortcuts.itemSelector);
			if(anchors.elements)
			{
				Blog.shortcuts.items=anchors.elements;
			}
		},
		event:function(e){
			var code=e.getKey();
			var tagName=e.getTarget().tagName;
			if(Blog.shortcuts.excludedElements.indexOf(tagName)>=0) return true;
			if(Blog.shortcuts.keyCodeFunctions.hasOwnProperty(code)) {
				Blog.shortcuts.keyCodeFunctions[code](e);
			}	
			return true;
		},
		start:function(){
			Blog.shortcuts.loadItems();
			Ext.get(document).on('keydown',Blog.shortcuts.event);
			return true;
		}
	},
	preview:{
		display:function(){
			if(Blog.getCookie('previewskin')!==null){		
				Ext.getBody().insertHtml('beforeEnd','<div id="previewBar" style="position:absolute;top:0px;left:0px;width:100%;height:25px;text-align:center;background-color:red;padding:5px 0px;font-size:18px;"><span style="color:yellow;font-family:sans">Előnézet módban vagy!</span><button style="margin-left:20px" onclick="Blog.preview.quit()">Kattints ide, hogy kilépj az előnézetből!</button></div>');
				Ext.getBody().setStyle('padding-top','35px');
			}
		},
		quit:function(){
			Blog.setCookie('previewskin',0,-20);
			document.location.href=document.location.href+((document.location.href.indexOf('?')>-1) ? '&' : '?' )+'rnd='+(Math.floor(Math.random()*1000));
		}
	},
	layer:{
		element:null,
		content:null,
		init:function(){
			Blog.layer.element=Ext.getBody().insertHtml('afterBegin','<div id="_layer" style="z-index:10;position:absolute;display:none;top:0px;left:0px;background-color: #000000;"></div>',true);
			Blog.layer.element.visibilityMode=Ext.Element.DISPLAY;
		},
		show:function(content){
			if(Blog.layer.element==null){
				Blog.layer.init();
			}
			if(content) {
				Blog.layer.content=Ext.select(content);
				Blog.layer.content.setDisplayed(false);
			}
			Blog.layer.resize();			
			Ext.EventManager.addListener(window,'resize',Blog.layer.resize);
			Blog.layer.element.show();
		},
		hide:function(){
			Ext.EventManager.removeListener(window,'resize',Blog.layer.resize)			
			if(Blog.layer.content){
				Blog.layer.content.setDisplayed(true);
			}
			Blog.layer.element.hide();
		},
		resize:function(){	
		    var viewSize;
		    if(Blog.layer.element!=null) {
		    	viewSize=Blog.getViewSize(true);
			    Blog.layer.element.setWidth(viewSize[0]);
			    Blog.layer.element.setHeight(viewSize[1]);
		    }
		}
	},
	dialog:{
		element:null,
		config:{},
		buttons:[],
		init:function(){
			Blog.dialog.element=Ext.getBody().insertHtml('beforeEnd','<div id="_dialog" style="z-index:20;visibility:hidden;position:absolute;"></div>',true);
			return Blog.dialog;
		},
		parseConfig:function(){
			var i,o;
			for(i in Blog.dialog.config) {
				if(Blog.dialog.config.hasOwnProperty(i)) {
					switch(i) {
						case 'width':
							Blog.dialog.element.setWidth(Blog.dialog.config[i]);
							break;
						case 'height':
							Blog.dialog.element.setHeight(Blog.dialog.config[i]);
							break;
						case 'buttons':
							for(o=0;o<Blog.dialog.config[i].length;o++){
								Blog.dialog.addButton(
									Blog.dialog.config[i][o].id,
									Blog.dialog.config[i][o].text,
									Blog.dialog.config[i][o].handler
								);
							}
							break;
					}
				}
			}
		},
		addButton:function(id,text,handler) {
			Blog.dialog.buttons.push({id:id,text:text,handler:handler});
		},
		setPosition:function(){
			var viewport=Blog.getViewSize(false);
			Blog.dialog.element.setX(viewport[0]/2-Blog.dialog.config.width/2);
			Blog.dialog.element.setY(viewport[1]/2-Blog.dialog.config.height/2);
		},
		show:function(config) {
			var buttonbar='',i;
			var bbe;

			if(Blog.dialog.element==null) {
				Blog.dialog.init();
			}
			else {
				Blog.dialog.element.child('#_dialog_text').remove();
				Blog.dialog.element.child('#_dialog_buttonbar').remove();
			}
			
			Blog.dialog.config=config;
			Blog.dialog.parseConfig();
			
			Blog.dialog.element.insertHtml('afterBegin','<div id="_dialog_text">'+Blog.dialog.config.text+'</div>');
			for(i=0;i<Blog.dialog.buttons.length;i++) {
				buttonbar+='<button id="'+Blog.dialog.buttons[i].id+'" onclick="Blog.dialog.buttons['+i+'].handler()">'+Blog.dialog.buttons[i].text+'</button>';
			}
			if(Blog.dialog.config.css){
				Blog.dialog.element.addClass(Blog.dialog.config.css);
			}
			
			bbe=Blog.dialog.element.insertHtml('beforeEnd','<div id="_dialog_buttonbar" style="text-align:center;width:100%">'+buttonbar+'</div>',true);

			if(Blog.dialog.config.style){
				Blog.dialog.element.setStyle(Blog.dialog.config.style);
			}
						
			Blog.dialog.setPosition();
			Ext.EventManager.addListener(window,'resize',Blog.dialog.setPosition);
			Blog.dialog.element.show();
		},
		hide:function(){
			Ext.EventManager.removeListener(window,'resize',Blog.dialog.setPosition)			
			Blog.dialog.element.hide();
		}
	},
	tracker:{
		traffic_code:null,
		webaudit_code:null,
		gemius_code:null
	},
	user:{
		data:null,
		callbacks:Array(),
		registerCallback:function( func ){
			if ( !this.isLoaded() )
			{
				this.callbacks.push( func );
			}
			else
			{
				func();
			}
		},
		load:function(){
			Ext.Ajax.request({
				url:Blog.url+'/user/json',
				success: this.loadSuccess.createDelegate( this )
			});
		},
		loadSuccess:function( response, opts )
		{
			Blog.user.data=Ext.decode(response.responseText);
			for ( i = 0; i < this.callbacks.length; i++)
			{
				this.callbacks[i]();
			}
		},
		isLoaded:function(){
			return (Blog.user.data!=null)
		},
		isLoggedIn:function(){
			if(!Blog.user.isLoaded()) {
				return null;
			}
			return Blog.user.data.loggedin;
		},
		getDefault:function() {
			var i;
			if(Blog.user.isLoggedIn()) {
				for(i=0;i<Blog.user.data.users.length;i++) {
					if(Blog.user.data.users[i].selected) {
						return Blog.user.data.users[i];
					}
				}
			}
			return false;
		}
	},
	setCookie:function(c_name,value,expiredays){
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+expiredays);
		document.cookie=c_name+ "=" +escape(value)+";path=/"+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
	},
	getCookie:function(c_name){
		if (document.cookie.length>0){
			c_start=document.cookie.indexOf(c_name + "=");
		  	if (c_start!=-1){
		  		c_start=c_start + c_name.length+1;
		    	c_end=document.cookie.indexOf(";",c_start);
		    	if (c_end==-1) c_end=document.cookie.length;
		    	return unescape(document.cookie.substring(c_start,c_end));
		  	}
		}
		return null;
	},
	getViewSize: function(criptic) {
            return [Ext.lib.Dom.getViewWidth(criptic), Ext.lib.Dom.getViewHeight(criptic)];
    },
	start:function(){
		Blog.shortcuts.start();
		Blog.preview.display();
	}
}

var Dialog={};

