$(function(){ 
// color box launch
$('#comments .thickbox, .login-link,a[title="Log In"]').colorbox({iframe:true,width:400, height:250});
$('#footer .thickbox').colorbox();
$('#content #text .slideshow').colorbox({
	slideshow:true,
	photo:true,
	preloading:true,
	slideshowSpeed:5000  
});
$('.rsvp .thickbox, a[title=Checkout].thickbox,a[title=Donate],.eventdetail a[title="Log In"]').colorbox({iframe:true,width:450, height:570});
$('#options .thickbox,.section .thickbox, a[title="Edit Your Profile"]').colorbox({iframe:true,width:500, height:570});
$(".togglecontent").hide();
if($(".toggle").length > 0){
    
    var exp = $(".toggle");
    exp.click(function(e){
        $(this).next('.togglecontent').toggle();
        e.preventDefault();
    });
}

$("#logo").click(function(e){
    window.location = "/";
});    

//Nav Contact Flyout Form

    //set global variables and cache DOM elements for reuse later
    var form = $('form#contact-us'),
        formElements = form.find('input[type!="submit"],textarea'),
        formSubmitButton = form.find('[type="submit"]'),
        captcha = form.find('[name="email-confirmation"]');
        errorNotice = $('#error'),
        successNotice = $('#success'),
        loading = $('#loading'),
        errorMessages = {
            required: ' is a required field',
            email: 'Entered a valid email address for: ',
            minlength: ' must be greater than '
        };

        /*Date.prototype.valid = function(){
            return isFinite(this);
        } 
              
        form.find('#contact-us-today').keyup(function(e){
            var value = $(this).attr("value");
            var vdate = new Date(Date.parse(value));
            var today = new Date();
            if(vdate.valid()){
                //included /_js/date.js to format dates
                if(vdate.format("m/d/Y") == today.format("m/d/Y")){
                    formSubmitButton.removeAttr("disabled");
                    $(this).hide();
                }
            }else{
                formSubmitButton.attr("disabled","disabled");
            }
        });*/
   

    //feature detection + polyfills
    formElements.each(function(){

        //if HTML5 input placeholder attribute is not supported
        if(!Modernizr.input.placeholder){
            var placeholderText = this.getAttribute('placeholder');
            if(placeholderText){
                $(this)
                    .addClass('placeholder-text')
                    .val(placeholderText)
                    .bind('focus',function(){
                        if(this.value == placeholderText){
                            $(this)
                                .val('')
                                .removeClass('placeholder-text');
                        }
                    })
                    .bind('blur',function(){
                        if(this.value == ''){
                            $(this)
                                .val(placeholderText)
                                .addClass('placeholder-text');
                        }
                    });
            }
        }

        //if HTML5 input autofocus attribute is not supported
        if(!Modernizr.input.autofocus){
            if(this.getAttribute('autofocus')) this.focus();
        }

    });

    //to ensure compatibility with HTML5 forms, we have to validate the form on submit button click event rather than form submit event.
    //An invalid html5 form element will not trigger a form submit.
    formSubmitButton.bind('click',function(){
        
        var formok = true,
            errors = [];

        formElements.each(function(){
            var name = this.name,
                nameUC = name.ucfirst(),
                value = this.value,
                placeholderText = this.getAttribute('placeholder'),
                type = this.getAttribute('type'), //get type old school way
                isRequired = this.getAttribute('required'),
                minLength = this.getAttribute('data-minlength');

            //if HTML5 formfields are supported
            if( (this.validity) && !this.validity.valid ){
                formok = false;

                //if there is a value missing
                if(this.validity.valueMissing){
                    errors.push(nameUC + errorMessages.required);
                }
                //if this is an email input and it is not valid
                else if(this.validity.typeMismatch && type == 'email'){
                    errors.push(errorMessages.email + nameUC);
                }

                this.focus(); //safari does not focus element an invalid element
                return false;
            }

            //if this is a required element
            if(isRequired){
                //if HTML5 input required attribute is not supported
                if(!Modernizr.input.required){
                    if(value == placeholderText){
                        this.focus();
                        formok = false;
                        errors.push(nameUC + errorMessages.required);
                        return false;
                    }
                }
            }

            //if HTML5 input email input is not supported
            if(type == 'email'){
                if(!Modernizr.inputtypes.email){
                    var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
                     if( !emailRegEx.test(value) ){
                        this.focus();
                        formok = false;
                        errors.push(errorMessages.email + nameUC);
                        return false;
                    }
                }
            }
            

            //check minimum lengths
            if(minLength){
                if( value.length < parseInt(minLength) ){
                    this.focus();
                    formok = false;
                    errors.push(nameUC + errorMessages.minlength + minLength + ' charcters');
                    return false;
                }
            }
        });

        //if form is not valid
        if(!formok){
            showNotice('error',errors);
        }else { //if form is valid 
            if(captcha.attr("value") === ""){ 
            	loading.show();
                $.ajax({
                    url: form.attr('action'),
                    type: "POST",
                    data: form.serialize(),
                    success: function(){
                        showNotice('success', "Your Email Was Sent!");
                        form.get(0).reset();
                        loading.hide();
                    }
                });
            }
        }

        return false; //this stops submission off the form and also stops browsers showing default error messages

    });

    //other misc functions
    function showNotice(type,data)
    {
        if(type == 'error'){
            successNotice.hide();
            errorNotice.find("p[id!='info']").remove();
            for(x in data){
                errorNotice.append('<p>'+data[x]+'</p>');
            }
            errorNotice.show();
        } else {
            errorNotice.hide();
            successNotice.show();
        }
    }

    String.prototype.ucfirst = function() {
        return this.charAt(0).toUpperCase() + this.slice(1);
    }
    
    
    $("#fl_nav_search").find("#search_term").marcoPolo({
        url:"/_inc/ajax-search.php",
        formatItem: function(record, $item, $input, $list){
            //return '<strong>'+data.type+' </strong>'+'<div>'+data.title+'</div>';
            if (record.isHeader) {
               $item.addClass('spotlightHeader');
               //console.log(record[+1]);
               //return '<h4>' + record.type + '</h4>';
             }
             else {
               var title  = record.title,
                   module = record.type;
               
               if (!title) {
                 title = '(No title)';
               }
               
               $item.attr('title', title);
               
               if (title.length > 220) {
                 title = title.substring(0, 220) + '&hellip;';
               }
               
               return "<div class='stitle'>"+title+"</div><div class='smod'>"+module+"</div>";
             }
        }, 
        onSelect:
             function(record, $item, $input, $list) {
                var title = record.title;
                
                if (!title) {
                  title = '(No title)';
                }
                
                $input.val(title);
                
                window.location = record.url;
              },
            selectable: ':not(.spotlightHeader)'
    });
    
    if($(".monkForm").length > 0){
        $(".monkForm").find("legend:first").remove();
    }
    
    $("#text > .last > .thumblist").find(".imgwrap").each(function(){
        var imgurl = $(this).find("img").attr("src");
        if( imgurl === "" || imgurl === "http://lifecenter.monkpreview.com/mediafiles/default-event-image.jpg"){
            $(this).find("img").attr("src","/mediafiles/default-event-image.jpg");
  	        $(this).find("img").css({"top":"0px"});
  	        $(this).find("img").css({"left":"0px"});
  	        $(this).find("img").css({"height":"54px"});
  	        $(this).find("img").css({"width":"90px"});
        }
    });

});
  
