/* $Id: global.js 13339 2009-05-28 14:31:51Z jsdalton $ */

//jquery code goes here
var $ = jQuery.noConflict();
$(document).ready(function() { 
	
	/*
	 * DOM and Event selectors go here....just like CSS
	 * First you select an object and/or event...
	 * Then you specify jquery properties for that object/event...
	 * Or a function you'd like to execute...
	 */
	
   	//$("#content form:first").focusOnFirstFieldinForm();
	
	// account-related form validation
/* 
	$("#content form").submit( function() {
	    return $(this).validateForm();
	});
*/
	// discussion digest selections
	$(".more-choices-control").each(function() {
	    var $container = $(this);
	    var $more_choices_box = $container.find(".more-choices-box");
	    var $checkbox = $container.find(":checkbox");
	    if ($checkbox.is(":checked")) {
	        $more_choices_box.show();
	    } else {
	        $more_choices_box.hide();
	    }
	    $checkbox.change(function() {
	        if ($checkbox.is(":checked")) {
    	        $more_choices_box.show();
    	    } else {
    	        $more_choices_box.hide();
    	    }
	    });
	});
	
	
	// directory flags
	
	$("#directory_ere_net #profile-corrections form").hide();
	$("#directory_ere_net #profile-corrections h3 a").click(function() {
	    the_form = "#directory_ere_net #profile-corrections form";
	    $(the_form).siblings("p.confirmation").hide();
	    if ( $(the_form).hasClass("revealed") ) {
	        $(the_form).removeClass("revealed").slideUp("normal");
	    } else {
    	    $(the_form).addClass("revealed").slideDown("normal");
	    }
	    return false;
	});
	$("#directory_ere_net #profile-corrections #correction-cancel").click(function() {
	    the_form = "#directory_ere_net #profile-corrections form";
	    $(the_form).removeClass("revealed").slideUp("normal")[0].reset();
	    return false;
	});
	$("#directory_ere_net #profile-corrections form").submit(function() {
	    $(this).submitFlag("/profiles/flag/");
	    return false;
	});
	
    
	
	
	
	// directory
	if (location.hash == "#all") {
	    $("#directory_ere_net .tag-view-chooser a.all").addClass('selected');
	} else {
	    $("#directory_ere_net .low-ranked").hide();
	    $("#directory_ere_net .tag-view-chooser a.top-20").addClass('selected');
    }
	$("#directory_ere_net .tag-view-chooser a").click(function() {
	    $("#directory_ere_net .tag-view-chooser a").each(function() {
	        $(this).removeClass('selected');
	    });
	    if ( $(this).hasClass('top-20') ) {
	        $("#directory_ere_net .low-ranked").hide();
	        $(this).addClass('selected');
	    } else if ( $(this).hasClass('all') ) {
            $("#directory_ere_net .low-ranked").show();
            $(this).addClass('selected');
	    }
	    return false;
	});
	
	// job posting, step 3
	
	$("#main .expanded-info-form .expanded-info").hide(); 
    
	$("#main .expanded-info-form input:radio[@checked]").parent().siblings(".expanded-info").show(); 

	$("#main .expanded-info-form input:radio").not( $("#main .expanded-info-form ul li ul li input:radio") ).not( $("#main .expanded-info-form ul li dl dt input:radio") ).click ( function () {
		$("#main .expanded-info-form input:radio").parent().siblings(".expanded-info:visible").hide();
		$(this).parent().siblings(".expanded-info").show();
	});
	
	
	// for delete button hover effect
	$("#cart-contents form.delete_item input").hover(function(){
        $(this).parent("form").parent("td").parent("tr").children("td, th").css({ background: "#fff2f2" });
    },function(){
        $(this).parent("form").parent("td").parent("tr").children("td, th").css({ background: "#ffffff" });
    }); 
	
	// for delete confirmation on order items
		$("#cart-contents form.delete_item input").click(function(e){
  		ret = confirm("Press \"OK\" to delete this item from your shopping cart.");
  		if(!ret) {
  			e.stopPropagation();
  			return false;
  		}
  		return true;
  	});
  	
  	// for tabbed navigation
  	
  	// initialize
    $("div.tab-pane").hide();
    $(".view-nav dd").removeClass("selected");
    
    // make sure link is selected if you are accessing that hash directly
    if (location.hash && $("div.tab-pane"+location.hash).length) {
        $(location.hash).show();
        $(".view-nav a[href='"+location.hash+"']").parent("dd").addClass("selected");
    } else {
        $("div.first").show();
        $(".view-nav dd.first").addClass("selected");
    }
    
    // when clicking tab, hide other sections and display clicked on section
    $(".view-nav a[href^='#']").click(function() {
        $(".tab-pane").hide();
        $(".view-nav dd").removeClass("selected");
        var selected_id = $(this).attr("href");
        $(".view-nav a[href='"+selected_id+"']").parent("dd").addClass("selected");
        $(selected_id).show();
        return false;
    });
	
});

$.fn.submitFlag = function(path) {
    var form_data = $(this).serialize();
    var the_form = this;
    $.ajax({
        async: true,
        type: "POST",
        url: path,
        data: form_data,
        dataType: "json",
        beforeSend: function(request) {
            $(the_form).find(":submit").addClass("loading").attr("disabled","disabled");
        },
        success: function(response){
            if (response) {
                $(the_form).removeClass("revealed").hide()[0].reset();
                $(the_form).siblings("p.confirmation").show();
            }
        },
        error: function() {
            alert("An error has occurred. Please try again.");
        },
        complete: function(request, status) {
            $(the_form).find(":submit").removeClass("loading").removeAttr("disabled");
        }
    });
}