
$(document).ready(function(){

	// comments.php - handle hover avatar image
	// thanks to: http://www.sohtanaka.com/web-design/fancy-thumbnail-hover-effect-w-jquery/
	/*
	$("ul#avatars img").hover(function() {
		$(this).css({'z-index' : '10'}); // Add a higher z-index value so this image stays on top
		$(this).addClass("hover").stop()  // Add class of "hover", then stop animation queue buildup
			.animate({			
				marginTop: '-35px',   // The next 4 lines will vertically align this image 
				marginLeft: '-35px',
				top: '50%',
				left: '50%',
				width: '60px',  // Set new width 
				height: '60px'  // Set new height 
			}, 400); // the speed of how fast/slow this hover animates
	
		} , function() {
		$(this).removeClass("hover").stop()  // Remove the "hover" class , then stop animation queue buildup
			.animate({
				marginTop: '0', // Set alignment back to default 
				marginLeft: '0',
				top: '0',
				left: '0',
				width: '30px', // Set width back to default 
				height: '30px' // Set height back to default 
			}, 400, 
				function() {
    				$(this).css({'z-index' : '0'}); // Set z-index back to 0 after animation complete
  				}
			);
	});
	*/

	// comments.php - handle selecting avatar image
	$("ul#avatars img").click(function() {
		$("ul#avatars img").removeClass("selected"); //Remove any "selected" class
		$(this).addClass("selected"); //Add "selected" class to selected tab
		$("#avatar").val($(this).attr('alt')); // update hidden field with the selected img value
		return false;
	});
	
	

	// index.php - handle "more..." links (under each language)
	$(".hever").css({"display":"none"}); // init
	$(".more_link").toggle(function() {
	
			$(this).parent().children("p").css({"overflow":"visible"});
			$(this).parent().children("p").animate({ "height":"300" }, 1500, 
				function() {
    				$(this).parent().children("p").css({"height":"auto"}); // after animation complete    				
  				});

			//$(this).parent().children("p").css({"height":"auto", "overflow":"visible"});
			$(this).parent().children("p").children(".hever").css({"display":"inline"});
			$(this).text("less...");
			
		}, function() {		
			
			$(this).parent().children("p").animate({ "height":"44px" }, 1500, 
				function() {
    				$(this).parent().children("p").css({"overflow":"hidden"}); // after animation complete
  				});

			//$(this).parent().children("p").css({"height":"44px", "overflow":"hidden"});			
			$(this).parent().children("p").children(".hever").css({"display":"none"});
			$(this).text("more...");
	});
	
	
	
	// index.php (languages tabs) - handle tabs click
	//When page loads...
	$(".link_type_content").hide(); //Hide all content
	$("ul#link_tabs li:first").addClass("active").show(); //Activate first tab
	$(".link_type_content:first").show(); //Show first tab content

	$("ul#link_tabs li").click(function() {

		$("ul#link_tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".link_type_content").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});

	

	// menu hover effect
	$('#menu ul li a')
		.css( {backgroundPosition: "0 0"} )
		.mouseover(function(){
			$(this).stop().animate(
				{backgroundPosition:"(0 -20px)"}, 
				{duration:500})
			})
		.mouseout(function(){
			$(this).stop().animate(
				{backgroundPosition:"(0 0)"}, 
				{duration:500})
			});
	
	
});




$.fn.tagName = function() {
    return this.get(0).tagName.toLowerCase();
}

function getNextSibling(obj) {
  obj  = obj.nextSibling;
  while (obj.nodeType != 1) {
    obj = obj.nextSibling;
  }
  return obj;
}

$.fn.delay = function(time, callback){
    // Empty function:
    jQuery.fx.step.delay = function(){};
    // Return meaningless animation, (will be added to queue)
    return this.animate({delay:1}, time, callback);
}

// make a click when pressing 'enter' in code entering
$(function() {  
	$("#cdextras #code").keypress(function (e) {  
   	if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {  
   		$('#btn_check_code').click(); 
   		return false;  
   	} else {  
   		return true;  
   	}  
	});  
});  

function numericValidation(obj) {
	var re = new RegExp(/^[.\d]+$/);
	if (!obj.value.match(re)) {  // not a numeric value
		// remove last typed character
		obj.value = obj.value.substring(0,obj.value.length-1);
	}
}


function startProcessAnimation(btn_id) {
	$("#in_process").show();
	$("#"+btn_id).attr("disabled", "disabled");
}

function stopProcessAnimation(btn_id) {
	$("#in_process").hide();
	$("#"+btn_id).removeAttr("disabled");
	
}

function displayErrorMsg(displayOrNot, msg) {
	// show general top error msg	
	if(displayOrNot) {
		if(msg) {
			$("#error_msg").text(msg);
		}
		$("#error_msg").show();
	} else { // hide error msg
		$("#error_msg").hide();
	}
}	

function validateRequiredFields(myForm, requiredTextFields, requiredSelectFields, requiredRadioFields) {
	var isSomethingMissing = false;

	// check required text fields
	if(requiredTextFields) {
		for(var i=0; i<requiredTextFields.length; i++) {
			$("#" + requiredTextFields[i]).removeClass("field_error"); // clear previous errors highlights
			
			if(document.getElementById(requiredTextFields[i]).value == "") {
				$("#" + requiredTextFields[i]).addClass("field_error");
				isSomethingMissing = true;
			}
		}
	}

	// check required select fields
	if(requiredSelectFields) {
		for(var i=0; i<requiredSelectFields.length; i++) {
			$("#" + requiredSelectFields[i]).removeClass("field_error"); // clear previous errors highlights
			
			//if(document.getElementById(requiredSelectFields[i]).value == "-1") {
			if(myForm[requiredSelectFields[i]].value == "-1") {
				$("#" + requiredSelectFields[i]).addClass("field_error");
				isSomethingMissing = true;
			}
		}	
	}

	// check required radio fields
	if(requiredRadioFields) {
		for(var i=0; i<requiredRadioFields.length; i++) {
			//$("#" + requiredRadioFields[i]).removeClass("field_error"); // clear previous errors highlights
			
			var radioItemSelected = false;
			
			var myRadio = myForm[requiredRadioFields[i]];
			
			$("input[name="+myRadio[0].name+"]").parent().removeClass("field_error"); // clear previous errors highlights
			
			// loop all the radio items of the current object and check if something is selected
			for(var j=0; j<myRadio.length; j++) {
				if(myRadio[j].checked) {
					radioItemSelected = true;
					break;
				}
			}
			if(!radioItemSelected) {
				$("input[name="+myRadio[0].name+"]").parent().addClass("field_error");
				isSomethingMissing = true;
			}				
			
		}	
	}	
	return isSomethingMissing;
}

