$(document).ready( function(){

/*************************************************
* Layout Javascript                              *
**************************************************/

// Setup rounded corners
	$('li.menu,#contact,#subscribe,#contacts,a.button.button,div.models > ul > li,ul.homecomparison div,').corner();
	$('#featureslist > li').gxcorners('outside');
	$('div.models>li:even').css('clear','both');

//Menu rollovers

	$('#side-nav>li').hover(function(){
		$(this).toggleClass('selected');
		$('.menu',this).show();
	},function(){
		$(this).toggleClass('selected');
		$('.menu',this).hide();
	});

//Tables Alt Row
	$('table').each(function(){
		$('tr:even',this).addClass('altrow');
	});

//Init magnifiers
	$('.magnify').click(function(){
	  $(this).siblings('ul,table').show();
	});


//Make all forms ajax forms via jquery Form plugin

//Init footer gallery
	$('.footer-gallery a, .modelimg, .main').lightBox();


/*************************************************
* Page Secific Javascript                        *
**************************************************/

	// headline on the home page
	if( $('#Home')[0] ){
		// use the first span if there's no span with a class of "on"
		//var $headline = !$('h1 span.on')[0] ? $('h1 span:eq(0)') : $('h1 span.on');
		//$headline.fadeIn('slow');
		$('#home-copy div, #home-copy a').click(function(e){
			var url = e.target.nodeName.toLowerCase() == 'a' ? $(e.target).attr('href') : $(e.target).attr('rel');
			document.location = url;
			return false;
		});
		// load the cycle plugin
		$('#home-copy').cycle('fade');
	}

	//Features page accordion
	if($('#Features')[0]){
		$('#featureslist').accordion({
			clearStyle: true,
			autoHeight: false,
			active: false
		}).show();
	}

	if( $('body#Newsletter')[0] ){
		$('#subscribe form').ajaxForm();
	}

	//Overview Page Init Map
	if( $('#Overview')[0] ){
		$('#Overview').overview();
		if($('.address').length > 0){
			$('#example li h3 span:gt(0)').append(' ...');
			$('.features>li').hover(function(){
				$(this).css('background','#EFEBE5');
			},function(){
				$(this).css('background','white');
			});
		}
	}

	//Models lightbox
	if($('#Models')[0]){
		$('.models .extras').each(function(){
			$('.image',this).each(function(){
				$(this).lightBox();
			});
		});
	}

	if( $('#About')[0] ){
		$('#featureslist').accordion({
			clearStyle: true,
			autoHeight: false,
			active: false
		}).show();
	}

	// enable directions behavior on any to-action block
	$('div.to-action form').mapdirections();

	//Setup form and form items
	if( $('#Contact')[0] ){
		$('#contact form').ajaxForm();
		$('#contact #reset').click(function(){
			$(this).parents('form').resetForm();
			$('#mailresponse').empty();
			//$('#contact form :input').css('background','#FFFFFF');
		});

		//$('#contact form :input').focus(function(){$(this).css('background','#FFFFFF')});

		var err = function(msg){
			$('#mailresponse').html('<span class="error">' + msg + '</span>');
		}

		$('#contact form').submit(function() {
			// make sure email is filled out
			var email = $('input[name=email]').val();
			var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
			var regex = new RegExp(emailReg);
			if(!email || !regex.test(email)){
				err('Please enter a valid email address.');
				return false;
			}
			// continue
			var getstring = $(':input',this).filter(function(){ return $(this).parents().not(':visible').length == 0; }).fieldSerialize();
			// submit the form
			$('#mailresponse').empty();
			$.get(
				$(this).attr('action') + '?' + getstring,
				function(rhtml){
					// put the response on the page
					$('#mailresponse').html(rhtml);
					// if we have an error, no further processing
					if($('.error', rhtml)[0]){
						return false;
					}
					// check for subscription request
					if($('#newsletter').attr('checked')){
						//console.log('Subscribing');
						var googleFrame = document.getElementById('google');
						//googleFrame.src = 'http://groups.google.com/group/montagedev/boxsubscribe?email='+$('.email').val()+'&sub=Subscribe';
						//console.log($('#googlemessage span'));
						$('#googlemessage span').empty().append($('.email').val());
						$('#googlemessage').show();
					}
					// return false to stop browser-level form submission
					return false;
				}
			);
			// track submission
			pageTracker._trackPageview( '/contact/' + $('#subject').val() );
			// return false to prevent normal browser submit and page navigation
			return false;
		});

		$('#subscribe form').submit(function() {
			subscribe();
			return false;
		});

		$('#send').click(function(){
			$('#mailresponse').empty();
			$(this).parents('form').submit();
		});

		$('#subject').change(function(){
			$('li.message, li.moreinfo, li.meeting, li.priority', $('#contact')).hide();
			$('#mailresponse').empty();
			$('#contact form :input').css('background','#FFFFFF');
			var section = $('option:eq(' + this.selectedIndex + ')',this).attr('alt');
			$('#contact li.' + section).slideDown();
		});

	}
// End onready
});

/**
 * mapdirections plugin
 *
 * handles the pdaq standard directions form.
 */
(function($){
	$.fn.mapdirections = function(){
		return this.each( function(){

			// the <form> element
			var self 	= this;

			// enable the on/off behavior of the "your address" field
			$('[name="saddr"]',self).focus( function(){
				if( $(this).val() == 'Your address' ){
					$(this).val('');
				}
			}).blur( function(){
				if( $(this).val() == '' ){
					$(this).val('Your address');
				}
			});

			// handle submission
			$(self).unbind('submit').submit( function(){
				if( $('[name="saddr"]',self).val() == 'Your address' ){
					return false;
				}

				// tell analytics
				if( window.pageTracker ){
					window.pageTracker._trackPageview($(self).attr('action'));
				}

				// just submit the form, which is set to GET and will do a google maps search automatically
				self.submit();
			});
		});
	}
})(jQuery);
