/* 
 * El Magico (II)
 *   - ajouter ze effects aux offres
 *
 * (Requires Mootools framework)
 *
 * (C) PriceViajes.com 2008
 * Author: Phil Moore (phil.moore@tmgmedia.com) - {available for marriages, funerals and Bar Mitzvahs}
 *
 */
 
PM = {};
PM.BT = {
  escape: function(s) {
    if (s.indexOf('?') == -1) {
      var split = new RegExp("([^:/]+://[^/]*)?([^#]*)(#.*)?", "").exec(s);
      var domain = split[1] || "";
      var query = split[2];
      var anchor = split[3] || "";
      query = unescape(query);
      query = escape(query);
      s = domain + query + anchor;
    }
    return s;   
  },
  ub: function() {
    var url = "";
    for (var i = 0; i < arguments.length; i++) {
      var word = arguments[i];
      if (typeof(word) == "string") {
        url = url + word;
      } else {
        url = url + String.fromCharCode(word);
      }
    }
    return url;
  },
  ubs: function() {
    window.location.href = PM.BT.escape(PM.BT.ub.apply(null, arguments));
  },
  ubp: function() {
    arguments.slice = Array.prototype.slice;
    var url = PM.BT.escape(PM.BT.ub.apply(null, arguments.slice(0, arguments.length - 2)));
    var width = arguments[arguments.length - 2];
    var height = arguments[arguments.length - 1];
	var args = 'width=' + width + ',height=' + height;
	window.open(url, '', args);
  },
  ubw: function() {
    window.open(PM.BT.escape(PM.BT.ub.apply(null, arguments)));
  } 
};

var inputTip = new Class({
	Implements:Options,

	options:{ tip:'', focusValue:''},
	
	initialize:function(obj, options) {
		this.setOptions(options);
		this.obj = obj;
		obj.setStyle('width', obj.getSize().x);	// explicit width required for differences in italic rendering etc
		obj.addEvent('focus', this.setFocusValue.bindWithEvent(this)).fireEvent('focus');
		obj.addEvent('blur', this.setBlurValue.bindWithEvent(this)).fireEvent('blur');
		obj.getParent('form').addEvent('submit', function(e)  { 
			if (this.obj.get('value') == this.options.tip) this.obj.set('value', '');	// remove the "tip" values if they've not been changed
		}.bindWithEvent(this));
	},

	setBlurValue:function() {		
		if (this.obj.get('value') == '' || this.obj.get('value') == this.options.focusValue)
			this.obj.addClass('tip').set('value', this.options.tip);
	},
			
	setFocusValue:function() {
		this.obj.removeClass('tip');
		if (this.obj.get('value') == this.options.tip || this.obj.get('value') == '')
			this.obj.set('value', this.options.focusValue);	
	}
	
});

window.addEvent('domready', function() {
									 
	// navigation hover
	$$('#nav a').set('morph', {'duration':150, 'transition':Fx.Transitions.Quad.easeOut})
				.addEvent('mouseenter', function() { 
					this.morph({'background-color':'#f9f9ff', 'color':'#fa6b03', 'border-color':'#aaa'}); 
				})
				.addEvent('mouseleave', function() {
					this.morph({'background-color':'#22259b', 'color':'#fff', 'border-color':'#191f90'});
				});

	// inline form submits
	$$('.submitToValue').addEvent('change', function() { 
		$(this).getParent('form').setProperties({'action':this.value, 'method':'post'}).submit() 
	});
	$$('.submitOnChange').addEvent('change', function() { $(this).getParent('form').submit(); });
	
	// listings hover effect
	$$('.listings .offre').set('tween',{'duration':200})
							.addEvent('mouseenter', function() { this.addClass('over').tween('background-color','#d2d2d2'); })
							.addEvent('mouseleave', function() { this.removeClass('over').tween('background-color','#e6e6e6'); })
							.addEvent('click', function(e) {
								e.control ? window.open(this.getElement('a').href) : document.location = this.getElement('a').href;
								return false;
							});
	/* listings title Gliss */
	$$('.glissme').each( function(elem) {
		var offset = elem.getParent().getStyle('width').toInt() - elem.getFirst().getSize().x;
		if (offset < 0) {
			elem.addClass('gliss');
			elem.scroller = new Fx.Tween(elem, {property : 'margin-left', wait:false});
			elem.getParent('.offre').addEvent('mouseenter', function() { elem.scroller.start(offset); });
			elem.getParent('.offre').addEvent('mouseleave', function() { elem.scroller.start(0); });
		}
	});
	
	// mini-offres blockout effect
	if ($('mini_offres')) {
		new Element('div', {'id':'mo_blockout'}).inject($('mini_offres')).setStyle('opacity', '.85');
		$$('#mini_offres .offre').each(function(offre, i) {
			offre.addEvent('mouseenter', function() {
				$('mo_blockout').setStyle('display', 'block');
				this.addClass('over');
			}).addEvent('mouseleave', function() {
				$('mo_blockout').setStyle('display', 'none');
				this.removeClass('over');
			}).addEvent('click', function(e) {
				e.control ? window.open(this.getElement('a').href) : document.location = this.getElement('a').href;
				return false;
			});
			if (i == 0 || i == 3) offre.getElement('.details').setStyle('left', '2px');
			else if (i == 2 || i == 5) offre.getElement('.details').setStyles({'left':'auto', 'right':'2px'});
		});
	}

	// input 'tips'
	$$('.tipme').each(function(textInput) { new inputTip(textInput, {'tip':textInput.get('title')}); });
	
	// faire le "jump" effet pour les tags
	var tags = $$('.tags a');
	tags.each( function (tag) {
		tag.set('lastHilite', null);
		tag.set('running', "0");
		tag.addEvent('mouseenter', function() {
			if (tag.get('running') == "0" && tag.get('lastHilite') != this.href) {
				tags.set('running', "1");
				tag.highlighted = true;			
				highlight = new Element('span', {"class":"tagtextflash", "text":this.get('text')}).inject(this, 'bottom');
				tag.fx = new Fx.Morph(highlight, {'duration':400});
				tag.fx.start({"font-size" : "368px", "opacity":0}).chain(function() { 
																				highlight.destroy();
																				tags.set('lastHilite', tag.href);
																				tags.set('running', "0");
																		});
			}
		});
	});
				
});
