
$(document).ready(function(){
	active_nav();
	active_subnav();
	loginbox();
	forms();
	tables();
});

function tables() {
	$("table").each(function(){
		$(this).attr('cellspacing', '0');         
    });
}

function active_nav() {
	
	var base_url = 'http://dev-pacommutes:8888';
	
	var thisURL = window.location.href;
	if (thisURL.charAt(thisURL.length - 1) !== "/") {
		thisURL += '/';
	}	
	
	var thisSegment = thisURL.split("/")[3] + '/';
	
	// activate li class on main nav
	$("div#navigation li a").each(function(){
		var current_href = $(this).attr("href");
		if (base_url + '/' + thisSegment == current_href) {
			$(this).addClass("active");
		}
	});
	
}


function active_subnav() {
	
	var base_url = 'http://dev-pacommutes:8888';
	
	var thisURL = window.location.href;
	if (thisURL.charAt(thisURL.length - 1) !== "/") {
		thisURL += '/';
	}	
	
	var thisSegment = thisURL.split("/")[3] + '/';
	
	// activate li class on sub nav
	$("div#subnavigation a").each(function(){
		var current_href = $(this).attr("href");
		if (thisURL == current_href) {
			$(this).parents('li').addClass("active");
			$(this).addClass("active");
		}			
	});
	
}


function loginbox() {

    $(".login").click(function(e) {
        e.preventDefault();
        $("div#loginbox").toggle();
        $(".login").toggleClass("menu-open");
    });

    $("div#loginbox").mouseup(function() {
        return false;
    });
    
   	$(document).mouseup(function(e) {
		if($(e.target).parent(".login").length==0) {
			$(".login").removeClass("menu-open");
			$("div#loginbox").hide();
		}
	});  

	 $(".menu-open").click(function(e) {
        e.preventDefault();
        $("div#loginbox").hide();
        $(".login").toggleClass("menu-open");
    }); 

}

function forms() {
	
	// control label width
	var max = 200;
    $("form.contextual label").each(function(){
        if ($(this).width() > max)
            max = $(this).width();   
    });
    $("form.contextual label").width(max);

    // focus and idle fields
    $('input[type="text"],input[type="password"],textarea').addClass("fieldidle");
	
	$('input[type="text"],input[type="password"],textarea').focus(function() {
		$(this).removeClass("fieldidle").addClass("fieldfocus");
	    $(this).parent("p").addClass("blockfocus");
		if ($.trim(this.value) == ''){
	    	//this.select();
	    	this.value = (this.defaultValue ? this.defaultValue : '');
	    	
		}
		/*
	    if (this.value == this.defaultValue){ 
	    	this.value = '';
		}
		if(this.value != this.defaultValue){
			this.select();
		}
		*/
	});

	$('input[type="text"], input[type="password"], ,textarea').blur(function() {
		$(this).removeClass("fieldfocus").addClass("fieldidle");
		$(this).parent("p").removeClass("blockfocus");
	    if ($.trim(this.value) == ''){
	    	this.value = (this.defaultValue ? this.defaultValue : '');
		}
	});
	
}




