// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function createAllDraggables() {
  var logoDraggables = [];
  for (var i = 1; i < 82; i++) {
    logoDraggables[i-1] = new Draggable('slot_'+i, {handle:'handle_'+i, revert:true, reverteffect:function(element, top_offset, left_offset) 
    { 
      new Effect.MoveBy(element, -top_offset, -left_offset, {duration:0});
    }
    })
  }
}

function destroyDraggable(idx) {
  idx.destroy();
}

function createAllUserDroppables(auth_token) {
  //Droppable for trash
  Droppables.add('trash', {accept:['bcontainer'], droponempty:true, onDrop:function(drag,drop) {
    Element.update('statusMessages', '');
    Element.show('loader');
    Element.hide(drag);
    new Ajax.Request('/logos/delete_bookmark', {asynchronous:true, evalScripts:true, parameters:'drag_id=' + encodeURIComponent(drag.id) + '&authenticity_token=' + encodeURIComponent(auth_token)});
  }});

  //Droppables for logos
  for(var i=1; i < 82; i++) {
    Droppables.add('slot_'+i, {accept:['bcontainer','logoSearchResult'], droponempty:true, onDrop:function(drag,drop) {
      Element.update('statusMessages', '');
      Element.show('loader');
      Element.hide(drag);
      Element.hide(drop);
      new Ajax.Request('/logos/switch', {asynchronous:true, evalScripts:true, parameters:'drop_id=' + encodeURIComponent(drop.id) + '&drag_id=' + encodeURIComponent(drag.id) + '&authenticity_token=' + encodeURIComponent(auth_token)});
 }});
  }
}

function createAllGuestDroppables(auth_token) {
  //Dropable for trash
  Droppables.add('trash', {accept:['bcontainer'], droponempty:true, onDrop:function(drag,drop) {
    Element.update('statusMessages', '<span class="notice">Please login to save your changes.</span>');
    var drag_id = $(drag).id;
    var drop_id = $(drop).id;
    var drag_arr = drag_id.split('_');
    var drop_arr = drop_id.split('_');
    var drag_slot_no = drag_arr[1];
    var drop_slot_no = drop_arr[1];
    Element.hide('rss_'+drag_slot_no);
    Element.hide('handle_'+drag_slot_no);
    //new
    $('link_'+drag_slot_no).href = 'http://vizurl.com';
    $('image_'+drag_slot_no).src = '/images/logos/0.png';
    $('image_'+drag_slot_no).alt = 'vizurl.com';
    $('image_'+drag_slot_no).title = 'vizurl.com';
  }});

  //Droppables for logos
  for(var i=1; i < 82; i++) {
    Droppables.add('slot_'+i, {accept:['bcontainer','logoSearchResult'], droponempty:true, onDrop:function(drag,drop) {
      Element.update('statusMessages', '<span class="notice">Please login to save your changes.</span>');
      var drag_id = $(drag).id;
      var drop_id = $(drop).id;
      var drag_arr = drag_id.split('_');
      var drop_arr = drop_id.split('_');
      var drag_slot_no = drag_arr[1];
      var drop_slot_no = drop_arr[1];
      var tmp_drag =  [];
      var tmp_drop =  [];
      tmp_drop[0] = $('image_'+drop_slot_no).src;
      tmp_drop[1] = $('image_'+drop_slot_no).alt;
      tmp_drop[2] = $('image_'+drop_slot_no).title;
      tmp_drop[3] = Element.visible('rss_'+drop_slot_no);
      if (drag_arr[0] == 'slot' && drop_arr[0] == 'slot') {
        tmp_drag[0] = $('image_'+drag_slot_no).src;
        tmp_drag[1] = $('image_'+drag_slot_no).alt;
        tmp_drag[2] = $('image_'+drag_slot_no).title;
        tmp_drag[3] = Element.visible('rss_'+drag_slot_no);
        //Swapping logos inside tab
        if ($('image_'+drop_slot_no).alt == '' || $('image_'+drop_slot_no).alt == 'vizurl.com') {
          //Dropped on an empty slot.
          if ( tmp_drag[3] == true) {
            Element.show('rss_'+drop_slot_no);
          } else {
            Element.hide('rss_'+drop_slot_no);
          }
          Element.hide('rss_'+drag_slot_no);
          Element.hide('handle_'+drag_slot_no);
          Element.show('handle_'+drop_slot_no);
        } else {
          //Swapping existing logos.
          if ( tmp_drag[3] == true) {
            Element.show('rss_'+drop_slot_no);
          } else {
            Element.hide('rss_'+drop_slot_no);
          }
          if ( tmp_drop[3] == true) {
            Element.show('rss_'+drag_slot_no);
          } else {
            Element.hide('rss_'+drag_slot_no);
          }
        }
          //new begins
          $('link_'+drop_slot_no).href = 'http://'+tmp_drag[1];
          $('image_'+drop_slot_no).src = tmp_drag[0];
          $('image_'+drop_slot_no).alt = tmp_drag[1];
          $('image_'+drop_slot_no).title = tmp_drag[2];
          $('link_'+drag_slot_no).href = 'http://'+tmp_drop[1];
          $('image_'+drag_slot_no).src = tmp_drop[0];
          $('image_'+drag_slot_no).alt = tmp_drop[1];
          $('image_'+drag_slot_no).title = tmp_drop[2];
      } else if (drag_arr[0] == 'search' && drop_arr[0] == 'slot') {
        //Dragging logo from search box.
        //var logo_id = drag_slot_no;
        var tmp_logo = [];
        tmp_logo[0] = $('search_'+drag_slot_no).src;
        tmp_logo[1] = $('search_'+drag_slot_no).alt;
        tmp_logo[2] = $('search_'+drag_slot_no).title;
        //new
        $('link_'+drop_slot_no).href = 'http://'+tmp_logo[1];
        $('image_'+drop_slot_no).src = tmp_logo[0];
        $('image_'+drop_slot_no).alt = tmp_logo[1];
        $('image_'+drop_slot_no).title = tmp_logo[2];
        Element.show('handle_'+drop_slot_no);
      }
   }});
  }
}
function goUrl(url){
  if (url == '') {
    window.open('/');
    //document.location.href='/';
  } else {
    window.open('http://'+url);
    //document.location.href='http://' + url;
  }
}
function checkBrowser() {
  if (Prototype.Browser.IE) {
    var ua      = navigator.userAgent;
    var offset  = ua.indexOf('MSIE ');
    var version = parseFloat(ua.substring(offset+5, ua.indexOf(';', offset)));
  }
  if (version < 7) {
    document.location.href='/upgrade.html';
  }
}

function bodyOnLoad() {
  $('loginLogout').style.display='block';
}

function openIdLogin(url) {
  $('openid_url').value = url;
  $('login_form').submit();
}
