var isSidebarExpanded = false;
var activeSidebarTagGroup = '';

jQuery.noConflict();

jQuery(document).ready(function () {
	// Add ModalBox to interactive Tour Button
	
	if(jQuery.browser.msie && jQuery.browser.version=="6.0") {
		jQuery('#btnTour').click(function() {
			alert("Unfortunately, the Tour is not working on Internet Explorer 6. Please update your browser.");	
			return false;						 
		});
	} else {
		jQuery('#btnTour').colorbox({href: '/static/tour/index.html',width: 998, height: 686, speed:500},function() {
			
			jQuery('#flash').flash({
				swf: '/house360.swf',
				width: 956,
				height: 609,
				quality: 'high',
				wmode: 'opaque',
				pluginspage: 'http://www.adobe.com/go/getflashplayer',
				base: '/'
			});
			
			jQuery('#btnLideLeft').click(function() {
				sidebarSlideOut();
				return false;
			});
		});
	}
});

function sidebarSlideIn() {
	jQuery('#tourSidebar').animate({
		left: 0
	}, function() {
		isSidebarExpanded = true;
	});
}

function sidebarSlideOut() {
	jQuery('#tourSidebar').animate({
		left: '-215px'
	}, function() {
		isSidebarExpanded = false;
	});
}

function tagJsGateway(tagName) {
	var tagGroup = tagName.split('_')[0];
	var tagAddition = tagName.split('_')[1];
	
	if(isSidebarExpanded && (activeSidebarTagGroup == tagGroup)) {
		sidebarSlideOut();
		return false;
	}
	
	activeSidebarTagGroup = tagGroup;
	
	sidebarSlideIn();

	var folderName = tagGroup.toLowerCase();
	if(tagGroup.toLowerCase() == 'laundryroom')
		folderName = folderName + tagAddition;

	jQuery('#sidebarContent').load('/static/tour/rooms/' + folderName + '/index.html', function() {
		imagePreview();
		jQuery('.tourRoomPictureGallery a').click(function() {
			return false;
		}); 
		
		jQuery('.tourRoomVideoGallery a').click(function() {
			// video Action
		
			return false;
		}); 
	});
}

function infoJsGateway() {
	var tagGroup = 'tourinfo';
	
	if(isSidebarExpanded && (activeSidebarTagGroup == tagGroup)) {
		sidebarSlideOut();
		return false;
	}
	
	activeSidebarTagGroup = tagGroup;
	
	sidebarSlideIn();
	jQuery('#sidebarContent').load('/static/tour/info.html');
}

this.imagePreview = function(){	
	/* CONFIG */
		
	xOffset = 5;
	yOffset = 5;
	
	// these 2 variable determine popup's distance from the cursor
	// you might want to adjust to get the right result
		
	/* END CONFIG */
	jQuery(".tourRoomPictureGallery a").hover(function(e){
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/><br/>" + this.t : "";
		//jQuery("body").append("<p id='preview'><img src='"+ this.href +"' alt='Image preview' />"+ c +"</p>");
		jQuery("body").append("<p id='preview'><img src='/static/tour/interface/load.gif' width='24' height='24' /></p>");
		jQuery("#preview")
				.css("top",(e.pageY - xOffset) + "px")
				.css("left",(e.pageX + yOffset) + "px")
				.fadeIn("fast");
		//jQuery("body").append("<div id='previewLoading'></div>");
		//var posLeft = jQuery(this).position().left + jQuery(this).width() + 6;
		
		var fullImg = new Image();
		fullImg.src = this.href;
		
		jQuery(fullImg).load(function() {
			jQuery("#preview")
				.css("top",(e.pageY - fullImg.height) + "px")
				.css("left",(e.pageX + yOffset) + "px")
				.html("<img src='"+ fullImg.src +"' alt='Image preview' />"+ c);
		});
		/*
		fullImg.onload = function() {
			jQuery("#preview")
				.css("top",(e.pageY - fullImg.height) + "px")
				.css("left",(e.pageX + yOffset) + "px")
				.html("<img src='"+ fullImg.src +"' alt='Image preview' />"+ c);
		};*/
		
					
    },
	function(){
		this.title = this.t;	
		jQuery("#preview").remove();
    });	
	jQuery("a.preview").mousemove(function(e){
		jQuery("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};
