$(function(){

	/**
	* Larger font toggling
	*/
	$text_size_toggle_limit = 2;
	$text_size_tracking_larger = 0;
	$text_size_tracking_smaller = 0;
	$text_size_larger = $('img#text_size_larger');
	$text_size_smaller = $('img#text_size_smaller');
	
	$text_size_larger.click(function(){
		if($text_size_tracking_larger <= $text_size_toggle_limit) {
			var $current_textsize = $('body').css('font-size');
			var $current_parsed_textsize = parseFloat($current_textsize, 10);
			var $new_font_size = $current_parsed_textsize * 1.1;
			$('div#content').css('font-size', $new_font_size);
			$text_size_tracking_larger++;
			$text_size_tracking_smaller--;
		}
	});

	$text_size_smaller.click(function(){
		if($text_size_tracking_smaller <= $text_size_toggle_limit) {
			var $current_textsize = $('body').css('font-size');
			var $current_parsed_textsize = parseFloat($current_textsize, 10);
			var $new_font_size = $current_parsed_textsize * .9;
			$('div#content').css('font-size', $new_font_size);
			$text_size_tracking_smaller++;
			$text_size_tracking_larger--;
		}
	});
	
	/**
	* Main menu hide and show
	*/
/*
	$tier1_link = $('li.tier1');
	
	$tier1_link.hover(function(){
		$('div.dropdown_wrap', this).show();
	}, function(){
		$('div.dropdown_wrap', this).hide();	
	});
*/	
	/**
	* Top Image Rotation
	*/
	if(ChanningHouse.rotate_images == 1) {
		$.get(ChanningHouse.base_url + '/topimages', function(data) {
			$top_image = $('div#top_image');
			$top_image.append(data);
			$top_image.slideShow({delay:7000}).startSlides();	
		});
	}

	/**
	* Slideshow Ajax
	*/
	$slideshow_images_large = $('div#slideshow_images_large');
	
	if(ChanningHouse.current_uri == '/amenities/apartments') {
		$.get(ChanningHouse.base_url + '/amenities/slideshow-apartments', function(data) {
			$slideshow_images_large.append(data);
		});
	}
	else if(ChanningHouse.current_uri == '/amenities/common-areas') {
		$.get(ChanningHouse.base_url + '/amenities/slideshow-common-areas', function(data) {
			$slideshow_images_large.append(data);
		});
	}
	else if(ChanningHouse.current_uri == '/amenities/fitness-center') {
		$.get(ChanningHouse.base_url + '/amenities/slideshow-fitness-center', function(data) {
			$slideshow_images_large.append(data);
		});
	}
	
	/**
	* Slideshow functionality
	*/
	var Slideshow = {
		'visible_images': 5,
		'total_images': 0,
		'left_image_showing': 0
	};
	
	$slideshow_image_thumbs = $('div#slideshow div.thumb_wrap');
	$slideshow_image_arrow_right = $('div#slideshow_arrows img#arrow_right');
	$slideshow_image_arrow_left = $('div#slideshow_arrows img#arrow_left');
	$slideshow_top_image = $('div#top_image');
	$slideshow_image_large_array = $('div#slideshow_images_large img');
	$slideshow_image_thumbs.click(function(){
		Slideshow.total_images = $slideshow_image_thumbs.length;
		
		$slideshow_top_image.stopSlides();
		$slideshow_image_thumbs.removeClass('selected');
		$(this).addClass('selected');
		
		var $top_pos = $slideshow_top_image.position();
		var $current_image_number = $('img', this).attr('id').replace(/^image_/, ''); 
		var $current_image_large_id = 'img#slideshow_image_large_'+ $current_image_number;
	
		if( $current_image_number == ( Slideshow.left_image_showing + Slideshow.visible_images ) ) {
			if((Slideshow.total_images - Slideshow.visible_images) > Slideshow.left_image_showing) {
				Slideshow.left_image_showing++;
				$slideshow_image_thumbs.animate({'margin-left':'-'+ (Slideshow.left_image_showing * 96) +'px'}, 'slow');
			}
		}
		else if( ($current_image_number-1) == Slideshow.left_image_showing ) {
			if(Slideshow.left_image_showing >= 1) {
				Slideshow.left_image_showing--;
				$slideshow_image_thumbs.animate({'margin-left':'-'+(Slideshow.left_image_showing * 96) +'px'}, 'slow');
			}
		}		
		
		$('div#slideshow_images_large img').hide();
		
		$($current_image_large_id).css({'position':'absolute', 'top':'-315px', 'left':'-270px', 'display':'block', 'z-index':1000});
	});
	
	$slideshow_image_arrow_right.click(function(){
		Slideshow.total_images = $slideshow_image_thumbs.length;
		if((Slideshow.total_images - Slideshow.visible_images) > Slideshow.left_image_showing) {
			Slideshow.left_image_showing++;
			$slideshow_image_thumbs.animate({'margin-left':'-'+ (Slideshow.left_image_showing * 96) +'px'}, 'slow');
		}
	});
	
	$slideshow_image_arrow_left.click(function(){
		Slideshow.total_images = $slideshow_image_thumbs.length;
		if(Slideshow.left_image_showing > 0) {
			Slideshow.left_image_showing--;
			$slideshow_image_thumbs.animate({'margin-left':'-'+(Slideshow.left_image_showing * 96) +'px'}, 'slow');
		}
	});	
	
	/**
	* Floorplan functionality
	*/
	var FLOORPLAN_TRACK = 0;
	
	$floorplan_link = $('div#floorplan_link');
	$floorplan_wrap = $('div#floorplan_wrap');
	$floorplan_close = $('div#floorplan_close');
	$floorplan_selector = $('span.floorplan_selector');
	$floorplan_image = $('div#floorplan_image');
	$floorplan_images = $('img.floorplan_image');
	$floorplan_title = $('span#floorplan_title');
	$floorplan_sqft = $('span#floorplan_sqft');
	
	$floorplan_link.click(function(){
			$floorplan_wrap.show();
			FLOORPLAN_TRACK = 1;
			return false;
	});
	
	$floorplan_close.click(function(){
		$floorplan_wrap.hide();
		FLOORPLAN_TRACK = 0;
		return false;
	});
	
	$floorplan_selector.click(function() {
		$current_selection = $(this);
		
		$floorplan_selector.removeClass('floorplan_selectors_selected');
		$floorplan_selector.addClass('floorplan_selectors_notselected');
		
		$current_selection.addClass('floorplan_selectors_selected');
		
		$current_selection_meta_data_string = $(this).attr('title');
		$current_selection_meta_data = $current_selection_meta_data_string.split('|');
		
		$floorplan_images.hide();
		
		$current_image_id = 'img#floorplan_'+ $current_selection_meta_data[3];
		
		$($current_image_id).show();
		
		$floorplan_title.empty().append($current_selection_meta_data[1]);
		$floorplan_sqft.empty().append($current_selection_meta_data[2]);
		
		return false;
	});
	
	$googlemap_link = $('div#googlemap_link');
	$googlemap_wrap = $('div#googlemap_wrap');
	$googlemap_close = $('div#googlemap_close');
	
	$googlemap_link.click(function(){
		$googlemap_wrap.css({
			'visibility':'visible'
		});
		$googlemap_wrap.show();
		return false;
	});
	
	$googlemap_close.click(function(){
		$googlemap_wrap.css({
			'visibility':'hidden'
		});
		return false;
	});
	
	
});
