//if(readCookie("text_size") == null) createCookie("text_size", $("#detailed-item").css("font-size"), 7);
//if(readCookie("o_text_size") == null) createCookie("o_text_size", $("#detailed-item").css("font-size"), 7);

$(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);
	}
	
	$("#hdmenu li").hover(
		function() { $(this).addClass("iehover"); },
		function() { $(this).removeClass("iehover"); }
	);
	
	rollover_image(".prerollover");
	
	$("a[rel='image']").colorbox({ transition: "fade" });
	
	if($(".tablet").length > 0){ setupRotator(".tablet", ".tablet img", 3000); }
	if($(".itestimonials").length > 0){ setupRotator(".itestimonials", ".testimonial", 3000); }
	
	/*
	$("#detailed-item").css("font-size", readCookie("text_size"));

	// Reset Font Size
	$(".resetFont").click(function(){
		createCookie("text_size", readCookie("o_text_size"), 7);
		$("#detailed-item").css("font-size", readCookie("text_size"));
	});
	// Increase Font Size
	$(".increaseFont").click(function(){ increaseFont($("#detailed-item"), 13); });
	// Decrease Font Size
	$(".decreaseFont").click(function(){ decreaseFont($("#detailed-item"), 11); });
	*/
});

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);
	});
}

/*$*$*$*$* content rotator */
function setupRotator(container, item, duration){
	if($(item).length > 1){
		$(item + ":first").addClass("current").fadeIn(1000);
		setInterval("textRotate('" + container + "', '" + item + "')", duration);
	}
}
function textRotate(container, item){
	var current = $(container + " > .current");
	if(current.next().length == 0){
		current.removeClass("current").fadeOut(1000);
		$(item + ":first").addClass("current").fadeIn(1000);
	}else{
		current.removeClass("current").fadeOut(1000);
		current.next().addClass("current").fadeIn(1000);
	}
}


/*$*$*$*$* j utils */

(function($){
	$.fn.truncate_ul = function(options) {
		var defaults = { target: $("#left-menu"), displayed_elements: 3 };
		var options = $.extend(defaults, options);
		
		if(options.displayed_elements <= 0){
			options.displayed_elements = 1;
		}
		
		init(options.target, options.displayed_elements-1); 
		
		return this.each(function() {
			$(this).click(function(){ 
				if(options.target.children("li:gt(" + (options.displayed_elements-1) + ")").css("display") == "none"){
					options.target.children("li:gt(" + (options.displayed_elements-1) + ")").css("display", "block");
					$(this).html("[-]");
				}else{
					options.target.children("li:gt(" + (options.displayed_elements-1) + ")").css("display", "none");
					$(this).html("[+]");
				}
			});
		});
	};
	function init(jq_obj, displayed_elements){
		jq_obj.children("li:gt(" + displayed_elements + ")").css("display", "none");
	}
})(jQuery);

/*$*$*$*$* 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 showHide(trigger_id, target_id, target_classname, hide_msg, show_msg, min_height){
	if($("#" + target_id).css("height") == min_height){
		var max_height = "";
		var newdiv = document.createElement("div");
		
		newdiv.innerHTML = $("#" + target_id).html();
		newdiv.className = target_classname;
		newdiv.style.width = $("#" + target_id).css("width");
		newdiv.style.position = "absolute";
		newdiv.style.height = "auto";
		newdiv.style.left = "-10000px";
		
		document.body.appendChild(newdiv);
		max_height = newdiv.offsetHeight;
		document.body.removeChild(newdiv);
		
		$("#" + target_id).animate({ height: max_height }, 500, function(){});
		$("#" + trigger_id + " span").html(hide_msg)
	}else{
		$("#" + target_id).css("height", min_height);
		$("#" + trigger_id + " span").html(show_msg)
	}
}

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 currentFontSize = container.css("font-size");
	var currentFontSizeNum = parseInt(currentFontSize, 10);
	if(currentFontSizeNum > min_size)
	var newFontSize = currentFontSizeNum - 1;
	container.css("font-size", newFontSize);
	createCookie("text_size", container.css("font-size"), 7);	
	return false;
}

function increaseFont(container, max_size){
  	var currentFontSize = container.css("font-size");
 	var currentFontSizeNum = parseInt(currentFontSize, 10);
	if(currentFontSizeNum < max_size)
    var newFontSize = currentFontSizeNum + 1;
	container.css("font-size", newFontSize);
	createCookie("text_size", container.css("font-size"), 7);	
	return false;
}

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;
}

/*$*$*$*$* cookies */

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		var expires = "; expires=" + date.toGMTString();
	}
	else var expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(";");
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==" ") c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

/*$*$*$*$* Google Map initialize */

function initialize(){
	var myOptions = {
		zoom: 16,
		center: new google.maps.LatLng(44.32173, 23.794219),
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	var map = new google.maps.Map(document.getElementById("google_map_content"), myOptions);
	var image = "img/common/logo-med-expert-map.png";
	var myLatLng = new google.maps.LatLng(44.32173, 23.794219);
	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();
	};
}
