/**
 * Slide Box : a jQuery Plug-in
 * Samuel Garneau <samgarneau@gmail.com>
 * http://samgarneau.com
 *
 * Released under no license, just use it where you want and when you want.
 */

$(document).ready(function(){
	$.getJSON('/index.php', {maybelater: "get"}, function(json){
		if(json == true){
			show_slidebox();
		}
	});
});

function show_slidebox(){
	if( jQuery.browser.msie == true && jQuery.browser.version.charAt(0) == '6' ){
		$("#slidebox").slideBox({width: '100%', height: '70px', position: 'top'});
		$('.slide-panel').css('position', 'absolute');
		$('#outer_wrapper').css('padding-top', '40');
		$('.slide-panel').css('position', 'absolute');
	} else{
		$("#slidebox").slideBox({width: '100%', height: '70px', position: 'bottom'});
	}

	$('.slide-bottom').hide();
	$('.slide-bottom').fadeIn('slow');

	$('.slide-content .healthbeat_email_field').focus(function() {
		if($(this).val() == 'Enter E-mail Address') { $(this).val(''); }
	}).blur(function() {
		if($(this).val() == '') { $(this).val('Enter E-mail Address'); }
	});

	$('.slide-content .healthbeat_name_field').focus(function() {
		if($(this).val() == 'Enter First Name (Optional)') { $(this).val(''); }
	}).blur(function() {
		if($(this).val() == '') { $(this).val('Enter First Name (Optional)'); }
	});

	$('.slide-content form').submit(function(){
		try {
			pageTracker._trackEvent("Chrome Bar", "Submit");
		} catch(err) {}
	});
}

(function($){

	$.fn.slideBox = function(params){

		var content = $(this).html();
		var defaults = {
			width: '100%',
			height: '200px',
			position: 'bottom'			// Possible values : "top", "bottom"
		}

		// extending the fuction
		if(params) $.extend(defaults, params);

		var divPanel = $("<div class='slide-panel'>");
		var divContent = $("<div class='slide-content'>");

		$(divContent).html(content);
		$(divPanel).addClass('slide-' + defaults.position);
		$(divPanel).css("width", defaults.width);

		// centering the slide panel
		$(divPanel).css("left", (100 - parseInt(defaults.width))/2 + "%");

		// if position is top we're adding
		if(defaults.position == "top")
			$(divPanel).append($(divContent));

		// adding buttons
		$(divPanel).prepend("<div class='slide-header'></div>");
		$(divPanel).append("<div class='slide-button' id='slide-teaser'><img src='/images/misc/icon-healthbeat.gif' width='24' height='24' align='absbottom' border='0'> Sign up for <span style='font-size: 18px; font-weight: bold;'><span style='color: #990000;'>HEALTH</span>beat</span> and get FREE health information from the experts at Harvard Medical School. Sign up today &raquo;</div>");
		//$(divPanel).append("<div class='slide-button' id='slide-teaser'>Save 25% on <b>ALL Special Health Reports</b> through Friday, June 26. Simply enter <b>SAVEBIG25</b> at checkout to take advantage of our biggest sale ever. Learn more &raquo;</div>");
		$(divPanel).append("<div class='slide-button' style='display: none;'><span id='close-button'>[ Maybe Later ]</span> <span id='close-button-permanent'>[ Close this Window ]</span></div>");
		//$(divPanel).append("<div class='slide-button' style='display: none;'><span id='close-button'>[ <a href=\"https://www.health.harvard.edu/special_health_reports/\">View Special Health Reports</a> ]</span> <span id='close-button-permanent'>[ <a href='#'>Close this Window</a> ]</span></div>");

		if(defaults.position == "bottom")
			$(divPanel).append($(divContent));

		$(this).replaceWith($(divPanel));

		// Buttons action
		$(".slide-button").click(function(){
			$(".slide-button").toggle();
		});
		
		$('#slide-teaser').click(function(){
			$(divContent).animate({height: params.height}, 500);
		});

		$('#close-button').click(function(){
			$(divContent).animate({height: "0px"}, 500);
		});

		// Close button action:
		$('#close-button-permanent').click(function(){
			$(divContent).animate({height: "0px"}, 500);

			$('.slide-panel').hide();

			$.getJSON('/index.php', {maybelater: "unset"}, function(json){
				//console.log('Data: ' + json);
			});
			return false;
		});
	};

})(jQuery);
