function showTooltip(itemID){
  // alert(itemID);
  // $('#item'+itemID).click();
  var targetElement = $('#item'+itemID);
  $('.memortaToolTip').remove();
    $('body').append('<div class="memortaToolTip"><div class="memortaToolTipHTML"></div><div class="memortaToolTipArrow"></div></div>');
    $('.memortaToolTipHTML').html(targetElement.find('.memortaToolTipContent').html());

    

if((targetElement.offset().top-$(window).scrollTop()) < ($('.memortaToolTip').height()+55)) { // below

  $('.memortaToolTip').addClass('below');
  var positionX = (targetElement.width()/2)-($('.memortaToolTip').width()/2)-20+targetElement.offset().left; // -20 because of padding
  var positionY = targetElement.height()+50+targetElement.offset().top;

} else { // above

  var positionX = (targetElement.width()/2)-($('.memortaToolTip').width()/2)-20+targetElement.offset().left; // -20 because of padding
  var positionY = 0-$('.memortaToolTip').height()-70+targetElement.offset().top;

}

    $('.memortaToolTip').css({top: positionY+"px",left: positionX+"px"}).fadeIn(200);
}

function showTooltipMap(memoID){
   // alert(memoID);
  // $('#item'+itemID).click();
  if(memoID == "current"){
    var targetElement = $('.currentMapMemo');
  } else {
  var targetElement = $('#memo_'+memoID);
  }
  $('.memortaToolTip').remove();
    $('body').append('<div class="memortaToolTip"><div class="memortaToolTipHTML"></div><div class="memortaToolTipArrow"></div></div>');
    $('.memortaToolTipHTML').html(targetElement.find('.memortaToolTipContent').html());

    

if((targetElement.offset().top-$(window).scrollTop()) < ($('.memortaToolTip').height()+55)) { // below

  $('.memortaToolTip').addClass('below');
  var positionX = (targetElement.width()/2)-($('.memortaToolTip').width()/2)-20+targetElement.offset().left; // -20 because of padding
  var positionY = targetElement.height()+50+targetElement.offset().top;

} else { // above

  var positionX = (targetElement.width()/2)-($('.memortaToolTip').width()/2)-20+targetElement.offset().left; // -20 because of padding
  var positionY = 0-$('.memortaToolTip').height()-70+targetElement.offset().top;

}

    $('.memortaToolTip').css({top: positionY+"px",left: positionX+"px"}).fadeIn(200);
}

function refreshGuestbook(){

     $.ajax({
      url: "/global/globalActions.php",
      dataType: "html",
      data: "dbObject=guestbook&dbAction=refreshGuestbook",
      type: "POST",
      cache: false,
      async: false,
      success: function(responseData) {
        $('#guestbookWrapper').html(responseData);
        // alert(responseData);

      }
    });
  
    $.ajax({
      url: "/global/globalActions.php",
      dataType: "html",
      data: "dbObject=guestbook&dbAction=refreshDecorations",
      type: "POST",
      cache: false,
      async: false,
      success: function(responseData) {
        
        $('#decorations').html(responseData);
        // alert(responseData);

      }
    });
  
}



function refreshImageGallery(){

     $.ajax({
      url: "/global/globalActions.php",
      dataType: "html",
      data: "dbAction=refreshImageGallery",
      type: "POST",
      cache: false,
      async: false,
      success: function(responseData) {
        $('#galleryWrapper').html(responseData);
        $("a.fancybox").fancybox();
        // alert(responseData);

      }
    });
  
}

function refreshVideoGallery(){

     $.ajax({
      url: "/global/globalActions.php",
      dataType: "html",
      data: "dbAction=refreshVideoGallery",
      type: "POST",
      cache: false,
      async: false,
      success: function(responseData) {
        $('#videoGalleryWrapper').html(responseData);
        $(".videoLink").fancybox({
            'width'        : 576,
            'height'      : 456,
            'overlayShow'    : false,
            'autoScale'       : false,
            'transitionIn'    : 'none',
            'transitionOut'    : 'none',
            'type'        : 'iframe',
            'titlePosition' : 'inside'
          });
        // alert(responseData);

      }
    });
  
}

/* ------------------------------------------------ MemoImages Slideshow ------------------------------------------------ */

/*
if($('.memoEditOverviewImageFrame div a').length > 1){
 
  $('.memoEditOverviewImageFrame').append('<span class="previous">previuos</span><span class="next">next</span>')
  $('.memoEditOverviewImageFrame .next').live('click', function(){
      memoImageSlideshowNext();
  });
  $('.memoEditOverviewImageFrame .previous').live('click', function(){
      memoImageSlideshowPrev();
  });
  // window.setInterval("memoImageSlideshowNext()", 4000);
}
*/
function memoImageSlideshowNext() {
  $('.memoEditOverviewImageFrame div a:last').fadeOut(500,function(){
      $(this).prependTo('.memoEditOverviewImageFrame div').show();
  })
}
function memoImageSlideshowPrev() {
  $('.memoEditOverviewImageFrame div a:first').hide().appendTo('.memoEditOverviewImageFrame div').fadeIn(500);
}


 
/* ------------------------------------------------ / MemoImages Slideshow ------------------------------------------------ */

  

/* ------------------------------------------------ vip Slideshow ------------------------------------------------ */

if($('.vipSlideshowWrapper ').length == 1){
 
  $('.vipSlideshowWrapper').append('<span class="previous">previuos</span><span class="next">next</span>')
  $('.vipSlideshowWrapper .next').live('click', function(){
      vipSlideshowNext();
  });
  $('.vipSlideshowWrapper .previous').live('click', function(){
      vipSlideshowPrev();
  });
  window.setInterval("vipSlideshowNext()", 8000);
}
function vipSlideshowNext() {
  var current = $('.vipSlideshowItem:last');
  $('.vipSlideshowItem:first').appendTo('.vipSlideshowWrapper').fadeIn();
  current.fadeOut();
}
function vipSlideshowPrev() {
  var current = $('.vipSlideshowItem:last').prependTo('.vipSlideshowWrapper');
  $('.vipSlideshowItem:last').fadeIn();
  current.fadeOut();
}


 
/* ------------------------------------------------ / vip Slideshow ------------------------------------------------ */  
  
  
  
  function scrollToCurrentMemo(memoID) {
      $('#mapContainer, #mapEdit').unbind('scroll');
  if(memoID!="current"){
  $('.memoMapScenarioRadio[value="'+$("#memo_"+memoID).attr('rel')+'"]').click();
  
  $('#mapGrass, #mapObjects, #mapMemos').removeClass('scenario1 scenario2 scenario3').addClass('scenario'+$("#memo_"+memoID).attr('rel'));
  $('.memortaToolTip').remove();
  $('#mapFullscreenLink').attr('href','memortaMapFullscreen.php?map='+$("#memo_"+memoID).attr('rel'));
} else {
  
  $('.memoMapScenarioRadio[value="'+$('.currentMapMemo').attr('rel')+'"]').click();
  
  $('#mapGrass, #mapObjects, #mapMemos').removeClass('scenario1 scenario2 scenario3').addClass('scenario'+$('.currentMapMemo').attr('rel'));
  $('.memortaToolTip').remove();
  $('#mapFullscreenLink').attr('href','memortaMapFullscreen.php?map='+$('.currentMapMemo').attr('rel'));
  
  }
   var divOffsetTop = $('#mapEdit').offset().top;
  var divOffsetLeft = $('#mapEdit').offset().left;
  
    var pOffsetTop = $(".currentMapMemo").offset().top - 200;
  var pOffsetLeft = $(".currentMapMemo").offset().left - 300;
  
    var pScroll1 = pOffsetTop - divOffsetTop;
  var pScroll2 = pOffsetLeft - divOffsetLeft;
  // alert($('#mapEdit').offset().top+'-'+$(".currentMapMemo").offset().top);
    $('#mapEdit').animate({scrollTop: '+=' + pScroll1 + 'px'}, 500);
    $('#mapEdit').animate({scrollLeft: '+=' + pScroll2 + 'px'}, 500, function(){
showTooltipMap(memoID);
      window.setTimeout("scrollHandler()", 1000);
      
});
}
  
  
  function scrollHandler(){
  $('#mapContainer, #mapEdit').scroll(function() {
  $('.memortaToolTip').remove();
  }); 
  }
  
  
  
function scrollToMemo(memoID){
      $('#mapContainer, #mapEdit').unbind('scroll');
  $('.memoMapScenarioRadio[value="'+$("#memo_"+memoID).attr('rel')+'"]').click();
  
  $('#mapGrass, #mapObjects, #mapMemos').removeClass('scenario1 scenario2 scenario3').addClass('scenario'+$("#memo_"+memoID).attr('rel'));
  $('.memortaToolTip').remove();
  $('#mapFullscreenLink').attr('href','memortaMapFullscreen.php?map='+$("#memo_"+memoID).attr('rel'));
  
  
  
  var divOffsetTop = $('#mapContainer').offset().top;
  var divOffsetLeft = $('#mapContainer').offset().left;
  
  var pOffsetTop = $("#memo_"+memoID).offset().top - ($('#mapContainer').height()/2);
  var pOffsetLeft = $("#memo_"+memoID).offset().left - ($('#mapContainer').width()/2);
  
  var pScroll1 = pOffsetTop - divOffsetTop;
  var pScroll2 = pOffsetLeft - divOffsetLeft;
  
    $('#mapContainer').animate({scrollTop: '+=' + pScroll1 + 'px'}, 500);
$('#mapContainer').animate({scrollLeft: '+=' + pScroll2 + 'px'}, 500, function(){
showTooltipMap(memoID);
  window.setTimeout("scrollHandler()", 1000);
});

    

}
  
  

function closeModalWindow(){
  $('#modalWindowOverlay, #modalWindow, .newDecoration').remove();
  $('#decoPlace').hide();
$('#decoSpacer').show();
}
function openModalWindow(){
  closeModalWindow();
  var scrollTopPosition = $(window).scrollTop()+100;
$('body').append('<div id="modalWindowOverlay"></div><div id="modalWindow" style="top:'+scrollTopPosition+'px;"></div>');
    $('#modalWindowOverlay').height($(document).height()).fadeTo(500, 0.5, function() {
          $('#modalWindow').show();
    $('#modalWindow input:first').focus();
      if($('#fileTitle').length){
         $('#modalWindow textarea').focus();
         }
      
      });
}
function closeModalMoreInfo(){
  //alert("hi");
  $('#modalWindowOverlay, #modalMoreInfo, .newDecoration').remove();
  $('#decoPlace').hide();
$('#decoSpacer').show();
}
function openModalMoreInfo(){
  //closeModalWindow();
  $('body').append('<div id="modalWindowOverlay"></div><div id="modalMoreInfo"></div>');
    $('#modalWindowOverlay').height($(document).height()).fadeTo(500, 0.5, function() {
          $('#organisationLabel').show();
          $('#modalMoreInfo').show();
          
      
      });
}
function refreshElement(contentSource,elementToRefresh){
 // alert("refreshElement="+elementToRefresh);
  $.ajax({
      url: contentSource,
      dataType: "html",
      data: "refreshElement="+elementToRefresh,
      type: "POST",
      cache: false,
      async: false,
      success: function(responseData) {
        // alert(responseData);

        $('#'+elementToRefresh).empty().append(responseData);
      }
  });
}

function createImageUploader(){
    var uploader = new qq.FileUploader({
                element: document.getElementById('chooseImagesButton'),
      uploadButtonText: 'Bilder&nbsp;auswählen',
      cancelButtonText: 'abbrechen',
      onComplete: function(id, fileName, responseJSON){
        
  // alert(responseJSON.error);
  
  
        
        // $('#gallery').append('<div class="galleryItem" rel="'+responseJSON['imageID']+'"><a rel="memoGallery" class="fancybox memoEditGalleryImage" href="'+responseJSON['filePath']+responseJSON['imagePath']+'"><span class="memoEditGalleryImageFrame"><div><img src="'+responseJSON['filePath']+responseJSON['thumbnailPath']+'" /></div></span></a><form name="deleteImage" class="deleteImage"><input type="hidden" name="modalWindowAction" value="delete" /><input type="hidden" name="modalWindowObject" value="memortaFile" /><input type="hidden" name="modalWindowObjectID" value="'+responseJSON['imageID']+'" /><a class="deleteMemortaFile openModalWindowForm">löschen</a></form><form name="editImage" class="editImage"><input type="hidden" name="modalWindowAction" value="edit" /><input type="hidden" name="modalWindowObject" value="memortaFile" /><input type="hidden" name="modalWindowObjectID" value="'+responseJSON['imageID']+'" /><a class="editMemortaFile openModalWindowForm">bearbeiten</a></form></div>');
        
  
         $('.qq-upload-success').fadeOut();
        refreshImageGallery();
        if($(".qq-upload-list li:not(.qq-upload-success)").length == 0){
        closeModalWindow();
        
        }

      },
      params: {
        fileType: 'memoImage'
      },
      action: '../global/fileUpload.php'
        });       
}
function createVideoUploader(){
    var uploader = new qq.FileUploader({
                element: document.getElementById('chooseVideosButton'),
      uploadButtonText: 'Videos&nbsp;auswählen',
      cancelButtonText: 'abbrechen',
      onComplete: function(id, fileName, responseJSON){
// alert(responseJSON);
    //  $('#videoGallery').append('<div class="galleryItem" rel="'+responseJSON['imageID']+'"><a target="_blank" class="memoEditGalleryImage" href="'+responseJSON['filePath']+responseJSON['imagePath']+'"><img src="/global/img/videoIcon.jpg" /></a><form name="deleteImage" class="deleteImage"><input type="hidden" name="modalWindowAction" value="delete" /><input type="hidden" name="modalWindowObject" value="memortaFile" /><input type="hidden" name="modalWindowObjectID" value="'+responseJSON['imageID']+'" /><a class="deleteMemortaFile openModalWindowForm">löschen</a></form></div>');
        
  
        $('.qq-upload-success').fadeOut();
        refreshVideoGallery();
        if($(".qq-upload-list li:not(.qq-upload-success)").length == 0){
        closeModalWindow();
        
        }
      },
      params: {
        fileType: 'memoImage'
      },
      action: '../global/fileVideoUpload.php'
        });       
}


function dom_init() {
  
  
  

  
  
  
  
  $('.sortableTable').dataTable({
        "sPaginationType": "full_numbers",
        "bLengthChange": true,
        "bFilter": false,
        "bAutoWidth": false,
        "aoColumns": [
              { "bSortable": false },
              { "bSortable": false },
              { "bSortable": false },
              { "bSortable": false },
                null,
                null,
                null,
                null,
                null
                ]
    });
  
  $('#changeSearch').click(function(){
      $('#searchForm').show();
      $('#searchResults').hide();
    return false;
  });

   

  $('.autoplayFalse').live('change',function(){
    if($(this).attr('checked')){
    var autoplayFalse=1;
    } else {
    var autoplayFalse=0;
    }

    $.ajax({
      url: "/global/globalActions.php",
      dataType: "html",
      data: "dbObject=memoSoundtrack&dbAction=edit&value="+autoplayFalse,
      type: "POST",
      cache: false,
      async: false,
      success: function(responseData) {
        // alert(responseData);

      }
    });
  });
  
  
  $('#memoTextBG,.gbItemCounterBG').css({opacity:0.7});
  
  $('.faqQuestion').click(function(){
  if($(this).hasClass('current')){} else {
  $('.faqQuestion').removeClass('current');
      $(this).addClass('current');
      $('.faqAnswer').slideUp();
      $(this).next('.faqAnswer').slideDown();
  }
  });
  
  
  $('#editMemoGallery .galleryItem').live('click', function(){
          
      closeModalWindow();
      $('body').append('<div id="modalWindowOverlay"></div><div id="modalWindow"></div>');
        $('#modalWindowOverlay').height($(document).height()).fadeTo(500, 0.5, function() {
          $('#modalWindow').show();
      });

    $.ajax({
      url: "/global/globalForms.php",
      dataType: "html",
      data: 'modalWindowAction=edit&modalWindowObject=memortaFile&modalWindowObjectID='+$(this).attr('rel'),
      type: "POST",
      cache: false,
      async: false,
      success: function(responseData) {
        // alert(responseData);
        $('#modalWindow').append(responseData);
      }
    });





  
  })
  
  
  $('#durationSelector').live('change',function(){
    if($(this).val()==1 || $(this).find('option:selected').text() == '4 Wochen - kostenlos'){
    $('#itemPayment').hide();
    } else {
      
    $('#itemPayment').show();
    }
  });

$('#usrCategory').live('change',function(){
    if($(this).val()==1){
    $('.showOnPayment').css({display:'none'});
    } else {
      $('.showOnPayment').css({display:'block'});
    }
  });
  
  $("a.fancybox").fancybox();


  $(".videoLink").fancybox({
    'width'        : 576,
    'height'      : 456,
    'overlayShow'    : false,
        'autoScale'       : false,
        'transitionIn'    : 'none',
    'transitionOut'    : 'none',
    'type'        : 'iframe'
  });
  
  $('.closeModalWindow').live('click', closeModalWindow);
  $('.closeModalMoreInfo').live('click', closeModalMoreInfo);

  $('.dataGroupEdit').live('click',function(){
    $('.dataGroup, .hiddenForm').removeClass('current').removeClass('deleteCurrent');
    $('.dataGroup .dataGroupForm, .hiddenForm .dataGroupForm').slideUp();
    $(this).parents('.dataGroup').addClass('current');
    $(this).parents('.dataGroup').find('.dataGroupForm').slideDown();
  });

  $('.dataGroupCancel').live('click',function(){
    $(this).parents('.dataGroup').removeClass('current');
    $(this).parents('.dataGroup').find('.dataGroupForm').slideUp();
    var formElement = $(this).parents('.ajaxForm');
    if(formElement.find('.refreshElement').length){

          var refreshElementArray = formElement.find('.refreshElement').val().split(',');
          for(i = 1; i < refreshElementArray.length; i++){
            refreshElement(refreshElementArray[0],refreshElementArray[i]);
    }
              }
    
    // window.location.reload();
  });  

/*
  $('.tabLink').live('click',function(){
    $('.tabLink, .tabContainer').removeClass('current');
    $(this).addClass('current');
    $($(this).children('a').attr('href')).addClass('current');
  if($(this).children('a').attr('href') == '#mapTabContainer'){
      scrollToCurrentMemo();
    }
    return false;
    

  });
*/
  
  $('.materialRadio').change(function(){
    $('#memoEditStone img').removeClass('current');
    $('#memoEditStone .stone'+$('.materialRadio:checked').val()).addClass('current');
  });
  
  

  $('.backgroundRadio').change(function(){
    $('#memoBGEdit img.background').removeClass('current');
    $('#memoBGEdit .background'+$('.backgroundRadio:checked').val()).addClass('current');
  });

  $('.memoMapScenarioRadio').change(function(){
    $('#mapGrass, #mapObjects, #mapMemos').removeClass('scenario1 scenario2 scenario3').addClass('scenario'+$('.memoMapScenarioRadio:checked').val());
    $('.memortaToolTip').remove();
    $('#mapFullscreenLink').attr('href','memortaMapFullscreen.php?map='+$('.memoMapScenarioRadio:checked').val());
  });



  $('.ajaxForm').live('submit',function(event) {
   // alert($(this).serialize());
    
    if($('#durationSelector').val()!=1 && $('#decoFormPayment').is(':hidden')){
      
      $('#decoFormPayment').slideDown();
      $('#decoFormLocationText').slideUp();
      
    } else {
    
    var formElement = $(this);
    formElement.find('.formErrorMessage').hide();
    
    var missingInput = 0;

    formElement.find('.mandatory').each(function(index) {
          if($(this).val() == ""){
        missingInput = 1;
      }
    });
    var missingInputYear = 0;
    if($(this).find('input[name="usrBirthYear"]').length){
      if($(this).find('input[name="usrBirthYear"]').val() != '' && $(this).find('input[name="usrBirthYear"]').val().length != 4){
        var missingInputYear = 1;
      }
    }
    if($(this).find('input[name="memoBirthYear"]').length){
      if($(this).find('input[name="memoBirthYear"]').val() == '' || $(this).find('input[name="memoBirthYear"]').val().length != 4 || $(this).find('input[name="memoPassingYear"]').val() == '' || $(this).find('input[name="memoPassingYear"]').val().length != 4){
        var missingInputYear = 1;
      }
    }
    if($(this).find('input[name="memoBirthYear"]').length){
      if($(this).find('input[name="memoBirthYear"]').val() > $(this).find('input[name="memoPassingYear"]').val()){
        var missingInputLifeDates = 1;
      }
    }  

    var missingUserGroup = 0;
      if(formElement.find('.userGroupRadio:checked').val() == 2){

      if($('input[name="memoPrivacyUserGroups[]"]').serialize() == ""){
        missingUserGroup = 1;
      }

    } else {
      missingUserGroup = 0;
    }
    

    if(missingInput == 1){
      alert('Bitte füllen Sie alle Felder, die mit einem * gekennzeichnet sind aus.');

    } else if(missingInputYear == 1){
      alert('Bitte achten Sie darauf, dass Jahreszahlen stets vierstellig sein müssen.');

    } else if(missingUserGroup == 1){
      alert('Bitte wählen Sie mindestens eine Benutzergruppe aus.');
    } else {

    
  
      $.ajax({
      url: "/global/globalActions.php",
      dataType: "html",
      data: $(this).serialize(),
      type: "POST",
      cache: false,
      async: false,
      success: function(responseData) {
        // alert('test');
       // alert(responseData);
        
        $('.newDecoration').removeClass('newDecoration');
        
        
        
        
        if(formElement.find('.memoSoundtrack').length){
          $('#currentYTSoundtrack').empty().append('<h3>Aktuell gewählter Soundtrack</h3><span style="display:block;padding-bottom:3px;font-size:11px;">'+formElement.find('.memoSoundtrackTitle').val()+'</span><div style="width:250px;height:25px;overflow:hidden;position:relative"><object width="250" height="150" style="position:absolute;bottom:0;left:0;"><param name="movie" value="http://www.youtube.com/v/'+formElement.find('.memoSoundtrack').val()+'?version=2&wmode=transparent&autoplay=0&rel=0&theme=light&loop=1&showinfo=0&disablekb=1&hd=1&autohide=0"></param><param name="wmode" value="transparent"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed wmode="transparent" src="http://www.youtube.com/v/'+formElement.find('.memoSoundtrack').val()+'?version=2&wmode=transparent&autoplay=0&rel=0&theme=light&loop=1&showinfo=0&disablekb=1&hd=1&autohide=0" type="application/x-shockwave-flash" allowfullscreen="true" width="250" height="150" allowscriptaccess="always"></embed></object></div><form class="deleteSountrackForm" name="deleteSountrackForm"><input type="hidden" value="delete" name="modalWindowAction"><input type="hidden" value="memoSoundtrack" name="modalWindowObject"><input class="openModalWindowForm submitInput" type="button" value="löschen"></form><br /><label><input type="checkbox" name="autoplayFalse" class="autoplayFalse" value="1"> Autoplay deaktivieren</label>');
          closeModalWindow();
        }
        if(formElement.find('.dbObject').val() == 'memoSoundtrack' && formElement.find('.dbAction').val() == 'delete'){
              $('#currentYTSoundtrack').empty();
        }
        
        if(formElement.find('.memoYTVideo').length){
          $('#currentYTVideo').empty().append('<h3>Aktuell gewähltes Video</h3><span style="display:block;padding-bottom:3px;font-size:11px;">'+formElement.find('.memoYTVideoTitle').val()+'</span><object width="258" height="195"><param value="http://www.youtube.com/v/'+formElement.find('.memoYTVideo').val()+'?version=3&wmode=transparent&rel=0&theme=light&showinfo=0&hd=1&autohide=0" name="movie"><param value="true" name="allowFullScreen"><param name="wmode" value="transparent"></param><param value="always" name="allowscriptaccess"><embed wmode="transparent" width="258" height="195" allowscriptaccess="always" allowfullscreen="true" type="application/x-shockwave-flash" src="http://www.youtube.com/v/'+formElement.find('.memoYTVideo').val()+'?version=3&wmode=transparent&rel=0&theme=light&showinfo=0&hd=1&autohide=0"></object><form class="deleteYTVideoForm" name="deleteYTVideoForm"><input type="hidden" value="delete" name="modalWindowAction"><input type="hidden" value="memoYTVideo" name="modalWindowObject"><input class="openModalWindowForm submitInput" type="button" value="löschen">');
          closeModalWindow();
          
        }
        if(formElement.find('.dbObject').val() == 'memoYTVideo' && formElement.find('.dbAction').val() == 'delete'){
              $('#currentYTVideo').empty();
        }
        
        
        if(formElement.find('.dbObject').val() == 'memo' && formElement.find('.dbAction').val() == 'delete'){
              $('.memo'+formElement.find('.dbObjectID').val()).remove();
        }
        
        if(formElement.find('.dbObject').val() == 'memoGuestbookItem' && formElement.find('.dbAction').val() == 'delete'){
              $('.item'+formElement.find('.dbObjectID').val()).remove();
        }
        if(formElement.find('.dbObject').val() == 'memoGuestbookItem' && formElement.find('.dbAction').val() == 'unlock'){
          $('.item'+formElement.find('.dbObjectID').val()).css({backgroundColor:'#ffffff'}).find('form.unlockGuestbookItem').remove();
        }
        
        if(formElement.find('.dbObject').val() == 'memortaAccount' && formElement.find('.dbAction').val() == 'delete'){
              self.location.href='/index.php?signOut=1'
        }
        if(formElement.find('.dbObject').val() == 'memortaFile' && formElement.find('.dbAction').val() == 'delete'){
          refreshImageGallery();
          refreshVideoGallery();
        }
        if(formElement.find('.dbObject').val() == 'memoFav' && formElement.find('.dbAction').val() == 'delete'){
          $('#fav'+formElement.find('.dbMemoID').val()).remove();
          $('.memoPreview:last').addClass('last');
        }
        

        
        if(responseData == 'refreshGuestbook'){
          refreshGuestbook();
          formElement.find('.closeForm').trigger('click');
        } else if(responseData == 'dateError'){
          alert('Der Geburtstag muss vor dem Todestag liegen.');
        } else if(responseData == 'missingInput'){
          formElement.find('.missingInput').slideDown(200);
        } else if (responseData == 'emailExists'){
          formElement.find('.emailExists').slideDown(200);
        } else if (responseData == 'usernameExists'){
          formElement.find('.usernameExists').slideDown(200);
        } else if (responseData == 'invalidEmail'){
          formElement.find('.invalidEmail').slideDown(200);
        } else if (responseData == 'badword'){
          formElement.find('.badword').slideDown(200);
        } else if (responseData == 'needsConfirm'){
          formElement.find('.needsConfirm').slideDown(200);
          formElement.find('.submitModalWindowForm').hide();
          formElement.find('.prevDeco').hide();
          refreshGuestbook();
        } else if (responseData == 'containsMemos'){
          formElement.find('.containsMemos').slideDown(200);
          formElement.find('.submitModalWindowForm').hide();
        } else {
          
          
          if($('#gallery').length){
          refreshImageGallery()
          }
          
          
          if(formElement.find('.refreshElement').length){
            
              var refreshElementArray = formElement.find('.refreshElement').val().split(',');
              for(i = 1; i < refreshElementArray.length; i++){
                refreshElement(refreshElementArray[0],refreshElementArray[i]);
              }  
            }
                  
                  
            if(formElement.find('.closeForm').length){
          formElement.find('.closeForm').trigger('click');
            }
            if(formElement.find('.successfulSubmit').length){
          $('.successfulSubmit').slideDown().delay(2000).slideUp();
            }
        }
        
      }
    });
    }
  } // duration selector != 1
    event.preventDefault();
  });
  








  $('.tellFriend').live('click',function(){

    openModalWindow();

    $.ajax({
      url: "/global/globalForms.php",
      dataType: "html",
      data: "modalWindowAction=tellFriend",
      type: "POST",
      cache: false,
      async: false,
      success: function(responseData) {
        // alert(responseData);
        $('#modalWindow').append(responseData);
      }
    });
    return false;
  });

    



    $('.chooseDecoLocation').live('click',function(){
      $('.memoTabLink:first').click(); // go to stone view
      
      if($('#durationSelector').val() > 1){
        
        if($('#termsAccepted').is(':checked')){
            var goAhead = 1;
        } else {
            var goAhead = 0;
            $('.acceptTerms').slideDown();
        }
      
      } else {
          var goAhead = 1;
      }
      
      if(goAhead == 1){
      
      if($('#itemName').val() != "" && $('#itemText').val() != "" && (($('#itemEmail').val() != "" && validateEmail($('#itemEmail').val())) || ($('#itemEmail').val() == ""))){
     
        
        
      $('#decoFormInputs').slideUp(); 
      $('#decoFormLocationText').slideDown();
      $('#modalWindowOverlay').hide();
      $('#decoPlace').show();
        $('#decoSpacer').hide();
      
      
      
      
      
      
      $("#decoPlace").click(function(e){
            $(".newDecoration").remove();
            $(".goAhead").show();
           
           
            
            var x = Math.round(e.pageX + document.getElementById('decorations').scrollLeft - $('#decorations').offset().left)-30;
            var y = Math.round(e.pageY + document.getElementById('decorations').scrollTop - $('#decorations').offset().top)-65;
            $('#positionX').val(x);
            $('#positionY').val(y);
            
        
        $("#decorations").append('<div class="decoration newDecoration memortaToolTipLink" style="background:url('+$('.itemTypeRadio:checked').next('img').attr('src')+') left top no-repeat;top:' + (y-20) + 'px;left:' + x + 'px;"><span class="memortaToolTipContent"><h3>'+$('#itemName').val()+'</h3><p>'+$('#itemText').val()+'</p></span></div>');
            $(".newDecoration").animate({"top": "+=20px"}, 100).animate({"top": "-=10px"}, 100).animate({"top": "+=10px"}, 100);
        // alert($('.itemTypeRadio:checked').next('img').attr('src'));
        
            /*
        $("#decorations").append('<div class="decoration '+decoration_class+' new_decoration" style="position:absolute;top:' + (y-20) + 'px;left:' + x + 'px;"></div>');
            $(".new_decoration").animate({"top": "+=20px"}, 400, "easeOutBounce");
            $("#submitButton").show();
        
        */
           });
        
        } else {
  
          $('.missingInput').slideDown();
          
  
      }
    }
      
      
      
      
    });

    $('.prevDeco').live('click',function(){
      $('#decoFormInputs').slideDown(); 
      $('#decoFormLocationText').slideUp();
      $(".goAhead").hide();
      $('#modalWindowOverlay').show();
      $('#decoPlace').hide();
      $(".newDecoration").remove();
      
    });










  /* ----------------------------------------------------  Privacy Tab  ---------------------------------------------------- */

  $('.optionGroupWrapper input').change(function(){
    if($('.userGroupRadio').attr('checked')){
      $('.privacyUserGroupWrapper').slideDown();
      $('.publicSettings').slideUp();
    } else if($('.publicRadio').attr('checked')){
      $('.publicSettings').slideDown();
      $('.privacyUserGroupWrapper').slideUp();
    } else {
      $('.privacyUserGroupWrapper').slideUp();
      $('.publicSettings').slideUp();
    }
  });

  /* ---------------------------------------------------- /Privacy Tab  ---------------------------------------------------- */
      
  /* ---------------------------------------------------- /ToolTip  ---------------------------------------------------- */


 $('.memortaToolTipLink').live('mouseenter',function(){

    $('.memortaToolTip').remove();
    $('body').append('<div class="memortaToolTip"><div class="memortaToolTipHTML"></div><div class="memortaToolTipArrow"></div></div>');
    $('.memortaToolTipHTML').html($(this).find('.memortaToolTipContent').html());

    

if(($(this).offset().top-$(window).scrollTop()) < ($('.memortaToolTip').height()+55)) { // below

  $('.memortaToolTip').addClass('below');
  var positionX = ($(this).width()/2)-($('.memortaToolTip').width()/2)-20+$(this).offset().left; // -20 because of padding
  var positionY = $(this).height()+50+$(this).offset().top;

} else { // above

  var positionX = ($(this).width()/2)-($('.memortaToolTip').width()/2)-20+$(this).offset().left; // -20 because of padding
  var positionY = 0-$('.memortaToolTip').height()-70+$(this).offset().top;

}

    $('.memortaToolTip').css({top: positionY+"px",left: positionX+"px"}).fadeIn(200);

  }).live('mouseleave',function(){

  $('.memortaToolTip').remove();

  });

  /* ---------------------------------------------------- /ToolTip  ---------------------------------------------------- */  
    
    

  /* ----------------------------------------------------  Modal Windows  ---------------------------------------------------- */

  $('.openModalWindowForm').live('click',function(){

    openModalWindow();

    $.ajax({
      url: "/global/globalForms.php",
      dataType: "html",
      data: $(this).parents('form').serialize(),
      type: "POST",
      cache: false,
      async: false,
      success: function(responseData) {
        // alert(responseData);
        $('#modalWindow').append(responseData);
        
      }
    });

  });
      
      
 $('.openModalMoreInfo').live('click',function(){

    openModalMoreInfo();

    $.ajax({
      url: "/global/globalForms.php",
      dataType: "html",
      data: $(this).parents('form').serialize(),
      type: "POST",
      cache: false,
      async: false,
      success: function(responseData) {
        //alert(responseData);
        $('#modalWindow').append(responseData);
        
      }
    });

  });

  



  /* ---------------------------------------------------- /Modal Windows  ---------------------------------------------------- */


    $('.hideGBText').live('change',function(){
    $(this).parents('form').submit();
    });

  
  

  /* ----------------------------------------------------  Multimedia  ---------------------------------------------------- */


  $('#addImagesButton').live('click',function(){

    openModalWindow();

    $.ajax({
      url: "/global/globalForms.php",
      dataType: "html",
      data: "modalWindowAction=add&modalWindowObject=memortaImage",
      type: "POST",
      cache: false,
      async: false,
      success: function(responseData) {
        // alert(responseData);
        $('#modalWindow').append(responseData);
        createImageUploader();
      }
    });
  });


  $('#addVideosButton').live('click',function(){

    openModalWindow();

    $.ajax({
      url: "/global/globalForms.php",
      dataType: "html",
      data: "modalWindowAction=add&modalWindowObject=memortaVideo",
      type: "POST",
      cache: false,
      async: false,
      success: function(responseData) {
        // alert(responseData);
        $('#modalWindow').append(responseData);
        createVideoUploader();
      }
    });
  });

  



  /* ---------------------------------------------------- /Multimedia  ---------------------------------------------------- */



    $('.memoTabLink').live('click',function(){
        $('.memortaToolTip').remove();
        $('#subNavigation .current').removeClass('current');
        $(this).parents('li').addClass('current');
        
        $('.memoTabContent').removeClass('current');
        $('#'+$(this).attr('rel')).addClass('current');
        return false;
    });


  $('.hideGBText').change(function(){
    if($(this).attr('checked')){
    var hide=1;
    } else {
    var hide=0;
    }
    var itemID=$(this).attr('rel');

    $.ajax({
      url: "/global/globalActions.php",
      dataType: "html",
      data: "dataType=guestbookItem&itemID="+itemID+"&action=hideText&hide="+hide,
      type: "POST",
      cache: false,
      async: false,
      success: function(responseData) {
        // alert(responseData);

      }
    });
  });




  



$("#mapGrass").click(function(e){
    //alert('test');
    var x = Math.round(e.pageX - this.offsetLeft + document.getElementById('mapEdit').scrollLeft - $('#mapEdit').offset().left) - 9;
    var y = Math.round(e.pageY - this.offsetTop + document.getElementById('mapEdit').scrollTop - $('#mapEdit').offset().top) -20;
    $('#memoX').val(x);
    $('#memoY').val(y);

  
      $(".currentMapMemo").css('top',(y-10) + 'px').css('left',x + 'px');
    $(".currentMapMemo").animate({"top": "+=10px"}, 200);
});



  $('#YouTubeAudioForm').submit(function(){
if($('#qAudio').val() != "") {
      // alert('test');
openModalWindow();
        $.ajax({
          url: '/global/getYouTubeAudio.php',
          data: $(this).serialize(),
          type: "POST",
          cache: false,
          async: false,
          success: function(data) {
          $('#modalWindow').css({'marginLeft' : '-280px', 'width' : '520px', 'position' : 'absolute', 'top' : '250px'}).append(data);
          // $('.resultAudio').html(data).show();
          // alert(data);
          }
        });
}
        return false;
      
    });
    $('#YouTubeVideoForm').submit(function(){
if($('#qVideo').val() != "") {
      // alert('test');
openModalWindow();
        $.ajax({
          url: '/global/getYouTubeVideo.php',
          data: $(this).serialize(),
          type: "POST",
          cache: false,
          async: false,
          success: function(data) {
          $('#modalWindow').css({'marginLeft' : '-310px', 'width' : '580px', 'position' : 'absolute', 'top' : '250px'}).append(data);
          // alert(data);
          }
        });
}
        return false;
      
    });




}
  
  

  
   
  
function validateEmail(elementValue){  
   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
   return emailPattern.test(elementValue);  
 }
   
   
   $('#quickSearchForm').submit(function(){
   
       if($('#quickSearchFirstName').val() != "" || $('#quickSearchLastName').val() != "" || $('#memoPassingMonthFrom').val() != "" || $('#memoPassingMonthTo').val() != "" || $('#memoPassingYearFrom').val() != "" || $('#memoPassingYearTo').val() != ""){
           return true;
      } else {
      return false;
  }
   
   });
