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() {
	});

	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);
					if ((arguments.length < 0) || (arguments[0] != 0)) {
						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;}
				if (site.game.obj != null) {site.game.obj.remove(); site.game.obj = null;}
				this.obj.removeClass('show');
			}
		}, // /overlay

		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

		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.substr(obj.href.length-4, 4);
				var label = ($(obj).find('ins').html());
				label = (label) ? '<div class="label">' + label + '</div>' : '';
				if (objtype == '.jpg') {
					this.html = this.htpl.split('##obj##').join('<img src="'+obj+'" alt="" />');
				}
				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);
				this.showing();
			},
			showing: function() {
				if (this.obj != null) {
					if ((this.obj.find('div.obj').width() > 100) && (this.obj.find('div.obj').height() > 100)) {
						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

		game: {
			html: Array('<div class="game"><div class="obj">' + ((IE) ?
						'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="600" height="600" id="hcgame"><param name="movie" value="/swf/hc_game_600x600.swf" /><param name="quality" value="high" /><param name="menu" value="false" /></object>' :
						'<object type="application/x-shockwave-flash" data="/swf/hc_game_600x600.swf" width="600" height="600" id="hcgame"><param name="quality" value="high" /><param name="menu" value="false" /></object>') +
						'</div><div class="clos"></div></div>',
						'<div class="game"><div class="obj">' + ((IE) ?
						'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="710" height="410" id="hcgame"><param name="movie" value="/swf/game_old/hungarocontrol.swf" /><param name="quality" value="high" /><param name="menu" value="false" /><param name="flashvars" value="folder=/swf/game_old/" /></object>' :
						'<object type="application/x-shockwave-flash" data="/swf/game_old/hungarocontrol.swf" width="710" height="410" id="hcgame"><param name="quality" value="high" /><param name="menu" value="false" /><param name="flashvars" value="folder=/swf/game_old/" /></object>') +
						'</div><div class="clos"></div></div>'),
			obj: null,
			w: Array(600, 710),
			h: Array(600, 410),
			show: function(gi) {
				this.obj = $(this.html[gi]);
				site.overlay.show(0);
				site.overlay.obj.append(this.obj);
				this.obj.css({
					width: this.w[gi] + 'px',
					height: this.h[gi] + 'px',
					left: (site.overlay.obj.width()-this.w[gi])/2 + 'px',
					top: (site.overlay.obj.height()-this.h[gi])/2 + 'px'
				});
				this.obj.find('div.obj').css({
					width: this.w[gi] + 'px',
					height: this.h[gi] + 'px'
				});
				this.obj.addClass('visible');
				this.obj.find('div.clos').click(function() {
					site.game.hide();
				});
			},
			hide: function() {
				this.obj.remove();
				this.obj = null;
				site.overlay.hide();
			}
		},

		quiz: {
			answers: Array(2,1,0,0,2,0,0,2,1,1,0),
			getresult: function(obj) {
				var form = $(obj).parent().parent().find('form:first');
				var pont = 0;
				var wrongs = '';
				var sheet = 1;
				if (form.find('input:checked').length < 11) {
					alert('Minden kérdésre adj választ!');
					return false;
				}
				for (var i=0; i<11; i++) {
					if (form.find('input#rg'+i+'_rb'+this.answers[i]+':checked').length > 0) {
						pont++;
					} else {
						wrongs += (i+1) + '. ';
					}
				}
				$('#rightanswer').html(pont);
				$('#wronganswer').html(11-pont);
				$('#wrongquests').html((wrongs.length>0) ? wrongs : '-');
				if (pont>4) {sheet=2;}
				if (pont>8) {sheet=3;}
				form.parents('div.tabsheet').removeClass('ts0').addClass('ts'+sheet);
				if (IE) {
					$('div.cont').css({height: 'auto'});
					if (IE6) {
						$('div.container').css({height: 'auto'});
						$('div.cont > div.bg:eq(0)').css({height: $('div.cont').height() + 'px'});
						$('div.cont > div.bg:eq(0) div.mid').css({height: $('div.cont').height() + 'px'});
					}
				}
			}
		},

		popup: function(obj) {
			window.open(obj.href, 'hcpopup', 'directories=0,location=0,menubar=0,personalbar=0,resizable=0,scrollbars=0,status=0,titlebar=0,toolbar=0,width=990,height=660,left='+String(Math.floor((screen.width-990)/2))+',top='+String(Math.floor((screen.height-660)/2)-20));
		},

		initialize: function() {

			var tmp = null;

			// common settings
			$('a._blank').click(function() {
				window.open(this.href);
				this.blur();
				return false;
			});
			// /common settings

			// image handling
			$('div.content div.gallery div.item a, div.content div.column div.img a').each(function(i) {
				this.index = i;
				site.media.items.push(this);
				$(this).click(function() {
					this.blur();
					site.media.show(this);
					return false;
				});
			});
			// /image handling

			// tabsheet handling
			$('div.content .tabsheet .tab').click(function() {
				var ts0 = $(this).parents('.tabsheet').attr('class').split(' ts')[1];
				ts0 = (ts0) ? ts0.substr(0,1) : '';
				var ts1 = $(this).attr('class').split(' tab')[1];
				ts1 = (ts1) ? ts1.substr(0,1) : '';
				$(this).parents('.tabsheet').removeClass('ts'+ts0);
				$(this).parents('.tabsheet').addClass('ts'+ts1);
			});
			// tabsheet handling

		} // /initialize



	} // /return

}(); // /site



