/*!
 * jQuery Custom Code for Sunnybrook website
 */
 
$(document).ready(function() {
	
	$('a.jToggleLink').click(function() {
		$(this).next('.jToggleBody').toggle(300);
		return false;

	});
	
	$('a.jToggleLinkAlt').click(function() {
		$(this).parent().next('.jToggleBody').toggle(300);
		return false;
	});
	
	$('a.jToggleLinkAlt2').click(function() {
		$(this).parent().parent().next('.jToggleBody').toggle(300);
		return false;
	});
	
	
	$('[class^=jLink]').click(function() {
		var $this = $(this);
		var x = $this.attr("class");
		$('.jToggleBody-' + x).toggle(300);
		return false;
		
	});
	
	$('a[href$=".pdf"]').click(function(){
		 _gaq.push(['_trackEvent', 'Download', 'pdf', this.href]);
	});
	
	$('a[href$=".doc"]').click(function(){
		 _gaq.push(['_trackEvent', 'Download', 'doc', this.href]);
	});
	

	$('a[href$=".ppt"]').click(function(){
		 _gaq.push(['_trackEvent', 'Download', 'ppt', this.href]);
	});

	var swfID = "video_overlay";
	

	$("body").append($("<div id='youtube'></div>"));
	$("#youtube").append($("<div id='vcontainer'></div>"));

	$("a[rel='#youtube']").overlay({
		
		mask: {
		color: '#000000',
		loadSpeed: 200,
		opacity: 0.4
		},
		onClose: function(){
			swfobject.removeSWF(swfID);
		},

		onBeforeLoad: function(){
			var c = document.getElementById(swfID);
			if(!c){
				var d = $("<div></div>");
				d.attr({id: swfID});
				$("#vcontainer").append(d);
			};
			
			var src = this.getTrigger().attr("href");
			var this_overlay = this;

			window.onYouTubePlayerReady = function (playerId){
				var player = $('#'+swfID).get(0);
				if (player.addEventListener) player.addEventListener("onStateChange", "videolb_YTStateChange");
				else player.attachEvent("onStateChange", "videolb_YTStateChange");
				window.videolb_YTStateChange = function(newState){
					if (!newState) this_overlay.close()
				}
			}
			swfobject.createSWF(
				{ data:src, width:"100%", height:"100%", wmode:"opaque" },
				{ allowScriptAccess: "always", allowFullScreen: true, FlashVars: (false?"complete_event=videolb_complite_event()&enablejsapi=1":"") },
				swfID
			);
		}
	});
	
	$("img[rel][rel!='#youtube']").overlay({
			fixed: true,
		closeOnClick: true
		});
		
	$("a[rel][rel!='#youtube']").overlay({
			fixed: true,
		closeOnClick: true
		});	

	$(".scrollable").scrollable();
	$(".scrollable .items img").click(function() {
		if ($(this).hasClass("active")) { return; }
	
		var url = $(this).attr("src");
		var title = $(this).attr("title");
	
		var wrap = $("#scrollable_image").fadeTo(0, 0);
		var img = new Image();
	
		img.onload = function() {
			wrap.fadeTo("medium", 1);
			wrap.find("img").attr("src", url);
		};
	
		img.src = url;
		$("div #scrollable_image #text").text(title);
		$(".scrollable .items img").removeClass("active");
		$(this).addClass("active");
	
	}).filter(":first").click();	
	

	
});

