
var IE6;
var GoogleMap;
$(document).ready( function() {

	// cached background images - IE6
	IE6 = (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6);
	if(IE6) document.execCommand("BackgroundImageCache",false,true);

	/** init **/
	if( !IE6 ) {
		$("#header").css('position', 'fixed');
		$("#navigate").css('position', 'fixed');
		$("#image").css('position', 'fixed');
		$("#footer").css('position', 'fixed');
		$("#content").css('padding-top', '168px');
	}

	flatsOnVineContentHeight();
	$(window).resize(flatsOnVineContentHeight);

	/** contacts drop-down **/
	$('a.contactlnk').mouseover(function() { $(this).addClass('on'); $('ul.contacts').show(); if(contactsTO) clearTimeout(contactsTO); }).mouseout(hideContacts);
	$('ul.contacts').mouseover(function() { if(contactsTO) clearTimeout(contactsTO); }).mouseout(hideContacts);
	var contactsTO = null;
	function hideContacts() {
		contactsTO = setTimeout( function() { $('ul.contacts').hide(); $('a.contactlnk').removeClass('on');  }, 800 );
	}
	
	// setup the lightbox.
	$(".lightbox").lightbox();

	/** homepage carousel **/
	$('ul#carousel').dynamItCarousel({
		next: 'a.next',
		prev: 'a.prev',
		width: 312,
		inview: 4,
		atatime: 1,
		vertical: true
	});

	var zeroOffset = 225;
	var maxOffset = 537;
	var bubbleHeight = 204;
	var speechHeight = 26;
	var itemSize = 78;
	//carousel click
	$('ul#carousel').find('a').click( function() {
		$("#introcopy").hide();

		// position and display bubble;
		$('#bubble').hide();
		$('#bubblearrow').hide();
		var offset = parseInt( $('ul#carousel').css('top') );
		if( isNaN(offset) ) offset = 0;
		if( offset < 0 ) offset = -1 * offset;
		var index = $('ul#carousel').find('a').index(this);
		var position = index - (offset / itemSize);

		var thisOffset = zeroOffset + (position * itemSize);
		if( thisOffset + bubbleHeight > maxOffset ) thisOffset = maxOffset - bubbleHeight;
		var arrowOffset = zeroOffset + (position * itemSize) + ( itemSize/2 - speechHeight/2 );
		$('#bubble').css('top', thisOffset + 'px').show();
		$('#bubblearrow').css('top', arrowOffset + 'px').show();


		// load the new image
		var img = $(this).children('img').attr('alt');
		if(IE6) {
			$("#image").children('img').attr('src', img);
			setTimeout(function() { $("#image").children('img').attr('src', img); }, 1000);
		} else {
			$("#image").children('img').remove().end().append('<img src="' + img + '" alt="" />');
		}

		// update the bubble content
		$('#bubble').children('.copy').children('h4').html( $(this).children('img').attr('title') );
		$('#bubble').children('.copy').children('p').html( $(this).children('img').attr('description') );

	});

	$("#bubble").children('a.close').click( function() {
		$("#bubble").hide();
		$("#bubblearrow").hide();
	});



	/** homepage neightborhood map **/
	$('a.maplnk').click( function() {
		$('div.mapdesc').show();
		$(this).addClass('on');

		// Load the Google Map
		$("#gcontain").height( $(window).height() ).show();
		if(IE6) $("#gcontain").css({
			'position' : 'absolute',
			'left' : '0',
			'bottom' : '0'
		}); 
		$("#controls").hide();
		$("#introcopy").hide();
		GoogleMap = new GMap2(document.getElementById("googlemap"));
		GoogleMap.setCenter(new GLatLng(39.97112634862256, -83.00870418548584), 15);
		GoogleMap.addControl(new GLargeMapControl(), new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10, 208)));
		GoogleMap.addControl(new GMapTypeControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10, 208)));

		var georss = 'http://maps.google.com/maps/ms?hl=en&gl=us&ptab=2&ie=UTF8&oe=UTF8&msa=0&output=georss&msid=109359928950509008747.0004880b53a05df9f7d13';

		var mapData = new GGeoXml(georss);
		GoogleMap.addOverlay(mapData);

		var lat = 39.971124293062665; var lng = -83.00900459289551;

		// add flats on vine.
		var myImg = "http://www.google.com/intl/en_us/mapfiles/ms/micons/red-dot.png";
		var myIcon = new GIcon(G_DEFAULT_ICON, myImg);
		myIcon.iconSize = new GSize(32, 32);

		var pt = new GLatLng(lat, lng);
		var mark = new GMarker(pt, myIcon);
		mark.bindInfoWindowHtml('<div style="font-family: Arial,sans-serif; font-size: small;"><div style="font-weight: bold; font-size: medium; margin-bottom: 0em;">Flats on Vine</div><div><font style="color: rgb(0, 128, 0);">maps.google.com</font></div><div id="iw_kml"><div dir="ltr">Vine St. & Neil Ave.</div></div><div style="padding-top: 0.7em;" id="wizard" firstcard="actions" class="iw"></div></div>');
		GoogleMap.addOverlay(mark);

	});
	$("div.mapdesc").children('a.close').click( function() {
		$("div.mapdesc").hide();
		$("a.maplnk").removeClass('on');
		$("#gcontain").hide();
		$("#controls").show();
	});

});


var flatsOnVineFixedHeight = 207;
function flatsOnVineContentHeight() {
	var h = parseInt( $(window).height() );
	var heightProp = (IE6) ? 'height' : 'min-height';
	$("#googlemap").height(h); //google maps box
/*	var controlsTop =  170 + parseInt( (h-213-420)/2 ) ;
	$("#controls").css('top', controlsTop+'px'); */
	$("#content").css(heightProp, (h - flatsOnVineFixedHeight) + 'px');
	$("#stripe").height('auto');
	var pageHeight;
	if( $("#stripe").height() <= (h - flatsOnVineFixedHeight) ) {
		$("#stripe").css('height', (h - flatsOnVineFixedHeight) + 'px');
		pageHeight = h;
		if(!IE6) $("#stripe").css('position', 'fixed');
	} else {
		$("#stripe").css('position', 'absolute');
		if($("#content").height() > $("#stripe").height()) {
			$("#stripe").css('height', $("#content").height() + 'px');
			pageHeight = $("#content").height() + flatsOnVineFixedHeight;
		} else {
			$("#content").css('height', $("#stripe").height() + 'px');
			pageHeight = $("#stripe").height() + flatsOnVineFixedHeight;
		}
	}

	if(IE6) $("#image").height(pageHeight).css('overflow', 'hidden');
}

function showFloorplanOrganizer(url, value) {
	var floorplanOrganizer = window.open("http://www.villagegreen.com/Apartments/module/floorplan_organizer/floorplan_image[url]/"+url+"/floorplan_scale[value]/"+value, "floorplanOrganizer", "width=827, height=582, top=505, left=480, scrollbars=no, toolbar=no, resizable=yes");
	floorplanOrganizer.focus();
}

