if (!window.DED) {
  DED = {};
}
(function() {
  var Y = YAHOO.util,
      Event = Y.Event,
      Dom = Y.Dom;
  var onTour = false;
  var getRandomPoint = function() {
  		return [ Math.floor(Math.random() * Dom.getViewportWidth()), 
  		Math.floor(Math.random() * Dom.getViewportHeight()) ];
  };
  var showTour = function(e, o) {
  		Event.stopEvent(e);
  		if ( onTour === true ) {
  			return;
  		}
  		onTour = true;
  		var bee = document.createElement('div');
  		var msg = document.createElement('div');
  		Dom.addClass(msg, 'tour-msg');
  		var x = Dom.getViewportWidth()-50;
  		var y = Dom.getViewportHeight()-50;
  		var counter = 0;
  		var current = [];
  		Dom.setStyle(bee, 'position', 'absolute');
  		Dom.setXY(bee, [0, 0]);
  		bee.id = 'buzzy';
  		Dom.get('tour').appendChild(msg);
  		Dom.get('tour').appendChild(bee);
  		var fn = function() {
  			current = o[counter];
  			var endPoint = Dom.getXY(current.id);
  			var attr = {
  				points : {
  					to : endPoint,
  					control : [getRandomPoint(), getRandomPoint(), getRandomPoint()]
  				}
  			};
  			var a = new Y.Motion(bee, attr, 2, YAHOO.util.Easing.easeOut);
  			a.onStart.subscribe(
  				function() {
  					msg.style.visibility = 'hidden';
  				}
  			);
  			a.onComplete.subscribe(
  				function() {
  					counter++;
  					Dom.setXY(msg, [endPoint[0]+50, endPoint[1]+30]);
  					msg.innerHTML = '<p>'+current.msg+'</p>';
  					msg.style.visibility = 'visible';
  				}
  			);
  			if ( counter < (o.length)-1 ) {
  				window.setTimeout(arguments.callee, 10000);	
  			}
  			else {
  				onTour = false;
  				Dom.get('tour').innerHTML = '';
  			}
  			a.animate();
  		}();
  	};
  	
  	
  	
  	var tourStops = [
			{ id: "rss", msg: "Hi there, I'm your Tour Guide, Buzzy! I'm going to show you around, so just sit back and follow me..." },
			{ id: "main", msg: "Over here is the featured (and latest) article. You can't miss it." },
			{ id: "recent", msg: "Then right here you'll see the titles of some of the other recent entries." },
			{ id: "dailies", msg: "This is my bookmark list. They lead off to other interesting sites on the net that I like. It's updated almost daily." },
			{ id: "book", msg: "This is the book I wrote called 'JavaScript Design Patterns.' Read more of the details if you're interested." },
			{ id: "popular", msg: "Here are some of the most popular well-known articles I've written here." },
			{ id: "rss", msg: "Here you can change font sizes, and also change the contrast if you click the link above me." },
			{ id: "screencasts", msg: "These are my video tutorials. Listen, watch, and learn." },
			{ id: "prototypes", msg: "These are links to various interaction prototype mockups. Nothing special" },
			{ id: "flickr", msg: "Lastly, these are some photos pulled from my Flickr. Click the thumbnails to see a larger version." },
			{ id: "", msg: "" }
			
		];
		var $cookie = jimAuld.utils.cookieLib;
		// get, set, del
		function clearCookie() {
		  if ( $cookie.get('contrast') ) {
		    $cookie.del('contrast');
		  }
	  }
		var toggleContrast = function(e) {
		  Event.preventDefault(e);
		  var b = document.body;
		  if ( Dom.hasClass(b, 'contrast') ) {
		    clearCookie();
		    $cookie.set('contrast', 'false', 9999999);
		    Dom.removeClass(b, 'contrast');
	    } else {
	      clearCookie();
	      $cookie.set('contrast', 'true', 9999999);
	      Dom.addClass(b, 'contrast');
      }
		};
		var sizes = ['normal', 'large', 'larger'];
    var scaleBridge = function(e) {
      Event.preventDefault(e);
      var which = this.id.split('-')[1];
      scale(which);
      
    };
    var scale = function(i) {
      var size = sizes[i];
      var b = document.body;
      Dom.replaceClass(b, 'normal', size);
      Dom.replaceClass(b, 'large', size);
      Dom.replaceClass(b, 'larger', size);
    };
		Event.on('tour-bee', 'click', showTour, tourStops);
		Event.on('contrast', 'click', toggleContrast);
		
		Event.on('scale-0', 'click', scaleBridge);
		Event.on('scale-1', 'click', scaleBridge);
		Event.on('scale-2', 'click', scaleBridge);
		Event.onDOMReady(function() {
      if ( $cookie.get('contrast') == 'true' ) {
		    Dom.addClass(document.body, 'contrast');
		  }
	  });

	  var SearchPlay = function(el) {
    	var el = Dom.get(el);
    	var orig = el.defaultValue;
    	var f = function() {
    		if ( el.value == orig ) {
    			el.value = '';
    		}
    	};
    	var b = function() {
    		if ( el.value == '' ) {
    			el.value = orig;
    		}
    	};
    	Event.on(el, 'focus', f);
      Event.on(el, 'blur', b);
    };
    function openLightBoxBridge (e) {
      Event.preventDefault(e);
      var target = Event.getTarget(e, true);
      DED.LightBox.open(this, target);
    }
    
    DED.LightBox = {
      open: function(element, target) {
        var href = element.href;
        if ( target.tagName.toLowerCase() === 'img' ) {
          var bd = document.getElementsByTagName('body')[0];
          var top = (document.documentElement.scrollTop ? 
            document.documentElement.scrollTop :
            document.body.scrollTop);
          var lightbox = Dom.get('lightbox');
          var overlay = Dom.get('overlay');
          Dom.setStyle(overlay, 'width', Dom.getDocumentWidth()+'px');
          Dom.setStyle(overlay, 'height', Dom.getDocumentHeight()+'px');
          
          Dom.setStyle(lightbox, 'top', top+50+'px');
          var img = element.getAttribute('rel');
          lightbox.innerHTML = '<p class="view-photo"><a target="_blank" href="'+ href + '">view photo on flickr</a> | ' + 
          '<a href="#close" onclick="return DED.LightBox.closeBridge();">close [x]</a></p>' + 
          '<p><img id="flickr-photo" onload="DED.LightBox.loaded()" src="'+img+'" alt="" /></p>' + 
          '<p class="photo-title">'+element.getAttribute('title')+'</p>';
          
        }
      },
      close: function() {
        Dom.removeClass(document.getElementsByTagName('body')[0], 'lightbox');
      },
      closeBridge: function(e) {
        DED.LightBox.close();
        return false;
      },
      loaded: function() {
        
        Dom.setStyle('lightbox', 'visibility', 'hidden');
        Dom.addClass(document.body, 'lightbox');
        var w = parseInt(Dom.get('flickr-photo').offsetWidth);
        Dom.setStyle('lightbox', 'left', Dom.getViewportWidth()/2 - (w/2) + 'px');
        Dom.setStyle('lightbox', 'visibility', 'visible');
      }
    };
    var overlay = Dom.get('overlay');
    var lightbox = Dom.get('lightbox');
    Dom.setStyle(overlay, 'width', Dom.getViewportWidth()+'px');
    var lightboxes = Dom.get('flickr').getElementsByTagName('a');
    Event.on(lightboxes, 'click', openLightBoxBridge);
    
	  Event.onAvailable('NTM_Login_UserName', SearchPlay, 'NTM_Login_UserName');
	  Event.onAvailable('NTM_Login_Password', SearchPlay, 'NTM_Login_Password');
	  Event.onAvailable('Email', SearchPlay, 'Email');
})();