var getQuerystring = function( key, default_ ) {
    if ( default_ == null ) {
        default_="";
    }
    var search = unescape( location.search );
    if ( search == "" ) {
        return default_;
    }

    search = search.substr( 1 );
    var params = search.split( "&" );
    for ( var i = 0; i < params.length; i++ ) {
        var pairs = params[i].split( "=" );
        if( pairs[0] == key ) {
            return pairs[1];
        }
    }
    return default_;
}



var setupIndexPage = function() {
	for ( i = 1 ; i <= currentDay ; i++ ) {
		var src = '/images/scrn' + i + '.png';
		var image = $( '<img />' ).attr( 'src', src );
	}

	$( 'area.day' ).each(
		function( i ) {
			var buttonDate = $( this ).attr( 'data-day' );
			if ( buttonDate <= currentDay ) {
				$( this ).mouseenter(
					function() {
						$( '#screenshot' ).attr( 'src', 'images/scrn' + buttonDate + '.png' );
					}
				);

			}
			else {
				$( this ).attr( 'href', '#' );
				$( this ).mouseenter(
					function() {
						$( '#screenshot' ).attr( 'src', 'images/scrnnotyet.png' );
					}
				);
			}
		}
	);

	$( 'area.day' ).mouseleave(
		function() {
			$( '#screenshot' ).attr( 'src', 'images/scrndefault.png' );
		}
	);
}


var setupAds = function() {
	$( '#copyright' ).append( '<iframe src="http://rcm.amazon.com/e/cm?t=httpadvent20c-20&o=1&p=48&l=ur1&category=electronicsrot&f=ifr" width="728" height="90" scrolling="no" border="0" marginwidth="0" style="border:none;" frameborder="0"></iframe>' );
}

function setupAddThis() {
	$( 'body' ).append( '\
	<div id="addThis">\
		<!-- AddThis Button BEGIN -->\
		<div class="addthis_toolbox addthis_default_style">\
		<a href="http://addthis.com/bookmark.php?v=250&amp;pub=proxyobjects" class="addthis_button_compact">Share</a>\
		<span class="addthis_separator">|</span>\
		<a class="addthis_button_email"></a>\
		<a class="addthis_button_facebook"></a>\
		<a class="addthis_button_twitter"></a>\
		</div>\
		<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pub=proxyobjects"></script>\
		<!-- AddThis Button END -->\
	</div>');
}

function setupSubMenu( type, sub ) {
	$( 'body' ).append( '<div id="submenu"></div>' );

	var buttons;
	var main = '';

	if ( type == 'iphone-christmas' ) {
		main = 'iPhone Christmas';

		if ( sub == 'games' ) {
			buttons = {
				'Games':'games-iphone-apps.html',
				'&nbsp;&nbsp;&nbsp;• Arcade':'arcade-iphone-games.html',
				'&nbsp;&nbsp;&nbsp;• Cards':'cards-iphone-games.html',
				'&nbsp;&nbsp;&nbsp;• Casino':'casino-iphone-games.html',
				'&nbsp;&nbsp;&nbsp;• Children':'children-iphone-games.html',
				'&nbsp;&nbsp;&nbsp;• Coloring':'coloring-book-iphone-games.html',
				'&nbsp;&nbsp;&nbsp;• Flight':'flight-iphone-games.html',
				'&nbsp;&nbsp;&nbsp;• Memory':'memory-iphone-games.html',
				'&nbsp;&nbsp;&nbsp;• Misc':'miscellaneous-iphone-games.html',
				'&nbsp;&nbsp;&nbsp;• Pinball':'pinball-iphone-games.html',
				'&nbsp;&nbsp;&nbsp;• Puzzle':'puzzle-iphone-games.html',
				'&nbsp;&nbsp;&nbsp;• Trivia':'trivia-iphone-games.html',
				'&nbsp;&nbsp;&nbsp;• Zombie':'zombie-iphone-games.html'
			};
		}
		else {
			buttons = {
				'Advent/<br>Countdown':'advent-and-countdown-iphone-apps.html',
				'Books':'books-iphone-apps.html',
				'Cards/<br>Greetings':'cards-and-greetings-iphone-apps.html',
				'Snow Globes':'snow-globe-iphone-apps.html',
				'Gifts/<br>Lists':'gifts-and-lists-iphone-apps.html',
				'Games':'games-iphone-apps.html',
				'Miscellaneous':'miscellaneous-iphone-apps.html',
				'Mistletoe':'mistletoe-iphone-apps.html',
				'Music':'music-iphone-apps.html',
				'Recipes':'recipes-iphone-apps.html',
				'Tree/<br>Wreath':'tree-and-wreath-iphone-apps.html'
			};
		}
	}
	else if ( type == 'giving' ) {
		main = 'Giving';
		buttons = {
			'Women':'christmas-gifts-for-women.html',
			'Men':'christmas-gifts-for-men.html',
			'Baby':'christmas-gifts-for-baby.html',
			'Kids':'christmas-gifts-for-kids.html',
			'Beauty':'christmas-beauty-gifts.html',
			'Electronics':'christmas-electronics-gifts.html',
			'Kitchen':'christmas-kitchen-gifts.html',
			'Games':'christmas-video-games-gifts.html'
		};
	}
	else if ( type == 'christmas-movies' ) {
		main = 'Christmas Movies';
		buttons = {
			'Children':'christmas-movies-for-children.html',
			'Comedy':'christmas-comedy-movies.html',
			'Drama':'christmas-drama-movies.html',
			'History':'christmas-history-movies.html',
			'Musical':'christmas-musical-movies.html',
			'Romantic<br>Comedy':'christmas-romantic-comedy-movies.html',
			'Western':'christmas-western-movies.html'
		};
	}

	var list_items = '';
	for ( var title in buttons ) {
		list_items += '<li><a href="/'+type+'/'+buttons[title]+'">'+title+'</a></li>';
	}
	$( '#submenu' ).append( '<a href="/'+type+'" class="main">'+main+'</a>' );
	$( '#submenu' ).append( '<ul>'+list_items+'</ul>' );
}

function setupCopyright() {
	$( '#content' ).append( '\n\
	<div id="copyright">\n\
		<p>Copyright &copy; 2009 <a href="http://www.proxyobjects.com/">ProxyObjects</a> and <a href="http://www.themakestudio.com/">The Make Studio</a>. All rights reserved.</p>\n\
		<p>Be sure to check out <a href="http://www.hymnsandcarolsofchristmas.com/">The Hymns and Carols of Christmas</a>.  It\'s a treasure trove!</p>\n\
	</div>'
	);
}

function setupMenu() {
	var buttons = {
		'home':'/index.html',
		'giving':'/giving',
		'iphone_xmas':'/iphone-christmas',
		'xmas_music':'/christmas-music',
		'xmas_movies':'/christmas-movies',
		'how_to':'/how-to.html',
		'support_contact':'http://forum.proxyobjects.com/viewforum.php?f=4'
	};

	var urlMatches = {
		'iphone-christmas':'iphone_xmas',
		'christmas-music':'xmas_music',
		'christmas-movies':'xmas_movies',
		'giving':'giving',
		'index.html':'home',
		'how-to.html':'how_to'
	};

	var urlMatcher = /^http:\/\/[^\/]+\/([^\/]*)/;
	var selected = 'home';
	var match = urlMatcher.exec( window.location.href );
	if ( match[1] ) {
		selected = urlMatches[match[1]];
	}

	var list_items = '';
	var i = 0;
	var button;
	for ( button in buttons ) {
		if ( i > 0 ) {
			list_items += '<li><img src="/images/menu_pipe.png"></li>';
		}
		list_items += '<li data-button="'+button+'" data-link="'+buttons[button]+'"></li>';
		i++;
	}

	$( 'body' ).append( '<div id="advent2009menu"><ul>'+list_items+'</ul></div>' );

	var absoluteUrlRegExp = /^http:\/\//

	$( 'div#advent2009menu ul li' ).each(
		function( i ) {
			var button = $( this ).attr( 'data-button' );
			if ( button ) {
//				window.alert( buttons[button] );
				var link = $( this ).attr( 'data-link' );
				var newWindow = link.match( absoluteUrlRegExp );
				var defaultImage = '';
				var hoverImage = "/images/menu_"+button+"_over.png";

				if ( selected == button ) {
					defaultImage = "/images/menu_"+button+"_selected.png";
					hoverImage = defaultImage;
				}
				else {
					defaultImage = "/images/menu_"+button+".png";
					hoverImage = "/images/menu_"+button+"_over.png";
				}

				if ( newWindow ) {
					$( this ).html( '<a href="'+link+'" target="_new"><img class="'+button+'" src="'+defaultImage+'" /></a>' );
				}
				else {
					$( this ).html( '<a href="'+link+'"><img class="'+button+'" src="'+defaultImage+'" /></a>' );
				}
				// Set the mouse over
				$( 'img.' + button ).mouseover(
					function() {
						$( this ).attr( 'src', hoverImage );
					}
				);

				// Set the mouse out
				$( 'img.' + button ).mouseout(
					function() {
						$( this ).attr( 'src', defaultImage );
					}
				);
			}
		}
	);
}

var setupDetailPage = function() {
	if ( detailDay > currentDay ) {
		window.location.replace( "/" );
	}
}

$( document ).ready(
	function() {
		currentDay = -1;
		var currentDate = new Date();
		var decemberFirst = new Date();
		var christmas = new Date();

		decemberFirst.setFullYear( 2009 );
		decemberFirst.setMonth( 11 ); // 0 - 11
		decemberFirst.setDate( 1 );   // 1 - 31
		decemberFirst.setHours( 0 );
		decemberFirst.setMinutes( 0 );
		decemberFirst.setSeconds( 0 );

		christmas.setFullYear( 2009 );
		christmas.setMonth( 11 ); // 0 - 11
		christmas.setDate( 25 );  // 1 - 31
		christmas.setHours( 0 );
		christmas.setMinutes( 0 );
		christmas.setSeconds( 0 );

		if ( currentDate > christmas ) {
			currentDay = 25;
		}
		else if ( currentDate > decemberFirst ) {
			currentDay = currentDate.getDate();
		}

		if ( getQuerystring( 'day', 'none' ) != 'none' ) {
			currentDay = getQuerystring( 'day', 'none' ) * 1;
		}


		setupMenu();
		setupAddThis();
		setupCopyright();
		setupAds();
		$( 'body' ).append( '<img src="/images/smallicon.png" style="position:fixed; top:0px;left:0px;" />' )
	}
);
