var selected = '';
var url = window.location.host;

var loc = new String(window.parent.document.location); 
if (loc.indexOf("https://")!= -1) 
	prefix = "https://"; 
else 
	prefix = "http://"; 

var site_url = prefix + url + '/';
var _editor_url  = prefix + url + '/globals/wysiwyg/xinha/';
var _editor_lang = "en";
var Xinha;
var xinha_plugins;
var xinha_editors; 

function floatval (mixed_var) {
    // +   original by: Michael White (http://getsprink.com)
    // %        note 1: The native parseFloat() method of JavaScript returns NaN when it encounters a string before an int or float value.
    // *     example 1: floatval('150.03_page-section');
    // *     returns 1: 150.03
    // *     example 2: floatval('page: 3');
    // *     returns 2: 0
    // *     example 2: floatval('-50 + 8');
    // *     returns 2: -50
 
    return (parseFloat(mixed_var) || 0);
}

function isEmpty( value )
{
    if( value == undefined || value == null || value == '' )
    {
        return false;
    }
    
    return true;
}

function xinha_init()
{
    if(!Xinha.loadPlugins(xinha_plugins, xinha_init))
    {
        return;
    }  
    var xinha_config = new Xinha.Config();
    xinha_config.width = '276px';
    xinha_config.iframeWidth = '276px';
    xinha_config.height = '276px';

    xinha_editors = Xinha.makeEditors(xinha_editors, xinha_config, xinha_plugins);
    
    Xinha.startEditors(xinha_editors);
}
    
function loadJsBlog(id)
{
  jQuery(function() {
    jQuery('#'+id).wymeditor();
});
}

function loadJs(id)
{          
    //Xinha = undefined;
    delete Xinha;
    //delete xinha_plugins;
    delete xinha_editors;
    //xinha_plugins = null;
    //xinha_editors = null;
    document.getElementById(id).onDblclick='';  
    xinha_plugins =
    [
        'Linker',
        'ImageManager'
    ];
    xinha_editors =
    [
        id
    ];
    xinha_init();
}
function loadJsSL(id)
{          
    //Xinha = undefined;
    delete Xinha;
    //delete xinha_plugins;
    delete xinha_editors;
    //xinha_plugins = null;
    //xinha_editors = null;
    document.getElementById(id).onDblclick='';  
    xinha_plugins =
    [
        'Linker'
    ];
    xinha_editors =
    [
        id
    ];
    
    var xinha_config = new Xinha.Config();
    xinha_config.width = '550px';
    xinha_config.height = '400px';  
    xinha_config.iframeWidth = '550px';
    xinha_config.iframeHeight = '350px';
    xinha_config.hideSomeButtons(" htmlmode help ");
    xinha_config.toolbar =
 [
   ["separator","formatblock","bold","italic","subscript","superscript"],
   ["separator","insertunorderedlist","outdent","indent"],
   ["separator","createlink"],
   ["separator","undo","redo"] 
 ];

 xinha_config.formatblock =
  {
    "Normal"   : "p",
    "Headline": "h4",
    "Sub-Headline": "h5"
};

xinha_config.pageStyle =
 'p { color:black; padding: 5px; margin: 5px; font-family: Arial; font-size: 14px; text-align:left;}\n' +
 'h4 {color: #f69404; font-family: Arial,sans-serif; font-size: 20px; font-weight: bold; text-align:center;}\n' +
 'h5 {color: #4c4c4c; font-family: Arial,sans-serif; font-size: 18px; text-align:center;}\n' +
 'a {text-decoration:none; }';



    xinha_editors = Xinha.makeEditors(xinha_editors, xinha_config, xinha_plugins);
    Xinha.startEditors(xinha_editors);

}
function xinha_init_blog()
{
    if(!Xinha.loadPlugins(xinha_plugins, xinha_init_blog))
    {
        return;
    }
    var xinha_config = new Xinha.Config();
    xinha_config.width = '200px';
    xinha_config.hideSomeButtons(" htmlmode ");
    xinha_editors = Xinha.makeEditors(xinha_editors, xinha_config, xinha_plugins);
    Xinha.startEditors(xinha_editors);
}
function ltrim( val )
{

  var ptrn = /\s*((\S+\s*)*)/;
  return val.replace(ptrn, "$1");
  
}

function rtrim( val )
{

  var ptrn = /((\s*\S+)*)\s*/;
  return val.replace(ptrn, "$1");
  
}

function trim( val )
{

  return ltrim( rtrim( val ) );
  
}

function selectImage( id, item )
{
  if( item == undefined )
  {
    item = '';
  }
  else
  {
    item = '_'+item;
  }

  $("#selImage"+item).empty();
  $("#selImage"+item).html('<img src="http://'+window.document.location.hostname+'/upload/fotobank/'+id+'"  alt="'+id+'"/>');

}

function changeUpDownIndex( table, id, action )
{
  $.post(
    window.location.protocol+"//"+url+"/admin/index.php",
    { 
      s:    "ajax",
      ajax: "ajax",
      ev:   "chngeIndex", 
      ident: id,
      table: table,
      action: action
    },
    function ( data )
    {
      if( data != 'false' )
      {
        $('#index_'+id).html(data);
      }
    }
  );
}

function validateEmail(email)
{
    var reg = /(\w|\.+)@([a-z]|-|\d|_)+(\.[a-z]+){1,10}$/i;
 
    var result = reg.test(email);
 
    if(!result)
    {
        return false;
    }
    else
    {
        return true;
    }
}

function validateForm(id)
{
    if( $("#"+id).validate().form() )
    {
        $("#"+id).submit();
    }
    else
    {
        $("#errors").html(translates['fill_correctly']);
        $(".error").html(translates['fill_correctly']);
        //$("div.oth_errors").html(translates['fill_correctly']);
        //$("div.oth_errors").show();
        //$("#error_img").html(translates['fill_in_field']);
       // $("#error_bio").html(translates['fill_textarea']);
    }
}

function validateFormExt(id, div_error_tmplt)
{
    if( $("#"+id).validate().form() )
    {
        $("#"+id).submit();
    }
    else
    {
        $(div_error_tmplt).html(translates['fill_correctly']);
    }
}

function setFieldFromArray(values)
{
    var id;

    for( id in values )
    {
        $("#"+id).html(values[id]);
    }
}

function updateZebra(id, className)
{
    $("#" + id).nextAll().toggleClass(className);
}

function putGifLoader(id)
{
    //$("#"+id).html("<div style='text-align:center; vertical-align:middle;'><img src='http://" + url + "/admin/tpl/ocms/en/images/ajax-loader.gif' /><br />loading...</div>");
    $("#"+id).html("<div style='text-align:center; vertical-align:middle;'><img src='http://" + url + "/loadinfo.gif' /></div>");
}

function confirmDelete()
{
    return confirm( translates['confirm_delete'] );
}

function showVersionComment(version_number)
{
    $(".version_comment_div").hide();
    $("#version_comment_" + version_number).show();
}

function changeVersion(function_name, id, class_name)
{
    window.location = window.location.protocol+"//"+url+"/admin/" + function_name + "/ident/" + id + "/version/" + $("#version_number_prev").val() + "/" + class_name + ".htm";
}

/*
$(document).ready(function(){
    // ---- tablesorter -----
    $("#table_list").tablesorter({
        sortList:[[1,0]],
        widgets: ['zebra']
    });
    // ---- tablesorter -----
});
*/

function alertBox(text)
{
    
}

function wait()
{
    $('#wait_bg').fadeIn('slow');
}

function openWindow(value)
{

    $('#wait_bg').after(value);
    $('#blockWindowEdit').fadeIn('slow');
}

function closeWindow()
{
    $('#blockWindowEdit').fadeOut('slow');
    $('#blockWindowEdit').remove();
}

function stop_wait()
{
    $('#wait_bg').fadeOut('slow');
}

function cancelWin()
{
    closeWindow();
    stop_wait();
}

function toogleBlock(el)
{
    //    $(el+":parent > .content").toogle("slow");    
    //    $(el).parent().get(1).(" > .content").toogle("slow");    
    $($(el).parent().children().get(1)).toggle("slow");
}

$(function() {
    $(".blockTitle").attr("onclick", "toogleBlock(this);");      
});



