var Site = {
	init: function() {		
		Site.Videos.init();
		
		toggleResourceTab(0); // select first tab	
		
		Site.VideoOverlay = new SimpleOverlay("videoOverlay", {
			withOverlay: true,
			position: {		
				relativeTo: document.body		
			}
		});		
		Site.VideoOverlay.addEvents({
			"show": function() {
				//Site.EmbedFlash();
			},
			"hide": function() {
				$("videoContent").empty();
			}
		});

		var vidToggles = $$(".videoToggle");
		vidToggles.each(function(tog, index) {
			var rel = tog.get("rel").toInt() || 0;
			
			tog.addEvent("click", function() {
				Site.CurrentVideo = rel;
				$("videoContent").adopt(new Element("div", {"id": "vidFlashContent"}));
				Site.EmbedFlash(rel);
				
				Site.Videos.update(rel);
				Site.VideoOverlay.show();
			});
		});

	},
	
	EmbedFlash: function(id) {
		var page = window.location.href, url;			
		url = page.split('/');
		page = url.getLast();
	
		var flashvars = {
			xmlConfigUrl: window.root + "docs/swf/xml/videoPlayerConfig.xml",
			pageUrl: page,
			autoStartId: id
		};
		var params = {
			menu: "true",
			wmode: "opaque",
			bgcolor: "#ffffff",
			base: window.root + "docs/swf/",
			swliveconnect: "true",
			allowscriptaccess: "always",
			allowfullscreen: "true"
		};
		var attributes = {
			id: "videoSection",
			name: "videoSection"
		};
		swfobject.embedSWF(window.root + "docs/swf/videoSection.swf", "vidFlashContent", "516", "337", "9.0.0", window.root + "docs/swf/expressInstall.swf", flashvars, params, attributes);		
	},
	
	Videos: {
		init: function() {
			this.chapter = $("capitulo");
			this.desc = $("descripcion");			
			this.transcript = $("transcriptLink");
			
			this.infoLength = $H(VideoInfo).getLength();			
		},
		
		update: function(id) {
			this.chapter.set("html", "<strong>Chapter " + id + "</strong>");
			this.desc.set("html", VideoInfo[id].desc || "");
			this.transcript.set("href", VideoInfo[id].transcript || "");
		},
		
		clear: function() {
			this.chapter.empty();
			this.desc.empty();
			this.transcript.set("href", "");
		},
		
		next: function() {			
			var next = (!(VideoInfo[Site.CurrentVideo + 1])) ? 1 : Site.CurrentVideo + 1;
			loadVideoById(next);
			this.update(next);
			Site.CurrentVideo = next;
			
		},
		
		previous: function() {
			var prev = (!(VideoInfo[Site.CurrentVideo - 1])) ? this.infoLength : Site.CurrentVideo - 1;
			loadVideoById(prev);
			this.update(prev);
			Site.CurrentVideo = prev;		
		}
	}
};

var VideoInfo = {
    1: {
        desc: "Introduction and Objectives",
        transcript: "/Docs/Pdf/Ch1_Introductions_Objectives.pdf"
    },
    2: {
        desc: "AF: Pathophysiology and Progression<br /><strong>Augustus Grant, MD, PhD</strong>",
        transcript: "/Docs/Pdf/Ch2_Grant.pdf"
    },
    3: {
        desc: "Current Treatment Paradigm in AF<br /><strong>Eric Prystowsky, MD, FACC</strong>",
        transcript: "/Docs/Pdf/Ch3_Prystowsky.pdf"
    },
    4: {
        desc: "MULTAQ<sup>&reg;</sup> Clinical Trial Review<br /><strong>Peter Kowey, MD, FACC</strong>",
        transcript: "/Docs/Pdf/Ch4_Kowey.pdf"
    },
    5: {
        desc: "Patient Case Review: Choosing the Appropriate Patient for MULTAQ<br /><strong>David S. Cannom, MD</strong>",
        transcript: "/Docs/Pdf/Ch5_Cannom.pdf"
    },
    6: {
        desc: "Summary<br /><strong>Eric Prystowsky, MD, FACC</strong>",
        transcript: "/Docs/Pdf/Ch6_Summary.pdf"
    }
};

var toggleTabIds = ["contentTab1", "contentTab2"];
var toggleOffClass = ["tab2_primary", "tab2_secondary"];
var toggleOnClass = ["tab2_primary_on", "tab2_secondary_on"];
var toggleContentIds = ["resourceTab1", "resourceTab2"];

function toggleResourceTab(id) {
	toggleTabIds.each(function(str, index) {
		var el = $(str);
		if (!el) return;
		$(str)[(index == id) ? "addClass" : "removeClass"](toggleOnClass[index]);
	});
	toggleContentIds.each(function(str, index) {
		var el = $(str);
		if (!el) return;		
		$(str).setStyle("display", (id == index) ? "block" : "none");
	});
};

var SimpleOverlay = new Class({
	Implements: [Events, Options],
	
	options: {
		withOverlay: true,
		zIndex: 1000
	},
	
	initialize: function(id, opts) {
		this.content = $(id);
		this.setOptions(opts || {});
		this.options.oldPosition = this.options.position || {};
		
		// move content to be child of document body
		if (this.content.getParent() != document.body) 
			this.content.inject(document.body);
		this.content.setStyle("zIndex", this.options.zIndex);
		
		this.overlay = null;
		if (this.options.withOverlay) {
			this.overlay = new Element("div", { 
				styles: {
					display: "none",
					backgroundColor: "#000000",
					left: 0,
					position:"absolute",				
					top:0,
					width: "100%",
					zIndex: this.options.zIndex
				},
				opacity: 0.75
			}).inject(this.content, "before");
		};
		
		this.positionBound = this.position.bind(this);		
		this.position();
		
		return this;

	},
	
	position: function() {				
		this.content.position(this.options.position)
		if (this.overlay) {
			this.overlay.setStyles({
				height: window.getScrollSize().y
			});
		}
	},
	
	updatePositionOpts: function(opts) {		
		if (opts) this.options.position = opts;
		else this.options.position = this.options.oldPosition || {};
	},
	
	show: function() {			
		this.position();
		this.content.show();
		if (this.overlay) this.overlay.show();
		
		this.fireEvent("show", [this.content]);
		
		// reset events
		window.removeEvent("resize", this.positionBound).removeEvent("scroll", this.positionBound);			
		window.addEvent("resize", this.positionBound).addEvent("scroll", this.positionBound);					
	},
	
	hide: function() {		
		if (this.overlay) this.overlay.hide()
		this.content.hide()
		
		this.fireEvent("hide", [this.content]);
		
		window.removeEvent("resize", this.positionBound).removeEvent("scroll", this.positionBound);		
	}
	
});

function thisMovie(movieName) {
	 if (navigator.appName.indexOf("Microsoft") != -1) {
		 return window[movieName];//docuemnt.getElementById(movieName);
	 } else {
		 return document[movieName];
	 }
}
function playVideo()
{
	thisMovie("videoSection").playVideo();
}
function resumeVideo()
{
	thisMovie("videoSection").resumeVideo();
}
function pauseVideo()
{
	thisMovie("videoSection").pauseVideo();
}
function stopVideo()
{
	thisMovie("videoSection").stopVideo();
}
function seekVideo(offset)
{
	thisMovie("videoSection").seekVideo(offset);
}
function setVideoVolume(level)
{
	thisMovie("videoSection").setVideoVolume(level);
}
function fullScreenVideo()
{
	thisMovie("videoSection").fullScreenVideo();
}
function loadVideoById(id)
{
	thisMovie("videoSection").loadVideoById(id);
}
function loadVideoPlayList(stringArray)
{
	thisMovie("videoSection").loadVideoPlayList(stringArray);
}
function nextVideo(arg)
{
	Site.Videos.next(arg);
}
function previousVideo(arg)
{
	Site.Videos.previous(arg);
}
		
window.addEvent("domready", function() {
	Site.init();
});