function max(a, b){
	return (a>b)?a:b;
}
function getRandomInt(min, max)
{
  return Math.floor(Math.random() * (max - min + 1)) + min;
}


var Site = function(){
	
	$(document).ready(function(){
		Site.initialize();
	});
	
	$(window).load(function(){
		Site.imageLinks()
	});	
	
	return {
		
		initialize: function(){
			
/**
 * DEV
 		
	$('.box, .videobox_3u, .videobox_2u, .banner_2u, .banner_1u').append('<span class="devclose">x</span>');
	$('.devclose').css({
		zIndex: 1000,
		position: 'absolute',
		display: 'block',
		background: 'magenta',
		width: 16,
		height: 16,
		color: 'white',
		top: 0,
		right: 0,
		cursor: 'pointer',
		textAlign: 'center',		
		fontWeight: 'bold',
		fontSize: 14
	}).click(function(){ $(this).parent().hide(); })
 */	

			
/**
 * van e fejlecbanner
 */			
			$('span#sportheads').addClass('pic'+String(parseInt(Math.floor(Math.random()*11))));
		

			if( $('div#HeaderBanner').find('img, object, iframe').length > 0 ) {
				$('div#HeaderBanner').show();
			
			}

			$('ul#Navigation > li').each(function(i,s){				
				s.menu = new MenuItem(s);
			});
		/*	$('ul#Navigation li ul li:first-child').addClass('first').append('<span></span>')
			$('ul#Navigation li ul li:last-child').addClass('last').append('<span></span>')
			*/
			
			if( $('div#HeadLines') ) new HeadLinePager('div#HeadLines');
			$('dl.tabs dt').click(function(){
				$(this).parent().find('.on').removeClass('on');
				$(this).addClass('on').next().addClass('on');
			});



/* google ad */

if( $('div#HeaderBanner').find('img, object, iframe').length > 0 ) {
} else {
	$('div#HeaderBanner').html('');
	$('div#HeaderBanner').show();
	$('div#HeaderBanner').append('<iframe frameborder="0" scrolling="no" src="/Sport/google_ads/728x90.html" style="width: 728px; height: 90px; overflow: hidden; background: #fff;"></iframe>');
}

$('div#googleads120x240').append('<iframe frameborder="0" scrolling="no" src="/Sport/google_ads/120x240.html" style="width: 120px; height: 240px; overflow: hidden; background: #fff;"></iframe>');
$('div#googleads250x250').append('<iframe frameborder="0" scrolling="no" src="/Sport/google_ads/250x250.html" style="width: 250px; height: 250px; overflow: hidden; background: #fff;"></iframe>');

/* /google ad */


/**
 * "legyenek azonos magassaguak a kis dobozok", lőn kettesével ill. hármasával 
 */
			$('div#Sekunder .box_1u').each(function(i,s){
				if ($(s).next().hasClass('box_1u') && !$(s).prev().hasClass('pair')) {
					$(s).addClass('pair');
					if( $(s).next().find('ul')[0].offsetHeight > $(s).find('ul')[0].offsetHeight ){
						$(s).next().find('ul').css('height', $(s).next().find('ul')[0].offsetHeight-5);
						$(s).find('ul').css('height', $(s).next().find('ul')[0].offsetHeight-5);
					} else {
						$(s).next().find('ul').css('height', $(s).find('ul')[0].offsetHeight-5);
						$(s).find('ul').css('height', $(s).find('ul')[0].offsetHeight-5);						
					}
				}
			});	
						
			$('div#Premier .box_1u').each(function(i,s){
				if ( $(s).next().hasClass('box_1u') && $(s).next().next().hasClass('box_1u') && !$(s).prev().hasClass('trio') && !$(s).prev().prev().hasClass('trio')) {
					$(s).addClass('trio');
					var maxx = max( $(s)[0].offsetHeight ,max( $(s).next()[0].offsetHeight ,$(s).next().next()[0].offsetHeight)); 
					$(s).next().next().css('height', maxx-5);
					$(s).next().css('height', maxx-5);
					$(s).css('height', maxx-5);
				}
			});					
			
			$('div#Premier .box_1u_alt').each(function(i,s){
				if ( $(s).next().hasClass('box_1u_alt') && $(s).next().next().hasClass('box_1u_alt') && !$(s).prev().hasClass('trio') && !$(s).prev().prev().hasClass('trio')) {
					$(s).addClass('trio');
					var maxx = max( $(s)[0].offsetHeight ,max( $(s).next()[0].offsetHeight ,$(s).next().next()[0].offsetHeight)); 
					$(s).next().next().css('height', maxx-5);
					$(s).next().css('height', maxx-5);
					$(s).css('height', maxx-5);
				}
			});
						
/**
 * magazin place cim korrigalasa
 */					
			$('div#Magazin h1 a').html(
				jQuery.map(
					jQuery.trim( $('div#Magazin h1 a')
						.html())
						.split(' '),
					function(element, index){
						return ['<b>',element,'</b>'].join('');
					}).join(''));
/**
 * kepgaleria / videogaleria doboz 
 */			
			$('h4#ImagesTab,h4#VideosTab').click(function(e){
				$(e).stopEvent();
				var target = this.id.replace('Tab','');
				$(this).parent().find('h4:not(.off)').addClass('off').end().end().removeClass('off');
				
				$(this).parent().find('div:not(.mplayer)').hide();				
				$('div#'+target).show();
				
			});
/**
 * TVmusor oldal 
 */			
			$('a#ChannelM1Tab,a#ChannelM2Tab').click(function(e){
				$(e).stopEvent();
				var target = this.id.replace('Tab','');
				$(this).parent().find('a').removeClass('on').end().end().addClass('on');				
				$(this).parent().parent().find('div.box').hide();				
				$('div#'+target).show();
			});
/**
 * TVmusor box
 */
			$('h4#TabM1 a,h4#TabM2 a').click(function(e){
				$(e).stopEvent();
				$(this).parent().parent().find('h4 a').css('opacity',0.4);
				$(this).css('opacity',1);
				$(this).parent().parent().find('dl').hide();
				$(this).parent().parent().find('dl#'+$(this).parent()[0].id.replace('Tab','')).show();
			}); 
/**
 * horoszkopok
 */			
			if(window.location.href.split('#')[1])$('div#'+window.location.href.split('#')[1]+'.zodiac').removeClass('box_3u').addClass('box_3u_alt');
			$('ul.zodiacs a').click(function(){
				$('div.zodiac').removeClass('box_3u_alt').addClass('box_3u');
				$('div#'+this.href.split('#')[1]+'.zodiac').removeClass('box_3u').addClass('box_3u_alt');
			})

			if( jQuery.browser.msie && parseInt(jQuery.browser.version,10)<7){
				$('ul.news li:first-child,ul.datenews li:first-child').css('borderTop','none');
				$('div.thumbNews ul.thumbNews li:first-child').css('borderTop','none');
				
			}

		},
		imageLinks: function(){
/**
 * kepgaleria linkek
 */
			$('a.link_galery').each(function(){				
				var text = $(this).find('img').attr('title');				
				$(this).append(['<span>',text,'</span>'].join(''));			
				switch( $(this).find('img').css('float') ){
					case "left":
						var pos = { 
									marginLeft: (jQuery.browser.msie && parseInt(jQuery.browser.version,10) < 7)?6:12,
									marginRight: 12							
								};
						break;
					case "right":
						var pos = { 
									marginRight: (jQuery.browser.msie && parseInt(jQuery.browser.version,10) < 7)?6:12,
									marginLeft: 12
								};
						break;
					default:
						var pos = {margin: '0 auto', marginTop: -5, marginBottom: 12}			
				}				
			
				$(this).find('span').css({
					width: ($(this).find('img')[0].offsetWidth-20),
					float: $(this).find('img').css('float'),
					clear: $(this).find('img').css('clear')
				});
				$(this).find('span').css(pos);	
				
				if( jQuery.browser.msie && parseInt(jQuery.browser.version,10) == 7){
					if($(this).find('img').css('float') == 'left'){
						 $(this).find('img').css({marginLeft: 0, marginRight: 12}).end().find('span').css('marginLeft',0)
					}else if($(this).find('img').css('float') == 'right'){
						$(this).find('img').css({marginLeft: 0, marginRight: 6})
					}else {
						$(this).find('img').css({marginLeft: 'auto', marginRight: 'auto'})
					}
				}							
				
			});
		}
		
		
		
	}
}();



var MenuItem = function( _obj ){	
	var that = this;
	this.obj = _obj;
	this.timer = null;
	
	var _t = Math.floor( $(that.obj).find('ul li').length / 7 );		
	var _w = 160 * ( ($(that.obj).find('ul li').length % 7 == 0)?_t:_t+1);
	
	$(that.obj).find('ul').append('<li class="bl"></li><li class="br"></li>');
	
	$(that.obj).find('ul').append('<li class="tr"></li>');
	
	/// hack... 
	try {
		$(that.obj).find('ul li.bl').css({
			'border-right-width': (_w - 3)
		})
	} catch(e) {}
	
	
	
	$(_obj).mouseover(function(event){ 
		$(event).stopEvent(); 
		clearTimeout(that.timer);
		$('ul#Navigation li.hover').removeClass('hover');
		$(this).find('ul').end().addClass('hover');
		$(this).find('ul').css({width: _w,  left:0 });
		/// hack... 
		try { $(this).find('ul li.tr').css({'border-bottom-width': ($(this).find('ul').height()-3) }) } catch(e) {}
		
	})
	
	$(_obj).mouseout(function(event){ 
		$(event).stopEvent(); 
		that.timer = jQuery.later(that, 50, 'hide');
	});	
	
}
MenuItem.prototype.hide = function(){
	$(this.obj).removeClass('hover');	
}




/**
 * fooldali idozitheto lapozos doboz
 * 
 * @param {Object} _id
 */
var HeadLinePager = function( _id ){	
	this.timer = 0;	
	this.element = $(_id);
	this.paged = 0;	
	if (this.element.find('.headline').length < 1) return false;	
	this.element.prepend([
		'<div id="HeadLine"><span class="meta"><strong></strong><small></small></span><h2></h2>',
		'<div id="HeadLinePager">',
		'</div><img src="" alt="" /><p></p>'
		].join('')
	)	
	var that = this;	
	this.element.find('.headline').each(that.add);	
	$('div#HeadLinePager span.page').click(function(){
		that.page(this.id.replace('Page',''));
	});
	this.page(0)
//	if($('span#Page'+0)[0].selfTimer)this.timer = jQuery.later(this,$('span#Page'+0)[0].selfTimer*1000,'next')
}
HeadLinePager.prototype.prev = function(){
	if( this.paged-1 > -1 ){
		this.page(this.paged-1)
	} else {
		this.page($('div#HeadLinePager span.page').length-1)
	}
}
HeadLinePager.prototype.next = function(){	
	if( (this.paged+1) < $('div#HeadLinePager span.page').length ){
		this.page(this.paged+1)
	} else {
		this.page(0)
	}
}
HeadLinePager.prototype.page = function( num ){
	clearTimeout(this.timer);
	this.paged = num;
	
	$('div#HeadLines div.on').removeClass('on');		
	$('div#HeadLinePage'+num).addClass('on');
	
	$('div#HeadLine h2').html( $('div#HeadLines div.on h2').html() );
	$('div#HeadLine img').attr('src', $('div#HeadLines div.on img').attr('src') );
	$('div#HeadLine p').html( $('div#HeadLines div.on p').html() );
	$('div#HeadLine .meta strong').html( $('div#HeadLines div.on .topic').html() );
	$('div#HeadLine .meta small').html( $('div#HeadLines div.on .date').html() );
	
	if($('span#Page'+num)[0].selfTimer) this.timer = jQuery.later(this,$('span#Page'+num)[0].selfTimer*1000,'next') 
}
HeadLinePager.prototype.add = function(i,s){		
	this.id = 'HeadLinePage'+i;
	$('div#HeadLinePager').append([
		'<span class="page" id="Page',i,'"><b style="background: ', $(this).find('img').css('backgroundImage'),'"></b></span>'
	].join(''));
	try {
		$('span#Page' + i)[0].selfTimer = this.className.match(/.*timer(\d{1,3}).*/)[1];
	} catch(e){}	 
}



/*
*
*	text noveles/csokkentes
*
*/
	var TextSizer = function () {
		var container = new Array();	
		container.push('div#Article, div#Article p, div#Article div p, div#Article li');
		container.push('div#Premier p');
		var size;			
		$(document).ready(function(){
			size = parseInt($('div#Article p').css('fontSize'),10);					
				$('span.TextIncrease').click(TextSizer.increase)
				$('span.TextDecrease').click(TextSizer.decrease)
		});			
		return {
			increase: function(e){
				$(e).stopEvent()
				size++;
				for (var i = 0; i < container.length; i++) {
					try {
						$(container[i])[0].style.fontSize = size + 'px';
					}catch(e){}
				}
			},
			decrease: function(e){
				$(e).stopEvent()
				size--;
				for (var i = 0; i < container.length; i++) {
					try {
						$(container[i])[0].style.fontSize = size + 'px';
					}catch(e){}
				}
			}
		}
	}();

/*
 * nyomtatas / elonezet
 */
	var Print =  function () {
		var tpl = ['<div id="PrintBar" style="display:none;"><p>',document.getElementsByTagName('title')[0].innerHTML,' nyomtatható változat','</p><button id="PrintCancel">Mégse</button><button id="PrintPrint">Nyomtatás</button></div>'].join('');
		var setActiveStyleSheet = function(title){  // select the stylesheet
		   var i, a, main;
		   for(i=0; (a = document.getElementsByTagName("link") [i]); i++) {
		     if(a.getAttribute("rel").indexOf("style") != -1
		        && a.getAttribute("title")) {
		       a.disabled = true;
		       if(a.getAttribute("title") == title) a.disabled = false;
		     }
		   }
		}

		$(document).ready(function(){
			$('body').append(tpl);
			$('#PrintPrint').click(Print.print);
			$('#PrintCancel').click(Print.cancel);
			$('.Print').click(function(e){
				$(e).stopEvent();
				Print.preview();
			});
		});		
			
		return {
			preview: function () {
				window.scrollTo(0,0);
				setActiveStyleSheet("Print Preview");
				$('div#PrintBar').show();
			},
			print: function () {
				window.print();
				Print.cancel();
			}, 
			cancel: function () {
				$('div#PrintBar').hide();
				setActiveStyleSheet("Hirado Main");
			}
		};			
	}();
	
/*
 * oldal tovabbkuldese layer
 */	
	var PageForwarder = function () {		
		$(document).ready(function(){			
			$('span.PageForward').click(function(){
				PageForwarder.open();
			});
			$('div#PageForwardLayer .cancel').click(function(e){
				$(e).stopEvent(); 
				PageForwarder.close();
			});
		});	
				
		return {
			open: function(){
				$('div#PageForwardLayer').animate({
					height: 276,
					opacity: 1
				},'fast');
			},
			close: function(){
				$('div#PageForwardLayer').animate({
					height: 0,					
					opacity: 0
				},'fast');
			}
		};
	}();


	function addTabOnClick() {
	    $('a#ChannelM1Tab,a#ChannelM2Tab').click(function(e) {
	        $(e).stopEvent();
	        var target = this.id.replace('Tab', '');
	        $(this).parent().find('a').removeClass('on').end().end().addClass('on');
	        $(this).parent().parent().find('div.box').hide();
	        $('div#' + target).show();
	    });
	}
