var toggle_slideshow =
	'<div class="media_slideshow">'+
	'<a href="#" id="toggle_show" onclick="slideshow.start_it();'+
	'return false;">start slideshow</a></div>';
function replace_media_nav ( type, content ) {
	if ( type == "img"){
		$('media_display').innerHTML = 
		'<img src="'+ content +'" />' + toggle_slideshow;
	}
}

function showAddress(address) {
	if( $( "media_display" )) return;
	var map = new GMap2( $( "display_map" ));
	var geocoder = new GClientGeocoder();
	geocoder.getLatLng(
	address,
	function(point) {
	  if (!point) {
		$('display_map').innerHTML = "<p><h3>" + address + " not found</h3></p>";
	  } else {
		this_html = "<p>"+address+"</p><a href=\"#\" id=\"map_show_dirs\">Get Directions</a>";
		directions_html = 
			'<p>Start Address<form action="http://maps.google.com/maps" method="get" target="_blank">' +
			'<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br><br>' +
			'<input value="Get Directions" type="submit"><br><br>' +
			'<input type="hidden" name="saddr" value="' + address + '"/></form></p>';
		map.setCenter(point, 13);
		var marker = new GMarker(point);
		map.addOverlay(marker);
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(this_html);
			Event.observe( $( "map_show_dirs" ), "click", function() {
				marker.openInfoWindowHtml(directions_html);
			});
		});

		marker.openInfoWindowHtml(this_html);

		Event.observe( $( "map_show_dirs" ), "click", function() {
		  marker.openInfoWindowHtml(directions_html);
		});
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GScaleControl());
	  }
	}
  );
}

function banner_click( uri ){
	//new Ajax.Request( "track/ ", { parameters: "type=banner&path="+uri, method: "post" } );
	new Ajax.Request( "track/ ", { parameters: "path=banner", method: "post" } );
}

function load_maps() {
	new Ajax.Request( "track/ ", { parameters: "path=maps", method: "post" } );
	$('media_display').innerHTML = $('main_maps').innerHTML;
	address = $( "display_map" ).readAttribute( "address" );
	tour_style = $( "display_map" ).readAttribute( "tour_style" );
	$('media_display').innerHTML = 
		"<iframe src=\"http://list.stellamedia.com/display_map/?address="+escape(address)+
		"&style="+tour_style+"\" frameborder=\"0\" scrolling=\"no\"></iframe>";
}

function load_video_embed(){
	$('media_display').innerHTML = $('main_video').innerHTML;
	src = $("display_video").readAttribute( "src" );
	loc = $("display_video").readAttribute( "loc" );
	_w = $("display_video").readAttribute( "w" );
	_h = $("display_video").readAttribute( "h" );
	
	if( src == "embed" ){
		$("media_display").innerHTML = "<embed src='"+loc+"' width='"+_w+"' height='"+_h+"' ></embed>";
	} else {
		so = new SWFObject("/src/flvplayer.swf","single","540","360","7");
		so.addParam("allowfullscreen", "true");
		so.addParam("displayheight", "360");
		so.addVariable("file", loc);
		so.addVariable("overstretch", "false");
		so.addVariable("autostart", "true");
		so.addVariable("backcolor", "0xffffff");
		so.addVariable("frontcolor" ,"0x999999");
		so.addVariable("lightcolor" ,"0x00000"); 
		so.write("media_display");
	}
	new Ajax.Request( "track/ ", { parameters: "path=video", method: "post" } );
}

function load_video(uri) {
	new Ajax.Request( "track/ ", { parameters: "path=video", method: "post" } );
	if( uri.length > 0 ) window.open(uri, "_blank");
}

function load_photos() {
	media_slider.setValue( 0 );
	new Ajax.Request( "track/ ", { parameters: "path=photos", method: "post" } );
	
	$('media_nav_content').innerHTML = $('nav_photos').innerHTML;
	$('media_display').innerHTML = $('main_photos').innerHTML;
	slideshow.startup();
}

function load_desc () {
	new Ajax.Request( "track/ ", { parameters: "path=desc", method: "post" } );
	$('media_display').innerHTML = $('main_desc').innerHTML;
	//$('media_nav_content').innerHTML = $('nav_desc').innerHTML;
}

function load_pans(){
	if ( typeof( media_slider ) != "undefined" ) media_slider.setValue( 0 );
	new Ajax.Request( "track/ ", { parameters: "path=pans", method: "post" } );
	if( $('main_pans').innerHTML.match( /resources\/listings/ )) 
		show_pan( $('main_pans').innerHTML );
	else 
		$( "media_display" ).innerHTML = $('main_pans').innerHTML;
	$('media_nav_content').innerHTML = $('nav_pans').innerHTML;
}

function show_pan(pan) {
	var so = new SWFObject("/src/pan.swf", "pan", "540", "360", "8", "#ffffff", true);
	so.addVariable("image0", pan);
	so.write("media_display");
}

slideshow = {
	startup: function() {
		if ( !slideshow.check_it()) return;
		$( "media_display" ).innerHTML = $( "media_all_photos" ).innerHTML + toggle_slideshow;
		$( "toggle_show" ).innerHTML = "stop slideshow";
		$( "toggle_show" ).onclick = function(){ slideshow.stop_it(); return false; }
		playing = true;
		image_list = $( "media_display" ).getElementsByTagName( "img" );
		slideshow.create_holder();
		if( typeof( current_slide ) == "undefined" ) current_slide = 0;
		img.src = image_list[current_slide].src;
		if ( typeof( pe ) == "undefined" ) pe = new PeriodicalExecuter(slideshow.cycle, 3.5);
	},
	create_holder: function(){
		if( $( "slideshow_holder" )) $( "slideshow_holder" ).remove();
		img_holder = document.createElement("div");
		img_holder.id = "slideshow_holder";
		img_holder = $( "media_display" ).appendChild( img_holder );
		img = document.createElement("img");
		img = img_holder.appendChild( img );
	},
	stop_it: function() {
		playing = false;
		if( $( "toggle_show" )){
			$( "toggle_show" ).innerHTML = "start slideshow";
			$( "toggle_show" ).onclick = function(){ slideshow.start_it(); return false; }
		}
		if ( typeof( pe ) == "undefined" ) return;
		pe.stop();
		pe = undefined;
	},
	start_it: function(){
		$( "media_display" ).innerHTML = $( "media_all_photos" ).innerHTML + toggle_slideshow;
		slideshow.startup();
	},
	check_it: function() {
		div_list = $$( "#media_display .media_full_photo");
		if ( div_list.length < 2 ) {
			slideshow.stop_it();
			return false;
		} else {
			if( typeof( img ) == "object" && playing && !$( "slideshow_holder" )){
				slideshow.create_holder();
			}
			return true;
		}
	},
	cycle: function() {
		if ( !slideshow.check_it()) return;
		image_list = $( "media_display" ).getElementsByTagName( "img" );
		img.src = image_list[current_slide].src;
		new Effect.Opacity( img_holder, { 
			duration: .5, from: 1, to: 0, 
			afterFinish: function() { 
				if ( !slideshow.check_it()) return;
				
				// check last slide. -2 because of img holder
				if ( (image_list.length-2) == (current_slide) ) { 
					current_slide = 0;
				} else { 
					current_slide++;
				}
				slideshow.scroll_nav();
				img.src = image_list[current_slide].src;
				new Effect.Opacity( img_holder, { from: 0, to: 1, duration: .5 }); 
			} // after function end
		}); // effectfade end
	 },	 
	 scroll_nav: function() {
		if ( ( image_list.length - 1 ) < 5 ) return; // dont ani less than 5
		if ( current_slide == ( image_list.length - 1 )) return; // dont ani last img
		curr_photo = $( "nav_photo_" + current_slide );
		Position.prepare();
		
		var offsets = Position.positionedOffset( curr_photo );
		var top     = offsets[1];
		var left    = offsets[0];
		img_margin_r = Element.getStyle( curr_photo, "margin-right" );
		img_margin_l = Element.getStyle( curr_photo, "margin-left" );
		img_margin_l = parseFloat( img_margin_l.replace( /px/, "" ));
		img_margin_r = parseFloat( img_margin_r.replace( /px/, "" ));
		offset_margins = ( img_margin_r + img_margin_l * ( current_slide + 1 )) + ( curr_photo.offsetWidth - curr_photo.clientWidth );
		move_to = ( ( left + offset_margins ) / $('media_nav_content').offsetWidth );
		media_slider.setValue( move_to );
	 }
}

function displayNewValue( percent, px, animate) {
	holder_w = Element.getStyle( "media_nav_holder", "width" );
	holder_w = holder_w.replace( /px/, "" );
	move_to = -( Math.round( $('media_nav_content').offsetWidth-holder_w) * percent);
	if ( px > 0 ) move_to = -px;
	if (animate) {
		new Effect.Move('media_nav_content', { x: move_to, y: 0, mode: 'absolute', duration: 0.5 } );
	} else {
		$('media_nav_content').style.left = move_to+"px";
	}
}
  
function startup() {
	if( !$( "media_display" )) return;
	media_check = $( "media_display" ).innerHTML;
	if( display_type = $("media_display").getElementsByTagName("div")[0] )
		display_type = $(display_type).readAttribute("displaytype");

	switch( display_type ){
		case "pans":
			load_pans();
			break;
		case "video":
			load_video_embed();
			break;
		case "maps":
			load_maps();
			break;
	}

	media_slider = new Control.Slider('slider_thumb','slider_bg',{
		maximum: 384,
		sliderValue: 0,
		onSlide: function (v) { displayNewValue(v, 0, false) },
		onChange: function (v) { displayNewValue(v, 0, true) }
	});
	
	Event.observe( "translate_click", "click", translate_pop );
	slideshow.startup();
	apply_each();
}

function translate_pop( event ){
	event.returnValue = false;
	if(event.preventDefault) event.preventDefault();
	
	pop_anchor = $("translate_click");
	pop_pos = Element.positionedOffset(pop_anchor);
	$("translate_parent").setStyle({
		top: (pop_pos[1]+pop_anchor.clientHeight)+"px",
		left: pop_pos[0]+"px",
		width: pop_anchor.clientWidth+"px"					 
	});
	
	new Effect.BlindDown( "translate_parent", { duration: .2 });
	
	Event.observe( "translate_parent", "mouseout", function(){
		$("translate_parent").out = 0;
		setTimeout("if($('translate_parent').out!=1) Element.hide($('translate_parent'))", 1500 );
	});
	
	Event.observe( "translate_parent", "mousemove", function(){
		$("translate_parent").out = 1;
	});
}

function apply_each(){
	// tab colors
	$$('#media_tabs ul li a').each(function(tab){
	  Event.observe(tab, 'click', function(event) {

		tabs = $A( tab.parentNode.parentNode.getElementsByTagName("li") );
		tabs.each( function ( current_tab ) {
			Try.these (
				function() {
					tab_a=$(current_tab).childNodes[0];
					Element.removeClassName(tab_a, "active");
				},
				function() {
					tab_a=$(current_tab).childNodes[1];
					Element.removeClassName(tab_a, "active");
				} 
			);				
		});
		$(tab).addClassName("active");

	  });
	});
	// tab colors end
	
	// tab roll ups
	$$(".section_tab").each( function( tab ) {
		Event.observe( tab, "click", function( event ){
			section_name = tab.id.replace(/_tab/,"");
			Effect.toggle(section_name,'blind',{duration:0.3});
		});
	});
	// tab roll ups end
}

function shutdown() {

}

Event.observe( window, "load", startup );
Event.observe( window, "unload", shutdown );

