// a function that allows setting of default values on forms input elements
jQuery.fn.DefaultValue = function(text) {
    return this.each(function(){
		// make sure we're dealing with text-based form fields
		if (this.type != 'text' && this.type != 'password' && this.type != 'textarea')
			return;
		// store field reference
		var fieldCurrent = this;
		var fieldText = text;
		// set value initially if none are specified
		if (this.value=='') {
			this.value = text;
		}
		else {
			// other a value exists, ignore
			$(this).css({'color' : '#2A2C2B'});
		}
		// remove values on focus
		$(this).focus(function() {
			if ((this.value == text) || ('' == this.value)) {
				this.value = '';
				$(this).css({'color' : '#2A2C2B'});
			}
		});
		// place values back on blur
		$(this).blur(function() {
			if ((this.value == text) || ('' == this.value)) {
				this.value = text;
				$(this).css({'color' : 'gray'});
			}
		});
		// capture parent form submission and remove field values that are still default
		$(this).parents("form").each(function() {
			// bind parent form submit
			$(this).submit(function() {
				if (fieldCurrent.value == text) {
					fieldCurrent.value = '';
				}
			});
		});
	});
};

$(document).ready(function() {
	// blog posting effects
	$(".excerpt").hover(function() {
		$(this).css({backgroundColor:'#aaa',color:'#fff',border:'1px #fff dashed'});
		$(this).find('a').css({color:'#2A2C2B'});
	}, function() {
		$(this).css({backgroundColor:'#fff',color:'#2A2C2B',border:'1px #fff dashed'});
		$(this).find('a').css({color:'#35A0B4'});
	});
	// append shadow image to each LI
	$('#nav-shadow li').append('<img class="shadow" src="images/icons-shadow.png" width="81" height="27" alt="" />');
	// animate buttons, shrink and fade shadow
	$('#nav-shadow li').hover(function() {
		var $this = $(this);
		$this.find('a').stop().animate({marginTop:'-14px'}, 250, function() {
			$this.find('a').animate({marginTop:'-10px'}, 250);
		});
		$this.find('img.shadow').stop().animate({width:'80%', height:'20px', marginLeft:'8px', opacity:0.25 }, 250);
	}, function() {
		var $this = $(this);
		$this.find('a').stop().animate({marginTop:'4px'}, 250, function() {
			$this.find('a').animate({marginTop:'0px'}, 250);
		});
		$this.find('img.shadow').stop().animate({width:'100%', height:'27px', marginLeft:'0', opacity:1}, 250);
	});
	// the selected app should stick out more so change the opacity
	$('#nav-shadow li a').click(function() {
		$('#nav-shadow li').animate({opacity:0.7}, 250);
		$(this).parent().animate({opacity:1.0}, 250);
	});
	// add all of the image reflections
	$('img.reflect').reflect({height:0.15, opacity:0.7});
	// add the zooming
	$('a.zoom').fancybox();
	// add the button behavior for selecting app icons
	$('a.app').click(function () {
		var oldActive = $('.screens .active');
		var newActive = $($(this).attr('href'));
		if (oldActive.attr('id') == newActive.attr('id')) {
			return false;
		}
		// hide the old active screens
		oldActive.fadeTo(1000, 0.0, function() {
			oldActive.removeClass('active').addClass('inactive');
		});
		// show the new screens by fading them in
		newActive.removeClass('inactive').fadeTo(1000, 1.0, function() {
			newActive.addClass('active');
		});
		// hide the fanguide logo for anything not fanguide
		var theHref = $(this).attr('href');
		if (theHref.match('fg')) {
			if (theHref.match('fgop')) {
				$('#appstorebadge').attr('href', "http://click.linksynergy.com/fs-bin/stat?id=XmQ73pkY/YA&offerid=146261&type=3&subid=0&tmpid=1826&RD_PARM1=http%253A%252F%252Fitunes.apple.com%252Fus%252Fapp%252Fid340641621%253Fmt%253D8%2526uo%253D6%2526partnerId%253D30");
			}
			else if (theHref.match('fgrf')) {
				$('#appstorebadge').attr('href', "http://click.linksynergy.com/fs-bin/stat?id=XmQ73pkY/YA&offerid=146261&type=3&subid=0&tmpid=1826&RD_PARM1=http%253A%252F%252Fitunes.apple.com%252Fus%252Fapp%252Fid340640349%253Fmt%253D8%2526uo%253D6%2526partnerId%253D30");
			}
			else if (theHref.match('fgchi')) {
				$('#appstorebadge').attr('href', "http://click.linksynergy.com/fs-bin/stat?id=XmQ73pkY/YA&offerid=146261&type=3&subid=0&tmpid=1826&RD_PARM1=http%253A%252F%252Fitunes.apple.com%252Fus%252Fapp%252Fid340632620%253Fmt%253D8%2526uo%253D6%2526partnerId%253D30");
			}
			else if (theHref.match('fgla')) {
				$('#appstorebadge').attr('href', "http://click.linksynergy.com/fs-bin/stat?id=XmQ73pkY/YA&offerid=146261&type=3&subid=0&tmpid=1826&RD_PARM1=http%253A%252F%252Fitunes.apple.com%252Fus%252Fapp%252Fid340638229%253Fmt%253D8%2526uo%253D6%2526partnerId%253D30");
			}
			$('#fgbadge').show();
		}
		else {
			$('#appstorebadge').attr('href', "http://click.linksynergy.com/fs-bin/stat?id=XmQ73pkY/YA&offerid=146261&type=3&subid=0&tmpid=1826&RD_PARM1=http%253A%252F%252Fitunes.apple.com%252Fus%252Fapp%252Ffairway-golf%252Fid289224238%253Fmt%253D8%2526uo%253D6%2526partnerId%253D30");
			$('#fgbadge').hide();
		}
		// we handled all of the events so return false
		return false;
	});
	$('.screens .inactive').fadeTo(0, 0.0);
	// add the button behavior for top level navigation
	$('a.navlink').click(function () {
		// animate the pointer to the correct position
		var marginLeft = (($(this).width() - $('#pointer').width()) / 2.0) + $(this).offset().left + 9;
		$('#pointer').animate({marginLeft:marginLeft+'px'}, {duration:500});
		// scroll the content in
		$('#wrapper').scrollTo($(this).attr('href'), 500);
		// clear the selected styling
		$('a.navlink').removeClass('selected');
		$(this).addClass('selected');
		return false;
	});
	// find the div.fade elements and hook the hover event
	$('img.badge').hover(function() {
		$(this).animate({opacity:1.0}, "normal");
	}, function() {
		$(this).animate({opacity:0.7}, "normal");
	});
	// if it's a nav element we're going to the click the section so it lays out right
	var currentLocation = document.location.toString();
	if (currentLocation.match('#nav')) {
		var theAnchor = '#' + currentLocation.split('#')[1];
		$('ul#menu li a[href="' + theAnchor + '"]').click();
	}
	// otherwise click the first navigation item
	else {
		$('ul#menu li a[href="#navhome"]').click();
	}
});
// when the window resizes we have to keep our position in the left to right scroller
$(window).bind('resize', function() {
	var selected = $('ul#menu li a.selected');
	var marginLeft = ((selected.width() - $('#pointer').width()) / 2.0) + selected.offset().left + 9;
	$('#pointer').css({marginLeft:marginLeft+'px'});
	$('#wrapper').scrollTo(selected.attr('href'), 0);
});

