$(document).ready(function(){
	
	// GESHI "SHOW PLAIN TEXT" LINK
	jQuery(".code").each(function() {
		var block = jQuery(this);
		var htmlText = block.html();
		var plainText = "";
		if (jQuery.browser.msie) {
			plainText = htmlText.replace(/\n/g, "+");
			plainText = jQuery(plainText).text().replace(/\+\+/g, "\r");
		} else {
			plainText = block.text().replace(/code/g, "code\n");
		}
		var state = 1;
		block.prev().click(function() {
		  if (state) {
			jQuery(this).html("Show Highlighted Code");
			block.text(plainText).wrapInner("<pre class=\"plain-text\"></pre>");
			state = 0;
		  } else {
			jQuery(this).html("Show Plain Text");
			block.html(htmlText);
			state = 1;
		  }
		});
	});
	
	
	// DIALOG TRIGGERS //
	// permissions page
	$("#RepoPermissionsForm .submit input").click(function() {
		$("#dialog").dialog('open');
	});
	// create repo
	$("#RepoCreateForm .submit input").click(function() {
		$("#dialog").dialog('open');
	});
	// delete repo
	$("#RepoDeleteForm a.yes").click(function() {
		$("#dialog").dialog('open');
	});
	// add SSH key
	$("#UserKeyForm .submit input").click(function() {
		$("#dialog").dialog('open');
	});
	// show repo page - enable Trac
	$("div#enableTrac a.button").click(function() {
		$("#dialog").dialog('open');
	});
	// cancel account page - user clicked yes
	$("#ClientCancelForm a.yesbutton").click(function() {
		$("#dialog").dialog('open');
	});
	
	// CLOSE DIALOG
	$("a#close").click(function() {
		$(this).parent().dialog('close');
	});
	
	
	// USER SSH KEY - display example dialog
	$("a.sshkeyexample").click(function() {
		$("div#sshkeyexample").dialog({ modal: true, resizable: false, draggable: false, width: 400, closeOnEscape: true, minHeight: 50, autoOpen: false });
		$("div#sshkeyexample").dialog('open');
		return false;
	});
	
	
	// LOGIN BOX - remove username in textbox
	$("form#UserLoginForm input#UserUsername").focus(function() {  
		$(this).filter(function() {  
			// We only want this to apply if there's not  
			// something actually entered  
			return $(this).val() == "" || $(this).val() == "username"  
			}).val("");  
	});
	$("form#UserLoginForm input#UserUsername").blur(function() {  
		$(this).filter(function() {  
			// We only want this to apply if there's not  
			// something actually entered  
			return $(this).val() == ""  
			}).val("username");  
	});
	
	
	// LOGIN BOX - remove password in textbox
	// set the password box to display=hidden initially 
	$("form#UserLoginForm input#UserPassword:visible").css("display","none");
	$("form#UserLoginForm input#UserPasswordPlain").focus(function() {  
		if ($(this).val() == "password") {
			
			// hide plain password box
			$(this).css("display","none");
			
			// show password box
			$('input#UserPassword').css("display","inline").focus();
		};
		
	});
	$("form#UserLoginForm input#UserPassword").blur(function(e) {  
		// We only want this to apply if there's not something actually entered  
		if ($(this).val() == "") {
			// hide plain password box
			$('input#UserPasswordPlain').css("display","inline").val("password");
			
			// show password box
			$(this).css("display","none");
		}
	});
	
	// LOGIN BOX - remove UserPasswordPlain textbox when form is submitted
	$("form#UserLoginForm div.submit input").click(function() {
		if ($("form#UserLoginForm input#UserPasswordPlain").val() !== "password") {
			$("form#UserLoginForm input#UserPasswordPlain").remove();
		}
	});
	
	
	
	// REPOSITORY INDEX PAGE - DISPLAY CLONE OR CHECKOUT TEXT
	// $("div.downloadtext").hide();
	$('.downloadbutton').click(function(){
		$("div#"+this.name).slideToggle(250);
		return false;
	});
	
	
	// REPOSITORY DELETE CONFIRMATION PAGE - yes/no confirm buttons
	$("form#RepoDeleteForm a.confirm").click(function() {
		BUTTONVALUE = $(this).attr("name");
		// alert(BUTTONVALUE);
		document.forms[0].RepoConfirm.value = BUTTONVALUE;
		// document.forms[0].action = BUTTONACTION;
		document.forms[0].submit();
		return false;
	});
	
	
	// CANCEL ACCOUNT CONFIRMATION PAGE - yes/no confirm buttons
	$("form#ClientCancelForm a.confirm").click(function() {
		BUTTONVALUE = $(this).attr("name");
		// alert(BUTTONVALUE);
		document.forms[0].ClientConfirm.value = BUTTONVALUE;
		document.forms[0].submit();
		return false;
	});
	
	
	
	// REPOSITORY CREATE PAGE - displays the ssh key div when git or bazaar is selected
	$("div.usersSshKeys").hide();
	$("select#RepoRepotypeId").change(function () {
		// alert($(this).val());
		
		SELECTLEFT = $(this).attr("offsetLeft");
		DIVLEFT = SELECTLEFT+370;
		$("div.usersSshKeys").css("left", DIVLEFT);
		
		// svn - hide the ssh key box
		if ($(this).val() == '1') $("div.usersSshKeys").hide();
		
		// git - display the ssh key box
		if ($(this).val() == '2') $("div.usersSshKeys").slideDown(250);
		
		// bzr - display the ssh key box
		if ($(this).val() == '3') $("div.usersSshKeys").slideDown(250);
		
	}).change();
	
	
	
	// REPOSITORY PERMISSIONS PAGE
	// and REPOSITORY SHOW INFO PAGE
	$("a.dropdown").click(function (){
		$(this).toggleClass('open');
		$(this).parent("h3").next("table").toggleClass("hidden");
		return false;
	});
	
	
	
	// REGISTER FORM - update the repoprefix when the user types their username
	$("form#ClientRegisterForm input#UserUsername").keyup(function () {
		var value = $(this).val();
		// alert(value);
		$("input#ClientRepoprefix").val(value);
	}).keyup();
	
	
	// REGISTER FORM (PLANS ELEMENT) - selectable price buttons
	var selectedbutton = $("div.box.selected");
	$("a.price").click(function() {
		// only select this price box if it is currently deselected
		if (!$(this).parent('.box').hasClass("selected")) {
			
			// select the parent div 
			$(this).parent('.box').addClass("selected");

			// and deselect the currently selected div
			$(selectedbutton).removeClass("selected");

			// set the value of the hidden text field
			planid = $(this).attr('name');
			$("input#ClientsPlanPlanId").val(planid);

			// set selectedbutton to the newly selected div so we know what to deselect next time a button is clicked
			selectedbutton = $(this).parent('.box');
		};
		return false;
	});
	
	// PLANS ELEMENT - ajax post and update div function for plans upgrade page
	$("#PlanUpgradeForm a.price").click(function() {
		cp = $("span.cp").attr('id');
		// alert(cp);
		bodyContent = $.ajax({
			url: "/plans/compareplans",
			global: false,
			type: "POST",
			data: ({newid : $(this).attr("name"), oldid : cp}),
			dataType: "html",
			success: function(htmlReturn) {
				// alert(msg);
				$("div.modplan").html(htmlReturn);
				
				// unhide the upgrade button
				$('div.hidden').css("display","block");
			}
		}).reponseText;
		
		// assign the new plan id to a hidden form field
		// NEWPLAN = $(this).attr("name");
		// document.forms[0].PlanId.value = NEWPLAN;
		
		return false;
	});
	
	
	// UPGRADE PLAN PAGE - upgrade or downgrade button; submit form
	$("#PlanUpgradeForm a#actionbutton").click(function() {
		document.forms[0].submit();
		return false;
	});
	
	
	// REGISTER FORM - display help when user hovers over the question mark
	$(".showHelp").hover(
		function () {
			prevEl = 'div';
			
			var OFFSETTOP = $(this).attr("offsetTop");
			var OFFSETLEFT = $(this).attr("offsetLeft");
			
			// the dimensions of help box
			var PREVWIDTH = $(this).prev(prevEl).css("width");
			var PREVHEIGHT = $(this).prev(prevEl).css("height");
			
			// width of the div that will trigger the help box
			var THISWIDTH = $(this).css("width");
			
			// strip off px from the end of the css width property
			PREVWIDTH = parseInt(PREVWIDTH.substr(0,PREVWIDTH.length-2));
			PREVHEIGHT = parseInt(PREVHEIGHT.substr(0,PREVHEIGHT.length-2));
			THISWIDTH = parseInt(THISWIDTH.substr(0,THISWIDTH.length-2));
			
			var TOPVAL = OFFSETTOP - PREVHEIGHT - 8;
			var RIGHTVAL = OFFSETLEFT;
			
			RIGHTVAL = RIGHTVAL + "px";
			TOPVAL = TOPVAL + "px";
			
			$(this).prev(prevEl).css("left", RIGHTVAL);
			$(this).prev(prevEl).css("top", TOPVAL);
			$(this).prev(prevEl+':hidden').css("display","block");
		}, 
		function () {
			$(this).prev(prevEl+':visible').css("display","none");
		}
	);
	
	
	function findPos(obj){
		this.X = obj.offsetLeft;
		this.Y = obj.offsetTop;
		while(obj.offsetParent){
			this.X=this.X+obj.offsetParent.offsetLeft;
			this.Y=this.Y+obj.offsetParent.offsetTop;
			if(obj==document.getElementsByTagName('body')[0]){break}
				else{obj=obj.offsetParent;}
		}
		return this;
	}
	

});
