	var _gallery_bg, _gallerySplash, _gallerySplash, _curr_gallery,
	_curr_gallery_index = 0, _gallery_imgs = [], _curr_gallery_img,
	_gallery_panos = [], _curr_gallery_pano, _gallery_links, _prev, _next,
	_gallery_pager_btns, _for_sale,
	$currIndex,
	$maxIndex;
	
	// for sale is a boolean is used on the for sale page
	function galleryInit(for_sale){
		_for_sale = for_sale;
		
		_gallery_bg = $("#gallery_bg");				
		_gallerySplash = $(".gallerySplash");
		_curr_gallery = $("#curr_gallery");
		_gallery_pager_btns = $(".gallery_pager_btn");	
		
		_gallery_links = $("#gallery_links");
		_gallery_links.children("img").css({opacity:0.25});
		
		
		_gallery_links.find(".sectionLabel").hide();
		_gallery_links.find("img").css({opacity:0.25});		
		_gallery_links.find("a").click(setupGalleryChoice).hover(function(){

			$(this).children(".sectionLabel").show();
			$(this).children("img").animate({opacity:1}, 200);			
		},function(){			
			$(this).children("img").stop().css({opacity:0.25});			
			$(this).children(".sectionLabel").hide();
		});
		
		_prev = $('#prev_gallery_btn');
		_next = $('#next_gallery_btn');
				
		$currIndex = _next.find(".currIndex");
		$maxIndex = _next.find(".maxIndex");
				
		$("a.galleryPicker_btn").hover(function(){			
			$(this).find(".background").css({opacity: 0.25});
		}, function(){
			$(this).find(".background").css({opacity: 0.55});
		}).find(".background").css({opacity: 0.55});
		
			
		_gallery_links.click(function(){
			setupGalleryChoice();		
		});
		
	}	
	
	
	function setupGalleryChoice(){		
		_gallery_pager_btns.hide();
		var $t = $(this);
		
		var id = $t.attr("href"),
		property_folder_name = $t.attr("rel");
						
		//destroy old gallery, BRAAAAAAAHHHHHHH!!!!!!
		_curr_gallery.html("").hide();		
			
		var url = ""
		if(_for_sale){
			url = "jax/forSaleJax.php";
		}else{
			url = "jax/galleryJax.php";
		}	
		
		$.ajax({
			url: url,
			dataType: "json",
			data: {id: id},
			success: function(data){				
				_gallery_imgs = data.flat;
				_gallery_panos = data.panos;
				
				_gallerySplash.show();
                $("#intro_text").hide();
				
				//if the section doesn't have stills or panos, go to the other one
				/* each section should have one or the other*/
				if(_gallery_imgs.length > 0){					
					$("#imagesLink").unbind("click").click(function(){			
						setUpImgGallery();
						return false;
					});
				}else{
					setUpTour();					
				}
				if(_gallery_panos.length > 0){		
					if(_gallery_imgs.length > 0){
						_gallery_bg.attr("src", _gallery_imgs[0].tour_preview_image);
					}					
					
					$("#tourLink").unbind("click").click(function(){
						setUpTour();
						return false;
					});
				}else{
					setUpImgGallery();
				}
			}
		});
		
		return false;				
		
	}
	
	function setUpTour(){
		_curr_gallery_index = 0;
		_gallerySplash.hide();
		//create swf
		var swf = createswf("swfs/krpano.swf");
		swf.addVariable("xml", "properties/"+_gallery_panos[0].folder_name);
		swf.addParam("wmode", "transparent");
		//embed Swf
		swf.embed(document.getElementById("curr_gallery"));
				
		_curr_gallery.show();				
		
		_prev.unbind("click").click(prev_gallery_pano);		
		_next.unbind("click").click(next_gallery_pano);
		
		if(_gallery_panos.length > 1){
			_gallery_pager_btns.show();
		}
		
		$currIndex.html(1);
		$maxIndex.html(_gallery_panos.length);
	}
	
	//
	function setUpImgGallery(){		
		_curr_gallery_index = 0;
		gallery_img = $('<img id="gallery_img" />');
		gallery_img.attr("src", "properties/"+_gallery_imgs[0].projectFolder+"/flat/"+_gallery_imgs[0].folder_name);
		gallery_img.attr("alt", _gallery_imgs[0].title);
				
		_curr_gallery.append(gallery_img);		
				
		_curr_gallery_img = gallery_img;
		
		_prev.unbind("click").click(prev_gallery_img);		
		_next.unbind("click").click(next_gallery_img);
		
		
		_gallerySplash.hide();
		_curr_gallery.show();
		
		if(_gallery_imgs.length > 1){
			_gallery_pager_btns.show();
		}
		
		$currIndex.html(1);
		$maxIndex.html(_gallery_imgs.length);
		
	}
	
	function next_gallery_img(){
		_curr_gallery_index++;
		if(_curr_gallery_index >= _gallery_imgs.length){
			_curr_gallery_index = 0;
		}			
		
		_curr_gallery_img = $("#gallery_img");
		var $newGalleryImg = $(document.createElement("img"));
		$newGalleryImg.attr("id", "gallery_img");		
		$newGalleryImg.attr("src", "properties/"+_gallery_imgs[_curr_gallery_index].projectFolder+"/flat/"+_gallery_imgs[_curr_gallery_index].folder_name);
		$newGalleryImg.attr("alt", _gallery_imgs[_curr_gallery_index].title);
		_curr_gallery_img.replaceWith($newGalleryImg);
		
		$currIndex.html(_curr_gallery_index+1);
		$maxIndex.html(_gallery_imgs.length);
		
		return false;
	}
	
	function prev_gallery_img(){
		_curr_gallery_index--;
		if(_curr_gallery_index < 0){
			_curr_gallery_index = _gallery_imgs.length-1;
		}				
		
		_curr_gallery_img = $("#gallery_img");
		var $newGalleryImg = $(document.createElement("img"));
		$newGalleryImg.attr("id", "gallery_img");
		$newGalleryImg.attr("src", "properties/"+_gallery_imgs[_curr_gallery_index].projectFolder+"/flat/"+_gallery_imgs[_curr_gallery_index].folder_name);
		$newGalleryImg.attr("alt", _gallery_imgs[_curr_gallery_index].title);
		_curr_gallery_img.replaceWith($newGalleryImg);
		
		$currIndex.html(_curr_gallery_index+1);
		$maxIndex.html(_gallery_imgs.length);
		
		return false;
	}
	
	function next_gallery_pano(){
		_curr_gallery_index++;		
		if(_curr_gallery_index >= _gallery_panos.length){
			_curr_gallery_index = 0;
		}
		
		var swf = createswf("swfs/krpano.swf");
		swf.addVariable("xml", "properties/"+_gallery_panos[_curr_gallery_index].folder_name);
		swf.addParam("wmode", "transparent");
		swf.embed(document.getElementById("curr_gallery"));
			
		$currIndex.html(_curr_gallery_index+1);
		$maxIndex.html(_gallery_panos.length);
								
		return false;		
	}
	
	function prev_gallery_pano(){		
		_curr_gallery_index--;
		
		if(_curr_gallery_index < 0){
			_curr_gallery_index = _gallery_panos.length-1;
		}
		
		if($.browser.msie){
			var swf = createswf("swfs/krpano.swf");
			swf.addVariable("xml", "properties/"+_gallery_panos[_curr_gallery_index].folder_name);
			swf.addParam("wmode", "transparent");
			//embed Swf
			swf.embed(document.getElementById("curr_gallery"));
		}else{
			document.getElementById("krpanoSWFObject").call("loadpano(../../"+_gallery_panos[_curr_gallery_index].folder_name+")");
		}		
		
		$currIndex.html(_curr_gallery_index+1);
		$maxIndex.html(_gallery_panos.length);
		
		return false;
	}
