/* This file stores JS that is used in all pages */
jQuery(document).ready(function() {		
	
	/* Fix Contact Form Textarea Focus */
	jQuery(".textarea textarea")
		.focus(function() {jQuery(this).parents('.textarea').addClass("focus");})
		.blur(function() {jQuery(this).parents('.textarea').removeClass("focus");});
	
	/* Remove the scrolbars from textarea in Opeara  */	
	jQuery.each(jQuery.browser, function(i) {
		if($.browser.opera) $("textarea").css("overflow","hidden");
	});
});
		
	// Make the custom select forms 
        jQuery(".selectForm").sexyCombo({
            showListCallback: function () {
                jQuery(".list-wrapper.visible").parent().addClass('showList');
            },
            hideListCallback: function () {
                jQuery(".list-wrapper.invisible").parent().removeClass('showList');
            }
        });


/* Start ToggleVal Plugin  */
/* -------------------------------------------------- *
 * ToggleVal 2.1
 * Updated: 1/16/09
 * -------------------------------------------------- *
 * Author: Aaron Kuzemchak
 * URL: http://aaronkuzemchak.com/
 * Copyright: 2008-2009 Aaron Kuzemchak
 * License: MIT License
** -------------------------------------------------- */
(function(jQuery){jQuery.fn.toggleVal=function(theOptions){if(!theOptions||typeof(theOptions)=="object"){theOptions=jQuery.extend({focusClass:"tv-focused",changedClass:"tv-changed",populateFrom:"default",text:null,removeLabels:false},theOptions)}else if(typeof(theOptions)=="string"&&theOptions.toLowerCase()=="destroy"){var destroy=true}return this.each(function(){if(destroy){jQuery(this).unbind("focus.toggleval").unbind("blur.toggleval").removeData("defText");return false}var defText="";switch(theOptions.populateFrom){case"alt":defText=jQuery(this).attr("alt");jQuery(this).val(defText);break;case"label":defText=jQuery("label[for='"+jQuery(this).attr("id")+"']").text();jQuery(this).val(defText);break;case"custom":defText=theOptions.text;jQuery(this).val(defText);break;default:defText=jQuery(this).val()}jQuery(this).addClass("toggleval").data("defText",defText);if(theOptions.removeLabels==true){jQuery("label[for='"+jQuery(this).attr("id")+"']").remove()}jQuery(this).bind("focus.toggleval",function(){if(jQuery(this).val()==jQuery(this).data("defText")){jQuery(this).val("")}jQuery(this).addClass(theOptions.focusClass).removeClass(theOptions.changedClass)}).bind("blur.toggleval",function(){if(jQuery(this).val()==""){jQuery(this).val(jQuery(this).data("defText"))}jQuery(this).removeClass(theOptions.focusClass);if(jQuery(this).val()!=jQuery(this).data("defText")){jQuery(this).addClass(theOptions.changedClass)}else{jQuery(this).removeClass(theOptions.changedClass)}})})}})(jQuery);
/* End ToggleVal Plugin  */

/* Start customInput() Plugin  */
/*-------------------------------------------------------------------- 
 * jQuery plugin: customInput()
 * by Maggie Wachs and Scott Jehl, http://www.filamentgroup.com
 * Copyright (c) 2009 Filament Group
 * Dual licensed under the MIT (filamentgroup.com/examples/mit-license.txt) and GPL (filamentgroup.com/examples/gpl-license.txt) licenses.
 * Article: http://www.filamentgroup.com/lab/accessible_custom_designed_checkbox_radio_button_inputs_styled_css_jquery/  
--------------------------------------------------------------------*/
jQuery.fn.customInput=function(){jQuery(this).each(function(i){if(jQuery(this).is('[type=checkbox],[type=radio]')){var input=jQuery(this);var label=jQuery('label[for='+input.attr('id')+']');var inputType=(input.is('[type=checkbox]'))?'checkbox':'radio';jQuery('<div class="custom-'+inputType+'"></div>').insertBefore(input).append(input,label);var allInputs=jQuery('input[name='+input.attr('name')+']');label.hover(function(){jQuery(this).addClass('hover');if(inputType=='checkbox'&&input.is(':checked')){jQuery(this).addClass('checkedHover');}},function(){jQuery(this).removeClass('hover checkedHover');});input.bind('updateState',function(){if(input.is(':checked')){if(input.is(':radio')){allInputs.each(function(){jQuery('label[for='+jQuery(this).attr('id')+']').removeClass('checked');});};label.addClass('checked');}
else{label.removeClass('checked checkedHover checkedFocus');}}).trigger('updateState').click(function(){jQuery(this).trigger('updateState');}).focus(function(){label.addClass('focus');if(inputType=='checkbox'&&input.is(':checked')){jQuery(this).addClass('checkedFocus');}}).blur(function(){label.removeClass('focus checkedFocus');});}});};
/* End customInput() Plugin  */

jQuery(document).ready(function () {

    /* Add a class for all the input fields that need a custom look */
    jQuery('.customRadioHere').addClass('customRadio');
    jQuery('.customCheckboxHere').addClass('customCheckbox');

    /* Using  customInput plugin make a custom looking radio and checkbox */
    jQuery('input').customInput();

    /* Hide the default looking radio and checkbox */
    jQuery('.customCheckboxHere input, .customRadioHere input').css({ 'left': '-9999px' });

    /* Lightbox Settings */

    /* Lightbox */
    jQuery('.Lightbox').nyroModal({
		 });

    jQuery('.rodape1').nyroModal({
		 });


    $(function () {
        $.nyroModalSettings({
            processHandler: function (settings) {
                var from = settings.from;
                if (from && from.title && from.title == "View all Images") {
                    $.nyroModalSettings({
                        width: 850,
                        height: 660,
                        type: 'iframe',
                        url: 'gallery.html',
						titleFromIframe: false,
                        closeButton: '<a href="#" class="nyroModalClose" id="closeBut" title="close window"><img src="images/nyroModal/close.png" alt="close" /></a>' // Adding automaticly as the first child of #nyroModalWrapper
	              });
                } else if(from && from.title && from.title == "View all") {
					$.nyroModalSettings({
                        width: 850,
                        height: 660,
                        type: 'iframe',
                        url: 'gallery2.php?idx='+from.rev,
						titleFromIframe: false,
                        closeButton: '<a href="#" class="nyroModalClose" id="closeBut" title="close window"><img src="images/nyroModal/close.png" alt="close" /></a>' // Adding automaticly as the first child of #nyroModalWrapper
                    });
                } else if(from && from.title && from.title == "Request Info") {
					$.nyroModalSettings({
                        width: 700,
                        height: 560,
                        type: 'iframe',
                        url: 'appointment2.php?idx='+from.rev,
						titleFromIframe: false,
                        closeButton: '<a href="#" class="nyroModalClose" id="closeBut" title="close window"><img src="images/nyroModal/close.png" alt="close" /></a>' // Adding automaticly as the first child of #nyroModalWrapper
                    });
                } else if(from && from.title && from.title == "play videos") {
					$.nyroModalSettings({
                        width: 850,
                        height: 660,
                        type: 'iframe',
                        url: 'galleryvideo.html',
						titleFromIframe: false,
                        closeButton: '<a href="#" class="nyroModalClose" id="closeBut" title="close window"><img src="images/nyroModal/close.png" alt="close" /></a>' // Adding automaticly as the first child of #nyroModalWrapper

                    });
                } else if(from && from.title && from.title == "view gallery") {
					$.nyroModalSettings({
                        width: 850,
                        height: 660,
                        type: 'iframe',
                        url: 'gallery.html',
						titleFromIframe: false,
                        closeButton: '<a href="#" class="nyroModalClose" id="closeBut" title="close window"><img src="images/nyroModal/close.png" alt="close" /></a>' // Adding automaticly as the first child of #nyroModalWrapper
                    });
				} else if(from && from.title && from.title == "open location") {
					$.nyroModalSettings({
                        width: 850,
                        height: 660,
                        type: 'iframe',
                        url: 'location.html',
						titleFromIframe: false,
                        closeButton: '<a href="#" class="nyroModalClose" id="closeBut" title="close window"><img src="images/nyroModal/close.png" alt="close" /></a>' // Adding automaticly as the first child of #nyroModalWrapper
                    });
				} else if(from && from.title && from.title == "more info") {
					$.nyroModalSettings({
                        width: 940,
                        height: 421,
                        type: 'iframe',
                        url: 'teamdetail.php?idx='+from.rev,
						titleFromIframe: false,
                        closeButton: '<a href="#" class="nyroModalClose" id="closeBut" title="close window"><img src="images/nyroModal/close.png" alt="close" style="margin-right:12px;margin-top:-5px;" /></a>' // Adding automaticly as the first child of #nyroModalWrapper
                   });
					} else if(from && from.title && from.title == "open form") {
					$.nyroModalSettings({
                        width: 960,
                        height: 580,
                        type: 'iframe',
                        url: 'recruitment-form.html',
						titleFromIframe: false,
                        closeButton: '<a href="#" class="nyroModalClose" id="closeBut" title="close window"><img src="images/nyroModal/close.png" alt="close" /></a>' // Adding automaticly as the first child of #nyroModalWrapper
                    });                   
					}
				else if(from && from.title && from.title == "media") {
					$.nyroModalSettings({
                        width: 970,
                        height: 500,
                        type: 'iframe',
                        url: 'destaquesH.php?idx='+from.rev,
						titleFromIframe: false,
                        closeButton: '<a href="#" class="nyroModalClose" id="closeBut" title="close window"><img src="images/nyroModal/close.png" alt="close" /></a>' // Adding automaticly as the first child of #nyroModalWrapper
                    });
				}
				else if(from && from.title && from.title == "artigos") {
					$.nyroModalSettings({
                        width: 970,
                        height: 500,
                        type: 'iframe',
                        url: 'artigos_cientificos.php?idx='+from.rev,
						titleFromIframe: false,
                        closeButton: '<a href="#" class="nyroModalClose" id="closeBut" title="close window"><img src="images/nyroModal/close.png" alt="close" /></a>' // Adding automaticly as the first child of #nyroModalWrapper
                    });
				}
				else if(from && from.title && from.title == "recruta") {
					$.nyroModalSettings({
                        width: 970,
                        height: 535,
                        type: 'iframe',
                        url: 'recrutaH.php?idx='+from.rev,
						titleFromIframe: false,
                        closeButton: '<a href="#" class="nyroModalClose" id="closeBut" title="close window"><img src="images/nyroModal/close.png" alt="close" /></a>' // Adding automaticly as the first child of #nyroModalWrapper
                    });
				}
				else if(from && from.title && from.title == "recruta5") {
					$.nyroModalSettings({
                        width: 970,
                        height: 535,
                        type: 'iframe',
                        url: 'recrutaH2.php?idx='+from.rev,
						titleFromIframe: false,
                        closeButton: '<a href="#" class="nyroModalClose" id="closeBut" title="close window"><img src="images/nyroModal/close.png" alt="close" /></a>' // Adding automaticly as the first child of #nyroModalWrapper
                    });
				}
					else if(from && from.title && from.title == "Schedule Appointment") {
					$.nyroModalSettings({
                        width: 960,
                        height: 660,
                        type: 'iframe',
                        url: 'appointment.php',
						titleFromIframe: false,
                        closeButton: '<a href="#" class="nyroModalClose" id="closeBut" title="close window"><img src="images/nyroModal/close.png" alt="close" /></a>' // Adding automaticly as the first child of #nyroModalWrapper
                    });
					} else if(from && from.title && from.title == "spontaneous form") {
					$.nyroModalSettings({
                        width: 960,
                        height: 580,
                        type: 'iframe',
                        url: 'spontaneous-form.html',
						titleFromIframe: false,
                        closeButton: '<a href="#" class="nyroModalClose" id="closeBut" title="close window"><img src="images/nyroModal/close.png" alt="close" /></a>' // Adding automaticly as the first child of #nyroModalWrapper
                    });
					} else if(from && from.title && from.title == "View description") {
					$.nyroModalSettings({
                        minWidth: 340,
                        minHeight: 400,
                        type: 'iframe',
                        url: 'glossary-item.html',
						titleFromIframe: false,
                        closeButton: '<a href="#" class="nyroModalClose" id="closeBut" title="close window"><img src="images/nyroModal/close.png" alt="close" /></a>' // Adding automaticly as the first child of #nyroModalWrapper
                    });
				} else {
                    $.nyroModalSettings({
                        minWidth: 340,
                        minHeight: 450,
                        autoSizable: false,
						titleFromIframe: false,
						title: null,
                        closeButton: '<a href="#" class="nyroModalClose" id="closeBut" title="close window"><img src="images/nyroModal/close.png" alt="close" /></a>',
                        showBackground: function (elts, settings, callback) {
                            elts.bg.css({ opacity: 0 }).fadeTo(500, 0.5, callback);
                        }
                    });
                }
            }
        });
    });
    // Gallery

    /* Image Lightbox */
    jQuery('.imageLightbox').nyroModal({});

    /* Footer Contact Form Settings */
    var contactForm = $("#Form");
    var subscribeForm = $("#Subscribe");

    var inputName = contactForm.find(".name");
    var inputEmail = contactForm.find(".email");
    var inputMessage = contactForm.find(".message");
    var loadingImage = contactForm.find('.loadingImage');
    var responseText = contactForm.find(".responseText");

    var inputSubscribe = subscribeForm.find("#subscribeEmail");

    // On Submitting  	
    contactForm.bind("submit", function (e) {
        if (validateName(e, inputName) & validateEmail(e, inputEmail) & validateMessage(e, inputMessage)) {
            //ajaxSend(contactForm, responseText, loadingImage);
        };
        return false;
    });

    subscribeForm.bind("submit", function (e) {
        if (validateEmail(e, inputSubscribe)) {
            //ajaxSend(contactForm, responseText, loadingImage);
        }
        return false;
    });

    // On key press  
    inputName.bind("keyup", function (e) {
        validateName(e, inputName);
    });
    inputEmail.bind("keyup", function (e) {
        validateEmail(e, inputEmail);
    });
    inputMessage.bind("keyup", function (e) {
        validateMessage(e, inputMessage);
    });
    inputSubscribe.bind("keyup", function (e) {
        validateEmail(e, inputSubscribe);
    });

    /* Using ToggleVal Plugin to make the text in textInput forms toggle */
    jQuery(".textInput").toggleVal();
});

/* Functions requierd by the contact forms */
function validateName(event, input){  
	if(input.val().length < 4 || input.val() == "your name"){
		if(event.type != "keyup") {
			input.addClass("error"); 
			input.parent().find('.errorText').slideDown(); 
		}
		return false;
	}else{input.removeClass("error"); input.parent().find('.errorText').slideUp(); return true;}
}

function validateEmail(event, input){
	var a = input.val();
	var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
	
	if(filter.test(a)){
		input.removeClass("error"); 
		input.parent().find('.errorText').slideUp(); 
		return true;
	}else{
		if(event.type != "keyup") {
			input.addClass("error"); 
			input.parent().find('.errorText').slideDown(); 
		}
		return false;
	}
} 

function validateMessage(event, input){  
	if(input.val().length < 10 || input.val() == "your message"){
		if(event.type != "keyup") {
			input.parent().addClass("error"); 
			input.parent().find('.errorText').slideDown(); 
		}
		return false;
	}else{input.parent().removeClass("error"); input.parent().find('.errorText').slideUp(); return true;}
} 

function ajaxSend(form, response, loading){	
	loading.show();
	response.slideUp().animate({X:""} , 200, "linear", function(){
		response.html('<small class="grey">Please wait, your message is being processed.</small><br />').slideDown();		
	});
	
	// Make AJAX request 		
	$.post('contactScript.xxx', form.serialize(), function(data){
		loading.hide(200);
		response.slideUp().animate({X:""} , 200, "linear", function(){response.html(data).slideDown();});
	});

	//Cancel default action
	return false;
};
/* validation fuctions for subscription form */

