var isIos = (((navigator.userAgent).indexOf("iPhone") > 0) || ((navigator.userAgent).indexOf("iPad") > 0))


var highlightActiveMenu = function(logged_in) {

	var paths = window.location.pathname.split('/');
	
	if ((logged_in == 'TRUE') && (paths[1] == '')) {
		paths[1] = 'news';
	}

	switch(paths[1]){
	
		case 'projects':
			$('#nav LI.projects').addClass('current');
			break;
		case 'people__offices':
			$('#nav LI.people_and_offices').addClass('current');
			break;
		case 'news':
			$('#nav LI.whats_new').addClass('current');
			break;
		default:
			break;	
	
	};
	
};

var showTab = function(show,hide) {

	if (show) $('#'+show).show();
	if (hide) $('#'+hide).hide();

	if ($('#'+show).find('#map_canvas')) {
		$('#'+show).find('#map_canvas').each(initMaps);
	}
	

};


var embedVideo = function(div, url, w, h) {

	if (isIos) {
	
		$('#'+div).html(
			'<video width="'+w+'" height="'+h+'" controls="controls">'
			+'<source src="'+url+'" type="video/mp4" />'
			+'</video>');
	
	} else {
	
		var flashvars 	= {
			configUrl: '/assets/swf/media_player.xml'
			, assetURL: url
			};	
		var params 		= {wmode:'opaque', bgcolor: "#000000", menu: "true", quality: "high", scale: "noScale", allowScriptAccess: "always", swLiveConnect: "true", allowFullScreen: "true"};		
		swfobject.embedSWF("/assets/swf/media_player.swf", div, w, h, "9.0.28","/assets/playerProductInstall.swf", flashvars, params);
	
	}

}


var geocoder = false;
var initMaps = function() {
	
		// http://gmaps-samples-v3.googlecode.com/svn/trunk/styledmaps/wizard/index.html

		var address		= $(this).attr('address');
		if (!address)	return;
		
		if (!geocoder) {
			geocoder 	= new google.maps.Geocoder();
		}
		
		var map_url		= 'http://maps.google.com/maps?spn=0.013302,0.025771&oe=utf-8&client=firefox-a&gl=us&vpsrc=0&z=12&iwloc=A&q=';

		geocoder.geocode( { 'address': address}, function(results, status) {
		
			if (status == google.maps.GeocoderStatus.OK) {

				var location	= results[0].geometry.location;

				var style	 	= [ { stylers: [ { saturation: -100 }, { gamma: 1.46 } ] },{ featureType: "poi", stylers: [ { visibility: "off" } ] } ];
				var map_style 	= new google.maps.StyledMapType(style, {name: "GrayMap"});
				var options 	= {
					zoom: 13,
					center: location,
					mapTypeControl: false,
					mapTypeIds: google.maps.MapTypeId.ROADMAP
					};
		
				var map = new google.maps.Map(document.getElementById("map_canvas"), options);
		
				map.mapTypes.set('GrayMap', map_style);
				map.setMapTypeId('GrayMap');
		
				var marker = new google.maps.Marker({
					position: location, 
					map: map, 
					icon: '/assets/img/map_marker.png'
				});  

				google.maps.event.addListener(marker, "click", function() {
					window.location = map_url+address;
				});

			}
			
		});

	};




var Site = function(string) {
	
	/*
	var $header 			= $('DIV#header');
	var $nav 				= $('#nav_mask');

	var _nav_expand = function() {
		$(this).data('hover',1);
		$nav.stop(true,true).animate({height: 90},'slow','easeOutQuart')
	};
	var _nav_collapse = function() {
		$(this).data('hover',0);
		$nav.stop(true,true).animate({height: 47},'slow','easeOutQuart')
	};
	*/

	if ($('#project_content').find('#map_canvas')) {
		$('#project_content').find('#map_canvas').each(initMaps);
	}
	
	
	var _do_blur_input = function() {
		if (!this.value) this.value = $(this).attr('default');
	};

	var _do_focus_input = function() {
		if (this.value == $(this).attr('default')) this.value = '';
	};
	
	var getMousePosition = function(eventObj) {
		if (eventObj.pageX && eventObj.pageY) {
			return {x: eventObj.pageX, y: eventObj.pageY};
		} else {
			return {x: (eventObj.clientX + document.body.scrollLeft - document.body.clientLeft), y: (eventObj.clientY + document.body.scrollTop - document.body.clientTop)};
		}
	};
	
	var initShare = function() {
	
		var button 	= $(this);
		var closed 	= button.width();
		var icons 	= button.find('.icons');
		var iconw 	= icons.width();

		button.click(function(e){
			e.preventDefault();		
		});

		button.mouseenter(function(){
			button.find('A').attr('title', '');
			button.stop(true,true).animate({width: closed+iconw}, function(){
				icons.fadeIn('fast');
			});
		});

		button.mouseleave(function(){
			icons.stop(true,true).hide();
			button.stop(true,true).animate({width: closed}, 'fast', function(){
				icons.hide();
			});
		});
		
		


	};

	
	var _init = function() {
	
		$('INPUT.default').each(_do_blur_input);
		$('INPUT.default').blur(_do_blur_input);
		$('INPUT.default').focus(_do_focus_input);
		
		/* expand nav when mouse enters header */
		if (!$('BODY').hasClass('nav_open')) {
			//$header.hover(_nav_expand, _nav_collapse);
			/*
			$('body').bind('mousemove',function(e){   
			
				var pageCoords = "( " + e.pageX + ", " + e.pageY + " )";
				var clientCoords = "( " + e.clientX + ", " + e.clientY + " )";
			
				$(this).unbind(e);
				
				if (e.clientY > 175) _nav_collapse();
				
			});
			*/

		}
		
		/* keep nav open for main slide show */
		/*
		if ($('.main_slideshow').length) {
			$header.unbind('mouseleave');
		};
		*/

		/* sortable lightbox list */
		/*
		$('#lightbox_list').sortable({
			axis : 'y',
			opacity : 0.8
		});
		*/
		
		$('#employee_commentary').each(function() {
		
		
			// Assign submit button listen
			$('.button', $(this)).click(function(e){
				
				e.preventDefault();
				
				// TODO: Show loading
				
				var params = {
					'comment' : $('#comment_form TEXTAREA').val()
					, 'page_id' :  $('#comment_form INPUT[name="page_id"]').val()
					};
				
				jQuery.ajax({
					url: '/SERVICE/addComment',
					type: 'post',
					dataType: 'html',
					data: params,					
					success: function(data){
					
						if ($(data).find('li')) {
							$('UL.mini_post_list').append($(data));
						}

						$('UL.mini_post_list LI.empty').hide();
						$('#comment_form TEXTAREA').val('')
					
					}
				});
				
			
			});		
		
		
		});		
		
		if (!isIos) {

			$('.dropdown').live('mouseenter', function() {
				// Mouseenter
				$(this).addClass('hover');
			});
			$('.dropdown').live('mouseleave', function() {
				// mouseleave
				$(this).removeClass('hover');
			});
		
		} else {

			$('.dropdown').live('click', function() {
				$(this).toggleClass('hover');
			});
		
		}		
		
		// Init Share buttons
		$('.share_button').each(initShare);


		
		// Function to copy filename to filename display fields on blog post page
		$('INPUT[type="file"]').change(function() {
			$('#file_text').html($(this).val());
		});
		
		$('INPUT[type="file"]').hover(
			function() {
				$('.file_button').addClass('hover');
			},
			function() {
				$('.file_button').removeClass('hover');
			}
		);
		
		$('#blog_post').find('INPUT[type="submit"]').click(function(e) {
			e.preventDefault();
			
			var post_title 		= $('INPUT[name="fields[post_title]"]');
			var post_title_val 	= post_title.val();
			var post_title_def 	= post_title.attr('default');
			
			var post_body		= $('TEXTAREA[name="fields[post_body]"]');
			var post_body_val 	= post_body.val();
			
			var post_location 	= $('SELECT[name="fields[location]"]').val();
			var post_category 	= $('SELECT[name="fields[category]"]').val();
			
			var error 			= false;
			
			$('#post_title_msg').html('');
			$('#post_body_msg').html('');
			$('#location_msg').html('');
			$('#category_msg').html('');
			
			if (post_title_val === '' || post_title_val === post_title_def) {
				$('#post_title_msg').html('Please enter a post title');
				error = true;
			}
			if ($.trim(post_body_val) === '' || $.trim(post_body_val) === '<br/>') {
				$('#post_body_msg').html('Please enter some content');
				error = true;
			}
			if (post_location == 0) {
				$('#location_msg').html('Please select location');
				error = true;
			}
			if (post_category == 0) {
				$('#category_msg').html('Please select category');
				error = true;
			}
			
			if (error === false) {
				$('#submit_button').after('<img src="/assets/img/ajax_spinner.gif" />').hide();
				$(this).closest('FORM').submit();
			}

		});

		
	};

	_init();

};

$(document).ready(function() {
	new Site();
});


