/** Clear Header Form Elements and Reset if no text **/
function clickclear(thisfield, defaulttext) {
	if (thisfield.value == defaulttext) {
		thisfield.value = "";
	}
}
function clickrecall(thisfield, defaulttext) {
	if (thisfield.value == "") {
		thisfield.value = defaulttext;
	}
}
/*********************
//* jQuery Multi Level CSS Menu (horizontal)- By Dynamic Drive DHTML code library: http://www.dynamicdrive.com
//* Menu instructions page: http://www.dynamicdrive.com/dynamicindex1/ddlevelsmenu/
//* Last modified: Sept 6th, 08'. Usage Terms: http://www.dynamicdrive.com/style/csslibrary/tos/
*********************/

//Specify full URL to down and right arrow images (25 is padding-right to add to top level LIs with drop downs):

// Arrows
//var arrowimages={down:['downarrowclass', '/images/global/spacer.gif', 25], right:['rightarrowclass', '/images/global/spacer.gif']}

var jquerycssmenu={

fadesettings: {overduration: 350, outduration: 100}, //duration of fade in/ out animation, in milliseconds

buildmenu:function(menuid, arrowsvar){
	jQuery(document).ready(function($){
		var $mainmenu=$("#"+menuid+">ul")
		var $headers=$mainmenu.find("ul").parent()
		$headers.each(function(i){
			var $curobj=$(this)
			var $subul=$(this).find('ul:eq(0)')
			this._dimensions={w:this.offsetWidth, h:this.offsetHeight, subulw:$subul.outerWidth(), subulh:$subul.outerHeight()}
			this.istopheader=$curobj.parents("ul").length==1? true : false
			$subul.css({top:this.istopheader? this._dimensions.h+"px" : 0})
			/** Arrows **/
			/*$curobj.children("a:eq(0)").css(this.istopheader? {paddingRight: arrowsvar.down[2]} : {}).append(
				'<img src="'+ (this.istopheader? arrowsvar.down[1] : arrowsvar.right[1])
				+'" class="' + (this.istopheader? arrowsvar.down[0] : arrowsvar.right[0])
				+ '" style="border:0;" />'
			)*/
			$curobj.hover(
				function(e){
					var $targetul=$(this).children("ul:eq(0)")
					this._offsets={left:$(this).offset().left, top:$(this).offset().top}
					var menuleft=this.istopheader? 0 : this._dimensions.w
					menuleft=(this._offsets.left+menuleft+this._dimensions.subulw>$(window).width())? (this.istopheader? -this._dimensions.subulw+this._dimensions.w : -this._dimensions.w) : menuleft
					//$targetul.css({left:menuleft+"px"}).fadeIn(jquerycssmenu.fadesettings.overduration)
					$targetul.css({left:menuleft+"px"}).show()
				},
				function(e){
					//$(this).children("ul:eq(0)").fadeOut(jquerycssmenu.fadesettings.outduration)
					$(this).children("ul:eq(0)").hide()
				}
			) //end hover
		}) //end $headers.each()
		$mainmenu.find("ul").css({display:'none', visibility:'visible'})
	}) //end document.ready
}
}

//build menu with ID="headernav" on page:
jquerycssmenu.buildmenu("headernav")
//jquerycssmenu.buildmenu("headernav", arrowimages)

/* Script to enable hover function */
$().ready(function() {
	  if ($('.hoverable') != null)
	  {
		  $('.hoverable').hover(
			   function() {
				   $(this).addClass('hovered');
		  }, function() {
				$(this).removeClass('hovered');
		  });
	  }
});

// Search Stories Flyout
// Reset Mask Values to fix overlapping on Mac Safari and Firefox
// Give the element an ID of top_level to fix this issue

searchStoriesOpen = 0; // Variable to see if both flyups are open when the get stronger flyup is called
getStrongerOpen = 0; // Var to see if get stronger is open

function allowOverLap() {
	$("#top_level").css({ 'z-index': "1" });
	$(".tools_share_box").css({ 'z-index': "1" });
	$("#refer_form_container").css({ 'z-index': "1" });
	$("#footer").css({ 'z-index': "997" });
}
function hideOverLap() {
	$("#top_level").css({ 'z-index': "997" });
	$(".tools_share_box").css({ 'z-index': "997" });
	$("#refer_form_container").css({ 'z-index': "997" });
	$("#footer").css({ 'z-index': "4" });
}
function allowOverLap2() {
	$("#links").css({ 'z-index': "1" });
	$("#search_content").css({ 'z-index': "1" });
	$("#edit_profile_content").css({ 'z-index': "1" });
	$("#refer_form_container").css({ 'z-index': "1" });
	$(".tools_share_box").css({ 'z-index': "1" });
	$("#refer_form_container").css({ 'z-index': "1" });
	$("#footer").css({ 'z-index': "997" });
}
function hideOverLap2() {
	$("#links").css({ 'z-index': "997" });
	$("#edit_profile_content").css({ 'z-index': "997" });
	$("#refer_form_container").css({ 'z-index': "997" });
	$(".tools_share_box").css({ 'z-index': "997" });
	$("#refer_form_container").css({ 'z-index': "997" });
	$("#footer").css({ 'z-index': "4" });
}
// Open and close Search Stories and Get Stronger
function hideSearchStories() {
	searchStoriesOpen = 0;
	$("#search_stories_window").animate({ top: "180px" }, 500, function(){
		$("#top_level").css({ 'z-index': "997" });
		hideOverLap();
		$("#search_stories_window_mask").css({top:"-39px",height:"52px"});
		$("#search_stories_window").css({top:"0px"});
	});
	$("#search_stories_x").hide();
	$("#search_stories_arrow").fadeIn();
	$("#show_search_stories").show();
}
function hideGetStronger() {
	getStrongerOpen = 0;
	$("#get_stronger_window").animate({ top: "180px" }, 500, function(){
		hideOverLap2();
		$("#get_stronger_window_mask").css({top:"-39px",height:"52px"});
		$("#get_stronger_window").css({top:"0px"});
	});
	$("#get_stronger_x").hide();
	$("#get_stronger_arrow").fadeIn();
	$("#show_get_stronger").show();
}
function showSearchStories() {
	if(getStrongerOpen == 0) {
	searchStoriesOpen = 1;
	allowOverLap();
	//
	$("#search_stories_window_mask").css({top:"-219px",height:"232px"});
	$("#search_stories_window").css({top:"180px"});
	//
	$("#top_level").css({ 'z-index': "1" });
	$("#search_stories_window").animate({ top: "0px" }, 500 );
	$("#search_stories_x").fadeIn();
	$("#search_stories_arrow").hide();
	$("#show_search_stories").hide();
	} else {
		hideGetStronger();
		setTimeout ( 'showSearchStories()', 550 );
	}
}
// Open and close Get Stronger with Curves
function showGetStronger() {
	if(searchStoriesOpen == 0) {
	getStrongerOpen = 1;
	allowOverLap2();
	//
	$("#get_stronger_window_mask").css({top:"-219px",height:"232px"});
	$("#get_stronger_window").css({top:"180px"});
	//
	$("#top_level").css({ 'z-index': "997" });
	$("#get_stronger_window").animate({ top: "0px" }, 500 );
	$("#get_stronger_x").fadeIn();
	$("#get_stronger_arrow").hide();
	$("#show_get_stronger").hide();
	} else {
		hideSearchStories();
		setTimeout('showGetStronger()', 550 );
	}
}
/*
$().mousemove(function(e){
if(searchStoriesOpen == 0 || getStrongerOpen == 0) {
  if(e.pageY > 520) {	
  	document.getElementById('footer').style.zIndex="99";
	document.getElementById('main').style.zIndex="1";
	}
  } 
 }); 
*/
function flag_comment(id)
{
	$("#flag_comment_" + id).html("Flagging...");

	$.ajax({
		type: "GET",
		url: "/StrongerTogether/flag-comment.php",
		data: "id=" + id,
		dataType: "json",
		success: function(data)
		{
			if (data.status == 'success')
			{
				//disable the link.  make it look like text.
				$("#flag_comment_" + id).attr("onclick",'');
				$("#flag_comment_" + id).css("text-decoration",'none');
				$("#flag_comment_" + id).html("" + data.message);
			}
			else
			{
				$("#flag_comment_" + id).html("" + data.message);
			}
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) 
		{
			$("#flag_comment_" + id).html("" + textStatus);
		}
	});
}


function post_comment()
{
	$('#comment-form').hide('slow');
	$('#new_comment_loading').show('slow');
	
	id = $("#new_comment_user_id").val();
	first_name = $("#new_comment_first_name").val();
	last_name = $("#new_comment_last_name").val();
	comment = $("#new_comment_comment").val();
	$.ajax({
		type: "POST",
		url: "/StrongerTogether/post-comment.php",
		data: "user_id=" + id + "&first_name=" + first_name + "&last_name=" + last_name + "&comment=" + comment,
		dataType: "json",
		success: function(post_comment_response)
		{
			if (post_comment_response.status == 'success')
			{
				$('#new_comment_loading').hide('fast');
				
				//clear the form
				$("#new_comment_first_name").val('');
				$("#new_comment_last_name").val('');
				$("#new_comment_comment").val('');
				
				document.getElementById('comments_within_profile').innerHTML = "" + $('#new_comment_loading').html();
				
				//get all comments again.
				$.ajax({
					type: "GET",
					url: "/StrongerTogether/get-comments.php",
					data: "user_id=" + id,
					dataType: "html",
					cache: false,
					timeout: 8000,
					success: function(get_comments_data)
					{
						document.getElementById('comments_within_profile').innerHTML = "" + get_comments_data;
					},
					error: function (XMLHttpRequest, textStatus, errorThrown) 
					{
						alert("error " + textStatus);
					}
				});				
			}
			else
			{
				alert(post_comment_response.message);
				$('#new_comment_loading').hide('fast');
				$('#comment-form').show('slow');
			}
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) 
		{
			alert("error " + textStatus);
			$('#new_comment_loading').hide('fast');
			$('#comment-form').show('slow');
		}
	});
	return false;
}


function test_search_country()
{
	country = $("#search_form_country").val();
	if (country !== 'US' && country !== 'CA')
	{
		$("#search_form_state").val("OT");
	}
	else
	{
		$("#search_form_state").val("");
	}
}


$(document).ready(function() {
	$("#get_stronger_x").bind("click", function(){
		hideGetStronger();
	});
	$("#search_form_country").bind("change",test_search_country);
});
$(document).ready(function() {
	if ($("#search_stories_x") != null)
	{
		$("#search_stories_x").bind("click", function(){
			hideSearchStories();
		});
	}
});