var _msie = ($.browser.msie && $.browser.version<7);
var _jump = new String();
var _href = new String();

//	TAB INTERPRETER

var tabs = {
	//tab1: 'koszonto',
	tab1: 'hungaria',
	tab2: 'autopalya',
	tab3: 'ut',
	tab4: 'alterra',
	tab5: 'cek'
}

var tabs_ref = {
	tab1: 'hungaria',
	tab2: 'autopalya',
	tab3: 'ut',
	tab4: 'alterra',
	tab5: 'cek'
}

//	GALERY LAYER FOR REFERENCES AND NEWS

var galery = {
	layoutBackground: '#fd5f00',
	layoutOpacity: 0.45,
	pageArrowHeight: 30,
	pageBack: 'Previous picture',
	pageBackImg: 'public/img/photogalery/arrow_left.gif',
	pageNext: 'Next picture',
	pageNextImg: 'public/img/photogalery/arrow_right.gif',
	pageEventFade: 1,
	imageSwapTime: 150,
	imageHolderBg: '#ffffff',
	
	defaultDir: '',

	create: function(){
		$('.photogalery').each(function(){$(this).remove()});
		$('body').append('<div class="photogalery"></div>');
		//	DEFINING THE LAYER
		$('.photogalery:first')
		.css({
			'position':'absolute',
			'top':'0px',
			'left':'0px',
			'background':this.layoutBackground,
			'width':'100%',
			'height':$(document).height()+'px',
			'z-index':'1000',
			'text-align':'center',
			'opacity':this.layoutOpacity
		})
		.click(function(){
			$('.photogalery, .photogalery-content').remove()
		});
		
		$('body').append('<div class="photogalery-content"></div>');
		//	DEFINING THE SWITCHER
		$('.photogalery-content:first')
		.css({
			'position':(_msie?'absolute':'absolute'),
			'margin':'10px auto',
			'top':($(window).scrollTop()+(Math.round(($(window).height()-480)/2)))+'px',
			'left':(Math.round(($(document).width()-700))/2)+'px',
			'z-index':'1100',
			'width':'700px',
			'height':'480px',
			'text-align':'center'
			
		})
		.append('<div class="page-left"></div>')
		.append('<div class="page-right"></div>')
		.append('<div class="image-rotator"></div>')
		

		//	PHOTO CONTENT
		$('.photogalery-content .image-rotator')
		.css({
			'position':'relative',
			'margin':'0px auto',
			'padding':'0px',
			'width':'640px',
			'height':'480px',
			'border':'2px solid #fd8a35',
			'overflow':'hidden'
		})
		.append('<div class="image-swap"></div>')
		
		//	IMAGE SWAP
		$('.photogalery-content .image-swap:first')
		.css({
			'position':'absolute',
			'top':'0px',
			'left':'0px',
			'width':'640px',
			'margin':'0px',
			'padding':'0px'
		});
		
		//	PAGE BACK
		$('.photogalery-content .page-left:first')
		.css({
			 'position':'absolute',
			 'left':'0px',
			 'top':(Math.round((480-this.pageArrowHeight)/2)+'px'),
			 'cursor':'pointer'
		})
		.append('<img src="'+this.pageBackImg+'" alt="'+this.pageBack+'" title="'+this.pageBack+'" />')
		.click(function(){
			if($(this).parent().data('current')>1){
				nl = (parseInt($('.photogalery-content .image-swap').css('left'))+640)+'px';
				$('.photogalery-content .image-swap').animate({'left':nl},galery.imageSwapTime);
				$(this).parent().data('current',$(this).parent().data('current')-1);
			}
			if($(this).parent().data('current')<$(this).parent().data('all')){$('.photogalery-content .page-right:first').show(galery.pageEventFade)} else{$('.photogalery-content .page-right:first').hide(galery.pageEventFade)}
			if($(this).parent().data('current')>1){$(this).show(galery.pageEventFade)} else{$(this).hide(galery.pageEventFade)}
		})
		
		//	PAGE NEXT
		$('.photogalery-content .page-right:first')
		.css({
			 'position':'absolute',
			 'right':'0px',
			 'top':(Math.round((480-this.pageArrowHeight)/2)+'px'),
			 'cursor':'pointer'
		})
		.append('<img src="'+this.pageNextImg+'" alt="'+this.pageNext+'" title="'+this.pageNext+'" />')
		.click(function(){
			if($(this).parent().data('current')<$(this).parent().data('all')){
				nl = (parseInt($('.photogalery-content .image-swap').css('left'))-640)+'px';
				$('.photogalery-content .image-swap').animate({'left':nl},galery.imageSwapTime);
				$(this).parent().data('current',$(this).parent().data('current')+1);
			}
			if($(this).parent().data('current')<$(this).parent().data('all')){$(this).show(galery.pageEventFade)} else{$(this).hide(galery.pageEventFade)}
			if($(this).parent().data('current')>1){$('.photogalery-content .page-left:first').show(galery.pageEventFade)} else{$('.photogalery-content .page-left:first').hide(galery.pageEventFade)}
		})
		
		$(window).resize(function(){$('.photogalery-content').css({'left':(Math.round(($(document).width()-700))/2)+'px','top':($(window).scrollTop()+(Math.round(($(window).height()-480)/2)))+'px'})});
		$(window).scroll(function(){if(_msie)$('.photogalery-content').css('top',($(window).scrollTop()+(Math.round(($(window).height()-480)/2)))+'px')});
	},
	show: function(){
		this.create();
		$('.photogalery-content:first').data('current',1).data('all',arguments.length);
		$('.photogalery-content .page-left').hide();
		if($('.photogalery-content').data('all')==1){$('.photogalery-content .page-right').hide()}
		for(var i=0; i<arguments.length; i++){
			$('.photogalery-content .image-swap').append('<div></div>')
			$('.photogalery-content .image-swap div:last').css({
				'position':'relative',
				'width':'640px',
				'height':'480px',
				'overflow':'hidden',
				'text-align':'center',
				'float':'left',
				'background':this.imageHolderBg
			}).append('<img src="'+this.defaultDir+arguments[i]+'" alt="'+this.loadingText+'" />');
			$('.photogalery-content .image-swap').css('width',function(){return ($(this).width()+640)+'px'})
		}
	}
}

$(function(){
	//	TEXT SHADOW TO MAIN MENUS FOR MODERN BROWSERS	
	$('ul.menu li a').css('text-shadow','#ffffff 0px 1px 1px');

	//	HOMEPAGE
	if($('body').hasClass('homepage')){$('html').css({'background':'#ffffff url(public/img/bg-main-top.jpg) repeat-x scroll 0px 0px','overflow':'hidden'})} else{$('html').css({'overflow-x':'visible','overflow-y':'scroll'})}
	
	//	TAB SELECTION
	$('.aboutus > div').hide();

	_href = window.location.href.toString();
	_jump = (_href.indexOf('#')!=-1?_href.split('#')[1]:'');
	// koszonto menupont kikerult, mivel a flash-t korulmenyesebb lenne szerkeszteni, megtettem ezt a csunyasagot - mrudi 2011.01.12
	if (_jump=="koszonto") _jump ="hungaria";

	if(_jump!=''){for(var i in tabs){if(tabs[i]==_jump){_jump=i.substr(3)}}} else{_jump = '1'}
	
	$('ul.submenu li a:eq('+(parseInt(_jump)-1)+')').addClass('selected');
	
	$('ul.submenu li a').each(function(i){
		$(this).data('tab','.tab-'+(i+1)+':first');
		if($(this).hasClass('selected')){$($(this).data('tab')).show()}
		$(this).click(function(){
			$(this).addClass('selected').blur().parent().siblings().each(function(){$(this).children('a:first').removeClass('selected')});
			$($(this).data('tab')).show().siblings().hide();

			if(_msie && ($(window).height()+4)<$(document).height()){$('.footer:last').css('position','relative')} else{$('.footer:last').css('position','absolute')}
			return false;
		});
	});
	
	//	TAB SELECTION REFERENCES
    _jump ='';
    _jump = (_href.indexOf('#')!=-1?_href.split('#')[1]:'');
    if(_jump!=''){for(var i in tabs_ref){if(tabs_ref[i]==_jump){_jump=i.substr(3)}}} else{_jump = '1'}
    $('ul.submenu_ref li a:eq('+(parseInt(_jump)-1)+')').addClass('selected');
	
	$('ul.submenu_ref li a').each(function(i){
		$(this).data('tab','.tab-'+(i+1)+':first');
		if($(this).hasClass('selected')){$($(this).data('tab')).show()}
		$(this).click(function(){
			$(this).addClass('selected').blur().parent().siblings().each(function(){$(this).children('a:first').removeClass('selected')});
			$($(this).data('tab')).show().siblings().hide();

			if(_msie && ($(window).height()+4)<$(document).height()){$('.footer:last').css('position','relative')} else{$('.footer:last').css('position','absolute')}
			return false;
		});
	});

	//	IE6 - PNG, FIRST-CHILD BUGFIX
	if(_msie){
		$('img[src$="png"]').addClass('png');
		$('.holder:first').addClass('png');
		$('div.references-block .todos').each(function(){$(this).children('.button:first').css('margin-left','4px')})
	}
});

