$(document).ready(function() {
	if($.browser.msie && $.browser.version == "6.0"){
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
		$("#dialogbox-transparency").css({ "width": maskWidth, "height": maskHeight });
		$("#dialogbox-transparency").fadeIn(100);	
		$("#dialogbox-transparency").fadeTo(100, 0.8);	
		var winH = $(window).height();
		var winW = $(window).width();
		$("#dialogbox").css("top",  winH / 2 - $("#dialogbox").height() / 2);
		$("#dialogbox").css("left", winW / 2 - $("#dialogbox").width() / 2);
		$("#dialogbox").fadeIn(100);
	}
	rollover_image(".prerollover");
	
	$("a[rel='image']").colorbox({ transition: "fade" });
	
	var originalFontSize = $("#detailed-item").css("font-size");
	// Decrease Font Size
	$(".decreaseFont").click(function(){ decreaseFont($("#detailed-item"), 11); });
	
	$(".resetFont").click(function(){ $("#detailed-item").css("font-size", originalFontSize); });
	
	// Increase Font Size
	$(".increaseFont").click(function(){ increaseFont($("#detailed-item"), 13); });
});

function rollover_image(hierarchy_elements){
	// Preload all rollovers
	$(hierarchy_elements + " img").each(function(){
		// Set the original src
		rollsrc = $(this).attr("src");
		rollON = rollsrc.replace(/.png$/ig,"-h.png");
		$("<img>").attr("src", rollON);
	});
	
	// Navigation rollovers
	$(hierarchy_elements + " a").mouseover(function(){
		imgsrc = $(this).children("img").attr("src");
		matches = imgsrc.match(/-h/);
		
		// don't do the rollover if state is already ON
		if (!matches){
			imgsrcON = imgsrc.replace(/.png$/ig,"-h.png"); // strip off extension
			$(this).children("img").attr("src", imgsrcON);
		}
		
	});
	$(hierarchy_elements + " a").mouseout(function(){
		$(this).children("img").attr("src", imgsrc);
	});
}


/*$*$*$*$* modal content */

function showModalContent(content, css_style){
	$("#basic-modal-content").html(content);
	$("#basic-modal-content").modal();
	$("#simplemodal-container").css(css_style);
	$("#simplemodal-container").css({
		"position" : "absolute",
		"top": ($(window).height() - $("#simplemodal-container").height()) / 2 + $(window).scrollTop() + "px",
		"left": ($(window).width() - $("#simplemodal-container").width()) / 2 + $(window).scrollLeft() + "px"
	});
}

function reloadCaptcha(captcha){
	captcha.src = captcha.src + "?" + new Date();
}


/*$*$*$*$* custom radio/checkbox */

function customLabel(){
	if($(".label_check input").length){
		$(".label_check").each(function(){ $(this).removeClass("c_on"); });
		$(".label_check input:checked").each(function(){ $(this).parent("label").addClass("c_on"); });                
	};
	if($(".label_radio input").length){
		$(".label_radio").each(function(){ $(this).removeClass("r_on"); });
		$(".label_radio input:checked").each(function(){ $(this).parent("label").addClass("r_on"); });
	};
}


/*$*$*$*$* j utils */

function chars_left(limitField, limitCount, limitNum) {
	if(limitField.value.length > limitNum){
		limitField.value = limitField.value.substring(0, limitNum);
	}else{
		limitCount.value = limitNum - limitField.value.length;
	}
}

function decreaseFont(container, min_size){ 
	var currentFontSizeNum = parseFloat($("#detailed-item").css("font-size"), 10);
	if(currentFontSizeNum > min_size) container.css("font-size", (currentFontSizeNum - 1) + "px");
}

function increaseFont(container, max_size){
	var currentFontSizeNum = parseFloat($("#detailed-item").css("font-size"), 10);
	if(currentFontSizeNum < max_size) container.css("font-size", (currentFontSizeNum + 1) + "px");
}

function submitenter(myfield, e){
	var keycode;
	
	if(window.event){
		keycode = window.event.keyCode;
	}else if(e){
		keycode = e.which;
	}else return true;
	
	if(keycode == 13){
		myfield.form.submit();
		return false;
	}else return true;
}

/*$*$*$*$* submit form */

function testKeyword(form, field_name, keyword_form, alert_message) {
	var keyword = document.forms[form][field_name].value;
	var trimmed = keyword.replace(/^\s+|\s+$/g, "");
	
	if(trimmed.length == 0 || keyword == keyword_form){
		alert(alert_message);
		return false;
	}
	
	return true;
}

function submit_form(form, field_name, keyword, alert_message){
	if(testKeyword(form, field_name, keyword, alert_message) == true) document.forms[form].submit();
}

function submit_enter(object, event, form, field_name, keyword, alert_message){
	var keycode;
	
	if(window.event){
		keycode = window.event.keyCode;
	}else if(event){
		keycode = event.which;
	}else{
		return true;
	}

	if (keycode == 13){
		submit_form(form, field_name, keyword, alert_message);
		return false;
	}
	
	return true;
}

/*$*$*$*$* input limits */

function decrement_value(id_input, min_value){
	var input = document.getElementById(id_input);
	if(input.value > min_value){ input.value--; }
	if(input.value == ""){ input.value = min_value; }
}

function increment_value(id_input, max_value){
	var input = document.getElementById(id_input);
	if(input.value < max_value){ input.value++; }
	if(input.value == ""){ input.value = min_value; }
}

function toBeBetween(input, min_value, max_value){
	if(parseInt(input.value) > max_value){ input.value = max_value; }
	if(parseInt(input.value) < min_value){ input.value = min_value; }
}

function limitText(limitField, maxVal){
	var maxValLen = (maxVal + "").length;
	
	if(parseInt(limitField.value) < 0){
		limitField.value = 0;
	}
	
	if(limitField.value.length >= maxValLen){
		limitField.value = limitField.value.substring(0, maxValLen);
		
		if(limitField.value > maxVal){
			limitField.value = limitField.value.substring(0, maxValLen - 1);
		}
		if(limitField.value == maxVal){
			limitField.value = maxVal;
		}
	}
}

function numbersonly(e, decimal){
	var key;
	var keychar;

	if(window.event){
		key = window.event.keyCode;
	}else if(e){
		key = e.which;
	}else{
		return true;
	}
	
	keychar = String.fromCharCode(key);
	
	if((key == null) || (key == 0) || (key == 8) ||  (key == 9) || (key == 13) || (key == 27)){
		return true;
	}else if((("0123456789").indexOf(keychar) > -1)){
		return true;
	}else if(decimal && (keychar == ".")){
		return true;
	}else{
		return false;
	}
}

/*$*$*$*$* open pop-up windows */

var WindowObjectReference = null; // global variable

function openRequestedPopup(strUrl, strWindowName, width, height){
	var wleft = parseInt((screen.availWidth/2) - (width/2));
	var wtop = parseInt((screen.availHeight/2) - (height/2));
	
	// IE5 and other old browsers might allow a window that is partially offscreen or wider than the screen. Fix that.
	if (wleft < 0) {
		w = screen.width; wleft = 0;
	}
	if (wtop < 0) {
		h = screen.height; wtop = 0;
	}
	
	if(WindowObjectReference == null || WindowObjectReference.closed){
		WindowObjectReference = window.open(strUrl, strWindowName, "width=" + width + ",height=" + height + ",menubar=1,resizable=no,left=" + wleft + ",top=" + wtop + ",scrollbars=yes,status=yes");
	}else{
		WindowObjectReference.focus();
	};
}
