// expand for content pages

jQuery(document).ready(function() {
  jQuery('#expand').click(
	  function() {
		  jQuery(this).parent().find(".hide").slideToggle('500');
			var jQuerythis = jQuery(this);
			if( jQuerythis.is('.up') ) {
        jQuerythis.removeClass('up');
				jQuerythis.html("Expand <span></span>");
      }
      else {
        jQuerythis.addClass('up');
				jQuerythis.html("Close <span></span>");
      }
      return false;
	  }
	);
});



// advanced search box toggle
jQuery(document).ready(function() {
  jQuery('#advanced-search').click(
	  function() {
			// jQuery(this).parent().parent().css("background", "pink");
		  jQuery(this).parent().parent().find(".hide").slideToggle('500');
			var jQuerythis = jQuery(this);
			if( jQuerythis.is('.up') ) {
        jQuerythis.removeClass('up');
				jQuerythis.html("Advanced Search <span></span>");
				$("#search-form").attr("action", "/search/"); 
				$("#search-scope").html(""); 
				$("#search-date").html("");
      }
      else {
        jQuerythis.addClass('up');
				jQuerythis.html("Close Advanced Search <span></span>");
      }
      return false;
	  }
	);
});

jQuery(document).ready(function() {
	search_scope = 0;
	search_date = 0;
});

// advanced search controls 
jQuery(document).ready(function() {
  jQuery('.advanced-serach-option').click(
	  function() {
			search_scope = 1;
			var search_section = 1;
			var date_string = "";
			if (search_date == 1){
				date_string = date_param;
			}
			var jQuerythis = jQuery(this);
			search_section = jQuery(this).attr('id').split('-')[2];
			var search_description = jQuery(this).attr('id').split('-')[1];
			scope_param = search_section;
			$("#search-form").attr("action", "/search/"+search_section+"/"+date_string); 
			if ($("#search-date").html() == "")
			{
				$("#search-date").html(" for:"); 
			}
			$("#search-scope").html("Searching "+ search_description.replace('_',' ').replace('_',' ').replace('_',' ') + " "); 
			jQuery("#search-status").animate({backgroundColor: '#CCCC00'}, 50); 
			jQuery("#search-status").animate({backgroundColor: '#ffff99'}, 500);
      return false;
	  }
	);
});

// advanced search dates 
// add jQuery everywhere to be safe
jQuery(document).ready(function() {
  jQuery('.advanced-serach-date-option').click(
	  function() {
			search_date = 1;
			var scope_string = "1";
			if (search_scope == 1){
				scope_string = scope_param;
			}
			var jQuerythis = jQuery(this);
			var search_section = jQuery(this).attr('id').split('-')[2];
			var search_description = jQuery(this).attr('id').split('-')[1];
			$("#search-form").attr("action", "/search/"+scope_string+"/"+search_section); 
			date_param = search_section;
			if ($("#search-scope").html() == "")
			{
				$("#search-scope").html("Searching all pages "); 
			} 
			$("#search-date").html(search_description.replace('_',' ').replace('_',' ') + " for:"); 
			jQuery("#search-status").animate({backgroundColor: '#CCCC00'}, 50); 
			jQuery("#search-status").animate({backgroundColor: '#ffff99'}, 500); 			
			return false;
	  }
	);
});

// search results -- include archive toggle
jQuery(document).ready(function() {
	jQuery("#include-checkbox").click(function() {
		var current_action = $("#results-search-form").attr("action");
		if ( jQuery(this).is(':checked') ) {
			$("#results-search-form").attr("action", current_action.replace("/search","/search_all"));
     }
     else {
			$("#results-search-form").attr("action", current_action.replace("/search_all","/search"));
     }
	});
});


// checkbox ajax for contents page
jQuery(document).ready(function() {
  jQuery(".archive-checkbox").click(function() {
    var doc_id = jQuery(this).attr('id').split('_')[1];
    if ( jQuery(this).is(':checked') ) {
      var check_box_status = 1
     }
     else {
      var check_box_status = 0
     }
    jQuery.ajax({
             type: "POST",
             url: "/docs/update_status",
             processData: false,
      data: '&doc_id=' + doc_id + '&check_box_status=' + check_box_status + '&authenticity_token=' + encodeURIComponent(AUTH_TOKEN),
      dataType: "script"
           });
  });
});


// add items to cart

// add to cart on products page
jQuery(document).ready(function() {
  jQuery(".product").click(function() {
    var product_id = jQuery(this).attr('id').split('_')[1];
    // if ( jQuery(this).is(':checked') ) {
    //       var check_box_status = 1
    //      }
    //      else {
    //       var check_box_status = 0
    //      }
    jQuery.ajax({
             type: "POST",
             url: "/products/add_to_cart",
             processData: false,
      data: '&id=' + product_id + '&authenticity_token=' + encodeURIComponent(AUTH_TOKEN),
      dataType: "script"
           });
  });
});

// add to cart on events page
jQuery(document).ready(function() {
  jQuery(".purchase-ticket").click(function() {
    var product_id = jQuery(this).attr('id').split('_')[2];
    var document_id = jQuery(this).attr('id').split('_')[0];
    jQuery.ajax({
             type: "POST",
             url: "/events/add_to_cart",
             processData: false,
      data: '&id=' + product_id + '&doc_id=' + document_id + '&authenticity_token=' + encodeURIComponent(AUTH_TOKEN),
      dataType: "script"
           });
  });
});

// add to cart on events page
jQuery(document).ready(function() {
  jQuery(".purchase-publication").click(function() {
    var product_id = jQuery(this).attr('id').split('_')[2];
    var document_id = jQuery(this).attr('id').split('_')[0];
    jQuery.ajax({
             type: "POST",
             url: "/publications/add_to_cart",
             processData: false,
      data: '&id=' + product_id + '&doc_id=' + document_id + '&authenticity_token=' + encodeURIComponent(AUTH_TOKEN),
      dataType: "script"
           });
  });
});



// giving totals


// test for numbers

function validateNumber(string) {
	var temp = ""
	var validStr = "0123456789."
	for (var i=0; i< string.length; i++) {
		if (validStr.indexOf(string.charAt(i)) != -1)
		temp += string.charAt(i)
 }
	return(temp)
}

function givingTotal () {
	var total = jQuery("#gift_amount").val();
	var checked_number = validateNumber(total);
	
	if (checked_number.length > 0) {
		jQuery("#total-gift").val(checked_number);
		jQuery("#gift-total-table").slideDown('500');
	} else {
		jQuery("#gift-total-table").slideUp('500');
	}

	


	
	// $("input#foo").value($("input#bar").value()).hide(); 
	// alert(total);
	
	
	
	  
    // jQuery(".container").toggleClass("showgrid");
    // jQuery("#togglegrid").text(gridstate());
    // return false;
}


// icon hover for edit page
jQuery(document).ready(function(){
	jQuery(".editable").hover(
	      function () {
			jQuery(this).find(".edit-area").css("background-image", "url(/images/admin/bg_trans_60.png)")
			jQuery(this).find(".edit-icons").css("display", "block")
	      }, 
	      function () {
			jQuery(this).find(".edit-area").css("background", "none")
			jQuery(this).find(".edit-icons").css("display", "none")
	      }
	    );
});


// icon hover for edit page
jQuery(document).ready(function(){
  jQuery(".editable-event").hover(
        function () {
      jQuery(this).find(".event-edit-area").css("background-image", "url(/images/admin/bg_trans_60.png)")
      jQuery(this).find(".event-edit-icons").css("display", "block")
        }, 
        function () {
      jQuery(this).parent().find(".event-edit-area").css("background", "none")
      jQuery(this).parent().find(".event-edit-icons").css("display", "none")
        }
      );
});
 
// TODO -- swap this out with dom traversal style method -- this is overly completx and requires specific view code for each instance
jQuery(document).ready(function() {
	jQuery('#toggle-wrap .toggle').click(function() {
		var toggle_id = 'hidden_text_' + jQuery(this).attr('id').split('_')[1];
		if (jQuery('#' + toggle_id ).is(':visible')) {
			jQuery('#' + toggle_id ).slideUp("slow");
		} else {	
			jQuery('#' + toggle_id ).slideDown("slow");
		}
	return false;
	});
});


// Donation -- add address checkbox
jQuery(document).ready(function() {
	jQuery("#gift_different_receipt_address").click(function() {
		var current_action = $("#results-search-form").attr("action");
		if ( jQuery(this).is(':checked') ) {
			jQuery("#different_receipt_table").slideDown('500');
		
			// $("#results-search-form").attr("action", current_action.replace("/search","/search_all"));
     }
     else {
				jQuery("#different_receipt_table").slideUp('500');
			// $("#results-search-form").attr("action", current_action.replace("/search_all","/search"));
     }
	});
});

 


// Add "Search" to Search Input, remove on focus and repace on blur
// currently using inline js
// $(document).ready(function(){
// 	$("#q").val("Search");
//     $("#q").focus(function() {
//  		$(this).val("");
//     });
//     $("#q").blur(function() {
// 	 $(this).val("Search");
//     });
// });

// GRID VIEW CODE 

// for development only //
// gridview toggle  //	
// place this in the view  //
//   	$(document).ready(function() {
//     $("body").append("<div id='debug'>turn grid: <a href='' id='togglegrid'>" + gridstate() + "</a></div>");
//     // $("#debug").css("position", "absolute");
//     // $("#debug").css("bottom", "0");
//     $("#togglegrid").click(toggle_grid);
// });


function toggle_grid () {
    jQuery(".container").toggleClass("showgrid");
    jQuery("#togglegrid").text(gridstate());
    return false;
}

function gridstate () {
    if (jQuery(".container").hasClass("showgrid")) {
        return 'off';
    } else {
        return 'on';
    };
}







