$(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);
	}
	
	$("a[rel='image']").colorbox({ transition: "fade" });
	
	var originalFontSize = $("#detailed-item").css("font-size");
	// Decrease Font Size
	$(".decreaseFont").click(function(){ decreaseFont($("#detailed-item"), 12); });
	
	$(".resetFont").click(function(){ $("#detailed-item").css("font-size", originalFontSize); });
	
	// Increase Font Size
	$(".increaseFont").click(function(){ increaseFont($("#detailed-item"), 14); });
});

/*$*$*$*$* 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;
}

/*$*$*$*$* Google Map initialize */

function initialize(){
	var myOptions = {
		zoom: 16,
		center: new google.maps.LatLng(44.316727, 23.796601),
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	var map = new google.maps.Map(document.getElementById("google_map_content"), myOptions);
	var image = "img/common/logo-map-noela-style.png";
	var myLatLng = new google.maps.LatLng(44.316727, 23.796601);
	var logoMarker = new google.maps.Marker({
		position: myLatLng,
		map: map,
		icon: image
	});
}

/*$*$*$*$* 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) < 1){
		limitField.value = 1;
	}
	
	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();
	};
}
