var menu_state = false;
var notification_state = false;
var notification_showed = false;
var dialog_showed = false;

$(document).ready(function()
{
    var menu = $('#menu');
    var notification = $('#notification-menu');
    var flash = $('#flash-message');
    var reference = $('.reference');
    var image_dialog = $('#image-dialog');
    var tweets = getTwitters('notification-area',
    {
	id: 'Net_CZ',
	count: 6,
	enableLinks: true,
	ignoreReplies: true,
	clearContents: true,
	callback: function()
	{
	    $('#notification-area .tweet-text').each(function()
	    {
		var tweet = $(this);
		if(tweet.text().length > 60) tweet.text(tweet.text().substr(0, 60)+' ...');
	    });
	},
	template: '<a class="tweet-time" target="_blank" href="http://twitter.com/%user_screen_name%/statuses/%id_str%/">%time%</a><span class="tweet-text">%text%</span>'
    });
    
    $.history.init(function(hash)
    {
	check_tabs(tabContainers,document.location.hash);
    },
    {
	unescape: ",/"
    });
    
    make_menu_draggable(menu);
    make_notification_draggable(notification);

    $('.page').each(function()
    {
	$(this).jScrollPane({autoReinitialise: true});
    });
    
    $('#notification-spot').mouseover(function()
    {
	if(!dialog_showed)
	{
	    notification.fadeIn('fast');
	    notification_show_toggle(notification);
	}
    });
    
    $('#notification-handle').mouseout(function()
    {
        if(notification_showed && !notification_state)
        {
            notification.fadeOut('fast');
            notification_show_toggle(notification);
        }
    });
    
    $('#phone-body .menu-handle').click(function()
    {
	if(!dialog_showed)
	{
	    menu.animate(
	    {
	       top: (!menu_state) ? '280px' : '384px'
	    }, 'slow');
	    $('.menu-handle').attr('title',(menu_state) ? 'Zobrazit nabídku' : 'Skrýt nabídku');
	    $('#screen .menu-handle').css('cursor',(menu_state) ? 'n-resize' : 's-resize');
	    menu_state = !menu_state;
	}
	return false;
    });
    
    $('#phone-body #search-button').click(function()
    {
	notification.animate(
	{
	   top: '-450px'
	}, 'slow');
	notification.fadeOut('fast');
	$('#notification-handle').attr('title',(notification_state) ? 'Zobrazit nabídku' : 'Skrýt nabídku');
	$('#notification-handle').css('cursor',(notification_state) ? 's-resize' : 'n-resize');
	notification_state = false;
	notification_showed = false;
	flash.find('.title').text('Nastala chyba');
	flash.find('.message').html('Aplikace "droidSearchEngine" provedla neplatnou operaci.<br />Kernel panic - Mem, killing handler! ');
	flash.fadeIn('fast');
	hide_menu(menu);
	dialog_showed = true;
	return false;
    });
    
    $('#phone-body #back-button').click(function()
    {
	if(!dialog_showed)
	{
	    hide_menu(menu);
	    history.go(-1);
	    menu.draggable('enable');
	}
	return false;
    });
    
    $('.reference img.thumb').click(function()
    {
	$('#image-dialog img.image').attr('src',$(this).parent('.reference').find('img.hidden').attr('src'));
	image_dialog.find('.title').text($(this).attr('alt'));
	image_dialog.fadeIn('fast');
	hide_menu(menu);
	dialog_showed = true;
    });
    
    $('.dialog .button').click(function()
    {
	$('#' + $(this).attr('id').replace(/-ok/,'')).fadeOut('fast');
	dialog_showed = false;
	menu.draggable('enable');
	return false;
    });
    
    setInterval(set_time,100);
})

function set_time()
{
    var months = ['Leden','Únor','Březen','Duben','Květen','Červen','Červenec','Srpen','Září','Říjen','Listopad','Prosinec'];
    
    var date = new Date();
    
    var y = date.getFullYear();
    var mo= months[date.getMonth()];
    var d = check_time(date.getDate());
    
    var h = check_time(date.getHours());
    var m = check_time(date.getMinutes());
    var s = check_time(date.getSeconds());
    
    $('#time').text(h+':'+m+':'+s);
    $('#date').text(d+'. '+mo+' '+y);
}

function check_time(i)
{
    if(i < 10)
    {
	i = "0" + i;
    }
    return i;
}

function hide_menu(menu)
{
    if(menu_state) $('#phone-body .menu-handle').first().click();
    menu.draggable('disable');
}

function make_menu_draggable(menu)
{
    if(!dialog_showed)
    {
	menu.draggable(
	{
	    axis:'y',
	    containment: [0,444,0,548],
	    stop: function()
	    {
		menu.animate(
		{
		   top: (!menu_state) ? '280px' : '384px'
		}, 'slow');
		$('.menu-handle').attr('title',(menu_state) ? 'Zobrazit nabídku' : 'Skrýt nabídku');
		$('#screen .menu-handle').css('cursor',(menu_state) ? 'n-resize' : 's-resize');
		menu_state = !menu_state;
	    }
	});
    }
}

function make_notification_draggable(notification)
{
    if(!dialog_showed)
    {
	notification.draggable(
	{
	    axis:'y',
	    containment: [0,-286,0,114],
	    stop: function()
	    {
		notification.animate(
		{
		   top: (notification_state) ? '-450px' : '-50px'
		}, 'slow');
		$('#notification-handle').attr('title',(notification_state) ? 'Zobrazit nabídku' : 'Skrýt nabídku');
		$('#notification-handle').css('cursor',(notification_state) ? 's-resize' : 'n-resize');
		notification_state = !notification_state;
		if(!notification_state)
		{
		    notification.fadeOut('fast');
		    notification_show_toggle(notification);
		}
	    }
	});
    }
}

function notification_show_toggle(notification)
{
    notification_showed = !notification_showed;
}

