// © Copyright Kenny Grant 2007 
// Released under the BSD licence

/* EXTRA EFFECTS */

// Would be nice to have a slideleft and slideright as well







// still to do - dropin
Effect.DropDown = function(element) {
  element = $(element);
  var oldStyle = {
    top: element.getStyle('top'),
    left: element.getStyle('left'),
    opacity: element.getInlineOpacity() };
  return new Effect.Parallel(
    [ new Effect.BlindDown(element), 
      new Effect.Opacity(element, { sync: true, from: 0.1, to: 1.0 }) ],
    Object.extend(
      { duration: 1.5,
        beforeSetup: function(effect) {
          effect.effects[0].element.makePositioned(); 
        },
        afterFinishInternal: function(effect) {
          effect.effects[0].element.show().undoPositioned().setStyle(oldStyle);
        } 
      }, arguments[1] || {}));
}





Effect.Bounce = function(element) {
  element = $(element);
  var oldStyle = {
    top: element.getStyle('top'),
    left: element.getStyle('left') };
    return new Effect.Move(element, 
      { x:  0, y: -8, duration: 0.3, afterFinishInternal: function(effect) {
    new Effect.Move(effect.element,
      { x: 0, y:  8, duration: 0.3,  afterFinishInternal: function(effect) {
    new Effect.Move(effect.element,
      { x:  0, y: -5, duration: 0.2,  afterFinishInternal: function(effect) {
    new Effect.Move(effect.element,
      { x: 0, y: 5, duration: 0.5,  afterFinishInternal: function(effect) {
    }}) }}) }}) }});
}



Effect.ShakeLight = function(element) {
 element = $(element);
 var oldStyle = {
    top: element.getStyle('top'),
    left: element.getStyle('left') };
 
 	return new Effect.Move(element, 
      { x:  2, y: 0, duration: 0.05, afterFinishInternal: function(effect) {
    new Effect.Move(effect.element,
      { x: -4, y: 0, duration: 0.1,  afterFinishInternal: function(effect) {
    new Effect.Move(effect.element,
      { x:  4, y: 0, duration: 0.1,  afterFinishInternal: function(effect) {
    new Effect.Move(effect.element,
      { x: -4, y: 0, duration: 0.1,  afterFinishInternal: function(effect) {
    new Effect.Move(effect.element,
      { x:  4, y: 0, duration: 0.1,  afterFinishInternal: function(effect) {
    new Effect.Move(effect.element,
      { x: -2, y: 0, duration: 0.05, afterFinishInternal: function(effect) {
        effect.element.undoPositioned().setStyle(oldStyle);
  }}) }}) }}) }}) }}) }});
}


// in_effect should be the name of an Effect. function
//
Effect.LinkEffect = function(element,in_effect) {
 	element = $(element);

	if (Effect[in_effect])
		{
		return new Effect[in_effect](element,{duration: 0.5, pulses:1, afterFinishInternal: function() {
			// make sure it's visible on return to page via back button
			element.undoPositioned();	
			new Effect.Appear(element,{duration:3.0});
			
			// Unfortunately window.location.href is broken in Safari - 
			// it's supposed to add to history (as opposed to replace, which replaces a page), but it doesn't
			// So we use this ugly hack, bah!
			if (Prototype.Browser.WebKit)
				window.location.href = window.location.href + "#";
		
			window.location.href = element.href;// What should be the one true way
			}});
		
		}
}


var LinkEffects = {
	version				: '1.0',
	// Attach to all links with rel="Effect:xxx" - text after colon is an effect name
	// and call LinkEffect with the effect name when they are clicked
	// would be nice to do this with any effect at some point
	initialize : function () {
		$A(document.getElementsByTagName('a')).each(function (link) {
	 		var rel = String(link.getAttribute('rel'));
	 		if (rel.include("Effect:"))
				{
				Event.observe(link,'click',LinkEffects.respond_click.bindAsEventListener(LinkEffects));
				}
			});
	},
	
	// Event observer hook
	respond_click : function (event) {
		link = Event.element(event);
		
		// find the link which is parent of this clicked element
		link = link.up().up();
		rel = String(link.getAttribute('rel'));
		if (rel)
			effect = rel.gsub("Effect:","");
			
		if (effect && Effect[effect])
			{
			new Effect.LinkEffect(link,effect);
			Event.stop(event);
			}
	}

};


Event.observe(window,'load',LinkEffects.initialize,false);
