var addreloaded = function (data)
    {
      $('#advertuna_adv').hide().html(data.data).fadeIn("slow");
      rebind();
    };

var nothing = function (data)
{
}

function adv_click(e)
{
  var url = this.getAttribute("url");
  var addid = this.getAttribute("addid");
  var block = this.getAttribute("block");
  var time_to_show = this.getAttribute("tts");
       
  window.open(url);
  $.ajax({
        type: 'POST', 
        url: '?q=advertuna/click',
        dataType: 'json',
        success: nothing,
        data: "js=1&block=" + block + "&addid=" + addid   
      });
}

function nextadd ()
{
  block = 0;
  $.ajax({
        type: 'POST', 
        url: '?q=advertuna/getadd',
        dataType: 'json',
        success: addreloaded,
        data: "js=1&block=" + block   
      });
}
    


function adv_slot_details(e)
{
  var sid = this.getAttribute('sid');
  var aid = this.getAttribute('aid');  
  var name = this.getAttribute('name');
  if(name=="slot_details") window.location='?q=adveruna/slotdetails/'+sid;
  if(name=="slot_adds") window.location='?q=adveruna/slotadds/'+sid;
  if(name=="slot_reports") window.location='?q=adveruna/slotreports/'+sid;
  if(name=="slot_delete") adv_confirm_slot_delete(sid);
  if(name=="slot_add") window.location='?q=adveruna/slotadd/';    
  if(name=="add_add") window.location='?q=adveruna/addadd/'+sid;
  if(name=="add_edit") window.location='?q=adveruna/addedit/'+sid;
  if(name=="add_report") window.location='?q=adveruna/addreport/'+sid;
  if(name=="add_delete") adv_confirm_add_delete(sid,aid);
}

function adv_confirm_slot_delete(sid)
{
  answer=confirm("are you sure?");
  if(answer)
  {
    window.location='?q=adveruna/slotdelete/'+sid;
  }
  
}

function adv_confirm_add_delete(sid,aid)
{
  answer=confirm("are you sure?");
  if(answer)
  {
    window.location='?q=adveruna/adddelete/'+sid+'/'+aid;
  }
}


function rebind ()
{
  $('#advertuna_adv').bind("click",adv_click);
  $('.slot_details').bind("click",adv_slot_details);
  $('.add_details').bind("click",adv_slot_details);
  var timetoshow = 5; 
  timetoshow = $('#advertuna_adv').attr('tts');
  timetoshow *=1000; //we think inseconds java thinks in miliseconds
  setTimeout("nextadd();",timetoshow);
}
    
if (Drupal.jsEnabled) 
{
  $(document).ready(function () {
    rebind();
    $('#edit-add-code').bind("click",advertuna_imce);
  });
}

var advertuna_imce = function ()
{
  imcePopup = window.open(Drupal.settings.advertuna.basePath + '/?q=imce', 'advertuna_imce', 'width=760,height=560,resizable=1'); 
}

function advertuna_imceImceFinish(path, w, h, s, imceWin) {
      imceWin.close();
      $("#edit-add-code").val(path);
     
  }