/**
 * Author: Liviu (liviu.hariton@iseom.com)
 * Version: 0.1
 * Started: 15 Sep 2008 14:56
 *
 * Copyright (c) 2008 International SEO Marketing. All rights reserved.
 * http://www.iseom.com <contact@iseom.com>
 */

/**
 * jQuery / Prototype compatibility issue
 */
var $j = jQuery.noConflict();

/**
 * Display and hide the message (confirm / warning) block
 */
function showMessage(block) {
    new Effect.Appear(block, {duration: 1});
    //new Effect.Fade(block, {duration: 1, delay: 5});
}

function noSpam(part_1, part_2) {
    document.location.href = "mailto:" + part_1 + "@" + part_2;
}

function updateMetaTK(newvalue) {
    //$('meta_title').value = newvalue;
    
    //updateMetaDesc(newvalue);
    
    
    newvalue = trim(newvalue);
    
    var keys_tags = newvalue.split(' ').join(', ');
    var last_chr_check = keys_tags.charAt(keys_tags.length-1);
    
    Array.prototype.in_array = function ( obj ) {
    	var len = this.length;
    	for ( var x = 0 ; x <= len ; x++ ) {
    		if ( this[x] == obj ) return true;
    	}
    	return false;
    }

    to_strip = new Array("!", "@", "#", "^", "&", "(", ")", "[", "]", ";", ":", "<", ">", "\"", "/", "\\", "-", "+", "*", "~", "`", ",");
    
    if(to_strip.in_array(last_chr_check)) {
        keys_tags = keys_tags.slice(0, -1);
    }
    
    updateAlias(keys_tags.toLowerCase());
    
    //$('meta_keywords').value = keys_tags.toLowerCase();
}

function updateMetaDesc(newvalue) {
    var words = newvalue.split(' ');
    var max_length = 240;
    
    var words_length = '';
    var final_string = '';
    
    words.each(function(item) {
      words_length = words_length + item;
      
      if(words_length.length <= max_length) {
          final_string = final_string + ' ' + item;
      }
    });
    
    $('meta_description').value = trim(final_string);
    
    return true;
}

function updateAlias(newvalue) {
    newvalue = trim(newvalue);
    
    var rExps=[ /[\xC0-\xC2]/g, /[\xE0-\xE2]/g, /[\xC8-\xCA]/g, /[\xE8-\xEB]/g, /[\xCC-\xCE]/g, /[\xEC-\xEE]/g, /[\xD2-\xD4]/g, /[\xF2-\xF4]/g, /[\xD9-\xDB]/g, /[\xF9-\xFB]/g, /[\xC7-\xC7]/g, /[\xE7-\xE7]/g, /[\xC3-\xC3]/g, /[\xCE-\xEE]/g, ',' ];
    
    var repChar=['A','a','E','e','I','i','O','o','U','u','C','c','A','i',''];
    
    for(var i = 0; i < rExps.length; i++)
        newvalue = newvalue.replace(rExps[i], repChar[i]);
    
    var alias = newvalue.split(' ').join('-');
    
    $('alias').value = alias.toLowerCase();
}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function deleteImage(id) {
    if(confirm('Vous avez choisi de supprimer ce fichier. Continuer?')) {
        $('loading-image').show();
        $('article-image').hide();
        
        var senddata = new Ajax.Request(site_url + 'ajax/deleteImage.php', 
        { 
            method: 'post', 
            parameters: {idarticle: id},
            onSuccess: function(transport) {
                //var response = transport.responseText || "nici un raspuns";
                //alert(response);
            },
            onComplete: function(transport) {
                $('loading-image').hide();
                $('deleted-image').show();
            }
        });
    }
}
