/* page.js|customers */
// inits script
function initPageJs() {
	fixQuirks();
	makeLogin();
	accessibleInputs();
	niceHr();
	hoverBox();
	calendarSelector();
	makePrint();
}

// make fancy loginbox
function makeLogin(){
	if($(".mdLoginLogout").length==0){
		if($(".mdBoxLoginNotice").length==0){
			$("#mdLogin").css("top","-57px");
		}
		$("#mdTop").addClass("jsTop").append('<li id="jsShowLogin">'+$("#mdLogin legend").text()+'</li>');
		$("#jsShowLogin").click(function(){$("#mdLogin").animate({"top": "0"},"")});
		$("body").click(function(e){
			if($("#mdLogin").is(":visible")&&$(".mdBoxLoginNotice").length==0){
				var hide=true;
				$("#jsShowLogin,#mdLogin,#mdLogin *").each(function(){if(e.target==$(this).get(0)){hide=false}})
				if(hide){$("#mdLogin").animate({"top": "-57px"},"")}
			}
		});
	}else{
		$("#mdTop").addClass("jsTop2");
	}
}

// fix headmenu
function fixQuirks(){
	$("#mdHeadMenu dd.selected").prev('dt').addClass('selected');
	$("#mdHeadMenu dd").each(function(i){
		if($(this).hasClass('selected')){
			$('body').addClass('photo'+i);
		}
	})
}

// make nice hr
function niceHr(){
	$("hr").not(".mdAccess").wrap('<div class="mdHr"></div>');
}

// hover on iconlist in box
function hoverBox(){
	$(".mdBoxIconList.hover").find("dd").hover(function(){
	    $(this).addClass("jshover").click(function(){
	    	document.location.href=$(this).find(":header a").attr("href");
	    });
	},function(){
	    $(this).removeClass("jshover");
	})
}

function calendarSelector(){
	$(".mdFormCalendar").find(":submit").hide().end().find("select").change(function(){
		$(this).parents("form").submit();
	});
}

function makePrint(){
	$("#mdTop").prepend('<li id="mdPrint"><span>Print</span></li>');
	$("#mdPrint span").click(function(){window.print()});
}

// fires hen DOM is loaded
$(document).ready(function(){
  initPageJs();
});

/* accessibleInputs.js|global */
//Accessible Inputs (requires jQuery)

// moves labels value to inputs if class 'mdValueToInput' is present & then adds focus/blur to inputs
function accessibleInputs(){
	$("label.mdValueToInput[for]").each(function(i){
		// fill input fields with labeltext - html tags
		var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi;
		var newVal = this.innerHTML.replace(regexp,"");
		//if el is type=input or textarea
		if($("#"+this.htmlFor).is("input") || $("#"+this.htmlFor).is("textarea")){
			if(($("#"+this.htmlFor).val() == "") || ($("#"+this.htmlFor).val() == newVal)){
				$("#"+this.htmlFor).attr("value",newVal);
			}
			// create onclick/blur functionality
			$("#"+this.htmlFor).focus(function(){if(this.value == newVal) this.value = "";});
			$("#"+this.htmlFor).blur(function(){if(this.value == "") this.value = newVal;});
		// if el is select	
		} else if($("#"+this.htmlFor).is("select")){
			var orgOptions = $("#"+this.htmlFor).html();
			var newOptions = '<option value="">'+newVal+'</option>'+orgOptions;
			// IE special Kung Fu
			if($.browser.msie && $.browser.version < 8){
				var go=0;
				$("#"+this.htmlFor).find("option").each(function(i){
					if($(this).get(0).defaultSelected){
						go=1;
					}
				})
				if(go==0){
					newOptions = newOptions.replace(/selected>/g,">");
				}
			}
			$("#"+this.htmlFor).html(newOptions);
			// IE8 force choose first option
			if($.browser.msie && $.browser.version <= 8){
				$("#"+this.htmlFor + " option:first").attr("selected","selected")
			}
		}
		// hide label
		$(this).hide();
	})
	cleanForms();
}

// makes sure that label values are not submitted to forms
function cleanForms(){
	$("form:has(label.mdValueToInput)").submit(function(){
		$("label.mdValueToInput[for]").each(function(){
			// check if value is same as label
			var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi;
			var newVal = this.innerHTML.replace(regexp,"");
			if($("#"+this.htmlFor).attr("value") == newVal){
				$("#"+this.htmlFor).attr("value","");
			}
		})
	})
}


