
var img_base = '/img/virtual_tour/';
var $canvas;
var $background;
var $loader;
var tour;
var virtualTourData = {
		victorian: [
		            {url: img_base + 'victorian/tv01.jpg', title: 'The students in the Victorian Farmhouse' },
		            {url: img_base + 'victorian/tv03.jpg', title: 'The students in the Victorian Farmhouse' },
		            {url: img_base + 'victorian/tv04.jpg', title: 'The students in the Victorian Farmhouse' },
		            {url: img_base + 'victorian/tv05.jpg', title: 'The students in the Victorian Farmhouse' },
		            {url: img_base + 'victorian/tv06.jpg', title: 'The students in the Victorian Farmhouse' },
		            {url: img_base + 'victorian/tv07.jpg', title: 'The students in the Victorian Farmhouse' },
		            {url: img_base + 'victorian/tv08.jpg', title: 'The students in the Victorian Farmhouse' },
		            {url: img_base + 'victorian/tv09.jpg', title: 'The students in the Victorian Farmhouse' },
		            {url: img_base + 'victorian/tv10.jpg', title: 'The students in the Victorian Farmhouse' },
		            {url: img_base + 'victorian/tv12.jpg', title: 'The students in the Victorian Farmhouse' },
		            {url: img_base + 'victorian/tv13.jpg', title: 'The students in the Victorian Farmhouse' },
		            {url: img_base + 'victorian/tv14.jpg', title: 'The students in the Victorian Farmhouse' },
		            {url: img_base + 'victorian/tv15.jpg', title: 'The students in the Victorian Farmhouse' },
		            {url: img_base + 'victorian/tv16.jpg', title: 'The students in the Victorian Farmhouse' },
		            {url: img_base + 'victorian/tv17.jpg', title: 'The students in the Victorian Farmhouse' },
		            {url: img_base + 'victorian/tv18.jpg', title: 'The students in the Victorian Farmhouse' },
		            {url: img_base + 'victorian/tv19.jpg', title: 'The students in the Victorian Farmhouse' },
		            {url: img_base + 'victorian/tv20.jpg', title: 'The students in the Victorian Farmhouse' },
		            {url: img_base + 'victorian/tv21.jpg', title: 'The students in the Victorian Farmhouse' },
		            {url: img_base + 'victorian/tv22.jpg', title: 'The students in the Victorian Farmhouse' },
		            {url: img_base + 'victorian/tv23.jpg', title: 'The students in the Victorian Farmhouse' }
		            ],
        outdoor: [
                  {url: img_base + 'outdoor/to01.jpg', title: 'The students enjoying our outdoor activities' },
                  {url: img_base + 'outdoor/to02.jpg', title: 'The students enjoying our outdoor activities' },
                  {url: img_base + 'outdoor/to03.jpg', title: 'The students enjoying our outdoor activities' },
                  {url: img_base + 'outdoor/to04.jpg', title: 'The students enjoying our outdoor activities' },
                  {url: img_base + 'outdoor/to05.jpg', title: 'The students enjoying our outdoor activities' },
                  {url: img_base + 'outdoor/to06.jpg', title: 'The students enjoying our outdoor activities' },
                  {url: img_base + 'outdoor/to07.jpg', title: 'The students enjoying our outdoor activities' },
                  {url: img_base + 'outdoor/to08.jpg', title: 'The students enjoying our outdoor activities' },
                  {url: img_base + 'outdoor/to09.jpg', title: 'The students enjoying our outdoor activities' },
                  {url: img_base + 'outdoor/to10.jpg', title: 'The students enjoying our outdoor activities' },
                  {url: img_base + 'outdoor/to11.jpg', title: 'The students enjoying our outdoor activities' }
                  ],
      	redbarn: [
      	          {url: img_base + 'redbarn/tR01.jpg', title: 'The students in the Red Barn' },
      	          {url: img_base + 'redbarn/tR02.jpg', title: 'The students in the Red Barn' },
      	          {url: img_base + 'redbarn/tR03.jpg', title: 'The students in the Red Barn' },
      	          {url: img_base + 'redbarn/tR04.jpg', title: 'The students in the Red Barn' },
      	          {url: img_base + 'redbarn/tR05.jpg', title: 'The students in the Red Barn' },
      	          {url: img_base + 'redbarn/tR06.jpg', title: 'The students in the Red Barn' },
      	          {url: img_base + 'redbarn/tR07.jpg', title: 'The students in the Red Barn' },
      	          {url: img_base + 'redbarn/tR08.jpg', title: 'The students in the Red Barn' },
      	          {url: img_base + 'redbarn/tR09.jpg', title: 'The students in the Red Barn' },
      	          {url: img_base + 'redbarn/tR10.jpg', title: 'The students in the Red Barn' },
      	          {url: img_base + 'redbarn/tR11.jpg', title: 'The students in the Red Barn' },
      	          {url: img_base + 'redbarn/tR12.jpg', title: 'The students in the Red Barn' },
      	          {url: img_base + 'redbarn/tR13.jpg', title: 'The students in the Red Barn' },
      	          {url: img_base + 'redbarn/tR14.jpg', title: 'The students in the Red Barn' }
      	          ],
	    greenfarm: [
	              {url: img_base + 'greenfarm/tG01.jpg', title: 'The students in the Green Farmhouse' },
	              {url: img_base + 'greenfarm/tG02.jpg', title: 'The students in the Green Farmhouse' },
	              {url: img_base + 'greenfarm/tG03.jpg', title: 'The students in the Green Farmhouse' },
	              {url: img_base + 'greenfarm/tG04.jpg', title: 'The students in the Green Farmhouse' },
	              {url: img_base + 'greenfarm/tG05.jpg', title: 'The students in the Green Farmhouse' },
	              {url: img_base + 'greenfarm/tG06.jpg', title: 'The students in the Green Farmhouse' },
	              {url: img_base + 'greenfarm/tG07.jpg', title: 'The students in the Green Farmhouse' },
	              {url: img_base + 'greenfarm/tG08.jpg', title: 'The students in the Green Farmhouse' },
	              {url: img_base + 'greenfarm/tG09.jpg', title: 'The students in the Green Farmhouse' },
	              {url: img_base + 'greenfarm/tG10.jpg', title: 'The students in the Green Farmhouse' },
	              {url: img_base + 'greenfarm/tG11.jpg', title: 'The students in the Green Farmhouse' },
	              {url: img_base + 'greenfarm/tG12.jpg', title: 'The students in the Green Farmhouse' },
	              {url: img_base + 'greenfarm/tG13.jpg', title: 'The students in the Green Farmhouse' },
	              {url: img_base + 'greenfarm/tG14.jpg', title: 'The students in the Green Farmhouse' }
	              ],
      middleschool: [
                     {url: img_base + 'middleschool/ms01.jpg', title: 'The activities at Oak Bridge Montessori Middle School' },
                     {url: img_base + 'middleschool/ms02.jpg', title: 'The activities at Oak Bridge Montessori Middle School' },
                     {url: img_base + 'middleschool/ms03.jpg', title: 'The activities at Oak Bridge Montessori Middle School' },
                     {url: img_base + 'middleschool/ms04.jpg', title: 'The activities at Oak Bridge Montessori Middle School' },
                     {url: img_base + 'middleschool/ms05.jpg', title: 'The activities at Oak Bridge Montessori Middle School' },
                     {url: img_base + 'middleschool/ms06.jpg', title: 'The activities at Oak Bridge Montessori Middle School' },
                     {url: img_base + 'middleschool/ms07a.jpg', title: 'The activities at Oak Bridge Montessori Middle School' },
                     {url: img_base + 'middleschool/ms07b.jpg', title: 'The activities at Oak Bridge Montessori Middle School' },
                     {url: img_base + 'middleschool/ms08.jpg', title: 'The activities at Oak Bridge Montessori Middle School' },
                     {url: img_base + 'middleschool/ms09.jpg', title: 'The activities at Oak Bridge Montessori Middle School' },
                     {url: img_base + 'middleschool/ms10.jpg', title: 'The activities at Oak Bridge Montessori Middle School' },
                     {url: img_base + 'middleschool/ms11.jpg', title: 'The activities at Oak Bridge Montessori Middle School' },
                     {url: img_base + 'middleschool/ms12.jpg', title: 'The activities at Oak Bridge Montessori Middle School' },
                     {url: img_base + 'middleschool/ms13.jpg', title: 'The activities at Oak Bridge Montessori Middle School' },
                     {url: img_base + 'middleschool/ms14.jpg', title: 'The activities at Oak Bridge Montessori Middle School' },
                     {url: img_base + 'middleschool/ms15.jpg', title: 'The activities at Oak Bridge Montessori Middle School' }
                     ]
};

$(function(){
	$canvas = $('#virtual_tour_canvas');
	$background = $('#virtual_tour_background').hide();
	$loader = $('#virtual_tour_loading').css('z-index','1000');

	tour = new Tour($canvas);
	
	var bgImg = new Image();
	startedLoadingImage();
	$(bgImg).load(function(){
		/*Overlay Images*/
		tour.addOverlay(TourRect(100, 30, 50, 80), TourOverlay('victorian_over.png',80,60));
		tour.addOverlay(TourRect(300, 30, 130, 110), TourOverlay('outdoor_over.png',300,30));
		tour.addOverlay(TourRect(440, 40, 162, 110), TourOverlay('outdoor2_over.png',440,40));
		tour.addOverlay(TourRect(20, 150, 118, 102), TourOverlay('farmhouse1_over.png',20,150));
		tour.addOverlay(TourRect(430, 170, 75, 43), TourOverlay('redbarn_over.png',430,170));
		tour.addOverlay(TourRect(315, 180, 71, 75), TourOverlay('redbarn2_over.png',315,180));
		tour.addOverlay(TourRect(355, 258, 30, 30), TourOverlay('redbarn3_over.png',315,258));
		tour.addOverlay(TourRect(430, 240, 136, 71), TourOverlay('redbarn4_over.png',430,240));
		tour.addOverlay(TourRect(68, 344, 116, 63), TourOverlay('entrance_over.png',68,344));
		tour.addOverlay(TourRect(187, 305, 122, 92), TourOverlay('farmhouse2_over.png',187,305));
		tour.addOverlay(TourRect(480, 325, 118, 61), TourOverlay('middle_over.png',480,325));
		
		/*Overlay Titles/Links*/
		tour.addOverlay(TourRect(140, 13, 180, 20,'victorian'), TourOverlay('victorian_overlay.gif',140,13));
		tour.addOverlay(TourRect(250, 155, 142, 22,'outdoor'), TourOverlay('outdoor_overlay.gif',250,155));
		tour.addOverlay(TourRect(390, 222, 108, 22,'redbarn'), TourOverlay('redbarn_overlay.gif',390,222));
		tour.addOverlay(TourRect(96, 282, 164, 22,'greenfarm'), TourOverlay('greenfarm_overlay.gif',96,282));
		tour.addOverlay(TourRect(339, 359, 264, 55,'middleschool'), TourOverlay('middle_overlay.gif',339,359));
		
		$background.append(this);
		finishedLoadingImage();
	}).attr('src','/img/virtual_tour/full_bg.jpg');
});





function Tour(elem) {
	this.element=elem;
	this.overlays=new Array();
	this.links={};
	
	
	this.addOverlay = function(trigger,element){
		triger = $(trigger);
		element = $(element);
		
		trigger.attr('overlay_index',this.overlays.length);
		this.overlays[this.overlays.length]=element;
		$(trigger).hover(function(){tour.showOverlay(this);},function(){tour.hideOverlay(this)});
	};
	
	this.showOverlay = function(trigger){
		trigger = $(trigger);
		element = this.overlays[trigger.attr('overlay_index')];
		element.show();
	}

	this.hideOverlay = function(trigger){
		trigger = $(trigger);
		element = this.overlays[trigger.attr('overlay_index')];
		element.hide();
	}
	return true;
}



function TourOverlay(src,x,y,link){
	$this = $('<div></div>');
	$this.css('z-index','100');
	$this.css('display','none');
	$this.css('position','absolute');
	$this.css('left',x+'px');
	$this.css('top',y+'px');
	var img = new Image();
	startedLoadingImage();
	var $img = $(img);
	$img.load(function(){
		finishedLoadingImage();
	});
	$img.attr('src',img_base+src);
	
	$this.append($img);
	$canvas.append($this);
	return $this;
}
function TourRect(x,y,w,h,link){
	var $this = $('<a></a>');
	$this.css('z-index','200');
	$this.css('display','block');
	$this.css('position','absolute');
	$this.css('left',x+'px');
	$this.css('top',y+'px');
	$this.css('width',w+'px');
	$this.css('height',h+'px');
	//$this.css('border','1px solid red');
	$this.css('background','url(/img/transparent.gif)');
	if(link!=undefined && virtualTourData[link]!=undefined){
		//$this.click(function(){tourShow(link);});
		$this.attr('href',virtualTourData[link][0]['url']);
		$this.attr('rel',link);
		$this.lightbox({
			fitToScreen: true,
			jsonData: virtualTourData[link],
		    loopImages: true,
		    imageClickClose: false,
		    disableNavbarLinks: false,
		    autoPlayInterval:5000,
		    hideNavLinks: false
		});
		$this.css('cursor','pointer');
	}
	
	$canvas.append($this);
	return $this;
}


var globalImageLoadCount=0;
function startedLoadingImage(){
	globalImageLoadCount++;
}
function finishedLoadingImage(){
	globalImageLoadCount--;
	if(globalImageLoadCount<=0){
		$loader.fadeOut().css('z-index','-100');
		$background.fadeIn();
	}
}

function tourShow(link){
	
}
