(function (){
  var astoria = window.astoria ={};
  var _init_tasks = [];

  astoria.initialize = function (fn){
    if (typeof fn === 'function') {
      _init_tasks.push( fn );
    }
  }


  astoria.init_tinymce = function() {
    tinyMCE.init({
  		mode : "specific_textareas", 
      editor_selector:"mceEditor",
      theme : "advanced",
			dialog_type : "modal",
	    width: 640, height: 400,
      theme_advanced_toolbar_location : "top",
      theme_advanced_toolbar_align : "left",
      theme_advanced_statusbar_location : "bottom",
      theme_advanced_resizing_use_cookie : false,
      theme_advanced_buttons1: 'bold,italics,underline,strikethrough,|,cut,copy,paste,pastetext,pasteword,|,justifyleft,justifycenter,justifyright, justifyfull,|,bullist,numlist,|,undo,redo,|,link,unlink,',
      theme_advanced_buttons2: 'formatselect,fontselect,fontsizeselect,|,link,unlink,anchor,image,cleanup,help,code',
      theme_advanced_buttons3:'',
      relative_urls:false,
      Archiv_settings_file : "config.php",
      plugins: 'advimage,advlink,Archiv'
    });
  }
  if ( $('textarea.mceEditor').length )  {
    astoria.init_tinymce();
  }

  $('.btnBack').click(function (){
    history.go(-1); return false;
  });



  // Delete Content
  astoria.Content = (function(){
    return {
      remove: function (href, id, elem){
        var tr = $(elem).parents('tr');
        if ( confirm('Are you sure you want to delete this Content?') ) {
          $.post( href, {id:id}, function (data){
            $(tr).fadeOut('slow');
          });
        }
      }
    }
  })();


  astoria.IForm = (function(frm){
    if (! $(frm).get(0) ) return false;
    var opts = $.extend({
      onError: null,
      onSuccess: null,
      beforeSubmit: null,
      afterSubmit: null 
    }, arguments[1]||{});

    // create the iframe
    var ifrname = 'ifrtgt' + (new Date().getTime());
    var ifr = $('<iframe name="'+ifrname+'" src="#" class="frmiframe"></iframe>');
    ifr.insertAfter( frm );
    $(frm).attr('target', ifrname);
    opts.frm = frm; opts.ifr = ifr;

    $(ifr).bind('load',
      (function(opts){
        return function (){
          var doc = this.contentWindow.document.documentElement;
          var cont = doc.textContent || doc.outerText;
          var resp = $.evalJSON(cont);
          if (typeof opts.afterSubmit == 'function') {
            opts.afterSubmit(cont, resp);
          } 
        }
      })(opts)
    );
  });

  
/*
  // look for form that needs an iframe
  astoria.initialize(function(){
    var frms = $('form[rel^=upload]');
    frms.each(function(idx, frm){
      var ifrname = '_ifrtgt' + (new Date().getTime()) +  idx;
      var ifr = $(frm).after('<iframe name="'+ifrname+'" src="#" class="frmiframe"></iframe>').next().get(0);
      $(this).attr('target', ifrname);

      $(ifr).bind('load', function(){
        
      });
    })
  });
*/
  astoria.initialize(function(){
    $('a[rel^=lightbox]').lightBox();
  });

  astoria.flashMsg = function (msg,dur) {
    dur = dur ? dur : 10;
    var span = $('<span class="fmsg"></span>').html(msg);
    span.prependTo('.flashmsg');
    setTimeout( (function(sp){
      return function (){
        $(sp).fadeOut(3000); 
      }
    })(span), dur * 1000);
  }

  $(document).ready(function (){
    $(_init_tasks).each( function (){
      this.call();
    });
  });
  
  
  $(document).ready(function(){
	  $("a[rel^='prettyPhoto']").prettyPhoto({
      allowresize:false,
      showTitle:false,
      theme:'light_square'
    });
	});


  astoria.randomTestimonials = function (){ 
    $('a#randomtesti').trigger('click');
    return false;
  }
  
  
})();
