

$(document).ready(function(){
   

    
    $('#closemessage').click( function() { $(this).parents('.message').slideUp() } );
    
    $('input[name=username], input[name=email]').blur( function(str) {
        $(this).val( $(this).val().toLowerCase() );
    });
/*
    $('input.only_figures').live('keypress', function(e){
		e = e || window.event;
		var charCode = e.which ? e.which : e.keyCode;
		if((charCode>=48 && charCode<=57)|| charCode == 8 || charCode== 46 || charCode ==37 || charCode == 39) return true;
		return false;
	});
*/  
    $('textarea.resizable:not(.processed)').TextAreaResizer();
    
    // http://www.filamentgroup.com/lab/update_styling_the_button_element_with_css_sliding_doors_now_with_image_spr/
    $('.submitBtn').hover( function(){ $(this).addClass('submitBtnHover'); }, function(){ $(this).removeClass('submitBtnHover'); });
    
    
    
    // Initialise Facebox Modal window:
	
	//$('a[rel*=modal]').facebox(); // Applies modal window to any link with attribute rel="modal"

    // Initialise jQuery WYSIWYG:
		

    $('#company_edit input[name="name"]').blur( function() {
        var value = $(this).val();
        value = makeSlug(value);
        $(this).parents('form').find('input[name="permalink"]').val(value);
    });
    
    $('#wine_edit input[name="name"], #wine_edit input[name="year"]').blur( function() {
        var value = $(this).parents('form').find('input[name=name]').val();
        var winery = $(this).parents('form').find('input[name=winery]').val();
        var year = $(this).parents('form').find('input[name=year]').val();
        if (winery != '') { value = winery + ' '+ value + ' ' + year };

        value = makeSlug(value);
        if (value[0] == '-') value = value.substr(1);
        if (value[value.length-1] == '-') value = value.substr(0, value.length-1);
        $(this).parents('form').find('input[name="permalink"]').val(value);
    });
    
    $('.share_favorite a').click( function() {
        $.post( $(this).attr('href') , function(data) {
            $('.share_favorite a').addClass('star_on');
    		//if ($('#dialog_share_favorite').dialog)
			$('#dialog_share_favorite').dialog('open');
        });
        return false;
    });
    
    
    $('.lightbox').colorbox();
    $('.lightbox_map').colorbox({iframe:true,width:'680',height:'460'});
    
    $('.delete').click( function() {
        var conf = confirm('Estas seguro que desea eliminar?');
        return conf;
    } );
    
    $('.ajax_delete').click( function() {
        var conf = confirm('Estas seguro que desea eliminar?');
        if (!conf) return false;
        
        var href = $(this).attr('href');
        var obj = this;
        $.post(href, function(res) {
            //alert(res);
            //alert(res.action);
            if (res.action && res.action=='delete') 
            {
                $(obj).parents('.entry').remove();
            }
        }, "json");
        return false;
    });
    
    $('.votelink').click( function() {
        var href = $(this).attr('href');
        $.post(href, function(res) {
            if (res.message && res.message=='ok') 
            {
                alert('Su voto será procesado, gracias.');
            }else
            {
                alert('Hubo un inconveniente con su voto.');
            }
        }, "json");
        return false;
    });
    
    $(".wysiwyg").wysiwyg(); // Applies WYSIWYG editor to any textarea with the class "wysiwyg"

    $('textarea.autoresize').autoResize({
        // On resize:
        onResize : function() {
            $(this).css({opacity:0.9});
        },
        // After resize:
        animateCallback : function() {
            $(this).css({opacity:1});
        },
        // Quite slow animation:
        animateDuration : 200,
        // More extra space:
        extraSpace : 40
    });
    
    initializeAdminFunctions();

});

function makeSlug(slugcontent)
{
    // convert to lowercase (important: since on next step special chars are defined in lowercase only)
    slugcontent = slugcontent.toLowerCase();
    // convert special chars
    var accents={a:/\u00e1/g,e:/u00e9/g,i:/\u00ed/g,o:/\u00f3/g,u:/\u00fa/g,n:/\u00f1/g}
    for (var i in accents) slugcontent = slugcontent.replace(accents[i],i);
        
	var slugcontent_hyphens = slugcontent.replace(/\s/g,'-');
	var finishedslug = slugcontent_hyphens.replace(/[^a-zA-Z0-9\-]/g,'');
    finishedslug = finishedslug.toLowerCase();
    return finishedslug;
}


function initializeAdminFunctions()
{
    
}


