function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

$(function() {
	var $addhistory = true;
	var $cookie = readCookie('js_enabled');
	if($cookie == undefined) {
		createCookie('js_enabled', 'true', 999);
	}
	
	var $hash = window.location.hash;
	
	$('.left-col .faq-section').hide();
	
	/*$('.left-col .faq-section h4').live('click', function() {
		var $this = $(this);
		$('#main .faq-section').hide();
		$this.parents('.faq-section').show();
		$this.siblings().show();
	});*/
	
	$('.faq-menu a').live('click', function() {
		var $this = $(this);
		var $href = $this.attr('href');
		if(!$this.hasClass('active')) {
			$('.faq-menu a').removeClass('active');
			$this.addClass('active');
			$('.faq-intro').hide();
			$('.faq-subsection').hide();
			$('#main .faq-section').hide();
			$($href).parents('.faq-section').show();
			$($href).show();
			if($($href).is('.faq-section')) {
				$($href).children('.faq-subsection').show();
			}
			
			if($addhistory) $.history.add($href.replace('#', ''));
			$(window).scrollTop(0);
			if($.browser.msie) $('body').hide().show();
		}
		return false;
	});
	
	$('.left-col a[href^="#faq"]:not(.toggle)').live('click', function() {
		var $this = $(this);
		var $href = $this.attr('href');
		$('.faq-menu a[href="'+$href+'"]').click();
		return false;
	});
	
	$('.left-col .faq').each(function() {
		var $this = $(this);
		$this.addClass('enhanced');
		$this.addClass('hide');
		$('a.toggle', $this).show();
		$('*:not(h6, a.toggle)', $this).hide();
	});
	
	$('.left-col .faq a.toggle').live('click', function() {
		var $this = $(this);
		var $href = $this.attr('href');
		var $sec = $this.parents('.faq-subsection:first');
		var $par = $this.parents('.faq:first');
		
		if(!$sec.is(':visible')) {
			$('.faq-menu a[href=#'+$sec.attr('id')+']').click();
		}
		
		if($par.hasClass('hide')) {
			$this.text('-');
			$par.removeClass('hide');
			$('*', $par).show();
			var $link = $href.substring(0, ($href.length-2)).replace('question', 'faq');
			if(!$('a[href="'+$link+'"]').hasClass('active') && !$sec.is(':visible')) {
				$('a[href="'+$link+'"]').click();
			}
		} else {
			$this.text('+');
			$par.addClass('hide');
			$('*:not(h6, a.toggle)', $par).hide();
		}
		
		if($addhistory) $.history.add($href);
		if($.browser.msie) $('body').hide().show();
		return false;
	});
	
	$('.left-col a[href^="#question"]:not(.toggle)').live('click', function() {
		var $this = $(this);
		var $href = $this.attr('href');
		$('.hide a.toggle[href="'+$href+'"]').click();
		return false;
	});
	
	$('.left-col .chapter').hide();
	
	$('.chapter-menu a').live('click', function() {
		var $this = $(this);
		var $href = $this.attr('href');
		if($href.match("#chapter-")) {
			if(!$this.hasClass('active')) {
				$('.chapter-menu a').removeClass('active');
				$this.addClass('active');
				$('.chapter').hide();
				$('.chapter-image').hide();
				$($href).show();
				$($href.replace('chapter', 'image')).show();
				if($('a.pdf').size() > 0) {
					$('a.pdf').attr('href', $('a.pdf').attr('name').replace('learning.pdf', 'learning-'+$href.replace('#chapter-', '')+'.pdf'));
				}
				if(!$.browser.msie) console.log($href);
				if($addhistory && $href != '#chapter-1') $.history.add($href.replace('#', ''));
				$(window).scrollTop(0);
				if($.browser.msie) $('body').hide().show();
			}
			return false;
		}
	});
	
	$('.left-col a[href^="#chapter"]').live('click', function() {
		var $this = $(this);
		var $href = $this.attr('href');
		$('.chapter-menu a[href="'+$href+'"]').click();
		return false;
	});
	
	if($hash == '') {
		$addhistory = false;
		$('.chapter-menu a:first').click();
		$addhistory = true;
	}
	
	$('input[type="text"].input, textarea.input').each(function() {
		var $this = $(this);
		$this.data('val', $this.attr('title'));
	});
	
	$('input[type="text"].input, textarea.input').focus(function() {
		var $this = $(this);
		if($this.val() == $this.data('val')) {
			$this.val('');
		}
	});
	
	$('input[type="text"].input, textarea.input').blur(function() {
		var $this = $(this);
		if($this.val() == '') {
			$this.val($this.data('val'));
		}
	});
	
	/*$('form').submit(function() {
		return false;					 
	});
	
	$('input[type="submit"]').live('click', function() {
		var $this = $(this);
		var $form = $this.parents('form:first');
		var $errors = false;
		$(':input.required', $form).each(function() {
			var $valid = true;
			if($(this).is(':checkbox')) {
				$valid = $(this).is(':checked');
			} else if($(this).is(':radio')) {
				$valid = ($(':radio[name="'+$(this).attr('name')+'"]:checked').val() != undefined);
			} else {
				$valid = ($(this).val() != '' &&  $(this).val() != $(this).data('val'));
			}
			if(!$valid) {
				$(this).addClass('error');
				$(this).parents('label:first').addClass('error');
				$errors = true;
			} else {
				$(this).removeClass('error');
				$(this).parents('label:first').removeClass('error');
			}
		});
		if($errors == true) {
			$('p.error').show();
		} else {
			$('p.error').hide();
			var $data = $form.serialize();
			$.post($form.attr('action'), $data, function() {
				if($form.siblings('p.complete').size() > 0) {
					$form.siblings('p.complete').show();
					$form.remove();
				} else {
					$form.get(0).reset();
				}
			});
		}
		return false;
	});*/
	
	$('input[name="anonymous"]').live('click', function() {
		var $this = $(this);
		if($this.is(':checked')) {
			$('p.anonymous').hide();
			$('p.anonymous').children('.required').removeClass('required').addClass('not-required');
		} else {
			$('p.anonymous').show();
			$('p.anonymous').children('.not-required').removeClass('not-required').addClass('required');
		}
	});
	
	$('input[name="anonymous"]:checked').click().click();
	
	$('input[name="tried"]').live('click', function() {
		var $this = $(this);
		if($this.is('[value="no"]')) {
			$('.not-tried').show();
		} else {
			$('.not-tried').hide();
		}
	});
	
	$('input[name="tried"][value="no"]:checked').click();
	
	$('input[name="responder"]').live('click', function() {
		var $this = $(this);
		if($this.val() == 'Patient') {
			$('p.patient').show();
			$('p.hcp').hide();
		} else {
			$('p.patient').hide();
			$('p.hcp').show();
		}
	});
	
	$('input[name="responder"]:checked').click();
	
	$('a.print').live('click', function() {
		window.print();
		return false;
	});
	
	$('#menu > li > ul').live('mouseover', function() {
		var $this = $(this);
		var $a = $this.siblings('a');
		if($.browser.msie) {
			$this.css('margin-top', '4px');
			$this.css('margin-left', '-4px');
		}
		$a.addClass('active');
	});
	
	$('#menu > li > ul').live('mouseout', function() {
		var $this = $(this);
		var $a = $this.siblings('a');
		$a.removeClass('active');
	});
	
	$('select[name="country"]').change(function() {
		var $this = $(this);
		if($this.val() != '') {
			var $val = $('option:selected', $this).attr('class');
			var $loc = window.location.toString().replace(window.location.search, '');
			var $cur = $this.attr('id');
			var $base = $('body').attr('rel');
			var $frm = $this.parents('form:first').serializeArray();
			//var $form = $this.parents('form:first').serialize();
			var $form = '';
			var $new;
			
			if($val == 'index.php') {
				$new = $loc.replace($base+$cur+'/', $base);
			} else if($cur == 'index.php') {
				$new = $loc.replace($base, $base+$val+'/');
			} else {
				$new = $loc.replace($base+$cur, $base+$val);
			}
			
			$.each($frm, function(i, $field){
				if($field.value != undefined && $(':input[name="'+$field.name+'"]').attr('title') != $field.value) {
					if($form == '') {
						$form = $field.name+'='+$field.value;
					} else {
						$form += '&'+$field.name+'='+$field.value;
					}
				}
			});
			
			$new += '?'+$form;
			
			if($cur != undefined) {
				window.location = $new;
			}
		}
	});
	
	if($.browser.msie && $.browser.version < 7) {
		$('#main').append('<div class="clear"></div>');
		$('#menu > li > a').live('mouseover', function() {
			var $this = $(this);
			$this.css('left', '-4px');
			var $ul = $this.siblings('ul:first');
			$ul.css('top', $this.height());
			//$ul.css('right', '-4px');
			$ul.show();
		});
		
		$('#menu > li > a').live('mouseout', function() {
			var $this = $(this);
			var $ul = $this.siblings('ul');
			$ul.hide();
		});
		
		$('#menu > li > ul').live('mouseover', function() {
			var $this = $(this);
			$this.show();
		});
		
		$('#menu > li > ul').live('mouseout', function() {
			var $this = $(this);
			$this.hide();
		});
		
		$('#footer').css('bottom', '0');
	}
	
	$('body').history(function(e, hash, prevHash) {
		if(hash != '' && hash != undefined) {
			$addhistory = false;
			$('a[href="#'+hash+'"]').click();
			$addhistory = true;
		} else {
			$addhistory = false;
			$('.chapter-menu a:first').click();
			$addhistory = true;
		}
    });
	
	if($hash != '') {
		$addhistory = false;
		$('a[href="'+$hash+'"]').click();
		$addhistory = true;
	}
});