document.observe('dom:loaded', function(){init()});

function init() {
  // This function should exist (with sensible namespacing) in each file, and be added to Window.onload using the Prototype helper
  // Call setup functions here
  
	$$('#status .login').each(function(link) {
    link.observe('click', function(e) {
      e.preventDefault();
      LoginWindow.show({
        onLogin: function() {
          window.location.reload();
        }
      });
    })
  });
	
	setupCarousel();
}

function insertDashboardTab() {
	$('tabs').insert('<li><a href="/dashboard">Dashboard</a></li>', {position: 'bottom'});

}
function redirectToLogin() {
    // This gets called by the dashboard controller when xhtmlhttprequest fails.
    window.location = "/dashboard/login?e=1"
}

/* General-purpose utility functions */

	function select_object(target) { //This takes any DOM node as a parameter, and will highlight/select the contents thereof
		if (typeof(target) == 'string') target = $(target);
		if (!target) return false;
		if (document.selection) {
			var div = document.body.createTextRange();
			div.moveToElementText(target);
			div.select();
		} else {
			var div = document.createRange();
			div.setStartBefore(target);
			div.setEndAfter(target) ;
			window.getSelection().addRange(div);
		}
	}

function confirm_delete_search() {
  if (confirm("Are you sure you want to delete this search?")) {
    return true;
  }
  return false;
  
}

function setupCarousel() {
  var carousel = $('carousel');

  if (carousel == null) return;
  if (typeof(carousel) == null) return; 
  
  carousel.addClassName('carousel_js'); // For the JS-specific styles in DNZ_HOME.css
  //Vars
  var carousel_left = $('carousel_left');
  var carousel_right = $('carousel_right');
  var carousel_slider = $('carousel_slider');
  var carousel_port = $('carousel_port');
  var count = carousel_slider.childElements().length;
  var number = count / 2;

  var stationary = true;

 //Events
  carousel_left.observe('click', exposeLeft);
  carousel_right.observe('click', exposeRight);

  //Functions
  function exposeRight(ev) { // Moves the ribbon to the left, exposing more on the right
   if (stationary) {
     stationary = false;
     if (current != count && current != (count - 2)) {
       current ++;
       new Effect.Move(carousel_slider, {x: -512, y: 0, duration: 0.05 });
       // carousel_left.style.backgroundPosition="left top";
       // carousel_left.style.backgroundPosition="";
        if (current == count) {
          carousel_right.style.backgroundPosition="left top";
        }
     } else if (current == (count - 2)) {
       carousel_slider.style.left = (carousel_slider.offsetLeft + (number * 512)) + 'px';
       current = current - number;
       current ++;
       new Effect.Move(carousel_slider, {x: -512, y: 0, duration: 0.05 });
       // carousel_left.style.backgroundPosition="left top";
       // carousel_left.style.backgroundPosition="";

     }
     carousel.focus();
     stationary = true;
     ev.stop();
   }
  }

  function exposeLeft(ev) { // vice-versa
   if (stationary) {
     stationary = false;
     if (current > 2) {
       current --;
       new Effect.Move(carousel_slider, {x: 512, y: 0, duration: 0.05 });
       // carousel_right.style.backgroundPosition="right top";
       // carousel_right.style.backgroundPosition="";
       if (current == 1) {
          carousel_left.style.backgroundPosition="-300px top";
        }
     } else if (current == 2) {
       carousel_slider.style.left = (carousel_slider.offsetLeft - (number * 512)) + 'px';
       current = current + number;
       current --;
       new Effect.Move(carousel_slider, {x: 512, y: 0, duration: 0.05 });
       // carousel_right.style.backgroundPosition="right top";
       // carousel_right.style.backgroundPosition="";
     }
     carousel.focus();
     stationary = true;
     ev.stop();
   }
  }
}













