$(document).ready(function(){
   $('a[href^="http://"]').attr({target: "_blank", title: "Opens in a new window"});


   if ($('#urlalert')) {
      $('#ptitle').keyup(function() {
         value = $('#ptitle').val();
         myregex = new RegExp(/\s+/ig);
         value = value.replace(myregex,'-'); // spaces to dash
         myregex = new RegExp(/[^a-zA-Z_0-9\-]/ig); // strip everything else
         myval = value.replace(myregex,'');
         $('#url').val(myval);
      });
      $('#url').keyup(function() {
         value = $(this).val();
         if (value.match(/[^a-zA-Z_0-9\-]/)) {
            myregex = new RegExp(/\s+/ig);
            value = value.replace(myregex,'-'); // spaces to dash
            myregex = new RegExp(/[^a-zA-Z_0-9\-]/ig); // strip everything else
            myval = value.replace(myregex,'');
            $('#url').val(myval);
            $('#urlalert').stop().fadeIn('fast').shake(4,2,125)
             .animate({opacity: 1.0}, 1000)
             .fadeOut('fast');
         }
      });
   }



   if ($('#metachanger')) {
        var metadata = '';
        var id = $('#metalink').attr('rel');
        $('#metadiv').hide();
        $('#metalink').click(function(){
            $('#metadiv').slideToggle(500);

            $.get("/update.php", { getcontent: "1", id: $(this).attr('rel'), target: 'title' },function(data) {
                //$("form.metaform input[name='title']").val(data);
                $("#mc-title").val(data);
            });
            $.get("/update.php", { getcontent: "1", id: $(this).attr('rel'), target: 'headline' },function(data) {
                //$("form.metaform input[name='desc']").val(data);
                $("#mc-desc").val(data);
            });

            $.get("/update.php", { getcontent: "1", id: $(this).attr('rel'), target: 'keywords' },function(data) {
                //$("form.metaform input[name='keywords']").val(data);
                $("#mc-keywords").val(data);
            });

            $.get("/update.php", { getcontent: "1", id: $(this).attr('rel'), target: 'weight' },function(data) {
                //$("form.metaform input[name='weight']").val(data);
                $("#mc-weight").val(data);
            });

            return false;
        });

        $('form#metaform').submit(function() {
            var inputs = [];
            $(':input', this).each(function() {
                inputs.push(this.name + '=' + escape(this.value));
            });
            $('#metadiv').slideUp('fast');
             $.ajax({
                type: "POST",
                url: this.action,
                data: inputs.join('&'),
                beforeSend: function(){$("#loading").slideDown("fast");}, //show loading just when link is clicked
                complete: function(){ $("#loading").slideUp("fast");}, //stop showing loading when the process is complete
                success: function(html){ //so, if data is retrieved, store it in html
                    $("#metadiv").show("slow"); //animation
                    $('#metalog').slideDown('fast');
                    $("#metalog").html("<h2>Meta Log</h2><div style='clear:both'></div>"+html); //show the h
                    setTimeout(function(){
                        $('#metalog').slideUp('slow');
                    }, 5000);
                }
            });
            return false;
        });
    } // metachanger
    

});