/**
 * Site specific javascript code.
 *
 * requires jquery to be already loaded
 */

/* ============================ image rotator ============================ */
var Rotator = new Object;

Rotator.operating = false;  // set to true if rotator is 'on'
Rotator.delay = 1500;       // time to transition from one image to the next
Rotator.duration = 4000;    // time a single image stays on view
Rotator.current = 0;
Rotator.nextSlide = -1;
Rotator.slides = new Array();
Rotator.timeoutId = 0;

Rotator.initiate = function() {

    Rotator.slides = $('#image_rotator .slide');
	if (Rotator.slides.length <= 1) {
	  // not enough slides to rotate ... abort
	  return;
	}
	
	// hide all but the initial slide
	for (var i=1; i < Rotator.slides.length; i++) {
	  $(Rotator.slides[i]).hide();
	}

    // set onClick handler to stop/start rotation
	$('#image_rotator').click( Rotator.toggle );	

	// start rotating
	Rotator.start();	
}

Rotator.start = function() {
    if (Rotator.timeoutId || Rotator.slides.length <= 1) return;
	
	Rotator.operating = true;
    Rotator.timeoutId = setTimeout(Rotator.transition, Rotator.duration);
}
Rotator.stop = function() {
    clearTimeout(Rotator.timeoutId);
	Rotator.timeoutId = 0;
	Rotator.operating = false;
}
/*
Rotator.transition = function() {
    Rotator.timeoutId = 0;

    Rotator.nextSlide = Rotator.current+1;
    if (Rotator.nextSlide >= Rotator.slides.length) {
	    Rotator.nextSlide = 0;
    }

    $(Rotator.slides[Rotator.nextSlide]).show();
    $(Rotator.slides[Rotator.current]).fadeOut(Rotator.delay, function() {
        $(Rotator.slides[Rotator.nextSlide]).css('position','relative');
        $(Rotator.slides[Rotator.current]).css('position','static');
	  
        Rotator.current = Rotator.nextSlide;
        if (Rotator.operating) {
		  Rotator.start();
		}
	});
}
 */
Rotator.transition = function() {
	Rotator.timeoutId = 0;

    Rotator.nextSlide = Rotator.current+1;
    if (Rotator.nextSlide >= Rotator.slides.length) {
	    Rotator.nextSlide = 0;
    }

//	var currentSlide = Rotator.slides[Rotator.current];
//	var nextSlide = Rotator.slides[Rotator.nextSlide];
	
	$(Rotator.slides[Rotator.nextSlide]).addClass('next_slide').show().animate({ left:"0" }, 'slow', function() {
		$(Rotator.slides[Rotator.current]).hide();
		$(Rotator.slides[Rotator.nextSlide]).removeClass('next_slide').css('left','');
		
		$('#project_tabs .current').removeClass('current');
		$('#project_tabs #tab-'+(Rotator.nextSlide+1)).addClass('current');
	  
       	Rotator.current = Rotator.nextSlide;
	    if (Rotator.operating) {
		  Rotator.start();
		}
	});
}
Rotator.toggle = function() {
  if (Rotator.operating) {
    Rotator.stop();
  } else {
    Rotator.start();
  }
}
Rotator.advance = function() {
  var operating = Rotator.operating;
  if (operating) Rotator.stop();
  Rotator.transition();
  if (operating) Rotator.start();
}
/* =========================== formlet control ============================ */

/* ============================ document ready ============================ */
$(document).ready( function() {
	// set up new window/tab for rel=external links
	$('a[rel~=external]').attr('target','_blank');
	
	// set up link transitions
	$('.go_link').click(function(e) {
       
	});

	// set up event handlers for login formlets
	$('#commentform input.text').
	  each ( function(i) { 
					  this.label = $(this).parent().children('label').get(0);
					  if (this.value) {
					    $(this.label).hide();
					  }
	  }).
	  focus( function(e) { $(this.label).hide(); }).
	  blur( function(e) { if (!this.value) $(this.label).show(); });
	
	$('#commentform label').
	  click( function(e) { $(this).hide(); $('#'+this.htmlFor).focus(); });

    // set up image rotator (home page only)
	if ($('#image_rotator').length) {
	  $.getJSON(nx_ajaxpath+'/image_rotator.ajax.php?t='+$('#image_rotator').attr('class'), function(json) {
	      if (json.html) {
			  $('#image_rotator').append(json.html).find('a[rel~=external]').attr('target','_blank');
		  }
          Rotator.initiate();
      });
	  $('.next').click(function(e) { 
		  Rotator.advance();
		  e.stopPropagation();
	  });
	}

    if ($('#map').length) {
      $("#map").jmap("init", {
                       "mapType":"map",
                       "mapCenter":[40.75968, -73.98120],
                       "mapZoom": 15,
                       "mapControl": "none",
                       "mapEnableDragging": false,
                       "mapEnableScaleControl" : false
                       });
       $("#map").jmap("AddMarker", {
                       "pointLatLng": [40.75968, -73.98120],
                       "pointHTML" : "<strong>Nexops US Headquarters</strong><br />1285 Avenue of Americas<br />New York, NY 10019"
                       });
    }
	
});
