
function ___getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};



function yearSelector() {
    if ( $('.topYear').is(':visible') || $('.bottomYear').is(':visible') ) {
        $('.topYear').hide("normal");
        $('.bottomYear').hide("normal");
    }
    else {
        $('.topYear').show("normal");
        $('.bottomYear').show("normal");
    }
}

function monthSelector() {
    if ( $('.topMonth').is(':visible') || $('.bottomMonth').is(':visible') ) {
        $('.topMonth').hide("normal");
        $('.bottomMonth').hide("normal");
    }
    else {
        $('.topMonth').show("normal");
        $('.bottomMonth').show("normal");
    }
}

function daySelector() {
    if ( $('.topDay').is(':visible') || $('.bottomDay').is(':visible') ) {
        $('.topDay').hide("normal");
        $('.bottomDay').hide("normal");
    }
    else {
        $('.topDay').show("normal");
        $('.bottomDay').show("normal");
    }
}

function changeDate( y, m, d ) {
    d = d || '';

    $.get( selector_url, 'lang=' + qtrans_lang + '&y=' + y + '&m=' + m, function( data ) {
      $('#dateSelector').html( data );
    });

    $.get( days_url, 'lang=' + qtrans_lang + '&y=' + y + '&m=' + m, function( data ) {
      $('#calendarDays').html( data );
    });

    $.get( program_days_url, 'lang=' + qtrans_lang + '&y=' + y + '&m=' + m + '&d=' + d, function( data ) {
      $('#theDaySelector').html( data );
    });
}

function initScrolls() {
    $("#newsScroll a.prev").click(function(){
        
        move = 100;

        if ( $("#newsTicker").position().left < 0 ) {

          if ($("#newsTicker").position().left > -100) {
            move += $("#newsTicker").position().left;
          }

          $("#newsTicker").stop();
          $("#newsTicker").animate({left: '+='+ move}, 'normal', 'linear', function() { $('#newsTicker').mouseleave(); });
        }

        return false;
    });

    $("#newsScroll a.next").click(function(){

      move = 100;

      if ( $("#newsTicker").position().left > -15000 ) {
        
        $("#newsTicker").stop();
        $("#newsTicker").animate({left: '-='+ move}, 'normal', 'linear', function() { $('#newsTicker').mouseleave(); });
      }
        return false;
    });

    $("#actualBox a.up").click(function(){
        $(this).parents("#actualBox").find("ul").scrollTo( '-=30px', { easing:'elasout', queue:true } );
        return false;
    });

    $("#actualBox a.down").click(function(){
        $(this).parents("#actualBox").find("ul").scrollTo( '+=30px', { easing:'elasout', queue:true } );
        return false;
    });

	$(".videoAdvise a.up").click(function(){
        $(this).parents(".videoAdvise").find(".videos").scrollTo( '-=30px', { easing:'elasout', queue:true } );
        return false;
    });

    $(".videoAdvise a.down").click(function(){
        $(this).parents(".videoAdvise").find(".videos").scrollTo( '+=30px', { easing:'elasout', queue:true } );
        return false;
    });

	$(".imageAdvise a.up").click(function(){
        $(this).parents(".imageAdvise").find(".images").scrollTo( '-=30px', { easing:'elasout', queue:true } );
        return false;
    });

    $(".imageAdvise a.down").click(function(){
        $(this).parents(".imageAdvise").find(".images").scrollTo( '+=30px', { easing:'elasout', queue:true } );
        return false;
    });

}
function loadImage(index) {
    $("#scroll").html( '<img resource="' + $("#toplist_image_" + index).attr('resource') + '" src="' + $("#toplist_image_" + index).attr('src') + '" id="toplist_current_'+index+'" width="182" height="195"  resourcewidth="' + $("#toplist_image_" + index).attr('resourcewidth') + '" resourceheight="' + $("#toplist_image_" + index).attr('resourceheight') + '"/>' );
}


function loadGaleryImage(index,width,height) {
    $("#scroll").attr('href',$('#feeder_'+index).attr('href'));
    $("#scroll").html( '<img rel="galerylightbox" resource="' + $("#toplist_image_" + index).attr('resource') + '" src="' + $("#toplist_image_" + index).attr('src') + '" id="toplist_current_'+index+'" width="'+width+'" height="'+height+'" resourcewidth="' + $("#toplist_image_" + index).attr('resourcewidth') + '" resourceheight="' + $("#toplist_image_" + index).attr('resourceheight') + '"/>' );
    toplist_current = index;
}


function galeryStarter() {
    var dimensions = ___getPageSize();
    $("#galeryPopUp").fadeIn(500,function() {
        $("#galeryPopUpContent").attr('src', $("#toplist_current_" + toplist_current).attr('resource') );
        $('#galeryPopUp').css({
            left:               '0px',
            width:				dimensions[0] + 'px',
            height:				dimensions[1] + 'px'
	    });
	    $('#galeryPopUpWrapper').css({
            margin:             '30px auto 0 auto'
	    });
	    $('#galeryPopUpContainer').css({
            width:				$("#toplist_current_" + toplist_current).attr('resourcewidth') + 'px',
	        margin:             '30px auto 0 auto'
	    });
    });
    big_current = toplist_current;

    $(".year, .month, .day").hide();
    
    $("#galeryPopUp").css( 'height: '+$(document).height() + 'px;' );

}


function loadNextPopupGaleryImage() {
    
    if (big_current == (toplist_count - 1)) return;
    big_current++;

    $("#galeryPopUp").css( 'height: '+$(document).height() + 'px;' );
    $("#galeryPopUpContent").attr( 'src', $("#toplist_image_" + big_current ).attr('resource') );

}


function loadPreviousPopupGaleryImage() {
    if (big_current == 1) return;
    big_current--;
   
    $("#galeryPopUp").css( 'height: '+$(document).height() + 'px;' );
    $("#galeryPopUpContent").attr( 'src', $("#toplist_image_" + big_current ).attr('resource') );

}


function initSite() {
    changeDate('', '', '');

    $("#dateFrom, #dateTill").datepicker();
    
    $("ul#newsTicker").liScroll( { travelocity: 0.02 } );

    /* lenyilo menu */
    $("#header ul li").hover(function(){
        $(this).find("ul").show();
    }, function() {
        $(this).find("ul").hide();
    });

    /* gorgetes */
    initScrolls();

	/* mupapedia tooltip */
	$("a.tp").tooltip({showURL: false});

    $(".mpoll_checkbox").click( function() {
        $(this).parent('.mpoll_chooseanswer').find('.mpoll_checkbox').removeClass('mpoll_checkbox_checked');
        $(this).addClass('mpoll_checkbox_checked');
        $(this).parent('.mpoll_chooseanswer').find('input').val( $(this).html()=='IGEN'?1:0 );
    } );
    
    $("#topImagePrev").click( function () {
    if (toplist_current == 1) return;
    toplist_current--;
    $("#scroll").fadeOut(500,function () {
        loadImage(toplist_current);
        $("#scroll").fadeIn(500);
    });
    
    });
    
    $("#topImageNext").click( function () {
        if (toplist_current == (toplist_count - 1)) return;
        toplist_current++;
        $("#scroll").fadeOut(500,function () {
            loadImage(toplist_current);
            $("#scroll").fadeIn(500);
        });
    });

}

$(document).ready( initSite );


function postLoginForm () {
	var dataString = 'email=' + $("input#lemail").val() + '&password=' + $("input#lpassword").val();
		
		$.ajax({
	      type: "POST",
	      url: base_url + '/index_ajax.php?page=login',
	      data: dataString,
	      success: function( data ) {
	      	$( '#login' ).html( data ).hide().fadeIn(250);
	      }
	     });
}

function logout () {
	var dataString = '';
		
		$.ajax({
	      type: "POST",
	      url: base_url + '/index_ajax.php?page=logout',
	      data: dataString,
	      success: function( data ) {
	      	$( '#login' ).html( data ).hide().fadeIn(250);
	      }
	     });
}

function loadDiv( div_id, url ) {
	//alert('/');
	var dataString = '';
	$.ajax({
      type: "GET",
      url: url,
      data: dataString,
      success: function( data ) {
      	$( '#'+div_id ).html( data );
      }
     });
	
}

var mpoll_error = false;

function mpoll_submit( poll_id ) {
    error = false;

    $('#mpoll_' + poll_id + ' .mpoll_chooseanswer input').each( function() {
        if ( $(this).val() < 0 ) {
            error = true;
        }
    } );

    if ( error ) {
        alert('Az összes kérdésre kötelező válaszolni!');
    }
    else {
        $.post(base_url + '/index_ajax.php?page=mupapoll',
               $('#mpoll_form_' + poll_id).serialize(),
               function( data ) {
                   $('#mpoll_' + poll_id).html( data );
               }
        );
    }
}

function stopPopUpPlayer() {
    $('#searchBox select').show();
    $('#video').fadeOut(250); 
    $('#mediaspace').empty();
}