google.load("jquery", "1.4.2");google.load("jqueryui", "1.8.2");google.setOnLoadCallback(function(){ $(document).ready(function(){ 

//posts
	//header toggle
	$('.dh_head').click(function() {
		$(this).toggleClass("dh_active");
		$(this).next().toggle('slow');
		return false;
	});

// general
	//for jquery ui to style all buttons
	$('button, input:button, input:submit').button();
	
	//prevent enter from submitting form on any text field
	$("input.noSubmit").keypress(function(e){
		var k=e.keyCode || e.which;
			if(k==13){
				e.preventDefault();
			}
	});

//homepage
	//flash signup section
	$("div#dh_signup").stop().css("background-color", "#FFFF9C").animate({ backgroundColor: "#FFFFFF"}, 1500);
	
//home
	//show more activity in home stream
	$('#showMore_submit').click(function(){
		var msgLast = $("ul#homeStream li:last-child").attr("id");
		$('#homeStream').mask();
		$(this).attr('disabled','disabled');
		$.post('http://dermhub.com/wp-content/themes/dermhub-v3/procWallMsg.php',{'oldestMessage': msgLast},
		function(data){
			$('#homeStream').unmask();
			if(data.dhStatus){
				$(data.dhStatus).hide().appendTo('ul#homeStream').slideDown('slow');
				$('#showMore_submit').removeAttr('disabled');
					if($("ul#homeStream li:last-child[id='msg1']").length > 0){$('#showMore_submit').hide();} 
				}
			},'json');
	});
	
	//add message to stream
	$('#homeInput_submit').click(function(){
		$('#homeStream').mask();
		$(this).attr('disabled', 'disabled');
		$('#homeInput').attr('disabled', 'disabled');
		$.post('http://dermhub.com/wp-content/themes/dermhub-v3/procWallMsg.php',
		{'message':$('#homeInput').val(), 'lastMsg': $("ul#homeStream li:first").attr("id")},
		function(data){
			$('#homeStream').unmask();
			if(data.dhStatus){
				$(data.dhStatus).hide().prependTo('ul#homeStream').slideDown('slow'); 
				$('#homeInput').val('');$('#homeInput_submit').removeAttr('disabled');
				$('#homeInput').removeAttr('disabled');
			} else if(data.dhError){
				$('ul#homeStream').html(data.Error);
			}
		},'json');
	});
	

//account registration

	//change profile pic
	$('#profilePic').change(function(){
		if($(this).val()=='fb'){
			$('#fbPhoto').show();
			$('#picFrame').removeClass('male1 male2 male3 male4 male5 male6 male7 male8 female1 female2 female3 female4 female5 female6 female7 female8');
		} else { 
			var classVal = $(this).val();
			$('#fbPhoto').hide();
			$('#picFrame').removeClass('male1 male2 male3 male4 male5 male6 male7 male8 female1 female2 female3 female4 female5 female6 female7 female8').addClass(classVal);
		
		}
	});

	//check for username availability
	$('#username').blur(function() {
		$.post("http://dermhub.com/wp-content/themes/dermhub-v3/procUsername.php",
		{"username": $(this).val()},
		function(data){
			if(data.success){
				$('#username_error').text(data.success);
				$('#username_error').removeClass('errorMsg').addClass('okMsg');
			}
			if(data.nogood){
				$('#username_error').text(data.nogood);
				$('#username_error').removeClass('okMsg').addClass('errorMsg');
			}
		},'json'
		); 
	});
	
	//create account
	$("#registerAccount").submit(function(){
		$("#contentMid").mask("<span>Registering your account...</span>",500);
		$.post("http://dermhub.com/wp-content/themes/dermhub-v3/procRegister.php",$("form#registerAccount").serialize(),function(data){ 
			$('#username_error').text('');
			$('#email_error').text('');
			$('#postalCode_error').text('');
			$('#birthdate_error').text('');
			if(data.username){$("#contentMid").unmask(); $('#username_error').text(data.username);}
			if(data.email){$("#contentMid").unmask(); $('#email_error').text(data.email);}
			if(data.postalCode){$("#contentMid").unmask(); $('#postalCode_error').text(data.postalCode);}
			if(data.birthdate){$("#contentMid").unmask(); $('#birthdate_error').text(data.birthdate);}
			if(data.success){$('.loadmask-msg div span ').text('Account created!'); window.location.href = data.success;}}, 'json');
		return false;
	});
	
	//show skintype quiz
	$('#showQuiz').click(function(){$('#dialog-quiz').dialog('open');});

	//create dialog for birthday
	$('#datepicker').datepicker({changeMonth: true,changeYear: true,yearRange: '1910:2000' });
	
	//array for skin conditions
	var availableTags = ["Acne", "Bed Sores", "Calluses", "Corns", "Dry Skin", "Keratosis Pilaris", "Pityriasis Rosea", "Psoriasis", "Rosacea", "Sebaceous Cysts", "Alopecia", "Scars", "Rashes", "Skin Sweating", "Skin Infection"];
	
	//autocomplete field for conditions
	$("#condition").autocomplete({source: availableTags});
	
	//condition submission
	$('#condition').keyup(function(e) {
	if(e.keyCode == 13) {
		if (/\S/.test($('#condition').val())) {
		$('ul#conditionList').prepend('<li>'+$(this).val()+'<input type="hidden" name="conditions[]" value="'+ $(this).val() + '"/><a href="#" class="clearitem">x</a></span></li>');
		$(this).val('');
		} return false;
	}
	});
	
	//remove condition from list
	$('ul#conditionList a.clearitem').live('click',function(){
		$(this).parent().remove();
		return false;
	});
	
	$('.favlink').click(function(){
		alert($(this).closest('li').attr('id'));
	});
	
	//$('ul#productResults li').draggable({helper:'clone',opacity:0.5});

//dermbox

	//create new dermbox list
	$('#dermBox_Create').click(function() {$('#dialog-form').dialog('open');});

	//form for creating new dermbox list
	$("#dialog-form").dialog({
		buttons: { Submit: function() {
			$('#dialog-form').dialog("close");
			$('ul#dermboxLists').mask();
			$.post('http://dermhub.com/wp-content/themes/dermhub-v3/procDermbox.php',
				{'listName': $('#dermboxListName').val(), 'listDescription': $('#dermboxListDescription').val()},
				function(data){
					if(data.listStatus){  
						$('#dermboxListName').val('');
						$('#dermboxListDescription').val('');
						$('ul#dermboxLists').unmask();
						$('p#emptyList').hide();
						$(data.listStatus).hide().prependTo('ul#dermboxLists').slideDown('slow');
						$('#dermboxListSelect').empty();
						$('ul#dermboxLists li').each(function(index){$('#dermboxListSelect').append('<option value="'+$(this).attr('id')+'">'+$(this).text()+'</option>');});
					}
				},'json');
				}},
		autoOpen: false,
		height: 220,
		width: 270,
		resizable: false,
		modal: true
	});
	
	//add item to dermbox list
	$('.dhBoxAddList').live('click',function(){
		$('#dermboxListItem').val($(this).parents('li').attr('id'));
		$('#dermboxListItemTitle').val($('#title'+$(this).parents('li').attr('id')).text());
		$('#dermboxListItemBrand').val($('#brand'+$(this).parents('li').attr('id')).text());
		$('#dermboxAddform').dialog('open');
	});
	
	//modal form for adding item to dermbox list
	$("#dermboxAddform").dialog({
		buttons: { 'Submit': function() { $(this).dialog("close");
		$('#'+$('#dermboxListItem').val()).mask();
		$.post('http://dermhub.com/wp-content/themes/dermhub-v3/procDermbox.php',
			{'dermboxList':$('#dermboxListSelect').val() ,
			'dermboxItem':$('#dermboxListItem').val(),
			'dermboxItemTitle':$('#dermboxListItemTitle').val(),
			'dermboxItemBrand':$('#dermboxListItemBrand').val(),
			'dermboxItemDescription':$('#dermboxItemDescription').val()},
			function(data){
				$('#'+$('#dermboxListItem').val()).unmask();
				$('#'+$('#dermboxListItem').val()+':not(:has(.inDHbox))').append('<span class="inDHbox"></span>');
			},
			'json');
		}},
		autoOpen: false,
		resizable: false,
		modal: true
	});
	
	//add item to general collection
	$('.dhBoxAddCollection').live('click',function(){
		var thisSpan = $(this).attr('id');
		var itemLI = $(this).parents('li').attr('id');
		$('#'+itemLI).mask();
		$.post('http://dermhub.com/wp-content/themes/dermhub-v3/procDermbox.php', 
		{'dermboxAddToCollection': itemLI, 
		'dermboxCollectionTitle':$('#title'+itemLI).text(), 
		'dermboxCollectionBrand':$('#brand'+itemLI).text()}, 
		function(data){
			$('#'+itemLI).unmask();
			if(data.success){
				$('#'+itemLI+':not(:has(.inDHbox))').append('<span class="inDHbox"></span>');
				//$('#modalFormText').html(data.success);
				//$('#modal-form').dialog('open');
				$('#'+thisSpan).removeClass('dhlink dhBoxAddCollection')
				$('#'+thisSpan).text('In Collection');
			}
		},'json');
	});
	
	//collection modal form
	$("#modal-form").dialog({autoOpen:false,modal:true,resizable:false,buttons: { Ok: function() { $(this).dialog('close'); } }});

	//create list prep
	$('#dermboxListSelect').empty();
	$('ul#dermboxLists li').each(function(index) {
		$('#dermboxListSelect').append('<option value="'+$(this).attr('id')+'">'+$(this).text()+'</option>');
	});
	
	//search for products
	$('#dermboxSearch_Submit').click(function(){
		$('#dermboxSearchKeyword').attr('disabled','disabled');
		$('#dermboxSearch_Submit').attr('disabled','disabled');
		$('#homeStream').mask();
		jQuery.bbq.pushState('#product='+$('#dermboxSearchKeyword').val()+"&page=1");
	});
	
	//search for product with keypress
	$('#dermboxSearchKeyword').keyup(function(e) {
		if(e.keyCode == 13) {
			$('#dermboxSearchKeyword').attr('disabled','disabled');
			$('#dermboxSearch_Submit').attr('disabled','disabled');
			$('#homeStream').mask();
			jQuery.bbq.pushState('#product='+$('#dermboxSearchKeyword').val()+"&page=1");
		}
		return false;
	});

$(window).bind( 'hashchange', function(e) {
	var deserial = $.deparam.querystring($.param.fragment());
	$('#homeStream').mask();
	var postString = '';
	
	
	if(deserial['product']) {
	$.post('http://dermhub.com/wp-content/themes/dermhub-v3/procDermbox.php',
		{
			'dermboxSearchKeyword': deserial['product'], 'dermboxSearchPageIndex': deserial['page']
		},
		function(data){
			$('#homeStream').unmask();
			$('#dermboxSearchKeyword').val('');
			$('#dermboxSearchKeyword').removeAttr('disabled');
			$('#dermboxSearch_Submit').removeAttr('disabled');
			if(data.productResults){
				$('#productResults').html(data.productResults);
				$('#productPages').html(data.productPages);
				$('#resultHeader').text('Search results for "'+data.searchTerm+'"');
			}
		},'json');
	} else if (deserial['list']) {
		$.post('http://dermhub.com/wp-content/themes/dermhub-v3/procDermbox.php',
			{
				'dermboxListID': deserial['list'], 'dermboxSearchPageIndex': deserial['page']
			},
			function(data){
				$('#homeStream').unmask();
				if(data.productResults){
					$('#productResults').html(data.productResults);
					$('#productPages').html(data.productPages);
					$('#resultHeader').text('Your DermBox Collection');
				}
			},'json');
		
	}
});
	
	//format help
	$('#formatAddSection').click(function(){
		$('.formatInput:first').clone().appendTo('#formatEditorContainer');
		$('.formatInput:last input').val('');
		$('.formatInput:last textarea').val('');
	});
	
	$('.removeFormatInput').live('click',function(){
		$(this).parents('.formatInput').remove();
	});
	
	$('#formatInputRefresh').click(function(){
		var formatHelper=new Array();
		$('div.formatInput').each(function(i){
			headerWrapString_pre = '<h3 class="dh_head dh_active">';
			headerString = $(this).find('input').val();
			headerWrapString_post = '</h3>';
			if ($(this).find('input').val() === 'Overview' || $(this).find('input').val() === 'overview' || $(this).find('input').val() === '') {
				contentWrapString = '<div class="dh_overview">';
				headerWrapString_pre = '';
				headerString = '';
				headerWrapString_post = '';
			} else if ($(this).find('input').val() === 'References' || $(this).find('input').val() === 'references') { 
				contentWrapString = '<div class="dh_references">';
			} else {
				contentWrapString = '<div class="dh_content">';
			}
			formatHelper[formatHelper.length] = headerWrapString_pre+headerString+headerWrapString_post;
			formatHelper[formatHelper.length] = contentWrapString+$(this).find('textarea').val()+'</div>';
 		});
		var formatString = '';
		$.each(formatHelper,function(){
			formatString = formatString+this;
		});
		$('#formatPreview').val(formatString);
	});
	
	//skintype quiz
	$("#dialog-quiz").dialog({
				buttons: { "Ok": function() { 
				var skinVal = parseInt($('#dialog-quiz #q1').val())+parseInt($('#dialog-quiz #q2').val())+parseInt($('#dialog-quiz #q3').val())+parseInt($('#dialog-quiz #q4').val())+parseInt($('#dialog-quiz #q5').val())+parseInt($('#dialog-quiz #q6').val())+parseInt($('#dialog-quiz #q7').val())+parseInt($('#dialog-quiz #q8').val());
				if (skinVal>=0 && skinVal<=6){$('#skintype').attr('value','type1');$('#skintype_blurb').text("You always burn and never tan in the sun. You are extremely susceptible to skin damage as well as cancers like basal cell carcinoma and squamous cell carcinoma. You are also at very high risk for melanoma, the deadliest type of skin cancer. Generally follow The Skin Cancer Foundation's prevention tips (check www.skincancer.org) but use a sunscreen with a SPF of 30+ and clothing with a UPF rating of 30 or higher. Seek the shade whenever you are out in the sun. Check your skin head-to-toe each month, paying careful attention to any suspicious growths, and make sure you have an annual professional skin checkup.");$('#skintype_blurb').show('slow');}
				else if(skinVal>=7 && skinVal<=12){$('#skintype').attr('value','type2');$('#skintype_blurb').text("You almost always burn and rarely tan in the sun. You are highly susceptible to skin damage as well as cancers like basal cell carcinoma and squamous cell carcinoma. You are also at high risk for melanoma, the deadliest type of skin cancer. Generally follow The Skin Cancer Foundation's prevention tips (check www.skincancer.org) but also consider using a sunscreen with a SPF of 30+ and clothing with a UPF rating of 30 or higher. Seek the shade whenever you are out in the sun. Check your skin head-to-toe each month, paying careful attention to any suspicious growths, and make sure you have an annual professional skin checkup."); $('#skintype_blurb').show('slow');}
				else if(skinVal>=13 && skinVal<=18){$('#skintype').attr('value','type3');$('#skintype_blurb').text("You sometimes burn and sometimes tan in the sun. You are susceptible to skin damage as well as cancers like basal cell carcinoma and squamous cell carcinoma. You are also at risk for melanoma, the deadliest type of skin cancer. Be sure to apply a sunscreen with an SPF of at least 15 every day, wear sun-protective clothing, and seek the shade between 10 AM and 4 PM, when the sun is strongest. Check your skin head-to-toe each month, paying careful attention to any suspicious growths, and make sure you have an annual professional skin checkup."); $('#skintype_blurb').show('slow');}
				else if(skinVal>=19 && skinVal<=24){$('#skintype').attr('value','type4');$('#skintype_blurb').text("You tend to tan easily and are less likely to burn. But you are still at risk; use sunscreen with an SPF of 15+ outside and seek the shade between 10 AM and 4 PM. Follow all other Prevention Tips from The Skin Cancer Foundation as well. (See www.skincancer.org). Check your skin head-to-toe each month, paying careful attention to any suspicious growths, and make sure you have an annual professional skin checkup."); $('#skintype_blurb').show('slow');}
				else if(skinVal>=25 && skinVal<=30){$('#skintype').attr('value','type5');$('#skintype_blurb').text("You tan easily and rarely burn, but you are still at risk. Use sunscreen with an SPF of 15+ and seek the shade between 10 AM and 4 PM. Acral lentiginous melanoma, a very virulent form of the disease, is more common among darker-skinned people. These melanomas tend to appear on parts of the body not often exposed to the sun, and often remain undetected until after the cancer has spread. Check your skin head-to-toe each month, paying careful attention to any suspicious growths, and make sure you have an annual professional skin checkup. Keep an eye out for any suspicious growths, especially on the palms, soles of the feet and mucous membranes."); $('#skintype_blurb').show('slow');}
				else if(skinVal>=31){$('#skintype').attr('value','type6');$('#skintype_blurb').text("Although you do not burn, dark-skinned people are still at risk for skin cancers, and should wear sunscreen with a SPF of 15+ and seek the shade between 10 AM and 4 PM. Acral lentiginous melanoma, a very virulent form of the disease, is more common among darker-skinned people. These melanomas tend to appear on parts of the body not often exposed to the sun, and often remain undetected until after the cancer has spread. Check your skin head-to-toe each month, paying careful attention to any suspicious growths, and make sure you have an annual professional skin checkup. Keep an eye out for any suspicious growths, especially on the palms, soles of the feet and mucous membranes."); $('#skintype_blurb').show('slow');}
				$(this).dialog('close');
				}},
				autoOpen: false,
				height: 470,
				width: 415,
				resizable: false,
				modal: true
			});
			
	$("#skintype").change(function(){
		$('#skintype_blurb').hide();
		if ($(this).val()=='type1') { $('#skintype_blurb').text("You always burn and never tan in the sun. You are extremely susceptible to skin damage as well as cancers like basal cell carcinoma and squamous cell carcinoma. You are also at very high risk for melanoma, the deadliest type of skin cancer. Generally follow The Skin Cancer Foundation's prevention tips (check www.skincancer.org) but use a sunscreen with a SPF of 30+ and clothing with a UPF rating of 30 or higher. Seek the shade whenever you are out in the sun. Check your skin head-to-toe each month, paying careful attention to any suspicious growths, and make sure you have an annual professional skin checkup.");$('#skintype_blurb').show('slow');}
		else if ($(this).val()=='type2') { $('#skintype_blurb').text("You almost always burn and rarely tan in the sun. You are highly susceptible to skin damage as well as cancers like basal cell carcinoma and squamous cell carcinoma. You are also at high risk for melanoma, the deadliest type of skin cancer. Generally follow The Skin Cancer Foundation's prevention tips (check www.skincancer.org) but also consider using a sunscreen with a SPF of 30+ and clothing with a UPF rating of 30 or higher. Seek the shade whenever you are out in the sun. Check your skin head-to-toe each month, paying careful attention to any suspicious growths, and make sure you have an annual professional skin checkup."); $('#skintype_blurb').show('slow');}
		else if ($(this).val()=='type3') { $('#skintype_blurb').text("You sometimes burn and sometimes tan in the sun. You are susceptible to skin damage as well as cancers like basal cell carcinoma and squamous cell carcinoma. You are also at risk for melanoma, the deadliest type of skin cancer. Be sure to apply a sunscreen with an SPF of at least 15 every day, wear sun-protective clothing, and seek the shade between 10 AM and 4 PM, when the sun is strongest. Check your skin head-to-toe each month, paying careful attention to any suspicious growths, and make sure you have an annual professional skin checkup."); $('#skintype_blurb').show('slow');}
		else if ($(this).val()=='type4') { $('#skintype_blurb').text("You tend to tan easily and are less likely to burn. But you are still at risk; use sunscreen with an SPF of 15+ outside and seek the shade between 10 AM and 4 PM. Follow all other Prevention Tips from The Skin Cancer Foundation as well. (See www.skincancer.org). Check your skin head-to-toe each month, paying careful attention to any suspicious growths, and make sure you have an annual professional skin checkup."); $('#skintype_blurb').show('slow');}
		else if ($(this).val()=='type5') { $('#skintype_blurb').text("You tan easily and rarely burn, but you are still at risk. Use sunscreen with an SPF of 15+ and seek the shade between 10 AM and 4 PM. Acral lentiginous melanoma, a very virulent form of the disease, is more common among darker-skinned people. These melanomas tend to appear on parts of the body not often exposed to the sun, and often remain undetected until after the cancer has spread. Check your skin head-to-toe each month, paying careful attention to any suspicious growths, and make sure you have an annual professional skin checkup. Keep an eye out for any suspicious growths, especially on the palms, soles of the feet and mucous membranes."); $('#skintype_blurb').show('slow');}
		else if ($(this).val()=='type6') { $('#skintype_blurb').text("Although you do not burn, dark-skinned people are still at risk for skin cancers, and should wear sunscreen with a SPF of 15+ and seek the shade between 10 AM and 4 PM. Acral lentiginous melanoma, a very virulent form of the disease, is more common among darker-skinned people. These melanomas tend to appear on parts of the body not often exposed to the sun, and often remain undetected until after the cancer has spread. Check your skin head-to-toe each month, paying careful attention to any suspicious growths, and make sure you have an annual professional skin checkup. Keep an eye out for any suspicious growths, especially on the palms, soles of the feet and mucous membranes."); $('#skintype_blurb').show('slow');}
	});
});});