$(function() {

// nyito es rovatoldali slideshow
// =================================

	if ( $('div.slideshow').length ) {
	
		//navigator elem letrehozasa:
		$("div.slideshow div.slshpager").append("<ol></ol>").css({display: 'block'});
	
		// Slideshow inicializalas, pager epitese
		$("div.slideshow ul").each(function(i) {
			$(this).cycle({
				timeout: 8000,
				prev: "div.slideshow:eq(" + i + ") .prev",
				next: "div.slideshow:eq(" + i + ") .next",
				pager: "div.slideshow:eq(" + i + ") ol",
				pagerAnchorBuilder: function(idx) { 
					return '<li><a href="#">' + (idx+1) + '</a></li>'; 
				},
				pause: 1,
				autostop: 0,
				timeout: slideshow_timeout,
				fx: slideshow_fx,
				startingSlide: slideshow_startingSlide
			});
		});
	
		// play / pause gomb
		$("div.slideshow a.playpause").click(function(e) {
			e.preventDefault();
			if ( $(this).hasClass('pause') ) {
				$(this)
					.removeClass('pause')
					.addClass('play')
					.attr('title', 'indítás');
				$("div.slideshow ul").cycle("pause");	
			} else {
				$(this)
					.removeClass('play')
					.addClass('pause')
					.attr('title', 'szünet');
				$("div.slideshow ul").cycle("resume");
			}
		});
		
	}


// Tabbox
// =================================
	$('div.tabbox')
		.find('.tab:gt(0)')
			.css('display', 'none')
		.end()
		.find('.tabs a')
			.each(function(i) {
				$(this)
					.data('eq', i)
					.click( function(e) {
						e.preventDefault();
						
						$(this).parent().parent()
							.siblings()
								.removeClass('sel')
							.end()
							.addClass('sel')
							.parent().next().eq(0).find('.tab').eq( $(this).data('eq') )
								.siblings()
									.css('display', 'none')
								.end()
								.css('display', 'block');
					});
			});


// Cikk szoveg noveles / csokkentes
// =================================

	if ( $("ul.cikkoptions").length ) {
		
		var actTxtSize = 0;
		
		$("li.txtsize a").eq(0).click(function(e) {
			e.preventDefault();
			if ( actTxtSize < 3 ) {
				actTxtSize++;
				$("div.cikkblock").attr( 'class', 'box cikkblock txts' + actTxtSize );
			}
		});
		
		$("li.txtsize a").eq(1).click(function(e) {
			e.preventDefault();
			if ( actTxtSize > 0 ) {
				actTxtSize--;
				$("div.cikkblock").attr( 'class', 'box cikkblock txts' + actTxtSize );
			}
		});
		$("li.print a").click(function(e) {
			e.preventDefault();
			window.print();
		});
	}

// Megosztasi funkciok inicializalasa. Csak a linket kell elhelyezni az oldalon egy meghatarozott ID-vel, 
// es automatikusan elhelyezi benne a szukseges linket, illetve a mereshez hasznalatos ajax hivast.
// ========================================================================================================
		
    // iwiw
	$("a.share-iwiw").each(function(){
        var u = $(this).attr('href');   
        $(this).attr('href', 'http://iwiw.hu/pages/share/share.jsp?u='+u);
        $(this).attr('title', 'Megosztás az iwiw-en');
        $(this).click(function(e) {
            measureShare('iwiw', decodeURIComponent(u));
    	});
     });
     
     // twitter
	 $("a.share-twitter").each(function(){
        var u = $(this).attr('href');
        var t = $(this).attr('title');	   
        $(this).attr('href', 'http://twitter.com/home?status='+u+' '+t);
        $(this).attr('title', 'Megosztás az twitteren');
        $(this).click(function(e) {
            measureShare('twitter', decodeURIComponent(u));
    	});
     });
     
     // facebook 
	 $("a.share-facebook").each(function(){
        var u = $(this).attr('href');
        var t = $(this).attr('title');	   
        $(this).attr('href', 'http://www.facebook.com/sharer.php?u='+u+'&t='+t);
        $(this).attr('title', 'Megosztás a facebookon');
        $(this).click(function(e) {
            measureShare('facebook', decodeURIComponent(u));
    	});
     });
     
     // google
	 $("a.share-google").each(function(){
        var u = $(this).attr('href');
        var t = $(this).attr('title');	   
        $(this).attr('href', 'http://www.google.com/bookmarks/mark?op=edit&bkmk='+u+'&title='+t);
        $(this).attr('title', 'Megosztás az Google könyvjelzőként');
        $(this).click(function(e) {
            measureShare('google', decodeURIComponent(u));
    	});
     });
     
     // mail
     $("a.share-email").each(function(){
        var u = $(this).attr('href');
        var t = $(this).attr('title');
        var c = $(this).parent().parent().attr('rel');
        $(this).attr('href', 'javascript:;');
        $(this).attr('title', 'Megosztás emailben');
        $(this).click(function(e) {
            var url ='/megosztas?url='+u+'&cikk_id='+c+'&title='+t;
            window.open(url, 't', 'status=1,width=620,height=510,location=no');
        });
     });
     
     // print
	 $("a.share-print").each(function(){
        var u = $(this).attr('href');   
        $(this).attr('href', 'http://www.printfriendly.com/print?url='+u+'&partner=sociable');
        $(this).attr('title', 'Nyomtatás');
        $(this).click(function(e) {
            measureShare('print', decodeURIComponent(u));
    	});
     });
    
     // tumblr
   	 $("a.share-tumblr").each(function(){
        var u = $(this).attr('href');
        var t = $(this).attr('title');	   
        $(this).attr('href', 'javascript:;');
        $(this).attr('title', 'Megosztás a thumblr-en');
        $(this).click(function(e) {
            var url = 'http://www.tumblr.com/share?v=3&amp;u='+u+'&amp;s='+t;
            window.open(url, 't', 'toolbar=0,resizable=0,status=1,width=450,height=430');
            measureShare('tumblr', decodeURIComponent(u));
    	});
     });
     
	/**
 * Hirlevelbox
 */
	$('#hirlevel_feliratkozas')
	    .submit(function() {
			var check = emailcimjoe($('#hirlevelbox_email').val());
	
			if(check) {
			    var checkboxes = $('#hirlevel_feliratkozas :checkbox');
			    var checked = false;
	
			    for(var i=0; i < checkboxes.length; i++) {
					if(checkboxes[i].checked) checked=true;
			    }
	
			    if(checked){
					 return true;
			    } else {
					$(".newsletter_error_message").html('<em class="error" style="color: #ff0000;">Nem választott hírlevelet!</em>');
			    }
			} else {
			    	$(".newsletter_error_message").html('<em class="error" style="color: #ff0000;">A megadott e-mailcím formátuma nem megfelelő!</em>');
			    return false;
			}		
			return false;
	    });

	$('#hirlevelbox_email')
		.blur(function() {
			if($(this).val()=='') $(this).val('E-mail cím');
		})
		.focus(function() {
			if($(this).val()=='E-mail cím') $(this).val('');
		})
		.blur();

/**
 * Galériák
 */	 	
	//galeria dobozok
	$("div.scrollable > div").scrollable({
		size: 4,
		clickable: false
	});
	
	//nyito oldali galeria kep es cikk cserelo
	$("div.gallery div.scrollable div a").click(function(e) {
		e.preventDefault();
	
		var imgUrl = $(this).siblings(".img").html();
		var title = $(this).siblings(".title").html();
		var link = $(this).siblings(".link").html();
		var txt = $(this).siblings(".txt").html();
		var id = $(this).siblings(".id").html();
		
		$(this).closest('.wrp').find('img').eq(0).attr('src', imgUrl);
		$(this).closest('.wrp').find('h3 a').eq(0).html( title );
		$(this).closest('.wrp').find('p').eq(0).html( txt );
		$(this).closest('.wrp').find('.id').eq(0).html( id );		
	});
	
	$(".buborek_galeria div.scrollable div a")
	.hover(
		function(){
			var ul_left=parseInt($(this).parent().parent().css('left'));
			if(!ul_left){ ul_left=0;}
			$(this).parent().parent().parent().next()
			.closest('.foto_galeria_buborek')
			.html( $(this).parent().find('.title').eq(0).html() )
			.css({top: -13 })
			.css({left:  (( parseInt($(this).find('img').eq(0).css('width')) + parseInt($(this).parent().css('margin-left'))+parseInt($(this).parent().css('margin-right')))*parseInt($(this).parent().find('span').eq(0).html())) + ul_left + parseInt($(this).find('img').eq(0).css('width'))/2 })
			.css({position:'absolute'})
			.css({display:'block'})
		},
		function(){
			$(this).parent().parent().parent().next()
			.closest('.foto_galeria_buborek')
			.css({display:'none'});
		}
	);
	
	//popup galéria cserélgető
	$(".popup_galery .container #galery_items .window ul li a")
		.each(function(i){
			$(this).click(function(){
				$('#cim').html($(this).siblings(".dspnone.cim").html());
				/*$('#letrehozva').html($(this).siblings(".dspnone.letrehozva").html());*/
				$('#leiras').html($(this).siblings(".dspnone.leiras").html());
				
				$(".popup_galery .container #pager ul #prev")
					.next()
					.html(i+1);
				$(".popup_galery .container #img_container div")
					.each(function(t){
						if(t==i){
							$(this).css({display:'block'});
						} else {
							$(this).css({display:'none'});
						}
					})
			});
		});
	
	//popup galéria lapozgató
	//vissza
	$(".popup_galery .container #pager ul #prev a")
		.click(function(){
			var tart=parseInt($(this).parent().next().html());
			var osszes=parseInt($(this).parent().next().next().next().html());
			if(tart>1){
				tart=tart-1;
			} else {
				tart=osszes;
			}
				$(this).parent().next().html(tart);
				$(".popup_galery .container #img_container div")
					.each(function(i){
						if((i+1)==tart){
							$('#cim').html($(this).children(".cim").html());
							$('#leiras').html($(this).children(".leiras").html());
							$(this).css({display:'block'});							
						} else {
							$(this).css({display:'none'});
						}
					})
			
		});
	//előre
	$(".popup_galery .container #pager ul #next a")
		.click(function(){
			var tart=parseInt($(this).parent().prev().prev().prev().html());
			var osszes=parseInt($(this).parent().prev().html());
			if(tart<osszes){
				tart=tart+1;
			} else {
				tart=1;
			}
				$(this).parent().prev().prev().prev().html(tart);
				$(".popup_galery .container #img_container div")
					.each(function(i){
						if((i+1)==tart){
							$('#cim').html($(this).children(".cim").html());
							$('#leiras').html($(this).children(".leiras").html());
							$(this).css({display:'block'});
						} else {
							$(this).css({display:'none'});
						}
					});
			
		});
	$(".popup_galery_container")
		.each(function(i){
			$("#container").css({width:'800px'}); //Ez a container miatt kell mert alapból 1000px
		});
	
	/**
	 * A boxok magasságának fixelése
	 */ 	 
	var i = 0;  
	var sorok = new Array();
	var max = 100;  
	var sor_max = 30;
	var headerek = new Array();
	var t = 0;
	$(".news_more")
		.find('.news_item')
			.find('h3')
				.each(function(index,value){
					if(parseInt($(this).css('height'))>sor_max){ sor_max = parseInt($(this).css('height')); }
					if(index%3==2){
						headerek[t] = sor_max;
						t++;
						sor_max = 30;
					}
				})
			.end()
			.find('p')
				.each(function(index,value){
					if(parseInt($(this).css('height'))>max){ max = parseInt($(this).css('height')); }
					if($(this).html()=='' || $(this).html()==' '){ max = 0 };
					if(index%3==2){
						sorok[i] = max;			
						i++;
						max = 100;
					}
				});
	var i = 0;
	var t = 0;
	$(".news_more")
		.find('.news_item')
			.find('h3')
				.each(function(index,value){
					$(this).css('height',headerek[t]);
					if(index%3==2){
						t++;					
					}
				})
			.end()
			.find('p')
				.each(function(index,value){
					$(this).css('height',sorok[i]);
					if(index%3==2){
						i++;					
					}
				});
});

/**
 * Ajax-al meri egy elem megosztasat.
 */  
function measureShare(tipus, url) {
	// Ugyanoda postoljuk az adatot, ahonnan indultunk, csak az ajax parameterrel kiegeszitve, mert akkor 
	// a beszedes URL feldolgozo lefut, es megkapjuk a cikkID-t es a csatornaID-t.
	url2 = url.split('?', 2);
	url2 = url2[0];
	$.get(url2+'?ajax=1&block=megosztas_meres', {url: url2, tipus: tipus});
}
/**
 * Galéria
 */ 
function open_popup_galery(id,kep){
	if(!kep){ kep=1;}
	window.open('/?block=fotogaleria_popup&galery='+id+'&kep='+kep,'','width=790,height=660,left=200,top=200,resizable');
}
 