﻿/**
 * Pop-up Script
 * author: James Revillini
 * date: 6/15/2008
 * purpose: find any links with rel='popup[w,h]' and make them pop up the url that they point to 
 */
 
YAHOO.namespace('popup');

YAHOO.popup.init = function () {
	var links = document.links;
	var i, n, lnk, rel, href;
	
	for (var i = 0, n = links.length; i < n; i++) {
		lnk = new YAHOO.util.Element(links[i]);
		rel = lnk.get('rel');
		if (/popup/.test(rel)) {
			lnk.on('click', YAHOO.popup.link_onclick, lnk, true);
		}
		href = lnk.get('href');
		if (/\.pdf/.test(href)) {
			lnk.on('click', YAHOO.popup.link_onclick, lnk, true);
		}
		if (lnk.hasClass('popup')) {
			lnk.on('click', YAHOO.popup.link_onclick, lnk, true);
		}
	}
};

YAHOO.popup.link_onclick = function (evt) {

	if (evt) YAHOO.util.Event.stopEvent(evt);	//kill default event (normal browser navigation)

	var rel = this.get('rel');
	
	//some default dimensions for the window
	var w = 800; 
	var h = 600;
	
	//get the dimensions from the rel statement
	if (rel) {
		var dimensions = rel.match(/[0-9]+/g);
		w = parseInt(dimensions[0]);
		h = parseInt(dimensions[1]);
	}
	
	//define some default options for the popup window.  maybe some day we will make these able to be overridden
	var popup_options = 'width=' + w + ',height=' + h + ',status=1,centerscreen=1,resizable=1,menubar=1,toolbar=1,location=1,scrollbars=1';

	//launch popup and try to set focus
	var popup_window = window.open(this.get('href'), 'popupwindow', popup_options);
	try { popup_window.focus(); } catch(e) {/*do nothing*/}
};

YAHOO.popup.pazow = function (url, width, height, title) {
	var fake = document.createElement('a');
	fake.setAttribute('href', url);
	fake.setAttribute('rel', width + ',' + height);
	fake.setAttribute('title', title);
	lnk = new YAHOO.util.Element(fake);
	lnk.set('id', YAHOO.util.Dom.generateId(fake));
	YAHOO.popup.link_onclick.call(lnk)
};

// Identify the components to load

YAHOO.dankloader.add('dom', 'event', 'element');
YAHOO.dankloader.add(YAHOO.popup.init);
