$(document).ready(function(){
	
	//Positioning the lightbox content
	var pw = $('#master').width()/2 - (($('.lb-content').width())/2)-30;
	var ph = $('#master').height() + 300;
	var pl = $('#master').width()/2;
	var fullWidth = $(window).width();
	$('.lb-content').css('left', pw);
	
	var siteWidth = (fullWidth / 2) - pl;
	
	
	$('.plan-tool .tabs li').click(function(){
		$('.plan-tool .tabs li').removeClass('current');
		$('.plan-tool > div').removeClass('current');
		var currClass = $(this).attr('class');
		$('.plan-tool > div.'+currClass).addClass('current');
		$(this).addClass('current');
		
		$('.memberSpotMed li').each(function() {
			$(this).html('');
			$(this).removeClass('selected');
		});
		
		$('.plan-list :checkbox').attr('checked', false);
		
		return false;
	});
	
	

	$('.textSize3').click(function(){
		$('.landing-header dl.questions').removeClass('large-text');
		$('.landing-header dl.questions').removeClass('med-text');
		$('.landing-header dl.questions').addClass('large-text');
	});
	$('.textSize2').click(function(){
		$('.landing-header dl.questions').removeClass('large-text');
		$('.landing-header dl.questions').removeClass('med-text');
		$('.landing-header dl.questions').addClass('med-text');
	});
	$('.textSize1').click(function(){
		$('.landing-header dl.questions').removeClass('large-text');
		$('.landing-header dl.questions').removeClass('med-text');
	});

	//############################
	//#### Lightbox Functionality
	$('.lb-background').css('height', ph).css('width', fullWidth).css('left',-siteWidth);
	
	$('a.request').click( function(){
		$('.lb-content').css('left', pw);
		
		$('.lb-background').fadeIn('medium');
		$('.lb-content').fadeIn('medium');

		return false;
	});
	
	//## Close lightbox
	$('.lb-background, .close').click(function(){
		$('.lb-background, .lb-content').fadeOut('medium');
		return false;
	});
	
	$(window).resize(function(){
	  	var fullWidth = $(window).width();
		var siteWidth = (fullWidth / 2) - pl;
		$('.lb-background').css('width', fullWidth).css('left',-siteWidth);
	});
	
	// $("#zipCode").bind("focus", function() {
	// 		$(this).attr("value", "");
	// 	});
	
	$('#zipCode').focus(function() { if( this.value == this.defaultValue ) { this.value = ""; } }).blur(function() { if( !this.value.length ) { this.value = this.defaultValue; } });
	
	
	$("#plan-table .close").bind("click", removeColumn);
	
	
	$('.plan-list :checkbox').attr("checked", false);
	$('.plan-list :checkbox').bind("click", function() {
	
		//if((this).checked==false){}
		// check to see if there are more than three...
		// if so then don't let them check any more!
		var i = 0;
		$('.plan-list :checkbox').each(function() {
			if($(this).is(":checked")) { i++; }
		});
		if(i > 3) {
			$(this).attr('checked', false);
			alert("You've already selected three plans, please uncheck one to continue");
			return false;
		}
	
		
		// Set the globals
		var plan = $(this).siblings('h6').html();	
		if($(this).attr('checked') == true) { 
			var found = false;
			$('.memberSpotMed li').each(function() {
				if(!$(this).hasClass('selected') && !found) {
					$(this).addClass('selected');
					var planStripped = plan.replace(/(<br\s*\/?>)+/gi, " ");
					
					$(this).html('<h5>' + plan + '</h5><a href="#" class="close"></a><input type="hidden" name="plan[]" value="'+planStripped+'" />');
					found = true;
				}
			});
		} else {
			$('.memberSpotMed dd li').each(function() {
			    if($(this).html().indexOf("<h5>" + plan + "</h5>") >= 0) { 
			    	$(this).html(""); 
			    	$(this).removeClass("selected");
			    }
			});
		}
		$("#SpotMed2").html($('.memberSpotMed:first').html());
		$('a.close').bind("click", searchAndDestroy);
	});
	
	
	$('.shadetabs li a').click(function(){
		var thisTab = $(this).attr('class');
		//alert(thisTab);
		$('#ajaxcontentarea').removeClass();
		$('#ajaxcontentarea').addClass('contentstyle');
		$('#ajaxcontentarea').addClass(thisTab);
	});
	

	
	$('.plan-tool td dt').each(function() {
		planText = $(this).children('strong').html();
		//console.log(planText);
		$(this).children('strong').html(planText.replace(/(^[a-zA-Z]+\s[a-zA-Z]+)/gi,'$1<br/>'));
		$(this).children('strong:contains("Original Medicare")').siblings('a').css('display','none');
		 
		//$(this).html(planText.replace('no','<br/>(no dRx)'));
	});
	
});

function removeColumn() {
	var index = parseInt($(this).closest("tr").children().index($(this).closest("td")));
	$("#plan-table").removeCol(index + 1);
}

function searchAndDestroy() {
	var plan = $(this).prev().html();
	var parent = $(this).parent();
	
	$('.plan-list .plans input').each(function() {
		
		if($(this).siblings('h6').html()==plan){
			$(this).attr("checked", '');
		}
	});
	
	parent.removeClass("selected");
	parent.html("");
	
	$("h6").filter(function (index) {
	    return $(this).html().search("^"+plan.replace("(","\\(").replace(")","\\)")+"$") + 1;
	}).next(":checkbox").attr("checked", false);
		
	if(parent.parent().parent().parent().attr("id") == "SpotMed2") {
		$('.memberSpotMed:first').html($("#SpotMed2").html());
	} else {
		$("#SpotMed2").html($('.memberSpotMed:first').html());
	}
	$('a.close').bind("click", searchAndDestroy);
	return false;
}

$.fn.removeCol = function(col){
    // Make sure col has value
    if(!col){ col = 1; }
    $('tr td:nth-child('+col+'), tr th:nth-child('+col+')', this).remove();
	//alert($('tr td:nth-child(2)').length);
	
		if($('.plan-tool td dt').length==0){
			history.go(-1);
		}

};