//Needs jQuery >= 1.3.2 to work

function apply_button_classes(){
	$(this).addClass('ui-state-default');
	$(this).addClass('ui-corner-all');	
	$(this).hover(function(){
		$(this).stop(true, true);
		$(this).addClass('ui-state-hover');
	}, function(){
		$(this).stop(true, true);
		$(this).removeClass('ui-state-hover');
	});
}

function transform_button_links(){
	var href = $(this).attr('href');
	var innerhtml = $(this).html();
	var classes = $(this).attr('class').replace('button_link', '');
	var title = $(this).attr('title');
	$(this).replaceWith("<button type='button' id='temp_repl_id'>" + innerhtml + "</button>");
	//$("#temp_repl_id").attr('type', 'button');
	//$("#temp_repl_id").addAttr('type', 'button');
	$("#temp_repl_id").click(function(){
		window.location.href = href;
	});
	//var button = $("#temp_repl_id");
	$("#temp_repl_id").attr('class', classes);
	$("#temp_repl_id").attr('title', title);
	$("#temp_repl_id").removeAttr('id');
	//return button;
}

function transform_admin_links(){	
	var href = $(this).attr('href');
	var innerhtml = $(this).html();
	var title = $(this).attr('title') || innerhtml;
	$(this).replaceWith("<button type='button' id='temp_repl_id'><span class='ui-icon ui-icon-wrench'></span>" + innerhtml + "</button>");
	//$("#temp_repl_id").attr('type', 'button');
	$("#temp_repl_id").click(function(){
		$(this).blur();
		window.location.href = href;
	});
	$("#temp_repl_id").addClass('admin_button');
	$("#temp_repl_id").attr('title', 'Adminisztrátori művelet: ' + title);
	$("#temp_repl_id").removeAttr('id');
}

$(function(){

	//Transform links to buttons
	$("a.button_link").each(transform_button_links);	
	$("a.admin_link").each(transform_admin_links);
	
	//Apply styling to buttons
	$("button").each(apply_button_classes);	
	
	//$("body").fadeIn(400);
	
	//If the username field is present, focus it
	if ($("#signin_username"))
	{
		$("#signin_username").focus();
		$("#signin_username").select();
	}
	/*
    $('#slideshow_images').crossSlide({
        speed: 45,
        fade: 1,
		shuffle: true,
    }, [{
        src: '/images/slideshow/IMG_1836.JPG',
        dir: 'up'
    }, {
        src: '/images/slideshow/IMG_1837.JPG',
        dir: 'down'
    }, {
        src: '/images/slideshow/IMG_1838.JPG',
        dir: 'up'
    }, {
        src: '/images/slideshow/IMG_1839.JPG',
        dir: 'down'
    }]);
	*/
	$(".menu_toggler").click(function (){
		$(this).parent().find('ul').toggle('normal');	
	});
	
	
	$(".submenu").hide();
	
	var current_url = window.location.href.split('/');
	//console.log(current_url);
	if ($("#submenu_" + current_url[4])){
		$("#submenu_" + current_url[4]).show();
	}
	$("#submenu_" + current_url[5]).show();
	
	/*
    $('#slideshow_images').cycle({
        fx: 'fade',
        speed: 300,
        timeout: 3000,
        next: '#s3',
        pause: 1
    });
	*/
});
