/*
SimpleAjaxWindow
	Loads content of specified URL into a window automatically.
	
*/
var myAjaxWindow;

var SimpleAjaxWindow = new Class({
	Implements: [Options, Events],
	options: {
		subject: 'myWindowHolder',
		siteID: 'siteWrap',
		popup: {
			overlay: 'myOverlay',
			wrap: 'myWrap'			
		},
		ajaxcontent: 'ajax_content',
		request: {
			method: 'get',
			link: 'ignore'			
		},
		cookie: {
		    showOnce: false,
		    cookieId: null,
		    duration: false
		}
	},
	initialize: function(url, options) {
		if ($type(url) != 'string') return				
		this.setOptions(options)
		
		if (this.processCookie()) return
		
		this.siteID = $(this.options.siteID)
		
		this.build();
		
		var opts = this.options.request
		opts.url = url
		opts.onSuccess = function(tree, els, html, js) {
			this.show(tree)
		}.bind(this)
		opts.onFailure = this.hide.bind(this)
		this.request = new Request.HTML(opts).send()
	},
	processCookie: function() {
	    var myCookie, cookieOpts = this.options.cookie

	    if (!cookieOpts.showOnce) return false
	    else { 
	        if (Cookie.read(cookieOpts.cookieId)) return true
	        Cookie.write(cookieOpts.cookieId, 'visited', { duration: cookieOpts.duration})
	    }
	},
	show: function(tree) {
		$(this.options.ajaxcontent).adopt(tree)
		this.resizeOverlay();
		this.overlay.set('opacity', 0.5).reveal();
		this.wrap.reveal();		
		
		window.addEvent("scroll",this.resizeOverlay.bind(this)).addEvent("resize", this.resizeOverlay.bind(this));	
	},
	hide: function() {
		this.overlay.reveal()
		this.wrap.reveal()
		$(this.subject).empty()
		window.removeEvents("scroll").removeEvents("resize");
	},
	resizeOverlay: function() {
		styles = {}
		styles.top = window.getScrollTop() + 275
		styles.left = (this.siteID.getPosition().x + (this.siteID.getSize().x / 2).toInt()) - (this.wrap.getStyle('width').toInt() / 2)		
		
		this.wrap.setStyles(styles)
		this.overlay.setStyles({top:window.getTop(), height:window.getScrollHeight()});
	},
	setURL: function() {
		
	},
	retrieveHTML: function() {
		
	},
	build: function(tree) {		
		if (!$(this.options.subject)) {
			subject = new Element('div', {id: this.options.subject}).inject($(document.body), 'top')
		}
		this.subject = $(this.options.subject)
		
		var popup = this.options.popup
		for (var item in popup) {
			this[item] = new Element('div', {id: popup[item], styles: { display: 'none'}}).inject(this.subject)
		}
		
		
		var table = new Element('table', {id: 'pop_table'})
		var tbody = new Element('tbody').inject(table)
		
		var trs = [
			[{'class': 'popNW'}, {'class': 'popN'}, {'class': 'popNE'}],
			[{'class': 'popW'}, {'id': this.options.ajaxcontent}, {'class': 'popE'}],
			[{'class': 'popSW'}, {'class': 'popS'}, {'class': 'popSE'}]		
		]
		for (var i=0; i < trs.length; i++) {
			tr = new Element('tr')
			for (var j=0; j < trs[i].length; j++) {
				td = new Element('td', trs[i][j]).inject(tr)								
			};
			tr.inject(tbody)
		};
		table.inject(this.wrap)
				
	}
})

Element.implement({
	reveal: function() {
		if (this.getStyle('display') == 'none') this.setStyle('display', '')
		else this.setStyle('display', 'none')
	}
})

function closeAjaxWindow() {
	myAjaxWindow.hide()
}

//document.addEvent('domready', function() {
//	myAjaxWindow = new SimpleAjaxWindow('ajaxhcp.html');
//})


function getPowerPointViewer()
{
window.location = window.root+"interstitial.aspx?dest=microsoft.com/downloads/details.aspx?FamilyID=048DC840-14E1-467D-8DCA-19D2A8FD7485&displaylang=en";
}


function getPdfViewer()
{
window.location = window.root+"interstitial.aspx?det=get.adobe.com/reader/";
}
