$(document).ready(function() {
	$('#start').hover(
		function() {
			$(this).addClass('hover');
		},
		function() {
			$(this).removeClass('hover');
		}
	).click(
		function() {
			$('#start-menu').slideToggle('fast');
		}
	);
	$('#start-menu').hide().click(
		function() {
			$('#start-menu').slideUp('fast');
		}
	);	
	if (logged && notesWindow) {
		$('#default-content').hide();
        showLoginForm()
		showMyNotes()
		var half = $(window).width()/2
		$('#login-window').find('.AeroWindow').animate({
			'left' : half - 520
		}, 500);
		$('#notes-window').find('.AeroWindow').animate({
			'left' : half - 170
		}, 500);		
	} else {
		showLoginForm()
		showStartWindow()
		var half = $(window).width()/2
		$('#login-window').find('.AeroWindow').animate({
			'left' : half - 520
		}, 500);
		$('#default-content').find('.AeroWindow').animate({
			'left' : half - 170
		}, 500);
	}
});

function barClick(id) {
	$('#'+id).find(".win-min-btn").trigger('click');
}

function createWindow(id, title, content, options) {
	if ( $('#'+id).length > 0)
		return;
	if (options == null)
		options = {}
	options.WindowTitle = title
	if (options.WindowIcon == null)
		options.WindowIcon = 'default'
	if (content == null)
		content = '<div class="loader"></div>';
	$('body').prepend('<div id="'+id+'">'+content+'</div>')
	addToBar(id, title, options.WindowIcon)
	return $('#'+id).AeroWindow(options); 
}

function showNewNote() {
	if (!logged) {
		showLoginForm();
		return
	}
	nowyPlik()
}

function showMyNotes() {
	if (!logged) {
		showLoginForm();
		return
	}
	content = '<div id="zawartosc"></div>'
	toolbar = '<a href="javascript:nowyPlik()" title="Nowy plik" class="p"><img src="'+BASE_URL+'/obrazki/pliknowy32.png" alt="Nowy plik" /></a>'+
		'<a href="javascript:nowyFolder()" title="Nowy folder" class="p"><img src="'+BASE_URL+'/obrazki/foldernowy32.png" alt="Nowy folder" /></a>'+
		'<a href="javascript:doGory()" title="Do góry"><img src="'+BASE_URL+'/obrazki/folderdogory32.png" alt="Do góry" /></a>'+
		'<img src="'+BASE_URL+'/obrazki/separator.png" alt="|" />'+
		'<a href="javascript:zmienNazwe()" title="Zmień nazwę"><img src="'+BASE_URL+'/obrazki/zmiennazwe.png" alt="Zmień nazwę" /></a>'+
		'<a href="javascript:usun()" title="Usuń"><img src="'+BASE_URL+'/obrazki/usun32.png" alt="Usuń" /></a>'

	createWindow('notes-window', 'Moje notatki', content, {
		WindowIcon:'folder',
		WindowWidth:500,
		WindowHeight:400,
		WindowToolbar:toolbar
	}).find('.table-mm-content');
	odswiez()
}

function showStartWindow() {
	$('#default-content').AeroWindow({
		WindowTitle:'Notatnik ONLINE',
		WindowWidth:600,
		WindowHeight:450		
	}); 
	addToBar('default-content', 'Notatnik ONLINE', 'default')
}

function showRules() {
	createWindow('rules-window', 'Regulamin', null, {
		WindowIcon:'rules'
	}).find('.table-mm-content').load('/site/rules');
}

function showContact() {
	createWindow('contact-window', 'Kontakt', null, {
		WindowIcon:'contact'
	}).find('.table-mm-content').load('/site/contact');
}

function showInformation() {
	createWindow('info-window', 'Informacje', null, {
		WindowIcon:'info',
		WindowWidth:600,
		WindowHeight:450				
	}).find('.table-mm-content').load('/site/index');
}

function showLoginForm() {
	try{
		createWindow('login-window', 'Logowanie', null, {
			WindowIcon:'login',
			WindowHeight: 450,
			WindowWidth: 300,
		}).find('.table-mm-content').load('/user');
	}catch(err){
		
	}
}

function showSubmitIssueForm() {
	createWindow('submit-issue-window', 'Zgłoś Błąd', null, {
		WindowIcon:'contact',
		WindowWidth:600,
		WindowHeight:450		
	}).find('.table-mm-content').load('/site/submitIssue');	
}


function addToBar(id, title, icon) {
	if (icon == null)
		icon = 'default'
	$('#bottom-bar ul').append('<li id="bar-'+id+'"><a href="javascript:void(0)" onclick="barClick(\''+id+'\')"><div>'+
		'<img src="'+BASE_URL+'img/icons/'+icon+'.png" class="small-icon"> ' +title+'</div></a></li>');
}

function safeName(str) {
		pl = new Array ('ó', 'ą', 'ś', 'ł', 'ż', 'ź', 'ć', 'ń', 'Ó', 'Ą', 'Ś', 'Ł', 'Ż', 'Ź', 'Ć', 'Ń', ' ');
		un = new Array ('o', 'a', 's', 'l', 'z', 'z', 'c', 'n', 'O', 'A', 'S', 'L', 'Z', 'Z', 'C', 'N', '_');
		tmp = Array()
		for(i=0; i<str.length;i++){
			tmp[i] = str.charAt(i)
			for(j=0; j < pl.length;j++){
				if(str.charAt(i)==pl[j])
					tmp[i] = un[j]
			}
		}
		return tmp.join().replace(/([^A-z0-9._-])/g, '')
	}

function canCloseWindow(id) {
	if (id.substr(0, 8) == 'notatka-') {
		nr = id.substr(8)
		if (zmiany[nr] == 1) {
			ret = confirm('Zmiany nie zostały zapisane, czy na pewno chcesz zamknąć okno?');
			if (ret)
				zmiany[nr] = 0
			return ret;	
		}
	}
	return true
}
