var gallery_count = 0;
var fbtimeout	= null;

$(document).bind('keydown', function(e) {
    if ( $('#gallery-scroll').length > 0 ) {
	if ( e.keyCode == 39 ) {
	    next_image();

	} else if ( e.keyCode == 37 ) {
	    prev_image();
	}
    }
});

next_image = function () {
    var previewimg	= $('#gallery-image-prev img');
    var nextimg		= $('#gallery-scroll .' + previewimg.attr('class')).parent().next().find('img');

    if ( nextimg.length ) {

	set_image(nextimg);
    }
}

prev_image = function () {
    var previewimg	= $('#gallery-image-prev img');
    var previmg		= $('#gallery-scroll .' + previewimg.attr('class')).parent().prev().find('img');

    if ( previmg.length ) {

	set_image(previmg);
    }
}

set_image = function (setimg, load) {
    if ( setimg.length ) {
	$('#gallery-image-prev img').attr( {'src' : setimg.attr('src').replace('/thumb/', '/'), 'alt' : setimg.attr('src'), 'class' : setimg.attr('class') } );

	var slices_location = document.location.toString().split('#');
	var parentindex = setimg.parent().index();

	$('.facebook-comments').html('');

	window.clearTimeout(fbtimeout);

	fbtimeout = window.setTimeout(function() {
	    // reinitialize facebook
	    var fburl = '/galeria/' + document.location.pathname.toString().split('/')[2] + '/' + setimg.attr('id');

	    $('.facebook-comments').html('<fb:like href="' + document.location.hostname + fburl + '" show_faces="false" width="700" font=""></fb:like><br /><br /><fb:comments href="' + document.location.hostname + fburl + '" num_posts="10" width="700"></fb:comments>');
	    FB.init({xfbml  : true });
	}, 1000);

	document.location = slices_location[0] + '#image' + setimg.attr('id');

	if ( parentindex == gallery_count ) {

	    $('#gallery-next-image').hide();
	} else {

	    $('#gallery-next-image').show();
	}
	
	if ( parentindex == 0 ) {

	    $('#gallery-prev-image').hide();
	} else {

	    $('#gallery-prev-image').show();
	}
    }
}

gallery_startup = function () {
    if ( $('#gallery-scroll').length > 0 ) {
        var doclocation	= document.location.toString();
        var s		= doclocation.split('#');

        if ( s.length > 1 ) {
	    var s_	= document.location.pathname.toString().split('/');

	    document.location = '/galeria/' + s_[2] + '/' + s[1].replace('image', '');
	}

	gallery_count = ($('#gallery-scroll img').length - 1);
    }
}

$(function() {
    gallery_startup();

    $('#login-line input').focus(function() {
	if ( $(this).val() == this.defaultValue ) {
	    $(this).val('');
	}
    });

    $('#gallery-image-prev').click(function() {
	next_image();

    });

    $('#gallery-scroll li img').click(function() {
	set_image($(this));

    });

    $('.events-menu li a').bind('click', function() {
	var selected	= $('.events-menu .selected a');

	if ( !selected.length || selected.attr('class') != $(this).attr('class') ) {
	    var type	= $(this).attr('class').replace('events-menu-', '');
	    var elem;
	
	    elem = $('#events-' + type);

	    if ( elem.length > 0 ) {
		$('.events-tab').hide();
	        $('.events-menu li').removeClass('selected');
	        $(this).parent().addClass('selected');

		elem.show();
	    }
	}

	return(false);

    });

    $('.leadbox_content, .multimedia_content').click(function() {
	var a = $('a', $(this));
	
	document.location = a.attr('href');
    });

    $("div#controller").jFlow({
        slides : ".leadbox_items",
	controller : '.news_jFlowControl',
	slideWrapper : '#news_jFlowSlide_',
        width : "440px",
        height : "300px",
        prev : '.news_jFlowPrev',
        next : '.news_jFlowNext',
        auto : true
    });

    $('div#party_controller').jFlow({
        slides : ".party_multimedia_items",
	controller : '.party_jFlowControl',
	slideWrapper : '#party_jFlowSlide',
        prev : '.party_jFlowPrev',
        next : '.party_jFlowNext',
        width : "400px",
        height : "300px",
        auto : false
    });

    $('div#rendezveny_controller').jFlow({
        slides : ".rendezveny_multimedia_items",
	controller : '.rendezveny_jFlowControl',
	slideWrapper : '#rendezveny_jFlowSlide',
        prev : '.rendezveny_jFlowPrev',
        next : '.rendezveny_jFlowNext',
        width : "400px",
        height : "300px",
        auto : false
    });

    $('div#sport_controller').jFlow({
        slides : ".sport_multimedia_items",
	controller : '.sport_jFlowControl',
	slideWrapper : '#sport_jFlowSlide',
        prev : '.sport_jFlowPrev',
        next : '.sport_jFlowNext',
        width : "400px",
        height : "300px",
        auto : false
    });

    $('div#papers_controller').jFlow({
        slides : ".papers_multimedia_items",
	controller : '.papers_jFlowControl',
	slideWrapper : '#papers_jFlowSlide',
        prev : '.papers_jFlowPrev',
        next : '.papers_jFlowNext',
        width : "400px",
        height : "300px",
        auto : false
    });
    
    $('#multimedia-right ul li a').hover(function() {
	var id = $(this).attr('id').replace('multimedia-select-', '');

	if ( $('#multimedia_' + id).length > 0 ) {

	    $('.multimedia_container').hide();
	    $('#multimedia_' + id).show();

	}
	
	return(false);
    });

    $('input.datepicker').datepicker({
        regional : 'hu'
    });

    $('#select-school').change(function() {
	if ( $(this).val() == 'egyeb' ) {
	    $('#uniq-school').show();

	} else {
	    $('#uniq-school').hide();
	}
    });
});
