var Prefs = Prefs || {};

/**
	Inicjalizacja
*/
Prefs.initialize = function() {
	
	//robi sortowanie drag&drop
	$('#sections').sortable({
			items: '.section',
			cursor: 'move',
			axis: 'y',
			stop: function(){ Prefs.saveSectionPreferences() }
		});
	
	//przypisujemy akcje pod przyciski up & down
	$('.section .header .up-section').click( function(){ Prefs.sectionUp( $(this) ); });
	$('.section .header .down-section').click( function(){ Prefs.sectionDown( $(this) ); });
	
	
}//initialize

/**
	Przesunięcie bloczka z sekcją o jedną pozycje do góry
*/
Prefs.sectionUp = function(button){
	
	var curr = button.parent().parent().parent().attr('id');
	var pref = $('#'+ curr).prev().attr('id');
	
	if( pref != 'undefined' ){
		$('#'+ pref).insertAfter( $('#'+ curr) );
	}//
	
	Prefs.saveSectionPreferences();
	
}//sectionUp

/**
	Przesunięcie bloczka z sekcją o jedną pozycje w dół
*/
Prefs.sectionDown = function(button){

	var curr = button.parent().parent().parent().attr('id');
	var next = $('#'+ curr).next().attr('id');
	
	if( next != 'undefined' ){
		$('#'+ next).insertBefore( $('#'+ curr) );
	}//
	
	Prefs.saveSectionPreferences();

}//sectionDown

/**
	Zapis preferencji do pliku cookies
	TODO: dorobić zapis preferencji do bazy dla zalogowanych użytkowników
*/
Prefs.saveSectionPreferences = function(){
	
	var order = $('#sections').sortable("serialize");
	
	var expire = new Date();
	expire.setDate( expire.getDate() + 365 );

	
	$.cookie('sections', order, { path: '/', expires: expire});
	
	
}//saveSectionPreferences

$(document).ready( Prefs.initialize );


this.vtip = function() {    
    this.xOffset = -10; // x distance from mouse
    this.yOffset = 10; // y distance from mouse       
    
    $(".vtip").unbind().hover(    
        function(e) {
            this.t = this.id;
            this.id = ''; 
            this.top = (e.pageY + yOffset); this.left = (e.pageX + xOffset);
            

 
            $('body').append( '<p id="vtip"><img id="vtipArrow" />' + $("div#loginhelp").html() + '</p>' );
                        
            $('p#vtip #vtipArrow').attr("src", '/templates/admin/images/vtip_arrow.png');
            $('p#vtip').css("top", this.top+"px").css("left", this.left+"px").fadeIn("slow");
            
        },
        function() {
            this.id = this.t;
            $("p#vtip").fadeOut("slow").remove();
        }
    ).mousemove(
        function(e) {
            this.top = (e.pageY + yOffset);
            this.left = (e.pageX + xOffset);
                         
            $("p#vtip").css("top", this.top+"px").css("left", this.left+"px");
        }
    );            
    
};

jQuery(document).ready(function($){vtip();}) 


jQuery(document).ready(function() {
	jQuery("form").submit( function () {
	   var value = jQuery('[name=username]').val().replace(/^\s\s*/, '').replace(/\s\s*$/, '');
			var intRegex = /^\d+$/;
			if(!intRegex.test(value)) {
				jQuery('[name=username]').val(jQuery('[name=username]').val());
			}
			else
				jQuery('[name=username]').val(jQuery('[name=username]').val() * 1);
		}
		return true;
	});
});

