var j = jQuery.noConflict();
j(document).ready(function(j) {
    var today = new Date();
    var thisYear = today.getFullYear();
    firstYear = thisYear*1 - 100 ;
    if(j(".zebra").length > 0) {
        zebra();
    }
	if(j("#pbr-tabs").length > 0) {
        j("#pbr-tabs").tabs();
	    j("#pbr-tabs").validate({
	        rules: {
	            password_again: {
	                equalTo: "#password"
	            },
	            p_sex : "required"
	        }
	    }) ;
	}
    if(j(".carousel-wrapper").length > 0 ) {

        j(".carousel-wrapper").each(function() {
            carousel(this.id) ;
        });
    }
    // Login / Register / Lost Password
    if(j("#register-new-link").length > 0) {
        j("#register-new-link").click(function() {
            register_new_form() ;
        });
    }
    if(j("#login-link").length > 0) {
        j("#login-link a").click(function() {
            login() ;
        });
        j("#lightbox-close").click(function() {
           login_close() ; 
        });
    }

    if(j("#lost-password-link").length > 0) {
        j("#lost-password-link").click(function() {
            lost_password_form() ;
        });
    }    
    if(j("#login-form-link").length > 0) {
        logindo_js() ;
        j("#login-form-link").click(function() {
            login_form() ;
        });
    }
    if(j("#goto-login").length > 0) {
        j("#goto-login").click(function() {
            login();
        });
    }

    /* EXTERNAL */

    j(".datepicker").each(function() {
    	j(this).datepicker({
    		changeMonth: true,
    		changeYear: true,
    		dateFormat: 'mm-dd-yy',
    		firstDay : 1,
    		yearRange: firstYear + ':' +thisYear,
    		gotoCurrent: true
    	});
    });
    // Validations

	j.validator.messages.required = "";
	if(j("#trial-form").length > 0) {
	    j("#trial-form").validate({
	        rules: {
	            tc_sex : "required"
	        }
	    });
	}
    if(j("#pbr-login").length > 0) {
        j("#pbr-login-form").validate({
            
	        invalidHandler: function(e, validator) {
	            
    			var errors = validator.numberOfInvalids();
        		if (errors) {
        			j("#errors4").html("Please enter your email address and your password.");
        			j("#errors4").show();
        		} else {
        			j("#errors4").hide();
        		}
	        }
        }) ;
    }
    
    if(j("#pbr-lost-password").length > 0) {
        j("#pbr-lost-password-form").validate({
	        invalidHandler: function(e, validator) {
    			var errors = validator.numberOfInvalids();
        		if (errors) {
        			j("#errors3").html("Please enter your email address.");
        			j("#errors3").show();
        		} else {
        			j("#errors3").hide();
        		}
	        }
        }) ;
    }

	if(j("#pbr-register-new-form").length > 0) {
	    j("#pbr-register-new-form").validate({

	        rules: {
	            tc_id: "required",
	            tc_sex : "required"
	        },
	        invalidHandler: function(e, validator) {
    			var errors = validator.numberOfInvalids();
        		if (errors) {
        			j("#errors1").html("Please fill all the fields marked in red.");
        			j("#errors1").show();
        		} else {
        			j("#errors1").hide();
        		}
	        }
	    });
	}

	if(j("#new-form-page").length > 0) {
	    j("#new-form-page").validate({
	        rules: {
	            tc_id: "required",
	            tc_sex : "required"
	        },
	        invalidHandler: function(e, validator) {
    			var errors = validator.numberOfInvalids();
        		if (errors) {
        		    alert("Please fill all the required fields marked in red");
        			j("#errors2").html("Please fill all the required fields marked in red.");
        			j("#errors2").show();
        		} else {

        			j("#errors2").hide();
        		}
	        }
        });
	}
});
// CAROUSEL
function carousel(id) {
    var slides = j("#" + id + " .carousel-inner").children().length ;
    var slides_width = 165 ; /* width + padding */
    j("#" + id + " .carousel-inner").width(slides * slides_width);

    j("#" + id + " .carousel-previous").css('opacity', '0.2').click(function() {
        carousel_prev(id);
    });
    j("#" + id + " .carousel-next").click(function() {
        carousel_next(id);
    });
    j("#" + id + " .trial-slide").show();
    j("#" + id + " .carousel").removeClass("start");
}
function carousel_next(id) {

    j("#" + id + " .carousel-previous").css('opacity', '1')
    var p = j("#" + id + " .carousel-inner").css('left');
    var p_num = (p.substr(0, (p.length - 2)) *-1);
    var c_w = j("#" + id + " .carousel").width();
    var ci_w = j("#" + id + " .carousel-inner").width();
    var ct_w = (((ci_w-55)-c_w)) ;

    if((p_num*1) + 165 == ct_w) {
        j("#" + id + " .carousel-next").css('opacity', '0.2') ;
    }
    if(p_num < ct_w) {
        j("#" + id + " .carousel-next").unbind('click');
        j("#" + id + " .carousel-inner").stop().animate({ left: '-=165'}, function() {     
            j("#" + id + " .carousel-next").click(function() {
                carousel_next(id);
            }); 
        });
    }
}
function carousel_prev(id) {
    j("#" + id + " .carousel-next").css('opacity', '1')
    var p = j("#" + id + " .carousel-inner").css('left');
    var p_num = (p.substr(0, (p.length - 2)) *1);

    if((p_num*1) + 165 == 0) {
        j("#" + id + " .carousel-previous").css('opacity', '0.2') ;
    }
    
    if(p_num < 0) {
        j("#" + id + " .carousel-previous").unbind('click');
        j("#" + id + " .carousel-inner").stop().animate({ left: '+=165'}, function() {     
            j("#" + id + " .carousel-previous").click(function() {
                carousel_prev(id);
            }); 
        });
    }
}

// LOGIN / REGISTER / LOST PASSWORD FUNCTIONS
function login() {
    j("#login-lightbox").fadeIn('fast');
}
function login_close() {
    j("#login-lightbox").fadeOut('fast', function() {
        j("#pbr-login").show();
        j("#pbr-register-new").hide();
        j("#pbr-lost-password").hide();
        j("#login-lightbox-inner").css('margin-top', '100px');
        j("#login-form-link").hide();        
        j("#register-new-link").show();
        j("#lost-password-link").show();

        lightbox_reset();
    });
}
function lightbox_reset() {
    j("#login-lightbox form").each(function() {
        this.reset();
    });
}
//
function login_form() {
    lightbox_reset();
    j("#pbr-login").show();
    j("#pbr-register-new").hide();
    j("#pbr-lost-password").hide();
    
    j("#login-form-link").hide();
    j("#register-new-link").show();
    j("#lost-password-link").show();

    j("#login-lightbox-inner").css('margin-top', '100px');
    

}
function register_new_form() {
    lightbox_reset();
    j("#pbr-login").hide();
    j("#pbr-register-new").show();
    j("#pbr-lost-password").hide();
    
    j("#login-form-link").show();
    j("#register-new-link").hide();
    j("#lost-password-link").show();

    j("#login-lightbox-inner").css('margin-top', '10px');

    registernew_js();

}
function lost_password_form() {
    lightbox_reset();

    j("#pbr-login").hide();
    j("#pbr-register-new").hide();
    j("#pbr-lost-password").show();
    
    j("#login-form-link").show();
    j("#register-new-link").show();
    j("#lost-password-link").hide();

    j("#login-lightbox-inner").css('margin-top', '100px');
    password_reset_js();

}
/* Zebra */
function zebra() {
    j(".zebra tr:even").addClass('even');
    j(".zebra tr:odd").removeClass('even');
    j(".zebra tr").mouseover(function() {
        j(this).addClass('over');
    });
    j(".zebra tr").mouseout(function() {
        j(this).removeClass('over');
    });

}

