
hsSite.loginS = hsSite.login;
hsSite.login = function(json){
	hsSite.loginS(json);
	hsSite.sajaths.addUserBoxes();
}

hsSite.Preferences.load = function() {
	if (!hsconf.user) {
		this.loadFromCookie();
	} else {
		hsSite.Preferences.change(hsconf.user);
	}
	hsSite.sajaths.addUserBoxes();
}

hsSite.sajaths = {
	boxesVisible: true,
	ajaxReorder: false,
	ajaxReorderTimerId: 0,
	
	init: function() {
		//információs doboz a nyitón
		if ($.cookie('SH_INFO')) $(':not(div#dialogs) div.info.closable').hide();
		else {
			$(':not(div#dialogs) div.info.closable a.close').click(function(){
				$(':not(div#dialogs) div.info.closable').hide();
				$.cookie('SH_INFO', 1, { expires: 365 });
				return false;
			});
		}
	},
	
	toggleBoxes: function(){
		var selector = 'div.fooldalBox > :not(h2, .settings_box)';
		var minimaze = 'div.fooldalBox > h2 a.minimaze';
		var link = $('a#closeall');
		if (this.boxesVisible) {
			$(selector).hide();
			$(minimaze).addClass('maximaze').attr('title', 'Kinyit');
			link.text('Minden dobozt kinyit');
		}
		else {
			$(selector).show();
			$(minimaze).removeClass('maximaze').attr('title', 'Összecsuk');
			link.text('Minden dobozt összecsuk');
		}
		link.toggleClass('closed');
		this.boxesVisible = !this.boxesVisible;
	},
	
	addUserBoxes: function(loaded){
		if (hsSite.userBoxes.baseItems && hsSite.userBoxes.baseItems.length){
			var boxes = {};
			var col = 0;
			var max = 0;
			for (var i=0;i<hsSite.userBoxes.baseItems.length; i++){
				var actualBox = hsSite.sajaths.createBox(hsSite.userBoxes.baseItems[i]);
				if (actualBox.pos_col != col){
					$('.oszlop'+ actualBox.pos_col).html('');
					col = actualBox.pos_col;
					max = actualBox.pos_row;
				}
				actualBox.maxInCol = max;
				boxes[hsSite.userBoxes.baseItems[i]['hash']] = actualBox;
				actualBox.render();
			}
			hsSite.userBoxes = boxes;
			hsSite.sajaths.setSortable();
		}
		else if (hsSite.Preferences.loggedIn && !loaded){
			$('.oszlop1, .oszlop2, .oszlop3').html('<div class="loading">Betöltés...</div>');
			$.getJSON(hsconf.ajax_url+'boxdatas.php', {op: 'getBoxes', id:1}, function(data) {
				if (data.error){
					$('.oszlop1, .oszlop2, .oszlop3').html('');
					return false;
				}
				else {
					hsSite.userBoxes.baseItems = data.boxes;
					hsSite.Feeds = data.feeds;
					hsSite.sajaths.addUserBoxes(true);
				}
			});
		}
	},
	
	createBox: function(prefs){
		switch (parseInt(prefs.tipus)) {
			case 1:
				return new hsSite.rssBox(prefs);
			break;
			case 4://időjárás
				return new hsSite.weatherBox(prefs);
			break;
			case 5://szavazás
				return new hsSite.voteBox(prefs);
			break;
			case 6://webesfelvágott
				return new hsSite.wfBox(prefs);
			break;
			case 10://horoszkóp
				return new hsSite.horoscopeBox(prefs);
			break;			
		}
	},
	
	setSortable: function(){
		var oszlopok = ['.oszlop1', '.oszlop2', '.oszlop3'];

		$(oszlopok.toString()).sortable({
			items: '> .fooldalBox',
			handle: 'h2',
			cursor: 'move',
			opacity: 0.7,
			appendTo: '.sajatHS',
			placeholder: 'placeholder',
			forcePlaceholderSize: 'true',
			tolerance: 'pointer',
			connectWith: oszlopok,
			dropOnEmpty: true,
			start: function(e, ui) {
				ui.helper.css('width', ui.item.width());
			},
			stop: hsSite.sajaths.sortableUpdate
		});
	},
	
	refreshSortable: function(){
		var oszlopok = ['.oszlop1', '.oszlop2', '.oszlop3'];

		if ($('.oszlop1').hasClass('ui-sortable'))
			$(oszlopok.toString()).sortable("refresh");
		else
			this.setSortable();
	},
	
	sortableUpdate: function(e, ui){
		hsSite.sajaths.reorder();
		$('.fooldalBox').removeClass('showSettings');
	},
	
	reorder: function(column){
		if (column){
			if ($('.oszlop'+ column +' .fooldalBox').size()){
				var max = $('.oszlop'+ column +' .fooldalBox').size();
				$('.oszlop'+ column +' .fooldalBox').each(function(i){
					var $this = hsSite.userBoxes[this.id];
					$this.pos_col = column;
					$this.pos_row = i+1;
					$this.maxInCol = max;
					$('#'+ this.id +' div.arrows').html($this.getPositionersHTML());
				});
			}
		}
		else {
			for(var i=1; i<4; i++)
				hsSite.sajaths.reorder(i);

			hsSite.sajaths.updateOrder();
		}
	},
	
	updateOrder: function(){
		if (hsSite.sajaths.ajaxReorder){//ha már folyamatban van
			hsSite.sajaths.ajaxReorderTimerId = setTimeout("hsSite.sajaths.updateOrder()", 500);
		}
		else {
			clearTimeout(hsSite.sajaths.ajaxReorderTimerId);
			hsSite.sajaths.ajaxReorder = true;
			var boxorder = [];
			$('.oszlop1 > .fooldalBox, .oszlop2 > .fooldalBox, .oszlop3 > .fooldalBox').each(function(index){
				var $this = hsSite.userBoxes[this.id];
				boxorder.push('{"id": '+ $this.saveTo +', "pos_col": '+ $this.pos_col +', "pos_row": '+ $this.pos_row +'}');
			});
			var params = {
				op: 'updateOrder',
				id: 1,
				'order[]': boxorder
			};
			
			$.post(hsconf.ajax_url + 'boxdatas.php', params, function(){
				hsSite.sajaths.ajaxReorder = false;
			});
		}
	}
}

hsSite.dlg.dobozHozzaadas = {
	newBoxes: [],
	deletedBoxes: {},
	actTab : 0,
	newCount: 0,
	filter: {category: 0, abc: 0, sort: 1, page: 1, limit: 30},
	sh_channels: [],
	shown: false,
	urls: {},
	boxCount: null,
	maxBox: 0,
	addProgress: false,
	messages: {
		invalidUrl:	'Nem helyes RSS cím!',
		rssExists:	'Ezt a címet már felvetted a dobozaid közé!',
		wrongRss:	'Hibás RSS!',
		emptyUrl:	'Nem adtad meg az RSS címet!'
	},
	
	openForAddRss: function(config) {
		this.config = config ? config :{};
		hsSite.dlgMgr.open(this, true);
	},

	focus: function() {
		if(this.config.addRss) {
			hsSite.topList.showTab(3, '.dobozHozzadas');
			$('#tl_tab4_body').children('.searchInput').val(this.config.addRss);
			hsSite.dlg.dobozHozzaadas.addRSS();
		}
	},
	
	onLoad: function() {
		hsSite.dlgBase.onLoad.call(this);
		var me=this;
		this.maxBox = hsSite.userLimits ? hsSite.userLimits.maxBox : 0;
		this.messages.maxBox =	'Most nem tudsz több dobozt felvenni az oldalra.\n'+
								'Belépett felhasználóink korlátlanul alakíthatják Saját hírlapjukat, nem belépett látogatóink összesen '+ this.maxBox +' dobozt tudnak felvenni.';
		$('.dobozHozzadasVisszaBtn', this.el).click(function(){
			me.newBoxes = [];
			me.deletedBoxes = {};
			me.close();
			return false;
		});
		$('.dobozHozzadasMentesBtn', this.el).click(function() {
			me.submit();
			return false;
		});
		//információs doboz a hozzáadásnál
		if ($.cookie('SH_INFO_ADD')) $('div#dialogs div.info.closable').hide();
		else {
			$('div#dialogs div.info.closable a.close').click(function(){
				$('div#dialogs div.info.closable').hide();
				$.cookie('SH_INFO_ADD', 1, { expires: 365 });
				return false;
			});
		}
	},
	
	reset: function() {
		if (hsSite.userBoxes){
			$('.jelenlegiDobozok').html('');
			var str = '';
			
			for(var i in hsSite.userBoxes){
				var $this = hsSite.userBoxes[i];
				if ($this) {
					str += '<div id="toDel_' + $this.id + '" class="doboz" ' + ($this.tipus != 1 ? 'rel="' + $this.tipus + '" ' : '') + '>' +
					'<span class="title"><h2>' +
					$this.title +
					'</h2></span><a href="#" onclick="hsSite.dlg.dobozHozzaadas.deleteBox(\'' +
					$this.id +
					'\');return false;" class="torol">Töröl</a>' +
					'<div style="clear: both;"></div>' +
					'</div>';
				}
			}
			$('.jelenlegiDobozok').html(str == '' ? 'Nincsenek dobozaid felvéve!' : str);
			this.stripList('.jelenlegiDobozok div.doboz');
		}
		this.newCount = 0;
		this.newBoxes = [];
		this.deletedBoxes = {};
		this.urls = {};
		this.initToplistaFul();
		this.initSpecialisFul();
		this.boxCount = $('.jelenlegiDobozok div.doboz').size();
		if (!this.shown) {
			this.initHirekFul();
			this.shown = true;
		}
		else {
			this.updateChannels();
			this.updatePager();
			this.updateAbc();
		}
	},
	
	stripList: function(selector){
		$(selector +':odd').removeClass('szurke');
		$(selector +':even').addClass('szurke');
	},

	alert: function(id){
		alert(this.messages[id]);
	},

	checkBoxCount: function(returnFalseToo){
		if (!hsSite.Preferences.loggedIn) {
			if (this.boxCount < this.maxBox) {
				return true;
			}
			else {
				if (returnFalseToo)
					return false;
				else
					this.alert('maxBox');
			}
		}
		else
			return true;
	},

	addRSS: function(url, title){
		if (!hsSite.dlg.dobozHozzaadas.addProgress){
			hsSite.dlg.dobozHozzaadas.addProgress = true;
			if (this.checkBoxCount()){
				if (!url)
					url = $('#tl_tab4_body input.searchInput').val();
				url = $.trim(url.toLowerCase());
				if (url.length > 0) {
					if (!isValidURL(url)) 
						this.alert('invalidUrl');
					else {
						if (this.urlExists(url)) 
							this.alert('rssExists');
						else {
							$('#tl_tab4_body input.addBtn').hide();
							$('#tl_tab4_body div#toltes').show();
							if (!hsSite.Feeds[url]) {
								var $this = this;
								$.getJSON(hsconf.ajax_url + 'feedproxy.php', {
									url: url
								}, function(feedData){
									if (feedData.error) {
										$this.alert('wrongRss');
									}
									else {
										hsSite.Feeds[url] = feedData;
										$this.add2Actual(url, title ? title : feedData.title);
										$this.stripList('.jelenlegiDobozok div.doboz');
										if($this.config.addRss) {
											$('.dobozHozzadasMentesBtn').click();
										}
									}
									$('#tl_tab4_body div#toltes').hide();
									$('#tl_tab4_body input.addBtn').show();
									hsSite.dlg.dobozHozzaadas.addProgress = false;
								});
							}
							else {
								feedData = hsSite.Feeds[url];
								
								if (feedData.error) {
									this.alert('wrongRss');
								}
								else {
									this.add2Actual(url, title ? title : feedData.title);
									this.stripList('.jelenlegiDobozok div.doboz');
								}
								$('#tl_tab4_body div#toltes').hide();
								$('#tl_tab4_body input.addBtn').show();
								hsSite.dlg.dobozHozzaadas.addProgress = false;
							}
						}
					}
				}
				else {
					hsSite.dlg.dobozHozzaadas.addProgress = false;
					this.alert('emptyUrl');
				}
			}
			else
				hsSite.dlg.dobozHozzaadas.addProgress = false;
		}
	},

	addSpecial: function(tipus){
		if (this.checkBoxCount()){
			var listElement = $('#tl_tab3_body .hircsatorna[rel='+ tipus +']');
			$('a.hozzaad', listElement).unbind('click');
			$('a.hozzaad', listElement).addClass('hozzaadva').click(function(){return false;}).text('Hozzáadva');
			
			this.add2Actual(tipus, $('span', listElement).text(), tipus);
		}
	},
	
	add2Actual: function(url, title, tipus){
		this.newBoxes[this.newCount] = {tipus: tipus ? tipus : 1, url: url, title: title};
		this.boxCount++;
		if ($(".jelenlegiDobozok").text() == 'Nincsenek dobozaid felvéve!')
			$(".jelenlegiDobozok").html('');
		$(".jelenlegiDobozok").prepend('<div id="toRemove_'+ this.newCount +'" rel="'+ url +'" class="doboz removable">'+
										'<span class="title"><h2 class="green">'+ title +' [új]</h2></span><a href="#" onclick="hsSite.dlg.dobozHozzaadas.removeFromActual(\''+ this.newCount +'\');return false;" class="torol">Mégsem kérem</a>'+
										'<div style="clear: both;"></div>'+
										'</div>');
		$('#tl_tab4_body input.searchInput').val('');
		this.urls[$.md5(url)] = true;
		this.newCount++;
		this.initToplistaFul();
		this.updateChannels();
	},
	
	removeFromActual: function(id){
		this.urls[$.md5(this.newBoxes[id].url)] = false;
		this.newBoxes[id] = null;
		this.boxCount--;
		$('#toRemove_'+ id).remove();
		this.stripList('.jelenlegiDobozok div.doboz');
		this.initToplistaFul();
		this.updateChannels();
		this.initSpecialisFul();
	},

	urlExists: function(url){
		var exists = false;
		url = $.trim(url.toLowerCase());
		if (this.urls.loaded){
			url = $.md5(url);
			if (this.urls[url]) 
				exists = true;
		}
		else {
			if (hsSite.userBoxes) {
				for (var i in hsSite.userBoxes) {
					var $this = hsSite.userBoxes[i];
					if ($this && $this.url) {
						this.urls[$.md5($this.url)] = true;
						if ($this.url == url) 
							exists = true;
					}
				}
			}
			var $this = this;
			$('.jelenlegiDobozok .doboz.removable').each(function(){
				$this.urls[$.md5($(this).attr('rel'))] = true;
				if ($(this).attr('rel') == url) 
					exists = true;
			});
			this.urls.loaded = true;
		}
		
		return exists;
	},

	deleteBox: function(id){
		var dobozDiv = $('#toDel_'+ id);
		
		var deleted = this.deletedBoxes[id] == true ? true : false;
		
		if (deleted){
			if (this.checkBoxCount(true)) 
				this.boxCount++;
			else {
				this.alert('maxBox');
				return false;
			}
		}
		else {
			this.boxCount--;
		}
		
		$($('h2', dobozDiv).get(0)).toggleClass('megsem');
		$($('a.torol', dobozDiv).get(0)).text(!deleted ? 'Mégsem töröl' : 'Töröl');
		
		this.deletedBoxes[id] = !deleted;
	},
	
	submit: function() {
		var boxes = {
			newBoxes: [],
			deletedBoxes: []
		};
		var update = false;
		var me = this;
		
		if (this.newBoxes.length > 0){
			update = true;
			boxes.newBoxes = this.newBoxes;
		}
		$.each(this.deletedBoxes, function(id){
			if (me.deletedBoxes[id]) {
				update = true;
				boxes.deletedBoxes.push(hsSite.userBoxes[id].saveTo);
			}
		});
		if (update){
			var params = {
				op: 'updateBoxes',
				id: 1,
				boxes: $.toJSON(boxes)
			};
			var callback = function(data) {
				$.each(me.deletedBoxes, function(id){
					$('#'+ id).remove();
					hsSite.userBoxes[id] = null;
				});
				if (data.boxes && data.boxes.length){
					for (var i=0;i<data.boxes.length; i++){
						var actualBox = hsSite.sajaths.createBox(data.boxes[i]);
						hsSite.userBoxes[data.boxes[i]['hash']] = actualBox;
						actualBox.render();
					}
				}
				hsSite.sajaths.reorder();
				hsSite.sajaths.refreshSortable();
				if (me.config.addRss) {
					alert('Az rss fekerült a saját hírlapodra.');
					document.location.href = document.location.href.split('?').shift();
				} else {
					me.close();
				}
			}
			$.post(hsconf.ajax_url +"boxdatas.php", params, callback, 'json');
		}
		else
			me.close();
	},
	
	filterHirek: function(options){
		var $this = this;
		switch(options.type){
			case 'category':
				if (this.filter.category != options.op){
					this.filter.category = options.op;
					this.filter.abc = 0;
					this.filter.page = 1;
					
					$('#tl_tab1_body .temak a').each(function(){
						if ($(this).attr('id') == 'tema_'+ options.op)
							$(this).addClass('aktiv');
						else
							$(this).removeClass('aktiv');
					});
					$.each(this.sh_channels, function(){
						if ($this.filter.category == this.category_id || $this.filter.category == 0) 
							this.visible = true;
						else
							this.visible = false;
					});
					this.updateAbc();
				}
				else {
					hsSite.dlg.dobozHozzaadas.filterHirek({type: 'abc', op: 0});
				}
			break;
			case 'abc':
				if (this.filter.abc != options.op){
					this.filter.abc = options.op;
					this.filter.page = 1;
					$.each(this.sh_channels, function(){
						if ($this.filter.category == this.category_id || $this.filter.category == 0) {
							if ($this.filter.abc == this.abc || $this.filter.abc == 0) 
								this.visible = true;
							else 
								this.visible = false;
						}
					});
				}
			break;
			case 'sort':
				if (this.filter.sort != options.op){
					this.filter.sort = options.op;
					if (this.filter.sort == 1)
						$('#tl_tab1_body .abc').show();
					else{
						$('#tl_tab1_body .abc').hide();
						hsSite.dlg.dobozHozzaadas.filterHirek({type: 'abc', op: 0});
					}
					this.updateChannels(true);
				}
			break;
		}
		$('#tl_tab1_body .abc a').each(function(){
			if ($(this).attr('rel') == $this.filter.abc)
				$(this).addClass('active');
			else
				$(this).removeClass('active');
		});
			
		this.updateCount();
		this.updateChannels();
		this.updatePager();
	},
	
	sortBy: function(one, two){
		switch(parseInt(hsSite.dlg.dobozHozzaadas.filter.sort)){
			case 1:
				return one.name.localeCompare(two.name);
			break;
			case 2:
				return parseInt(two.c) - parseInt(one.c);
			break;
			case 3:
				return parseInt(two.date) - parseInt(one.date);
			break;
		}
	},
	
	updateCount: function(){
		var count = 0;
		$.each(this.sh_channels, function(){
			if (this.visible)
				count++; 
		});
		$($('.hircsatornaRendezes h2 span').get(0)).text(count +' ');
	},
	
	updateAbc: function(){
		$('#tl_tab1_body .abc a').hide();
		$.each(this.sh_channels, function(){
			if (this.visible)
				$('#tl_tab1_body .abc a[rel='+ this.abc +']').show();
		});
	},
	
	updateChannels: function(forceSort){
		var $this = this;
		var hircsatornak = $($('#tl_tab1_body .hircsatornak').get(0));
		var html = '';
		var index = 0;
		if (forceSort){
			this.sh_channels.sort(this.sortBy)
		}
		$.each(this.sh_channels, function(){
			if (this.visible) {
				index++;
				if (index > ($this.filter.page - 1) * $this.filter.limit && index <= ($this.filter.page * $this.filter.limit)) {
					var urlx = false;
					if ($this.urlExists(this.url)) 
						urlx = true;
					
					html += '<div class="hircsatorna">'+
					'<span class="title"><h2>'+ this.name +'</h2></span>'+
					(urlx ? '<a href="#" class="hozzaad hozzaadva" onclick="return false;">Hozzáadva</a>' : '<a href="#" class="hozzaad" onclick="hsSite.dlg.dobozHozzaadas.addRSS(\'' + this.url + '\', \'' + this.name + '\');return false;">Hozzáadás</a>') +
					'</div>';
				}
			}
		});
		hircsatornak.html(html);
		$this.stripList('#tl_tab1_body .hircsatornak div.hircsatorna');
	},
	
	updatePager: function(){
		var count = parseInt($($('.hircsatornaRendezes h2 span').get(0)).text());
		var pageCount = Math.ceil(count / this.filter.limit);
		if (this.filter.page > pageCount) this.filter.page = pageCount;
		
		var html = '';
		if (pageCount > 1) {
			if (this.filter.page != 1) {
				html += '<li><a href="#" onclick="hsSite.dlg.dobozHozzaadas.paging(1);return false;" style="width:43px;">&nbsp;<img src="' + hsconf.site_url + 'res/img/laquo.gif" alt="" />&nbsp;Első</a></li>';
				html += '<li><a href="#" onclick="hsSite.dlg.dobozHozzaadas.paging('+ (this.filter.page - 1) +');return false;">&nbsp;<img src="' + hsconf.site_url + 'res/img/laquo.gif" alt="" /></a></li>';
			}
			
			var b = this.filter.page;
			var e = this.filter.page;
			while (e - b < 6 && (b > 1 || e < pageCount)) {
				if (b > 1) 
					b--;
				if (e < pageCount) 
					e++;
			}
			
			for (var i = b; i <= e; i++) {
				html += '<li><a href="#"' + (i == this.filter.page ? ' class="active" onclick="return false;"' : ' onclick="hsSite.dlg.dobozHozzaadas.paging(' + i + ');return false;"') + '>' + i + '</a></li>';
			}
			
			if (this.filter.page != pageCount) {
				html += '<li><a href="#" onclick="hsSite.dlg.dobozHozzaadas.paging(' + (this.filter.page + 1) + ');return false;">&nbsp;<img src="' + hsconf.site_url + 'res/img/raquo.gif" alt="" /></a></li>';
				html += '<li><a href="#" onclick="hsSite.dlg.dobozHozzaadas.paging(' + pageCount + ');return false;" style="width:57px;">Utolsó&nbsp;&nbsp;<img src="' + hsconf.site_url + 'res/img/raquo.gif" alt="" /></a></li>';
			}
		}
		$('#tl_tab1_body .paginationDiv .pagination').html(html);
	},
	
	paging: function(page){
		if (page > 0)
			this.filter.page = page;
		this.updateChannels();
		this.updatePager();
	},
	
	initHirekFul: function(){
		var categories = [];
		var $this = this;
		$.each(hsSite.SHFeeds, function(index){
			if (!categories[this.category_id]){
				categories[this.category_id] = true;
				if (this.category_id != 0)
					$('#tl_tab1_body ul.temak').append('<li><a'+ (index+1 == hsSite.SHFeeds.length ? ' style="background-image:none;"' : '') +' href="#" onclick="hsSite.dlg.dobozHozzaadas.filterHirek({type: \'category\', op: '+ this.category_id +'});return false;" id="tema_'+ this.category_id +'">'+ this.category +'</a></li>');
			}
			this.visible = true;
			$this.sh_channels.push(this);
		});
		this.updateCount();
		this.updateChannels(true);
		this.updatePager();
		this.updateAbc();
	},
	
	initToplistaFul: function(){
		var $this = this;
		$('#tl_tab2_body .hircsatornak a.hozzaad').each(function(){
			$(this).unbind('click');
			if($this.urlExists($(this).attr('rel')))
				$(this).text('Hozzáadva').addClass('hozzaadva').click(function(){
					return false;
				} );
			else
				$(this).text('Hozzáadás').removeClass('hozzaadva').click(function(){
					hsSite.dlg.dobozHozzaadas.addRSS($(this).attr('rel'));
					return false;
				});
		});
	},
	
	initSpecialisFul: function(){
		var $this = this;
		$('#tl_tab3_body .hircsatornak .hircsatorna').each(function(){
			$('a.hozzaad', this).unbind('click');
			var tipus = $(this).attr('rel');
			if ($('.jelenlegiDobozok .doboz[rel='+ tipus +']').size()){//már hozzá van adva
				$('a.hozzaad', this).addClass('hozzaadva').click(function(){return false;}).text('Hozzáadva');
			}
			else {
				$('a.hozzaad', this).removeClass('hozzaadva').click(function(){hsSite.dlg.dobozHozzaadas.addSpecial(tipus);return false;}).text('Hozzáadás');
			}
		});
	}
	
};

hsSite.box = function(prefs) {
	this.rendered = false;
	this.url = prefs.url ? prefs.url : '';
	this.title = prefs.title != '' ? prefs.title : 'Új doboz';
	this.tipus = prefs.tipus ? prefs.tipus : 1;
	this.pos_row = prefs.pos_row ? prefs.pos_row : 1;
	this.pos_col = prefs.pos_col ? prefs.pos_col : 1;
	this.rows = prefs.rows ? prefs.rows : 8;
	this.color = prefs.color ? prefs.color : 1;
	this.id = prefs.hash ? prefs.hash : 'newBox_'+ Math.floor(Math.random()*10000);
	this.saveTo = prefs.id ? prefs.id : -1;
	this.items = null;
	this.shortTitle = null;
	this.headerTitle = this.title;
	
	this.remove = function(){
		if (confirm('Biztosan törölni szeretnéd ezt a dobozt ('+ this.title +')?')){
			if (this.saveTo > 0) {
				var params = {
					op: 'remove',
					id: this.saveTo,
					pos_row: this.pos_row,
					pos_col: this.pos_col
				};
				$.post(hsconf.ajax_url + 'boxdatas.php', params);
			}
			$('#'+ this.id).remove();
			hsSite.userBoxes[this.id] = null;
			hsSite.sajaths.reorder(this.pos_col);
		}
	};
	
	this.move = function(direction){
		if (direction) {
			var box = $('#'+ this.id);
			box.removeClass('showSettings');
			switch (direction) {
				case 'top':
					box.insertBefore($('.oszlop'+ this.pos_col +' .fooldalBox').get(0));
					break;
				case 'up':
					box.insertBefore($('.oszlop'+ this.pos_col +' .fooldalBox').get(this.pos_row-2));
					break;
				case 'left':
					var c = $('.oszlop'+ (this.pos_col-1) +' .fooldalBox').size();
					if (c){
						if (this.pos_row == 1)
							box.insertBefore($('.oszlop' + (parseInt(this.pos_col) - 1) + ' .fooldalBox').get((c >= this.pos_row ? this.pos_row - 1 : c - 1)));
						else
							box.insertAfter($('.oszlop' + (parseInt(this.pos_col) - 1) + ' .fooldalBox').get((c >= this.pos_row ? this.pos_row - 2 : c - 1)));
					}
					else
						box.appendTo($('.oszlop'+ (parseInt(this.pos_col)-1)));
					break;
				case 'right':
					var c = $('.oszlop'+ (parseInt(this.pos_col)+1) +' .fooldalBox').size();
					if (c) {
						if (this.pos_row == 1)
							box.insertBefore($('.oszlop' + (parseInt(this.pos_col) + 1) + ' .fooldalBox').get((c >= this.pos_row ? this.pos_row - 1 : c - 1)));
						else
							box.insertAfter($('.oszlop' + (parseInt(this.pos_col) + 1) + ' .fooldalBox').get((c >= this.pos_row ? this.pos_row - 2 : c - 1)));
					}
					else
						box.appendTo($('.oszlop'+ (parseInt(this.pos_col)+1)));
					break;
				case 'down':
					box.insertAfter($('.oszlop'+ this.pos_col +' .fooldalBox').get(this.pos_row));
					break;
				case 'bottom':
					box.insertAfter($('.oszlop'+ this.pos_col +' .fooldalBox').get(this.maxInCol-1));
					break;
			}
			hsSite.sajaths.reorder();
		}
	};
	
	this.toggle = function(){
		var settings = $('#'+ this.id +' > .settings_box').get(0);
		if ($(settings).css('display') == 'none')
			$('#'+ this.id +' > :not(h2, .settings_box)').toggle();
		else
			$('#'+ this.id +' > :not(h2)').toggle();
		var minimaze = $('#'+ this.id +' a.minimaze');
		minimaze.toggleClass('maximaze');
		if (minimaze.attr('title') == 'Összecsuk')
			minimaze.attr('title', 'Kinyit');
		else
			minimaze.attr('title', 'Összecsuk');
	};
	
	this.edit = function(){
		var $sb = $('#'+ this.id +' > .settings_box').toggle();
		var sbv = $sb.is(':visible'); 
		var sbnv = $sb.next('div').is(':visible'); 
		$sb.css('borderBottom', (hsSite.Preferences.borderedBox && sbv && !sbnv) ? '0px solid #CCC' : '1px solid #CCC');
	};
	
	this.getShortTitle = function(title, width){
		if (!width)
			width = 180;
		
		if (!title && this.shortTitle)
			return this.shortTitle;
		
		var h2 = $($('#'+ this.id +' h2 b').get(0));
		if (title)
			h2.text(title);
		var thisW = h2.width();
		var shortTitle = this.title;
		if (width <= thisW) {
			while (width < thisW) {
				var text = h2.text();
				h2.text(text.substring(0, (text.length - 1)));
				
				thisW = h2.width();
			}
			shortTitle = h2.text().substring(0, (h2.text().length - 3)) +'...';
		}
		if (width == 180)
			this.shortTitle = shortTitle;
		return shortTitle;
	};
	
	this.refresh = function(){
		this.loading();
		this.getFeedData(true);
	};
	
	this.addEvents = function(){
		if(this.rendered){
			this.headerTitle = this.getShortTitle(this.title, 290);
			$($('#'+ this.id +' h2 b').get(0)).text(this.headerTitle);
			var $this = this;
			$('#'+ this.id).hover(
				function(){
					$(this).addClass('showSettings');
					$($(this).find('h2 b').get(0)).text($this.getShortTitle());
				},
				function(){
					$(this).removeClass('showSettings');
					$($(this).find('h2 b').get(0)).text($this.headerTitle);
				}
			);
		}
	};
	
	this.changeColor = function(id){
		this.color = id;
		$('#'+ this.id +' .settings_box .colorChoose').each(function(){
			if ($(this).attr('id') == 'color'+ id)
				$(this).addClass('aktiv');
			else
				$(this).removeClass('aktiv');
		});
		var header = $('#'+ this.id +' h2').get(0);
		$(header).css({'background-image': 'url('+ hsSite.boxColors[id] +')'});
	};
	
	this.changeTitle = function(title){
		var title = $.trim(title);
		if (this.title != title){
			this.title = title;
			this.headerTitle = this.getShortTitle(this.title, 290);
			$($('#'+ this.id +' h2 b').get(0)).text(this.getShortTitle(title));
		}
	};
	
	this.getFooter = function(){
		return $($('#'+ this.id +' > .osszes_box').get(0));
	};
	
	this.loading = function(){
		this.getFooter().html('');
		this.getBody().html('<div class="loading">Betöltés...</div>');
	};
	
	this.getPositionersHTML = function(){
		return '<a href="#" class="top'+ (this.pos_row == 1 ? ' inaktiv' : '" onclick="hsSite.userBoxes[\''+ this.id +'\'].move(\'top\');return false;') +'"></a><a href="#" class="up'+ (this.pos_row == 1 ? ' inaktiv' : '" onclick="hsSite.userBoxes[\''+ this.id +'\'].move(\'up\');return false;') +'"></a>'+
		'<a href="#" class="left'+ (this.pos_col == 1 ? ' inaktiv' : '" onclick="hsSite.userBoxes[\''+ this.id +'\'].move(\'left\');return false;') +'"></a><a href="#" class="right'+ (this.pos_col == 3 ? ' inaktiv' : '" onclick="hsSite.userBoxes[\''+ this.id +'\'].move(\'right\');return false;') +'"></a>'+
		'<a href="#" class="down'+ (this.pos_row == this.maxInCol ? ' inaktiv' : '" onclick="hsSite.userBoxes[\''+ this.id +'\'].move(\'down\');return false;') +'"></a><a href="#" class="bottom'+ (this.pos_row == this.maxInCol ? ' inaktiv' : '" onclick="hsSite.userBoxes[\''+ this.id +'\'].move(\'bottom\');return false;') +'"></a>';
	};
};

hsSite.rssBox = function(prefs){
	this.inheritFrom = hsSite.box;
	this.inheritFrom(prefs);
	this.linkTitle = prefs.linkTitle ? prefs.linkTitle : '';
	this.linkURL = prefs.linkURL ? prefs.linkURL : '';
	this.feed = prefs.feed != 0 ? 1 : 0;
	this.page = 1;
	
	this.getBody = function(){
		return $('#hirek_'+ this.id);
	};
	
	this.changePage = function(page){
		$('#'+ this.id).removeClass('showSettings');
		this.getBody().html(this.getLinksHTML(page));
		this.getFooter().html(this.getFooterHTML());
	};
	
	this.saveData = function(){
		var url = '';
		if (!this.feed)
			url = $($('#'+ this.id +' input[name=url]').get(0)).val();
		else
			url = this.url;
		if (isValidURL(url)){
			var $this = this;
			var params = {
				id: this.saveTo,
				title: this.title,
				url: url,
				rows: this.rows,
				color: this.color,
				op: 'saveData'
			};
			
			$.post(hsconf.ajax_url+'boxdatas.php', params, 
				function(data){
					data = eval('('+ data +')');
					if (!data.error) {
						if (data.url != $this.url) {
							$this.url = data.url;
							$this.refresh();
						}
					}
					else
						$this.getBody().html('<div class="error">Hibás RSS!</div>');
					
					$('#'+ $this.id +' > .settings_box').hide();
				}
			);
		}
		else {
			alert('A megadott RSS nem érvényes URL!');
		}
	};
	
	this.getFeedData = function(forceUpdate){
		if (!forceUpdate && this.items && this.items.length != 0)
			return this.items;

		if (!this.url || this.url == '')
			return false;

		if (!forceUpdate && hsSite.Feeds[this.url].items && hsSite.Feeds[this.url].items.length != 0){
			this.items = hsSite.Feeds[this.url].items;
			return true;
		}
		else {
			var $this = this;
			$.getJSON(hsconf.ajax_url+'feedproxy.php', {url: this.url}, function(feedData) {
				if (feedData.error){
					$this.getBody().html('<div class="error">Hibás RSS!</div>');
					return false;
				}
				else {
					hsSite.Feeds[$this.url] = feedData;
					$this.items = feedData.items;
					$this.getBody().html($this.getLinksHTML(1));
					$this.getFooter().html($this.getFooterHTML());
				}
			});
			return true;
		}
	};
	
	this.render = function(){
		var toRender = '<div class="fooldalBox" id="'+ this.id +'">'+
		'<h2 style="background-image:url('+ hsSite.boxColors[this.color] +');"><b class="title">'+ this.headerTitle +'</b><span class="settings_container"><a href="#" class="text" onclick="hsSite.userBoxes[\''+ this.id +'\'].edit();return false;">Beállít</a><div class="box_icon iealpha" style="margin-left:6px;"><a href="#" class="refresh" title="Frissít" onclick="hsSite.userBoxes[\''+ this.id +'\'].refresh();return false;"></a></div><b>|</b><div class="box_icon iealpha"><a href="#" class="minimaze" title="Összecsuk" onclick="hsSite.userBoxes[\''+ this.id +'\'].toggle();return false;"></a></div><b>|</b><div class="box_icon iealpha" style="margin-right:0;"><a href="#" class="close" title="Töröl" onclick="hsSite.userBoxes[\''+ this.id +'\'].remove();return false;"></a></div></span></h2>';

		toRender += this.getEditorHTML();
		
		if (hsSite.Feeds[this.url] && hsSite.Feeds[this.url].error){
			toRender += '<div id="hirek_'+ this.id +'"><div class="error">Hibás RSS!</div></div>';
		}
		else if (this.getFeedData()){
			toRender += '<div id="hirek_'+ this.id +'">'+ this.getLinksHTML() +'</div>';
		}
		toRender += '<div class="osszes_box">'+ this.getFooterHTML() +'</div>';
		
		toRender += '</div>';

		$('.oszlop'+ this.pos_col).prepend(toRender);
		this.rendered = true;
		this.addEvents();
	};
	
	this.getEditorHTML = function(){
		var str = '<div class="settings_box">'+
		'<table cellpadding="0" cellspacing="0" border="0" width="100%">'+
		'<tr>'+
		'<td width="60">Név:</td>'+
		'<td><input type="text" value="'+ this.title +'" onblur="hsSite.userBoxes[\''+ this.id +'\'].changeTitle(this.value);" /></td>'+
		'</tr>'+
		(!this.feed ? '<tr>'+
		'<td>RSS:</td>'+
		'<td><input type="text" name="url" value="'+ this.url +'" /></td>'+
		'</tr>' : '') +
		'<tr><td>Sorok:</td>'+
		'<td><select name="rows" onchange="hsSite.userBoxes[\''+ this.id +'\'].changeRows();">';
		for (var i=1; i<11; i++){
			str += '<option value="'+ i +'"'+ (this.rows == i ? ' selected="selected"' : '') +'>'+ i +'</option>';
		}
		
		str += '</select></td></tr>'+
		'<tr>'+
		'<td>Szín:</td><td>';
		
		var color = this.color;
		var hash = this.id;
		$.each(hsSite.boxColors, function(id, src){
			if (src) {
				str += '<a href="#" id="color'+ id +'" class="colorChoose'+ (color == id ? ' aktiv' : '') +'" style="background-image:url('+ src +')" onclick="hsSite.userBoxes[\''+ hash +'\'].changeColor('+ id +');return false;"><img src="'+ hsconf.site_url +'res/img/sh_op_1pix15.png" class="iealpha" /></a>';
			}
		});
		
		str += '</td></tr>'+
		'</table>'+
		'<input type="button" value="" class="saveBtn" onclick="hsSite.userBoxes[\''+ this.id +'\'].saveData();return false;" /><div style="clear:both;"></div>'+
		'<span style="float:left;">Doboz mozgatása:&nbsp;&nbsp;</span><div class="arrows">'+
		this.getPositionersHTML() +'</div><div style="clear:both;"></div></div>';
		
		return str;
	};
	
	this.getLinksHTML = function(page){
		if (page){
			this.page = page;
		}
		
		var str = '';
		if (this.items){
			var max = (this.items.length >= (this.page * this.rows)) ? (this.page * this.rows) : this.items.length;
			var even = true;
			for (var i=((this.page-1) * this.rows); i<max; i++){
				str += '<div class="boxhir'+ (even ? ' even' : '') +'"><a'+ (hsSite.Preferences.linkTarget == '_blank' ? ' target="_blank"' : '') +' href="'+ this.items[i]['link'] +'" onclick="hsSite.stat.urlclick(this.href);"><h3>'+ this.items[i]['title'] +'</h3></a></div>';
				even = !even;
			}
			return str;
		}
	};
	
	this.getFooterHTML = function(){
		var pager = '';
		if (this.items && this.items.length){
			var pos = this.page * this.rows;
			if (this.page > 1){
				pager += '<a href="#" onclick="hsSite.userBoxes[\''+ this.id +'\'].changePage('+ (this.page-1) +');return false;" class="backward"></a>';
			}
			if (pos < this.items.length){
				pager += '<a href="#" onclick="hsSite.userBoxes[\''+ this.id +'\'].changePage('+ (this.page+1) +');return false;" class="forward"></a>';
			}
		}
		var link = '';
		if (this.linkURL != '')
			link = '<a href="'+ this.linkURL +'" class="osszes" target="_blank">'+ this.linkTitle +'</a>';
		return link + pager;
	};
	
	this.changeRows = function(){
		var rows = $($('#'+ this.id +' .settings_box select[name=rows]').get(0)).val();
		this.rows = rows;
		this.getBody().html(this.getLinksHTML(1));
		this.getFooter().html(this.getFooterHTML());
	};
};

hsSite.weatherBox = function(prefs){
	this.inheritFrom = hsSite.box;
	this.inheritFrom(prefs);
	this.linkTitle = prefs.linkTitle ? prefs.linkTitle : '';
	this.linkURL = prefs.linkURL ? prefs.linkURL : '';
	
	this.getBody = function(){
		return $('#weather_'+ this.id);
	};
	
	this.loading = function(){
		this.getBody().html('<div class="loading">Betöltés...</div>');
	};
	
	this.saveData = function(){
		var $this = this;
		var params = {
			id: this.saveTo,
			title: this.title,
			rows: this.rows,
			color: this.color,
			op: 'saveData'
		};
		
		$.post(hsconf.ajax_url+'boxdatas.php', params, 
			function(data){
				$('#'+ $this.id +' > .settings_box').hide();
			}
		);
	};
	
	this.getFeedData = function(forceUpdate){
		if (!forceUpdate && this.items && this.items.length != 0)
			return this.items;

		if (!this.url || this.url == '')
			return false;
		
		if (!forceUpdate && hsSite.Feeds[this.url] && hsSite.Feeds[this.url].length != 0){
			this.items = hsSite.Feeds[this.url];
			return true;
		}
		else {
			var $this = this;
			$.getJSON(hsconf.ajax_url+'feedproxy.php', {url: this.tipus}, function(feedData) {
				if (feedData.error){
					$this.getBody().html('<div class="error">Hibás Időjárás feed!</div>');
					return false;
				}
				else {
					hsSite.Feeds[$this.url] = feedData;
					$this.items = feedData;
					$this.getBody().html($this.getTabsHTML());
				}
			});
			return true;
		}
	};
	
	this.render = function(){
		var toRender = '<div class="fooldalBox" id="'+ this.id +'">'+
		'<h2 style="background-image:url('+ hsSite.boxColors[this.color] +');"><b class="title">'+ this.headerTitle +'</b><span class="settings_container"><a href="#" class="text" onclick="hsSite.userBoxes[\''+ this.id +'\'].edit();return false;">Beállít</a><div class="box_icon iealpha" style="margin-left:6px;"><a href="#" class="refresh" title="Frissít" onclick="hsSite.userBoxes[\''+ this.id +'\'].refresh();return false;"></a></div><b>|</b><div class="box_icon iealpha"><a href="#" class="minimaze" title="Összecsuk" onclick="hsSite.userBoxes[\''+ this.id +'\'].toggle();return false;"></a></div><b>|</b><div class="box_icon iealpha" style="margin-right:0;"><a href="#" class="close" title="Töröl" onclick="hsSite.userBoxes[\''+ this.id +'\'].remove();return false;"></a></div></span></h2>';

		toRender += this.getEditorHTML();
		
		if (hsSite.Feeds[this.url] && hsSite.Feeds[this.url].error){
			toRender += '<div id="weather_'+ this.id +'"><div class="error">Hibás Időjárás feed!</div></div>';
		}
		else if (this.getFeedData()){
			toRender += '<div id="weather_'+ this.id +'">'+ this.getTabsHTML() +'</div>';
		}
		
		toRender += '</div>';

		$('.oszlop'+ this.pos_col).prepend(toRender);
		this.rendered = true;
		this.addEvents();
	};
	
	this.getEditorHTML = function(){
		var str = '<div class="settings_box">'+
		'<table cellpadding="0" cellspacing="0" border="0" width="100%">'+
		'<tr>'+
		'<td width="60">Név:</td>'+
		'<td><input type="text" value="'+ this.title +'" onblur="hsSite.userBoxes[\''+ this.id +'\'].changeTitle(this.value);" /></td>'+
		'</tr>'+
		'<tr>'+
		'<td>Szín:</td><td>';
		
		var color = this.color;
		var hash = this.id;
		$.each(hsSite.boxColors, function(id, src){
			if (src) {
				str += '<a href="#" id="color'+ id +'" class="colorChoose'+ (color == id ? ' aktiv' : '') +'" style="background-image:url('+ src +')" onclick="hsSite.userBoxes[\''+ hash +'\'].changeColor('+ id +');return false;"><img src="'+ hsconf.site_url +'res/img/sh_op_1pix15.png" class="iealpha" /></a>';
			}
		});
		
		str += '</td></tr>'+
		'</table>'+
		'<input type="button" value="" class="saveBtn" onclick="hsSite.userBoxes[\''+ this.id +'\'].saveData();return false;" /><div style="clear:both;"></div>'+
		'<span style="float:left;">Doboz mozgatása:&nbsp;&nbsp;</span><div class="arrows">'+
		this.getPositionersHTML() +'</div><div style="clear:both;"></div></div>';
		
		return str;
	};
	
	this.getTabsHTML = function(){
		var str = '<div class="weather"><ul class="fulek">';
		var content = '';
		
		if (this.items){
			var i = 1;
			for (var j in this.items){
				if (this.items[j].title){
					str += '<li><a id="weather_tab'+ i +'"'+ (i == 1 ? ' class="aktiv"' : '') +' onclick="hsSite.topList.showTab('+ (i-1) +', \'.weather\');return false;" href="#">'+ this.items[j].title +'</a></li>';
					content += '<div '+ (i != 1 ? 'style="display:none;" ' : '') +'id="weather_tab'+ i +'_body">'+
						'<div class="weather_box">'+
						'<div class="ter1 '+ this.items[j].ter1.ikon +'">'+ this.items[j].ter1.fok +' C°</div>'+
						'<div class="ter2 '+ this.items[j].ter2.ikon +'">'+ this.items[j].ter2.fok +' C°</div>'+
						'<div class="ter3 '+ this.items[j].ter3.ikon +'">'+ this.items[j].ter3.fok +' C°</div>'+
						'<div class="ter4 '+ this.items[j].ter4.ikon +'">'+ this.items[j].ter4.fok +' C°</div>'+
						'<div class="ter5 '+ this.items[j].ter5.ikon +'">'+ this.items[j].ter5.fok +' C°</div>'+
						'<span class="napkelte"><b>Napkelte:</b> '+ this.items[j].napkelte +' | <b>Napnyugta:</b> '+ this.items[j].napnyugta +'</span>'+
						'</div>'+
					'</div>';
					i++;
				}
			}
		}
		str += '</ul><div class="lista">'+ content +'</div></div>';
		return str;
	};
};


hsSite.voteBox = function(prefs){
	this.inheritFrom = hsSite.box;
	this.inheritFrom(prefs);
	this.linkTitle = prefs.linkTitle ? prefs.linkTitle : '';
	this.linkURL = prefs.linkURL ? prefs.linkURL : '';
	this.hirdetes = 0;
	this.page = 1;
	this.voteId = 0;
	
	this.getBody = function(){
		return $('#vote_'+ this.id);
	};
	
	this.getFooter = function(){
		return $('#vote_footer_'+ this.id);
	};
	
	this.loading = function(){
		this.getFooter().hide();
		$('#v'+ this.voteId).html('<div class="loading">Betöltés...</div>');
	};
	
	this.refresh = function(){
		this.loading();
		this.getFeedData(true);
	};
	
	this.saveData = function(){
		var $this = this;
		var params = {
			id: this.saveTo,
			title: this.title,
			color: this.color,
			op: 'saveData'
		};
		
		$.post(hsconf.ajax_url+'boxdatas.php', params, 
			function(data){
				$('#'+ $this.id +' > .settings_box').hide();
			}
		);
	};
	
	this.getFeedData = function(forceUpdate){
		if (!forceUpdate && this.items && this.items.length != 0)
			return this.items;

		if (!this.url || this.url == '')
			return false;
		
		if (!forceUpdate && hsSite.Feeds[this.url] && hsSite.Feeds[this.url].items.length != 0){
			this.items = hsSite.Feeds[this.url].items;
			this.voteId = this.items[0].vote_id;
			this.hirdetes = this.items[0].hirdetes;
			return true;
		}
		else {
			var $this = this;
			if ($.cookie('hs_vote' + this.voteId)) {
				$.post(hsconf.ajax_url + "vote.php", {voteId: this.voteId, boxId: -50}, function(data){
					$('#v' + $this.voteId).html(data);
					$this.getFooter().show();
				});
			}
			else {
				$.getJSON(hsconf.ajax_url + 'feedproxy.php', {
					url: this.tipus
				}, function(feedData){
					if (feedData.error) {
						$this.getBody().html('<div class="error">Hibás Szavazás feed!</div>');
						return false;
					}
					else {
						hsSite.Feeds[$this.url] = feedData;
						$this.items = feedData.items;
						$this.voteId = $this.items[0].vote_id;
						$this.hirdetes = $this.items[0].hirdetes;
						$this.linkURL = feedData.all_url;
						$this.linkTitle = feedData.all_title;
						$this.getBody().html($this.getVoteHTML());
						$this.getFooter().show();
					}
				});
			}
			return true;
		}
	};
	
	this.render = function(){
		var toRender = '<div class="fooldalBox" id="'+ this.id +'">'+
		'<h2 style="background-image:url('+ hsSite.boxColors[this.color] +');"><b class="title">'+ this.headerTitle +'</b><span class="settings_container"><a href="#" class="text" onclick="hsSite.userBoxes[\''+ this.id +'\'].edit();return false;">Beállít</a><div class="box_icon iealpha" style="margin-left:6px;"><a href="#" class="refresh" title="Frissít" onclick="hsSite.userBoxes[\''+ this.id +'\'].refresh();return false;"></a></div><b>|</b><div class="box_icon iealpha"><a href="#" class="minimaze" title="Összecsuk" onclick="hsSite.userBoxes[\''+ this.id +'\'].toggle();return false;"></a></div><b>|</b><div class="box_icon iealpha" style="margin-right:0;"><a href="#" class="close" title="Töröl" onclick="hsSite.userBoxes[\''+ this.id +'\'].remove();return false;"></a></div></span></h2>';

		toRender += this.getEditorHTML();
		
		if (hsSite.Feeds[this.url] && hsSite.Feeds[this.url].error){
			toRender += '<div id="vote_'+ this.id +'"><div class="error">Hibás Szavazás feed!</div></div>';
		}
		else if (this.getFeedData()){
			toRender += '<div id="vote_'+ this.id +'">'+ this.getVoteHTML() +'</div>';
		}
		toRender += '<div id="vote_footer_'+ this.id +'" style="position:relative;">'+ this.getFooterHTML() +'</div>';
		
		toRender += '</div>';

		$('.oszlop'+ this.pos_col).prepend(toRender);
		this.rendered = true;
		this.addEvents();
		this.checkVoted();
	};
	
	this.checkVoted = function(){
		if ($.cookie('hs_vote' + this.voteId)) {
			hsSite.vote.submit(this.voteId, true);
		}
	};
	
	this.getEditorHTML = function(){
		var str = '<div class="settings_box">'+
		'<table cellpadding="0" cellspacing="0" border="0" width="100%">'+
		'<tr>'+
		'<td width="60">Név:</td>'+
		'<td><input type="text" value="'+ this.title +'" onblur="hsSite.userBoxes[\''+ this.id +'\'].changeTitle(this.value);" /></td>'+
		'</tr>'+
		'<tr>'+
		'<td>Szín:</td><td>';
		
		var color = this.color;
		var hash = this.id;
		$.each(hsSite.boxColors, function(id, src){
			if (src) {
				str += '<a href="#" id="color'+ id +'" class="colorChoose'+ (color == id ? ' aktiv' : '') +'" style="background-image:url('+ src +')" onclick="hsSite.userBoxes[\''+ hash +'\'].changeColor('+ id +');return false;"><img src="'+ hsconf.site_url +'res/img/sh_op_1pix15.png" class="iealpha" /></a>';
			}
		});
		
		str += '</td></tr>'+
		'</table>'+
		'<input type="button" value="" class="saveBtn" onclick="hsSite.userBoxes[\''+ this.id +'\'].saveData();return false;" /><div style="clear:both;"></div>'+
		'<span style="float:left;">Doboz mozgatása:&nbsp;&nbsp;</span><div class="arrows">'+
		this.getPositionersHTML() +'</div><div style="clear:both;"></div></div>';
		
		return str;
	};
	
	this.getVoteHTML = function(){
		var str = '';
		if (this.items) {
			this.voteId = this.items[0].vote_id;
			str = '<div class="szavazas" id="v' + this.voteId + '"><b>' + this.items[0].question + '</b><form action="">' +
			'<input type="hidden" name="boxId" value="-50" />' +
			'<input type="hidden" name="voteId" value="'+ this.voteId +'" />';
			var i = 0;
			for (var j in this.items) {
				str += '<input class="valasz" type="radio" id="valasz' + this.items[i].id + '" name="group' + this.items[i].vote_id + '" value="' + +this.items[i].id + '"> <label for="valasz' + +this.items[i].id + '">' + this.items[i].answer + '</label><br />';
				i++;
			}
			str += '<input type="button" value="" class="voteBtn" onclick="hsSite.vote.submit(' + this.voteId + ');" /><div style="clear:both;"></div></form></div>';
		}
		return str;
	};
	
	this.getFooterHTML = function(){
		var link = '';
		if (this.linkURL != '')
			link = '<div class="osszes_box"><a href="'+ this.linkURL +'" class="osszes" target="_blank">'+ this.linkTitle +'</a></div>';
		else
			link = '<div style="height:7px;"></div>';
		if (this.hirdetes == 1)
			link += '<div class="szavazas_hirdetes" style="position:absolute;top:5px;right:0;"></div>';
		return link;
	};
};

hsSite.wfBox = function(prefs){
	this.inheritFrom = hsSite.box;
	this.inheritFrom(prefs);
	this.linkTitle = prefs.linkTitle ? prefs.linkTitle : '';
	this.linkURL = prefs.linkURL ? prefs.linkURL : '';
	this.page = 1;
	
	this.getBody = function(){
		return $('#wf_'+ this.id);
	};
	
	this.saveData = function(){
		var $this = this;
		var params = {
			id: this.saveTo,
			title: this.title,
			rows: this.rows,
			color: this.color,
			op: 'saveData'
		};
		
		$.post(hsconf.ajax_url+'boxdatas.php', params, 
			function(data){
				$('#'+ $this.id +' > .settings_box').hide();
			}
		);
	};
	
	this.getFeedData = function(forceUpdate){
		if (!forceUpdate && this.items && this.items.length != 0)
			return this.items;

		if (!this.url || this.url == '')
			return false;
		
		if (!forceUpdate && hsSite.Feeds[this.url] && hsSite.Feeds[this.url].length != 0){
			this.items = hsSite.Feeds[this.url].items;
			return true;
		}
		else {
			var $this = this;
			$.getJSON(hsconf.ajax_url+'feedproxy.php', {url: this.tipus}, function(feedData) {
				if (feedData.error){
					$this.getBody().html('<div class="error">Hibás Webesfelvágott feed!</div>');
					return false;
				}
				else {
					hsSite.Feeds[$this.url] = feedData;
					$this.items = feedData.items;
					$this.linkURL = feedData.all_url;
					$this.linkTitle = feedData.all_title;
					$this.getBody().html($this.getWfHTML());
					$this.getFooter().html($this.getFooterHTML());
				}
			});
			return true;
		}
	};
	
	this.render = function(){
		var toRender = '<div class="fooldalBox" id="'+ this.id +'">'+
		'<h2 style="background-image:url('+ hsSite.boxColors[this.color] +');"><b class="title">'+ this.headerTitle +'</b><span class="settings_container"><a href="#" class="text" onclick="hsSite.userBoxes[\''+ this.id +'\'].edit();return false;">Beállít</a><div class="box_icon iealpha" style="margin-left:6px;"><a href="#" class="refresh" title="Frissít" onclick="hsSite.userBoxes[\''+ this.id +'\'].refresh();return false;"></a></div><b>|</b><div class="box_icon iealpha"><a href="#" class="minimaze" title="Összecsuk" onclick="hsSite.userBoxes[\''+ this.id +'\'].toggle();return false;"></a></div><b>|</b><div class="box_icon iealpha" style="margin-right:0;"><a href="#" class="close" title="Tötöl" onclick="hsSite.userBoxes[\''+ this.id +'\'].remove();return false;"></a></div></span></h2>';

		toRender += this.getEditorHTML();
		
		if (hsSite.Feeds[this.url] && hsSite.Feeds[this.url].error){
			toRender += '<div id="wf_'+ this.id +'"><div class="error">Hibás Webesfelvágott feed!</div></div>';
		}
		else if (this.getFeedData()){
			toRender += '<div id="wf_'+ this.id +'">'+ this.getWfHTML() +'</div>';
		}
		toRender += '<div class="osszes_box">'+ this.getFooterHTML() +'</div>';
		
		toRender += '</div>';

		$('.oszlop'+ this.pos_col).prepend(toRender);
		this.rendered = true;
		this.addEvents();
	};
	
	this.getEditorHTML = function(){
		var str = '<div class="settings_box">'+
		'<table cellpadding="0" cellspacing="0" border="0" width="100%">'+
		'<tr>'+
		'<td width="60">Név:</td>'+
		'<td><input type="text" value="'+ this.title +'" onblur="hsSite.userBoxes[\''+ this.id +'\'].changeTitle(this.value);" /></td>'+
		'</tr>'+
		'<tr><td>Sorok:</td>'+
		'<td><select name="rows" onchange="hsSite.userBoxes[\''+ this.id +'\'].changeRows();">';
		for (var i=1; i<5; i++){
			str += '<option value="'+ i +'"'+ (this.rows == i ? ' selected="selected"' : '') +'>'+ i +'</option>';
		}
		str += '</select></td></tr>'+
		'<tr>'+
		'<td>Szín:</td><td>';
		
		var color = this.color;
		var hash = this.id;
		$.each(hsSite.boxColors, function(id, src){
			if (src) {
				str += '<a href="#" id="color'+ id +'" class="colorChoose'+ (color == id ? ' aktiv' : '') +'" style="background-image:url('+ src +')" onclick="hsSite.userBoxes[\''+ hash +'\'].changeColor('+ id +');return false;"><img src="'+ hsconf.site_url +'res/img/sh_op_1pix15.png" class="iealpha" /></a>';
			}
		});
		
		str += '</td></tr>'+
		'</table>'+
		'<input type="button" value="" class="saveBtn" onclick="hsSite.userBoxes[\''+ this.id +'\'].saveData();return false;" /><div style="clear:both;"></div>'+
		'<span style="float:left;">Doboz mozgatása:&nbsp;&nbsp;</span><div class="arrows">'+
		this.getPositionersHTML() +'</div><div style="clear:both;"></div></div>';
		
		return str;
	};
	
	this.getWfHTML = function(page){
		if (page){
			this.page = page;
		}
		
		var str = '';
		if (this.items) {
			var max = (this.items.length >= (this.page * this.rows)) ? (this.page * this.rows) : this.items.length;
			for (var i = ((this.page - 1) * this.rows); i < max; i++) {
				str += '<div class="boxhir_bovebben">' +
				'<div class="image"><a href="' +
				this.items[i].url +
				'"><img src="img/wf/' +
				this.items[i].img +
				'" alt="" /></a></div>' +
				'<div class="text">' +
				'<a href="' +
				this.items[i].url +
				'"><h3>' +
				this.items[i].title +
				'</h3></a>' +
				'<p>' +
				this.items[i].lead +
				'</p>' +
				'</div>' +
				'</div>' +
				'<div class="clear"></div>';
			}
		}
		return str;
	};
	
	this.changeRows = function(){
		var rows = $($('#'+ this.id +' .settings_box select[name=rows]').get(0)).val();
		this.rows = rows;
		this.getBody().html(this.getWfHTML(1));
		this.getFooter().html(this.getFooterHTML());
	};
	
	this.changePage = function(page){
		$('#'+ this.id).removeClass('showSettings');
		this.getBody().html(this.getWfHTML(page));
		this.getFooter().html(this.getFooterHTML());
	};
	
	this.getFooterHTML = function(){
		var pager = '';
		if (this.items && this.items.length){
			var pos = this.page * this.rows;
			if (this.page > 1){
				pager += '<a href="#" onclick="hsSite.userBoxes[\''+ this.id +'\'].changePage('+ (this.page-1) +');return false;" class="backward"></a>';
			}
			if (pos < this.items.length){
				pager += '<a href="#" onclick="hsSite.userBoxes[\''+ this.id +'\'].changePage('+ (this.page+1) +');return false;" class="forward"></a>';
			}
		}
		var link = '';
		if (this.linkURL != '')
			link = '<a href="'+ this.linkURL +'" class="osszes" target="_blank">'+ this.linkTitle +'</a>';
		return link + pager;
	};
};

hsSite.horoscopeBox = function(prefs){
	this.inheritFrom = hsSite.box;
	this.inheritFrom(prefs);
	this.linkTitle = prefs.linkTitle ? prefs.linkTitle : '';
	this.linkURL = prefs.linkURL ? prefs.linkURL : '';
	this.zodiac = prefs.zodiac ?  prefs.zodiac : 0; 
	
	this.getBody = function(){
		return $('#horoscope_'+ this.id);
	};
	
	this.loading = function(){
		this.getBody().html('<div class="loading">Betöltés...</div>');
	};
	
	this.saveData = function(){
		var $this = this;
		var params = {
			id: this.saveTo,
			title: this.title,
			zodiac: this.zodiac,
			color: this.color,
			op: 'saveData'
		};
		
		$.post(hsconf.ajax_url+'boxdatas.php', params, 
			function(data){
				$('#'+ $this.id +' > .settings_box').hide();
			}
		);
	};
	
	this.getFeedData = function(forceUpdate){
		if (!forceUpdate && this.items && this.items.length != 0){
			return this.items;
		}

		if (!this.url || this.url == '')
			return false;
		
		if (!forceUpdate && hsSite.Feeds[this.url] && hsSite.Feeds[this.url] && hsSite.Feeds[this.url].length != 0 && hsSite.Feeds[this.url]['zodiac'] == this.zodiac){
			this.items = hsSite.Feeds[this.url];
			this.getBody().html(this.getHoroscopeHTML());
			return true;
		}
		else {
			var $this = this;
			$.getJSON(hsconf.ajax_url+'feedproxy.php', {url: this.tipus, zodiac: this.zodiac}, function(feedData) {
				if (feedData.error){
					$this.getBody().html('<div class="error">Hibás horoszkóp feed!</div>');
					return false;
				}
				else {
					hsSite.Feeds[$this.url] = feedData;
					$this.items = feedData;
					$this.getBody().html($this.getHoroscopeHTML());
				}
			});
			return true;
		}
	};
	
	this.render = function(){
		var toRender = '<div class="fooldalBox horoscopeBox" id="'+ this.id +'">'+
		'<h2 style="background-image:url('+ hsSite.boxColors[this.color] +');"><b class="title">'+ this.headerTitle +'</b><span class="settings_container"><a href="#" class="text" onclick="hsSite.userBoxes[\''+ this.id +'\'].edit();return false;">Beállít</a><div class="box_icon iealpha" style="margin-left:6px;"><a href="#" class="refresh" title="Frissít" onclick="hsSite.userBoxes[\''+ this.id +'\'].refresh();return false;"></a></div><b>|</b><div class="box_icon iealpha"><a href="#" class="minimaze" title="Összecsuk" onclick="hsSite.userBoxes[\''+ this.id +'\'].toggle();return false;"></a></div><b>|</b><div class="box_icon iealpha" style="margin-right:0;"><a href="#" class="close" title="Töröl" onclick="hsSite.userBoxes[\''+ this.id +'\'].remove();return false;"></a></div></span></h2>';

		toRender += this.getEditorHTML();
		
		if (hsSite.Feeds[this.url] && hsSite.Feeds[this.url][this.zodiac] && hsSite.Feeds[this.url][this.zodiac].error){
			toRender += '<div id="horoscope_'+ this.id +'"><div class="error">Hibás horoszkóp feed!</div></div>';
		} else {
			toRender += '<div id="horoscope_'+ this.id +'"></div>';
		}
		
		toRender += '</div>';

		$('.oszlop'+ this.pos_col).prepend(toRender);
		this.getFeedData();
		this.rendered = true;
		this.addEvents();
	};
	
	this.jumpToPage = function(index){
		var pages = ['', 'heti', 'havi', 'wellness', 'karma'];
		var page = pages[index];
		if (page) {
			window.open(this.items[page], "_blank");
		}
	};
	
	this.getHoroscopeHTML = function(){
		return '<div class="zodiac">' + 
		'<div class="zodiacPic zodiac' + this.zodiac + '"><div></div></div>' + 
		'<div class="zodiacContent">' + this.items.napi + '</div>' + 
		'<div style="clear:both;"></div>' +
		'<div class="zodiacSelector"><p>Még több horoszkóp:<select onchange="hsSite.userBoxes[\''+ this.id +'\'].jumpToPage(this.selectedIndex)"><option value="-1">Válassz!</option><option value="hefi">Heti horoszkóp</option><option value="havi">Havi horoszkóp</option><option value="wellness">Wellness horoszkóp</option><option value="karma">Karma horoszkóp</option></select></p></div>' +
		//'<div class="zodiacSeparator"></div>' +
		//'<div><a href="#" class="arrowed">Aktuális holdnaptár</a></div>' +
		'</div>';
	};
	
	this.changeZodiac = function(zodiac){
		this.zodiac = zodiac;
		this.getFeedData(true);
	};	
	
	// bak, vízöntő, halak, kos, bika, ikrek, rák, oroszlán, szűz, mérleg, skorpió és nyilas
	this.getEditorHTML = function(){

		var zodiacNames = ['Bak', 'Vízöntő', 'Halak', 'Kos', 'Bika', 'Ikrek', 'Rák', 'Oroszlán', 'Szűz', 'Mérleg', 'Skorpió', 'Nyilas'];
		var zodiacs = [];
		var $this = this;
		$.each(zodiacNames, function(index, zodiacName){
			zodiacs.push('<option value="' + index + '"' + ($this.zodiac == index ? ' selected="selected"' : '') + '>' + zodiacName + '</option>');
		});	
		zodiacs = zodiacs.join('');
		
		var color = this.color;
		var hash = this.id;
		var colors = [];
		$.each(hsSite.boxColors, function(id, src){
			if (src) {
				colors.push('<a href="#" id="color'+ id +'" class="colorChoose'+ (color == id ? ' aktiv' : '') +'" style="background-image:url('+ src +')" onclick="hsSite.userBoxes[\''+ hash +'\'].changeColor('+ id +');return false;"><img src="'+ hsconf.site_url +'res/img/sh_op_1pix15.png" class="iealpha" /></a>');
			}
		});
		colors = colors.join('');	
	
		var str = '<div class="settings_box">'+
		'<table cellpadding="0" cellspacing="0" border="0" width="100%">'+
		'<tr>'+
			'<td width="70">Név:</td>'+
			'<td><input type="text" value="'+ this.title +'" onblur="hsSite.userBoxes[\''+ this.id +'\'].changeTitle(this.value);" /></td>'+
		'</tr>'+
		'<tr>'+
			'<td>Csillagjegy:</td>' +
			'<td><select name="zodiac" onchange="hsSite.userBoxes[\''+ this.id +'\'].changeZodiac(this.selectedIndex);">' + zodiacs + '</select></td>'	+
		'</tr>'+	
		'<tr>'+
			'<td>Szín:</td><td>' + colors + '</td></tr>'+
		'</table>'+
		'<input type="button" value="" class="saveBtn" onclick="hsSite.userBoxes[\''+ this.id +'\'].saveData();return false;" /><div style="clear:both;"></div>'+
		'<span style="float:left;">Doboz mozgatása:&nbsp;&nbsp;</span><div class="arrows">'+
		this.getPositionersHTML() +'</div><div style="clear:both;"></div></div>';
		
		return str;
	};
};