$(document).ready(function(){
			
	GET_vimeo_id = function () {
		return window.vimeo_id;	
	}
	
	GET_virtualtour_path = function () {
		return window.virtualtour_path;	
	}
	
	var vimeo_embed = "<div class='vimeo_embed'><object width='280' height='210'><param name='allowfullscreen' value='true' /><param name='allowscriptaccess' value='always' /><param name='movie' value='http://vimeo.com/moogaloop.swf?clip_id="+GET_vimeo_id()+"&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=E2D0A7&amp;fullscreen=1' /><embed src='http://vimeo.com/moogaloop.swf?clip_id="+GET_vimeo_id()+"&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=E2D0A7&amp;fullscreen=1' type='application/x-shockwave-flash' allowfullscreen='true' allowscriptaccess='always' width='280' height='210'></embed></object></div>";
	
	var vtour_embed = "<div class='vtour_embed'><object classid='clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B' codebase='http://www.apple.com/qtactivex/qtplugin.cab' width='280' height='210'><param name='controller' value='false'><param name='cache' value='true'><param name='src' value='"+GET_virtualtour_path()+"'><embed src='"+GET_virtualtour_path()+"' pluginspage='http://www.apple.com/quicktime/' width='280' height='210' type='video/quicktime' cache='true' controller='false'></object><div class='ImageSubtext' style='margin-top: 5px;'>'shift' = zoom in, 'cntrl' = zoom out</div></div>";
	
	// lightbox init
	$("a.mg_link_main").colorbox();
	
		
	mg_init = function () {
				
		// place all main photos in the display div			
		$("div.mg_main").appendTo("div.mg_win_main");
			
		// hide everything in mg_win_main
		clear_mg_win_main();
		
		// show the active main photo
		$("div.mg_main_active").hide().fadeIn(500);
			
		// bind the photo swap function to the thumbnails
		$("ul.sc_menu li").bind('click', photo_swap);
			
		// bind the media swap function to the radio buttons
		if ($("div.btn_radio").size() > 0) {
			$("div.btn_radio").bind('click', media_swap);
		}
		
		// set photo btn ON state
		$(".btn_radio[rel=1]").css("background", "url(/img/radio_mg.gif) no-repeat 0px -25px");
			
		// show photo thumbnail menu
		$("div.sc_menu").show();
		$("ul.sc_menu").fadeIn(500);
		
		// if only 1 photo, clear sc_menu
		if ($("ul.sc_menu li").size() < 2) {
			$("div.sc_menu").css({
				"background" : "transparent"
			});
		}
		
		// show left menu border if there's more than 1 photo; show both borders if there's more than 3 photos
		if ($("ul.sc_menu li").size() > 1) {
			$("div.sc_menu").css({
				"border-left" : "1px solid #ad9259",
				"background" : "transparent"
			});
		}
		if ($("ul.sc_menu li").size() > 3) {
			$("div.sc_menu").css({
				"border-left" : "1px solid #ad9259",
 				"border-right" : "1px solid #ad9259",
				"background" : "transparent"
			});
		}
		
		// show photo instructions
		$("div.mg_instructions").html("Click to enlarge photo.");
			
		// remove vimeo_embed
		$("div.vimeo_embed").remove();
			
		// remove vtour_embed
		$("div.vtour_embed").remove();			
	}
		
	mg_video_init = function () {
		clear_mg_win_main();			
		$("div.mg_main").fadeOut(500);
		$("div.mg_instructions").html("&nbsp;");
		$("div.mg_instructions").html("Click to play button to begin.");
		$("ul.sc_menu").fadeOut(500, function () {
			$("div.mg_win_main").append(vimeo_embed);
		});
		$("div.sc_menu").css({
			"border-left" : "0px solid #ad9259",
 			"border-right" : "0px solid #ad9259",			
			"background" : "transparent"
		});
	}
		
	mg_vtour_init = function () {
		clear_mg_win_main();
		$("div.mg_main").fadeOut(500);
		$("div.mg_instructions").html("&nbsp;");
		$("div.mg_instructions").html("Click and drag for 360 view.");
		$("ul.sc_menu").fadeOut(500, function () {
			$("div.mg_win_main").append(vtour_embed);
		});
		$("div.sc_menu").css({
			"border-left" : "0px solid #ad9259",
 			"border-right" : "0px solid #ad9259",			
			"background" : "transparent"
		});		
	}
		
	photo_swap = function () {
		$("div.mg_main").hide();
		$("div.mg_main[id="+$(this).attr('id')+"]").hide().fadeIn(500);		
		return false;	
	}
		
	media_swap = function () {
		
		// switch ALL btns to OFF state
		$("div.btn_radio").css("background", "url(/img/radio_mg.gif) no-repeat 0px 0px");
		
		// switch THIS btn to ON state
		$(this).css("background", "url(/img/radio_mg.gif) no-repeat 0px -25px");
		
		switch (parseFloat($(this).attr('rel'))) {
			case 2:
				mg_video_init();
			break;
			case 3:
				mg_vtour_init();
			break;							
			default:
				mg_init();
			break;
		}
		
		return false;
	}
		
	clear_mg_win_main = function () {
		// hide photos
		$("div.mg_main").fadeOut(500);
			
		// remove vimeo_embed
		$("div.vimeo_embed").remove();
			
		// remove vtour_embed
		$("div.vtour_embed").remove();
			
		// hide menu borders
		$("div.sc_menu").css({
			"background" : "#d4c8aa"
		});
		
	}
		
	mg_init();
		
	//
		
	//get elements for faster access and set overlay width
  	var div = $('div.sc_menu'),
		div_count = $('div.sc_menu').children().size(),
  		ul = $('ul.sc_menu'),
  		// distance from last thumbnail to right edge of menu on full left scroll; should match margin-right in ul.sc_menu in mediagallery.css
  		ulPadding = 5;

  	//Get menu width
  	var divWidth = div.width();

  	//Remove scrollbars
  	div.css({overflow: 'hidden'});

  	//Find last image container
  	var lastLi = ul.find('li:last-child');

  	//When user move mouse over menu
  	div.mousemove(function(e){
		
		if (lastLi.length > 0) {
			//As images are loaded ul width increases,
			//so we recalculate it each time
			var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;
			var left = (e.pageX - div.offset().left) * (ulWidth - divWidth) / divWidth;
			div.scrollLeft(left);
		}
  	});
		
		
});
