var virtualtour;

// Virtual Tour
var VirtualTour = Class.create({
	//
    //  initialize()
    //  	
	initialize: function(el, options){
		this.options = {
			campus: '',
			abs_path: '',
			proxy: '/mailpage/virtualtour/',
			leadURL: 'http://www.wyotech.edu/contact/?cta=virtualtour',
			tours: {'blairsville':'', 'daytona':'','laramie':'','sacramento':''}
		}
		
		Object.extend(this.options, options || {});
		
		var qty = $$('a.vt');
		if(qty.length > 0){
			$$('a.vt').invoke('observe', 'click', this.show.bind(this));
			
			$(document.body).insert(new Element('div', {'id' : 'overlay'}).setStyle({display: 'none'}));
			$(document.body).insert(new Element('div', {'id' : 'flash_vt_container'}).setStyle({display: 'none'}));
			
			Prototype.Browser.IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6;
			if(Prototype.Browser.IE6){
				$('overlay').setStyle({position: 'absolute'});
			}
			
			if(window.location.host.match(/drew.devsite(\.cc)?$/)){
				this.options.abs_path = 'http://www.wyotech.edu.drew.devsite.cc';
			}
			else if(window.location.host.match(/devsite(\.cc)?$/)){	
				this.options.abs_path = 'http://www.wyotech.edu.devsite.cc';
			}
			else{
				this.options.abs_path = 'http://www.wyotech.edu';
			}
			
			if(window.location.hostname !== 'www.wyotech.edu' && window.location.hostname !== 'wyotech.edu' && !window.location.host.match(/devsite(\.cc)?$/)){
				//this.options.proxy = '/proxy.php';
				this.omniture = false;
			}
			// on load open tour
			if(window.location.href.substring(window.location.href.indexOf('#')+1) == 'tour'){
				var _campus = window.location.href.substring(window.location.href.indexOf('campus/')+7).replace('#tour', '');
				if(_campus in this.options.tours){
					this.options.campus = _campus;
					this.open();
				}
			}
		}
	},
	//
    //  show()
    //
	show: function(e){
		e.stop();
		var element = Event.findElement(e, 'a');
		this.options.campus = element.href.substring(element.href.indexOf('#')+1);
		this.open();
	},
	//
    //  quickshow()
    //
	quickshow: function(){
		this.open();
	},
	//
	//	open()
	//
	open: function(){	
		if(this.omniture != false){
			var req = new Ajax.Request(this.options.proxy + '?action=init&name=' + this.options.campus, {
				onComplete: function(request, json) {
					if(json.response.success) {
						for(i in json.omniture) {
							s[i] = json.omniture[i];
						}
						s.tl();
					}
				}
			});
		}
		
		var vt_link = this.options.abs_path + '/multimedia/virtualtour/xml/' + this.options.campus + '.xml';
		
		// Hide the stuff nao
		$$('embed', 'object', 'select').each(function(el){
			el.hide();
			el.replace(el);
		});
		
		$('overlay').appear();
		$('flash_vt_container').show();
		
		if (swfobject.hasFlashPlayerVersion("10.0.0")){
			if (!$('flash_vt')){
				$('flash_vt_container').insert(new Element('div', {'id' : 'flash_vt'}));
			}
		}
		else {
		  $('flash_vt_container').insert(new Element('div', {'id' : 'no_flash'}).setStyle({display: 'none'}));
		  $('no_flash').insert('<p>Please upgrade your flash player <a href="http://get.adobe.com/flashplayer/" target="_blank">here</a> to watch the virtual tour.</p><p style="text-align: right;"><a href="javascript:void(0);" onclick="javascript:virtualtour.close();">CLOSE</a></p>');
		  $('no_flash').appear();
		}
		
		var att = { data:this.options.abs_path + '/multimedia/virtualtour/wyotech_virtualTour.swf', width:'1020', height:'650' };
		var params = { quality: 'best', bgcolor: '#000000', wmode: 'transparent', allowScriptAccess: 'always', flashvars: 'xmlfile=' + vt_link };
		swfobject.createSWF(att, params, 'flash_vt');
	},	
	//
    //  close()
    //
	close: function(){
		// Clean up
		$('overlay').fade();
		$('flash_vt_container').hide();
		
		if ($('no_flash')){
			$('no_flash').remove();
		}
		else{
			swfobject.removeSWF('flash_vt');
		}
		
		// Bring it back nao
		$$('embed', 'object', 'select').invoke('show');
	},	
	//
    //  lead()
    //
	lead: function(){
		var url = this.options.leadURL;
		if(this.omniture != false){
			var req = new Ajax.Request(this.options.proxy + '?action=lead&name=' + this.options.campus, {
				onComplete: function(request, json) {
					if(json.response.success) {
						for(i in json.omniture) {
							s[i] = json.omniture[i];
						}
						s.tl();
					}
					window.location = url;
				}
			});
		} else {
			window.location = url;
		}
	}
	
});

document.observe('dom:loaded', function () { virtualtour = new VirtualTour(); });

