var IE = /*@cc_on!@*/false;
var IE6 = (IE && (navigator['appVersion'].indexOf('MSIE 6') > 0)) ? true : false;

var site = function() {

	$(document).ready(function() {
		site.initialize();
	});

	$(window).load(function() {
		// img
		$('div.cont div.content div.inner img').each(function() {
			if ($(this).parents('td').length == 0) {
				var classname = this.className;
				var obj	= (String(this.parentNode.tagName).toLowerCase() == 'a') ? $(this).parent() : $(this);
				obj.wrap('<div class="image ' + classname + '"></div>').parent().append('<div class="tl"></div><div class="tr"></div><div class="bl"></div><div class="br"></div>');
				if (IE6 && ($(this).height() % 2 == 1)) {
					$(this).height($(this).height()-1);
				}
				obj.parent().width($(this).width()+20);
				obj.parent().height($(this).height()+20);
			}
			$(this).load(function() {
				site.IEfix();
			});
		});
		if (IE) {
			$('div.cont div.content div.inner div.images').each(function() {
				$(this).width(0);
				$(this).height(0);
				$(this).find('div.image').each(function() {
					$(this).parent().width($(this).parent().width() + $(this).width() + 18);
					$(this).parent().height(Math.max($(this).parent().height(), $(this).height()));
				});
			});
			site.IEfix();
		}
		// /img
	});

	return {

		overlay: {
			html: '<div class="overlay" id="overlay">' + ((IE6) ? '<iframe frameborder="0" scrolling="no" allowtransparency="true"></iframe>' : '') + '<div class="cover"></div></div>',
			obj: null,
			show: function() {
				if (!this.obj) {
					this.obj = $(this.html);
					this.obj.find('div.cover').click(function() {
						site.overlay.hide();
					});
					$('body').append(this.obj);
				}
				this.obj.addClass('show');
			},
			hide: function() {
				if (site.loading.obj != null) {site.loading.obj.remove(); site.loading.obj = null;}
				if (site.media.obj != null) {site.media.obj.remove(); site.media.obj = null;}
				this.obj.removeClass('show');
			}
		},
		// /overlay

		media: {
			items: Array(),
			index: -1,
			htpl: '<div class="media"><div class="obj">##obj##</div>##label##</div>',
			html: '',
			obj: null,
			tmp: null,
			/*prev: {
				html: '<div class="tool prev"></div>',
				obj: null
			},
			next: {
				html: '<div class="tool next"></div>',
				obj: null
			},
			clos: {
				html: '<div class="tool clos"></div>',
				obj: null
			},*/
			show: function(obj) {
				//this.index = obj.index;
				var objtype = obj.href.split('?')[0].split('.').pop();
				var label = ($(obj).find('ins').html());
				obj = obj.href;
				label = (label) ? '<div class="label">' + label + '</div>' : '';
				if (objtype == 'jpg') {
					this.html = this.htpl.split('##obj##').join('<img src="'+obj+'" alt="" />');
				}
				if (objtype == 'swf') {
					var w = parseInt(obj.split('w=')[1], 10);
					var h = parseInt(obj.split('h=')[1], 10);
					this.html = this.htpl.split('##obj##').join('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="'+w+'" height="'+h+'" style="width: '+w+'px; height: '+h+'px;"><param name="movie" value="'+obj+'"><param name="menu" value="false"><param name="bgcolor" value="#ffffff"><embed src="'+obj+'" menu="false" bgcolor="#ffffff" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="'+w+'" height="'+h+'"></embed></object>');
				}
				this.html = this.html.split('##label##').join(label);
				this.obj = $(this.html);
				/*this.prev.obj = $(this.prev.html);
				this.next.obj = $(this.next.html);
				this.clos.obj = $(this.clos.html);
				this.prev.obj.click(function() {
					$(this).parent().find('div.tool').addClass('hidden');
					site.media.tmp = site.media.obj;
					site.media.tmp.fadeOut(100, function() {site.media.tmp.remove(); site.media.tmp = null;});
					setTimeout(function() {site.media.show(site.media.items[Math.max(0, site.media.index-1)])}, 100);
				});
				this.next.obj.click(function() {
					$(this).parent().find('div.tool').addClass('hidden');
					site.media.tmp = site.media.obj;
					site.media.tmp.fadeOut(100, function() {site.media.tmp.remove(); site.media.tmp = null;});
					setTimeout(function() {site.media.show(site.media.items[Math.min(site.media.items.length-1, site.media.index+1)])}, 100);
				});
				this.clos.obj.click(function() {
					$(this).parent().find('div.tool').addClass('hidden');
					site.media.obj.fadeOut(100, function() {site.overlay.hide();});
				});
				this.obj.append(this.prev.obj);
				this.obj.append(this.next.obj);
				this.obj.append(this.clos.obj);*/
				site.overlay.show();
				site.overlay.obj.append(this.obj);
/*				if (objtype == 'jpg') {
					var thatshowing = this.showing;
					this.obj.find('img').load(function() {thatshowing();});
				} else {*/
					this.showing();
/*				}*/
			},
			showing: function() {
				if (this.obj != null) {
					if ((this.obj.find('div.obj').width() > 3) && (this.obj.find('div.obj').height() > 3)) {
						this.obj.css({
							width: this.obj.find('div.obj').width() + 'px',
							height: 'auto'
						});
						if (this.obj.height() > site.overlay.obj.height()-40) {
							setTimeout(function() {
								site.media.obj.find('div.obj *').css({
									width: 'auto',
									height: Math.max(100, site.media.obj.find('div.obj').height() - (site.media.obj.height()-site.overlay.obj.height()+40)) + 'px'
								});
							}, 40);
							setTimeout(function() {
								site.media.obj.find('div.obj').css({
									width: '100%'
								});
							}, 80);
						}
						setTimeout(function() {
							site.media.obj.css({
								left: (site.overlay.obj.width()-site.media.obj.width())/2 + 'px',
								top: (site.overlay.obj.height()-site.media.obj.height())/2 + 'px'
							});
						}, 120);
						setTimeout(function() {
						/*site.media.prev.obj.css({
								left: (site.media.obj.find('div.obj *:first').offset().left - site.media.obj.offset().left + 2) + 'px',
								top: (site.media.obj.find('div.obj *:first').offset().top - site.media.obj.offset().top + site.media.obj.find('div.obj *:first').height() - 21) + 'px'
							});
							site.media.next.obj.css({
								left: (site.media.obj.find('div.obj *:first').offset().left - site.media.obj.offset().left + site.media.obj.find('div.obj *:first').width() - 21) + 'px',
								top: (site.media.obj.find('div.obj *:first').offset().top - site.media.obj.offset().top + site.media.obj.find('div.obj *:first').height() - 21) + 'px'
							});
							site.media.clos.obj.css({
								left: (site.media.obj.find('div.obj *:first').offset().left - site.media.obj.offset().left + site.media.obj.find('div.obj *:first').width() - 21) + 'px',
								top: (site.media.obj.find('div.obj *:first').offset().top - site.media.obj.offset().top + 2) + 'px'
							});*/
							site.media.obj.addClass('visible');
						}, 160);
					} else {
						setTimeout(function() {site.media.showing()}, 50);
					}
				}
			},
			hide: function() {
				this.obj.remove();
				this.obj = null;
			}
		}, // /media

		hint: {
			htpl: '<div class="hint" id="hint">' + ((IE6) ? '<iframe frameborder="0" scrolling="no"></iframe>' : '') + '<div class="tl"></div><div class="t"></div><div class="tr"></div><div class="l"></div><div class="m">##hint##</div><div class="r"></div><div class="bl"></div><div class="b"></div><div class="br"></div></div>',
			html: '',
			obj: null,
			show: function(o) {
				if (this.obj) {this.obj.remove();}
				if (!o.hint){
					o.hint = o.title;
					o.title = '';
				}
				this.html = String(this.htpl).split('##hint##').join(o.hint);
				this.obj = $(this.html);
				$('body').append(this.obj);
				var h = this.obj.find('div.m').height();
				this.obj.height(h+46);
				this.obj.find('div.m').height(h+2);
				this.obj.find('div.l').height(h+2);
				this.obj.find('div.r').height(h+2);
				this.obj.find('div.bl').css({'top': (h+15)+'px'});
				this.obj.find('div.b').css({'top': (h+15)+'px'});
				this.obj.find('div.br').css({'top': (h+15)+'px'});
				$(o).mousemove(function(e) {
					site.hint.obj.css({'left': e.pageX+'px', 'top': (e.pageY-site.hint.obj.height()-3)+'px'});
				});
			},
			hide: function() {
				if (this.obj) {
					this.obj.remove();
				}
			},
			refresh: function() {
				$('.hashint').mouseover(function() {
					site.hint.show(this);
				}).mouseout(function() {
					site.hint.hide();
				});
			}			
		},
		// /hint

		contact: function(url) {
			var w = 622;
			var h = 442;
			return window.open(url,'metropopup','directories=0,location=0,menubar=0,personalbar=0,resizable=0,scrollbars=0,status=0,titlebar=0,toolbar=0,width='+String(w)+',height='+String(h)+',left='+String(Math.floor((screen.width-w)/2))+',top='+String(Math.floor((screen.height-h)/2)-22));
		},
		// /hint
		
		loading: {
			html: '<div class="loading"><img src="i/loading.gif" alt="loading" /></div>',
			obj: null,
			show: function() {
				this.obj = $(this.html);
				site.overlay.show();
				site.overlay.obj.append(this.obj);
			},
			hide: function() {
				this.obj.remove();
				this.obj = null;
				site.overlay.hide();
			}
		},
		// /loading

		// IE fixes
		IEfix: function() {
			if (IE) {
				setTimeout(function() {
					$('div.bg:has(div.top)').each(function() {
						$(this).height(Math.round($(this).parent().height()/2)*2);
						$(this).find('div.mid').height(Math.round($(this).parent().height()/2)*2);
					});
				}, 10);
			}
			return false;
		},
		IE6fix: function() {
			if (IE6) {
				$('div.head div.menu ul li').hover(function() {
						$(this).addClass('hover');
					},
					function() {
						$(this).removeClass('hover');
					}
				);
				$('select:not([multiple])').wrap('<div class="ie6customselect"></div>');
				$('div.content form select:not([multiple])').change(function() {
					$(this).parents('div.select').eq(0).find('input:text').focus();
				});
			}
			return false;
		},
		// /IE fixes

		setTabsheet: function(ts, obj) {
			var tabsheet = $(obj).hasClass('tabsheet') ? $(obj).eq(0) : $('body .tabsheet').eq(0);
			var ts0 = tabsheet.attr('class').split('ts')[1];
			ts0 = (ts0) ? ts0.substr(0,1) : '';
			var ts1 = String(ts);
			ts1 = (ts1) ? ts1.substr(0,1) : '';
			tabsheet.removeClass('ts'+ts0);
			tabsheet.addClass('ts'+ts1);
			site.IEfix();
		},

		initialize: function() {

			var tmp = null;

			// common settings
			$('a._blank').click(function() {
				window.open(this.href);
				this.blur();
				return false;
			});
			// /common settings

			// setashomepage
			$('div.setashomepage a').click(function() {
				if (IE) {
					this.style.behavior = 'url(#default#homepage)';
					this.setHomePage(this.href);
				} else {
					alert('A kezdőlap beállításához az URL bar-ban lévő ikont a házikó ikonra kell húzni!');
				}
				return false;
			});
			// setashomepage
			
			// addtofavorites
			$('div.addtofavorites a').click(function() {
				if (window.sidebar) {
					window.sidebar.addPanel(document.title, this.href, "");
				} else if(window.external) {
					window.external.AddFavorite(this.href, document.title);
				}
				return false;
			});
			// addtofavorites

			// image handling
			$('a img').each(function() {
				var a = $(this).parent();
				if (String(' jpg swf ').indexOf(a.attr('href').split('?')[0].split('.').pop()) >= 0) {
					site.media.items.push(a);
					a.index = site.media.items.length - 1;
					a.click(function() {
						this.blur();
						site.media.show(this);
						return false;
					});
				}
			});
			// /image handling

			// contact handling
			$('div.content a.contact').each(function() {
				var link = this;
				$.post('contact/chat', {link:link.href}, function(url) {
					link.href = url;
				}, 'json');
			}).click(function() {
				if (site.contact(this.href)) {
					setTimeout(function() { location.reload(); }, 1000);
				}
				return false;
			});
			// /contact handling

			// table handling
			$('div.content table[class!=normal]').each(function() {
				if ($(this).parent().parent().parent().get(0).className != 'table') {
					$(this).wrap('<div class="table ' + this.className + '"><div><div></div></div></div>');
					site.IEfix();
				}
			});
			// /table handling

			// tabsheet handling
			$('div.content .tabsheet .tab').click(function() {
				this.blur();
				var ts0 = $(this).parents('.tabsheet').eq(0).attr('class').split('ts')[1];
				ts0 = (ts0) ? ts0.substr(0,1) : '';
				var ts1 = $(this).attr('class').split('tab t')[1];
				ts1 = (ts1) ? ts1.substr(0,1) : '';
				$(this).parents('.tabsheet').eq(0).removeClass('ts'+ts0);
				$(this).parents('.tabsheet').eq(0).addClass('ts'+ts1);
				site.IEfix();
				return false;
			});
			// /tabsheet handling

			// faq handling
			$('div.content div.faq dl dt a').click(function() {
				this.blur();
				var faqitem = $(this).parents('dl').eq(0);
				faqitem.toggleClass('open');
				site.IEfix();
				return false;
			});
			// /faq handling

			// form handling... ommm
			if ($('div.content form').length > 0) {
				$('div.content form select[multiple]').each(function() {
					$(this).parent().append('<select>' + $(this).html() + '</select>');
//					$(this).parent().append('<div class="add"></div>');
					if ($(this).find('option:selected').length > 0) {
						var multiselection = $('<div class="multiselection"></div>');
						$(this).parent().append(multiselection);
						$(this).find('option').each(function(i) {
							if ($(this).attr('selected')) {
								multiselection.append('<div>' + (($(this).parent('optgroup').length == 1) ? '<b>' + $(this).parent('optgroup').attr('label') + '</b> - ' : '') + $(this).text() + '<del>' + i + '</del></div>').find('del').click(function() {
									$(this).parents('div.select.multiple').eq(0).find('select[multiple] option:eq(' + $(this).text() + ')').removeAttr('selected');
									$(this).parent().remove();
									if (multiselection.find('div').length == 0) {multiselection.remove();}
									site.IEfix();
								});
							}
						});
					}
					$(this).parent().find('div.add').click(function() {
						var selects = $(this).parent().find('select:not([multiple])')[0];
						var selectm = $(this).parent().find('select[multiple]')[0];
						if (selects.selectedIndex >= 0 && selects.options[selects.selectedIndex].text != '') {
							if ($(this).parent().find('div.multiselection').length > 0) {
								var multiselection = $(this).parent().find('div.multiselection').eq(0);
							} else {
								var multiselection = $('<div class="multiselection"></div>');
								$(this).parent().append(multiselection);
							}
							if (!selectm.options[selects.selectedIndex].selected) {
								multiselection.append('<div>' + (($(selects.options[selects.selectedIndex]).parent('optgroup').length == 1) ? '<b>' + $(selects.options[selects.selectedIndex]).parent('optgroup').attr('label') + '</b> - ' : '') + selects.options[selects.selectedIndex].text + '<del>' + selects.selectedIndex + '</del></div>').find('del').click(function() {
									$(this).parents('div.select.multiple').eq(0).find('select[multiple] option:eq(' + $(this).text() + ')').removeAttr('selected');
									$(this).parent().remove();
									if (multiselection.find('div').length == 0) {multiselection.remove();}
									site.IEfix();
								});
							}
							selectm.options[selects.selectedIndex].selected = "selected";
						}
						site.IEfix();
					});
					$(this).parent().find('select:not([multiple])').change(function() {
						if ($(this).parents('div.select.multiple').eq(0).find('div.multiselection').length != 1) {
							$(this).parents('div.select.multiple').eq(0).find('div.add').click();
						}
					});
				});
	
				if (!IE6) {
				$('div.content form select:not([multiple])').each(function() {
					$(this).parents('div.select').eq(0).find('input:text').attr('value', $(this).find('option:selected').text());
					$(this).change(function() {
						$(this).parents('div.select').eq(0).find('input:text').attr('value', $(this).find('option:selected').text());
					});
				});
				}
	
				$('div.content form input:file').each(function() {
					$(this).parent().find('input:text').attr('value', $(this).attr('value').split('/').pop().split('\\').pop());
					$(this).mouseout(function() {
						$(this).parents('div.file').eq(0).find('input:text').attr('value', $(this).attr('value').split('/').pop().split('\\').pop());
					});
				});
	
				/*$('div.content form input, div.content form select').blur(function() {
					$('div.content form div.field.focus').removeClass('focus');
				});
				$('div.content form input:not([readonly]), div.content form select').focus(function() {
					$(this).parents('div.field').eq(0).addClass('focus');
				});*/
	
				$('div.content form div.field *').blur(function() {
					$('div.content form div.field.focus').removeClass('focus');
				});
/*				$('div.content form div.field div').click(function() {
					$('div.content form div.field.focus').removeClass('focus');
					$(this).parents('div.field').eq(0).addClass('focus');
				});*/
				$('div.content form div.field input, div.content form div.field select, div.content form div.field textarea').focus(function() {
					$('div.content form div.field.focus').removeClass('focus');
					$(this).parents('div.field').eq(0).addClass('focus');
				});
				
				$('div.content form div.field label').each(function() {
					if ($(this).height()>33) {
						$(this).css({'paddingTop': '0px', 'paddingBottom': '1px'});
					}
				});
				
			}
			$('div.head div.search input, div.side div.login input:[type!=password], div.functions div.search input').each(function() {
				if (this.title && this.title.length > 0) {
					if (this.value == '') {
						this.value = this.title;
					}
					$(this).focus(function() {
						if (this.value == this.title) {
							this.value = '';
						}
					});
					$(this).blur(function() {
						if (this.value == '') {
							this.value = this.title;
						}
					});
				}
			});
			$('div.side div.login input:[type=password]').each(function() {
				$(this).parent().append('<label class="labelin"></label>');
				if (this.title && this.title.length > 0) {
					if (this.value == '') {
						$(this).parent().find('label.labelin').text(this.title);
					}
					$(this).focus(function() {
						if ($(this).parent().find('label.labelin').text() == this.title) {
							$(this).parent().find('label.labelin').text('');
						}
					});
					$(this).blur(function() {
						if (this.value == '') {
							$(this).parent().find('label.labelin').text(this.title);
						}
					});
					$(this).parent().find('label.labelin').click(function() {
						$(this).parent().find('input:[type=password]').focus();
					});
				}
			});
			$('div.head div.search form, div.side div.login form, div.functions div.search form').submit(function() {
				$(this).find('input').each(function() {
					if (this.title && this.title.length > 0) {
						if (this.value == this.title) {
							this.value = '';
						}
						$(this).parent().find('label.labelin').text('');
					}
				});
			});
			if ($('div#international').length > 0) {
				$('div#international form select').each(function() {
					if (!IE6) {$(this).parents('div.select').eq(0).find('input:text').attr('value', $(this).find('option:selected').text());}
					$(this).change(function() {
						if (!IE6) {$(this).parents('div.select').eq(0).find('input:text').attr('value', $(this).find('option:selected').text());}
						if ($(this).find('option:selected').attr('value') != '') {
							window.open($(this).find('option:selected').attr('value'));
						}
					});
				});
			}
			// /form handling

			// login form
			if ($('#login-card_number').length > 0) {
				card_number = $('input#login-card_number');
				email = $('input#login-email');
				passwd = $('input#login-passwd');
				card_number.focus(function() {
					if ((email[0].value != email[0].title) || (passwd[0].value != '')) {
						this.blur();
						if (email[0].value != email[0].title) {
							if (passwd[0].value != '') {
								setTimeout(function(){card_number.parents('form').addClass('hashint').attr('title', 'Bejelentkezéshez kérjük, kattintson a belépési adatok alatti ' + (($('body').hasClass('index')) ? 'Ok' : 'Belépés') + ' gombra!').find('input[type="submit"]').focus(); site.hint.refresh();}, 69);
							} else {
								setTimeout(function(){passwd.focus();}, 69);
							}
						} else {
							setTimeout(function(){email.focus();}, 69);
						}
					}
				});
				email.focus(function() {
					if (card_number[0].value != card_number[0].title) {
						this.blur();
						setTimeout(function(){card_number.parents('form').addClass('hashint').attr('title', 'Bejelentkezéshez kérjük, kattintson a belépési adatok alatti ' + (($('body').hasClass('index')) ? 'Ok' : 'Belépés') + ' gombra!').find('input[type="submit"]').focus(); site.hint.refresh();}, 69);
//						setTimeout(function(){card_number.focus();}, 69);
					}
				});
				passwd.focus(function() {
					if (card_number[0].value != card_number[0].title) {
						this.blur();
						setTimeout(function(){card_number.parents('form').addClass('hashint').attr('title', 'Bejelentkezéshez kérjük, kattintson a belépési adatok alatti ' + (($('body').hasClass('index')) ? 'Ok' : 'Belépés') + ' gombra!').find('input[type="submit"]').focus(); site.hint.refresh();}, 69);
//						setTimeout(function(){card_number.focus();}, 69);
					}
				});
			}
			// /login form

			// hint
			$('.hashint').mouseover(function() {
				site.hint.show(this);
			}).mouseout(function() {
				site.hint.hide();
			});
			// /hint
			
			// banner kezeles
			if ($('div.pbanner').length > 0){
				$('p.banner').before($('div.pbanner'));
			}
			
			// /banner kezeles
			
			site.IEfix();
			site.IE6fix();

		} // /initialize



	} // /return

}(); // /site



