// Hover Behaviour for Nav etc.
var HoverBehavior = Class.create();
HoverBehavior.prototype = {
	initialize: function() {
		$A(document.styleSheets).each( function(stylesheet) {
		$A(stylesheet.rules).each( function(rule) {
		if( rule.selectorText.match(/:hover/i) ) {
			stylesheet.addRule( rule.selectorText.replace(/:hover/ig, '.hover'), rule.style.cssText );
		}
		});
	});

	$A(arguments).each( function(arg) {
		$$(arg).each( function(tag) {
			Event.observe(tag, 'mouseenter', function() { Element.addClassName(tag, 'hover'); }, true);
			Event.observe(tag, 'mouseleave', function() { Element.removeClassName(tag, 'hover'); }, true);
			});
		});
	}
};

// Pop Image from utils
function popImage(img, alt) {
	url = '/imageviewer.php?pic='+img+'&alt='+alt;
	window.open(url, 'ImageZoom', 'width=435,height=495');
}

// Scan for rel=blank
function popExternalLinks () {
	$$('a[rel="blank"]').each(function(link){link.target = "_blank"});
}

// Preview link for Evites
function previewEvites() {

  if (($('evite-preview')) && ($('evite-submit-button')) && ($('evite_form'))) {  
    $('evite-preview').innerHTML = '<a id="evite-preview-link" href="#preview" style="background-image:url(/images/evite/'+ $('evite_form')['venue'].value +'/preview.jpg);"></a>';
    $('evite-submit-button').insert({before: '<input id="evite-preview-button" name="__preview__" value="Preview your Evite" type="button">&nbsp;&nbsp;'});
        
    Event.observe('evite-preview-link', 'click', function(e){
      e.stop(); previewEviteAction();
    });
    
    Event.observe('evite-preview-button', 'click', function(e){
      e.stop(); previewEviteAction();
    });
  }
}

function previewEviteAction(){
  previewPath = '/evite?preview=true&';
  previewValues = Form.serialize($('evite_form'));
  window.open(previewPath+previewValues, 'popup', 'width=800, height=600, status=no, menubar=no, resizable=no, scrollbars=no, toolbar=no, location=no, directories=no');
}






// Global Window Onload
Event.observe(window, 'load', function() {
	
	// IE FIXES
	if (Prototype.Browser.IE) {
		$$('#nav li:last-child').each( function(tag) { tag.addClassName('last-child'); });
		new HoverBehavior('#nav li'); // keep at the end!
	}	
	
	popExternalLinks();
	
	 // Make room for CLF
	if ($('topnav')) { $('topnav').setStyle({right:'355px'}); }
	
	
	// Preview link for Evites
	previewEvites();	
});	