$(document).ready(function() {
	mac_pc();
	clear_text();
	if ($('.wysiwyg blockquote').length > 0) blockquote();
	if ($('body.home').length > 0) home_setup();
	if ($('.expand').length > 0) hide_reveal();
	if ($('.now_playing').length > 0) video_page();
	if ($('#option_blocks').length > 0) option_blocks();
	if ($('p.three_meds').length > 0) three_overs();
	if ($('#resize').length > 0) text_resize();
	if ($('.tooltip').length > 0) {
		$('a.tooltip').tooltip({
			fade:300,
			showURL:false,
			track:true,
			top:-45
		});
	}
});
function text_resize() {
	if ($.cookie('size') != '' || $.cookie('size') != null) {
		$('#container').attr('class',$.cookie('size'));
		$('#resize a').removeClass('active');
		$('#resize a.'+$.cookie('size')).addClass('active');
	}
	
	$('#resize a').click(function() {
		$('#container').attr('class',$(this).attr('class'));
		$.cookie('size',$('#container').attr('class'),{expires:365,path:'/'});
		$('#resize a').removeClass('active');
		$(this).addClass('active');
		return false;
	});
	
}
function option_blocks() {
	$('a.option').click(function() {
		$('a.option').removeClass('active');
		$(this).addClass('active');
		find_active();
		var i = $('a.option').index(this);
		if (!$('.howto:eq('+i+')').hasClass('open')) {
			if ($('.howto.open').length > 0) {
				$('.howto.open').slideUp(300,function() {
					$(this).removeClass('open');
					$('.howto:eq('+i+')').slideDown(300,function() { $('.howto:eq('+i+')').addClass('open'); });
				});	
			} else {
				$('.howto:eq('+i+')').slideDown(300,function() { $('.howto:eq('+i+')').addClass('open'); });			
			}
		}
		return false;
	});
	$('a.close').click(function() {
		$('a.option').removeClass('active');
		$('.howto').removeClass('open').slideUp(300);
		$('.row').css({'background-position':'-2000px 0px'});
		return false;
	});
	
	function find_active() {
		var x = $('#option_blocks a.active').position().left+91-15,//91 is half of a.option, 15 is half of traingle width
			ind = 0;
		$('.row').each(function(i,e) {
			$('.option',this).each(function(ia,ea) {
				if ($(this).hasClass('active')) ind = ia;
			});
		});
		$('.row').css({'background-position':'-2000px 0px'});
		var newx = (ind != 0) ? (x+(ind*14)-(14*(ind-1))) : (x+(ind*14));
		$('#option_blocks a.active').parent('.row').css({'background-position':newx+'px 182px'});
	}
}
function hide_reveal() {
	$('.expand').slideUp(0);
	$('.more').click(function() {
		$(this).parent('p').next('.expand').slideToggle(300);
		return false;
	});
}
function home_setup() {
	// carousel slideshow
	$('a.video img').fadeTo(0,.5);
	$('a.video').hover(function() {
		$('img',this).animate({'opacity':1},{duration:300,queue:false});
	},function() {
		$('img',this).animate({'opacity':.5},{duration:300,queue:false});
	});
	var c = 0,
		t = $('ul#center_scroll li').length;
	$('#carousel a.imglink').click(function() {
		var w = $('ul#center_scroll li:eq('+c+')').width()+14,
			x = $('ul#center_scroll').offset().left,
			d = x-w;
		if ($(this).attr('id') == 'b_left') {
			w = $('ul#center_scroll li:eq('+(c-1)+')').width()+14,
			d = x+w;
			c--;
		} else {
			c++;
		}
		$('ul#center_scroll').animate({'margin-left':d+'px'},{duration:300,queue:false,complete:function() { check_width(); }});
		enable_btns();
		return false;
	});
	function enable_btns() {
		if (t > 1) {
			if (c <= 0) {
				$('#carousel a#b_left').hide();
				$('#carousel a#b_right').show();
			} else if (c > 0 && c < (t-1)) {
				$('#carousel a#b_left').show();
				$('#carousel a#b_right').show();
			} else {
				$('#carousel a#b_left').show();
				$('#carousel a#b_right').hide();
			}
		}
	}
	enable_btns();
	function check_width() {
		var w = $(window).width()
			d = 0,
			y = $('ul#center_scroll').offset().left;
		
		$('ul#center_scroll li').each(function() {
			d += $(this).width()+14;
		});
		var a = d-(y*-1);
		if (a < w) {
			$('#carousel a#b_right').hide();
		} else {
			$('#carousel a#b_right').show();
		}
	}
	check_width();
	$(window).resize(function() {
		check_width();
	});
	// end carousel
	
	// slideshow at top
	$('#slideshow .circle img:gt(0), #slideshow .slide:gt(0)').fadeTo(0,0,function() { $(this).css({'display':'none'}); });
	var curr = 0,
		ttl = $('#slideshow .slide').length;
	function advance_slide() {
		$('#dots .circle').removeClass('active');
		$('#dots .circle:eq('+curr+')').addClass('active');
		$('#slideshow .slide:gt('+curr+'),#slideshow .slide:lt('+curr+')').animate({'opacity':0},{duration:300,queue:false,complete:function() { $(this).css({'display':'none'}); }});
		$('#slideshow .slide:eq('+curr+'),#slideshow .circle img:eq('+curr+')').css({'display':'block'});
		$('#slideshow .circle img:gt('+curr+'),#slideshow .circle img:lt('+curr+')').animate({'opacity':0},{duration:300,queue:false});
		$('#slideshow .slide:eq('+curr+'),#slideshow .circle img:eq('+curr+')').animate({'opacity':1},{duration:300,queue:false,complete:function() {
			if (jQuery.browser.msie) $(this).get(0).style.removeAttribute('filter');
		}});
	}
	$('#slideshow .circle').hover(function() {
		//$(this).css({'z-index':100});
		$('img',this).css({'display':'block'});
		$('img',this).animate({'opacity':1},{duration:300,queue:false,complete:function() {
			if (jQuery.browser.msie) $(this).get(0).style.removeAttribute('filter');
		}});
	},function() {
		//$(this).css({'z-index':0});
		if (!$(this).hasClass('active')) $('img',this).animate({'opacity':0},{duration:300,queue:false});
	});
	$('#slideshow .circle').click(function() {
		clearInterval(slide_int);
		curr = $('#slideshow .circle').index(this);
		advance_slide();
		return false;
	});
	var slide_int = setInterval(function() {
		if (curr < (ttl-1)) {
			curr++;
		} else {
			curr = 0;
		}
		advance_slide();
	},6000);
	// end slideshow
}
function clear_text() {
	$('input.text').each(function() {
		var text = $(this).attr('value');
		$(this).click(function() { if ($(this).attr('value') == text) $(this).attr('value',''); });
		$(this).blur(function() { if ($(this).attr('value') == '') $(this).attr('value',text); });
	});
}
function mac_pc() {
	if (navigator.appVersion.indexOf("Mac")!=-1) {
		$('body').addClass('mac');
	} else {
		$('body').addClass('pc');
	}
	$.each($.browser, function(i,e) {
		if (i != 'version') {
			$('body').addClass(i);
		} else {
			var v = String(e).split('.');
			$('body').addClass('v'+v[0]);
		}
	});
}
function blockquote() {
	$('.wysiwyg blockquote').each(function() {
		$(this).prepend('<img class="bubble" src="/library/images/background-blockquote.png" alt="Did You Know?" />');
	});
}
function video_page() {
	load_video();
	$('.video_link').each(function() {
		$(this).click(function() {
			var swfaddressPath = this.href.split("#");
			var l = swfaddressPath[1].replace('/','').replace('/','');
			SWFAddress.setValue(l);
			load_video();
			return false;
		});
	});
}
function load_video() {
	$('html, body').animate( { scrollTop: 0 },300);
	var swfaddressPath = SWFAddress.getPath().split("/");
	var video = swfaddressPath[1];
	if (video =='immune-attack') {
		var mybuttons = 'Copaxone..http://www.mslivingwell.org/new/treatment-options/copaxone/2/..Interferon..http://www.mslivingwell.org/new/treatment-options/interferon/4/..Tysabri..http://www.mslivingwell.org/new/treatment-options/tysabri/1/';
	} else if (video == 'carroles-story') {
		var mybuttons = 'How to Manage Your Symptoms..http://www.mslivingwell.org/new/manage-symptoms';
	} else {
		var mybuttons = '';
	}
	if (video != '') {
		var flashvars = { movie: '/library/videos/'+video+'.flv', poster:'/library/videos/'+video+'.jpg',buttons:mybuttons};
		var params = {wmode:'transparent'};
		var attributes = {};	
		swfobject.embedSWF("/library/flash/fancy_singerplayer.swf", "flash_player", "580", "326", "9.0.0", "/library/flash/expressInstall.swf", flashvars, params, attributes);
		
		$('.now_playing .video_wrap').each(function() {
			if ($(this).attr('id') == video) {
				$(this).removeClass('hide');
			} else {
				$(this).addClass('hide');
			}
		});
	}
}
function three_overs() {
	$('p.three_meds a').hover(function() {
		var id = $(this).attr('id');
		$(this).parent().css({'background-image':'url(/library/images/background-three-meds-'+id+'.gif)'});
	},function() {
		var id = $(this).attr('id');
		$(this).parent().css({'background-image':'url(/library/images/background-three-meds.gif)'});
	});
}
