/*
    JS DIRECTORY
        __FORMS
        __SLIDESHOW
        __MODALWINDOWS
        __MISC

/*__FORMS
==========================================================*/

    /* Regular Expressions */
    var REGEXP_PHONE = new RegExp("^((1)?[(-.\\s]*\\d{3}[)-.\\s]*)?[-.\\s]*\\d{3}[-.\\s]*\\d{4}$");
    var REGEXP_EMAIL = new RegExp("^[a-zA-Z0-9-_.]+@([a-zA-Z0-9-_]+\\.)+[a-zA-Z]{2,6}$");
    var REGEXP_NAME = new RegExp("^[a-zA-Z]+\\s?[a-zA-Z]*$");

	function validate_field(field, regex, message){
        if( field.value == "" || (regex && !regex.test(field.value)) ){
            alert(message);
            field.focus();
            return false;
        }
        /* valid imput, return true */
        return true;
	}

	function validate_form(theform){
        if( theform.name && !validate_field(theform.name, REGEXP_NAME, 'Not a valid name!') ) return false;
		if( theform.FirstName && !validate_field(theform.FirstName, REGEXP_NAME, 'Not a valid first name!') ) return false;
		if( theform.LastName && !validate_field(theform.LastName, REGEXP_NAME, 'Not a valid last name!') ) return false;
        if( theform.phone && !validate_field(theform.phone, REGEXP_PHONE, 'Not a valid phone number!') ) return false;
		if( theform.Phone && !validate_field(theform.Phone, REGEXP_PHONE, 'Not a valid phone number!') ) return false;
		if( theform.ref_phone && !validate_field(theform.ref_phone, REGEXP_PHONE, 'Not a valid phone number!') ) return false;
        if( theform.email && !validate_field(theform.email, REGEXP_EMAIL, 'Not a valid email address!') ) return false;
		if( theform.submit_by && !validate_field(theform.submit_by, REGEXP_EMAIL, 'Not a valid email address!') ) return false;
		if( theform.ref_email && !validate_field(theform.ref_email, REGEXP_EMAIL, 'Not a valid email address!') ) return false;
        if( theform.company && !validate_field(theform.company, null, 'Company cannot be blank!') ) return false;
        if( theform.address && !validate_field(theform.address, null, 'Address cannot be blank!') ) return false;
        if( theform.city && !validate_field(theform.city, null, 'City cannot be blank!') ) return false;
        if( theform.state && !validate_field(theform.state, null, 'State cannot be blank!') ) return false;
        if( theform.zip && !validate_field(theform.zip, null, 'Zip cannot be blank!') ) return false;

     // Submit Data
        $.ajax({
            url: "/form-handler",
            type: "POST",
            data: 'json=true&' + $(theform).serialize(),
            dataType: 'json',
            cache: false,
            error: function(a,b,c){ alert('A server error occured. Please try again later.'); },
            success: function(json){
                if(json.error){
                    alert(json.error);
                }else{
                    $(theform).html(json.success);
                }
            }
        });
        return false;
	}

	function validate_form_nojax(theform){
        if( theform.name && !validate_field(theform.name, REGEXP_NAME, 'Not a valid name!') ) return false;
		if( theform.FirstName && !validate_field(theform.FirstName, REGEXP_NAME, 'Not a valid first name!') ) return false;
		if( theform.LastName && !validate_field(theform.LastName, REGEXP_NAME, 'Not a valid last name!') ) return false;
        if( theform.phone && !validate_field(theform.phone, REGEXP_PHONE, 'Not a valid phone number!') ) return false;
		if( theform.Phone && !validate_field(theform.Phone, REGEXP_PHONE, 'Not a valid phone number!') ) return false;
		if( theform.ref_phone && !validate_field(theform.ref_phone, REGEXP_PHONE, 'Not a valid phone number!') ) return false;
        if( theform.email && !validate_field(theform.email, REGEXP_EMAIL, 'Not a valid email address!') ) return false;
		if( theform.submit_by && !validate_field(theform.submit_by, REGEXP_EMAIL, 'Not a valid email address!') ) return false;
		if( theform.ref_email && !validate_field(theform.ref_email, REGEXP_EMAIL, 'Not a valid email address!') ) return false;
        if( theform.company && !validate_field(theform.company, null, 'Company cannot be blank!') ) return false;
        if( theform.address && !validate_field(theform.address, null, 'Address cannot be blank!') ) return false;
        if( theform.city && !validate_field(theform.city, null, 'City cannot be blank!') ) return false;
        if( theform.state && !validate_field(theform.state, null, 'State cannot be blank!') ) return false;
        if( theform.zip && !validate_field(theform.zip, null, 'Zip cannot be blank!') ) return false;
	}

	function validate_form_mul(theform){
        if( theform.name && !validate_field(theform.name, REGEXP_NAME, 'Not a valid name!') ) return false;
		if( theform.FirstName && !validate_field(theform.FirstName, REGEXP_NAME, 'Not a valid first name!') ) return false;
		if( theform.LastName && !validate_field(theform.LastName, REGEXP_NAME, 'Not a valid last name!') ) return false;
        if( theform.phone && !validate_field(theform.phone, REGEXP_PHONE, 'Not a valid phone number!') ) return false;
		if( theform.Phone && !validate_field(theform.Phone, REGEXP_PHONE, 'Not a valid phone number!') ) return false;
		if( theform.ref_phone && !validate_field(theform.ref_phone, REGEXP_PHONE, 'Not a valid phone number!') ) return false;
        if( theform.email && !validate_field(theform.email, REGEXP_EMAIL, 'Not a valid email address!') ) return false;
		if( theform.submit_by && !validate_field(theform.submit_by, REGEXP_EMAIL, 'Not a valid email address!') ) return false;
		if( theform.ref_email && !validate_field(theform.ref_email, REGEXP_EMAIL, 'Not a valid email address!') ) return false;
        if( theform.company && !validate_field(theform.company, null, 'Company cannot be blank!') ) return false;
        if( theform.address && !validate_field(theform.address, null, 'Address cannot be blank!') ) return false;
        if( theform.city && !validate_field(theform.city, null, 'City cannot be blank!') ) return false;
        if( theform.state && !validate_field(theform.state, null, 'State cannot be blank!') ) return false;
        if( theform.zip && !validate_field(theform.zip, null, 'Zip cannot be blank!') ) return false;

     // Submit Data
        $.ajax({
            url: "/form-handler",
            type: "POST",
            data: 'json=true&' + $(theform).serialize(),
            dataType: 'json',
            cache: false,
            error: function(a,b,c){ alert('A server error occured. Please try again later.'); },
            success: function(json){
                if(json.error){
                    alert(json.error);
                }else{
                    theform.submit();
                }
            }
        });

		return false;
	}


function goToByScroll(id){
	$('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');
}


/*__SLIDESHOW
    Functions used for slideshows, call slideshowInit to make a slideshow
==========================================================*/

    var slideshows = [];
    function slideshowInit(containerid, transitionTime, slideTimeLength){

     // Set Slideshow Array & get container
        var slideshow = slideshows[slideshows.length] = [];
        var container = $('#'+containerid);

     // Set Slideshow Buttons
        $('a.'+containerid+'-btn').each( function(){
            $(this).click(function(e){ e.preventDefault(); slideBtnClick( slideshow, $(this).attr('href') ); });
        });
        if( $('a.'+containerid+'-btn[href="1"]').length ){
            slideshow.trackNumberButtons = true;
        } else {
            slideshow.trackNumberButtons = false;
        }

     // Get Slideshow Timings
        if( transitionTime == null ) transitionTime = 1000;
        if( slideTimeLength == null ) slideTimeLength = 5000;

     // Get Slideshow Slides
        var slides = $(' > div', container);
        $(slides).hide();

     // Set Slideshow Variables
        slideshow.containerid = containerid;
        slideshow.container = container;
        slideshow.slides = slides;
        slideshow.maxSlide = slides.length;
        slideshow.transitionTime = transitionTime;
        slideshow.slideTimeLength = slideTimeLength;
        slideshow.inTransition = false;
        slideshow.currentSlide = 1;
        slideshow.previousSlide = 1;

     // Start Slideshow
        advanceSlide(slideshow, 1);
    }


    function advanceSlide(slideshow, action){
        slideshow.inTransition = true;

     // figure out new slide number
        if(action == 'next'){
            var newSlide = slideshow.currentSlide + 1;
        } else if(action == 'prev'){
            var newSlide = slideshow.currentSlide - 1;
        } else{
            var newSlide = Number(action);
        }

     // make sure new slide within bounds
        if ( newSlide > slideshow.maxSlide ){
            newSlide = 1;
        } else if ( newSlide < 1 ){
            newSlide = slideshow.maxSlide;
        }

     // activate / animate new slide
        slideshow.previousSlide = slideshow.currentSlide;
        slideshow.currentSlide = newSlide;
        newSlide = newSlide - 1;
        $(slideshow.slides).hide();
        slideshow.container.append(slideshow.slides[newSlide]);
        $(slideshow.slides[newSlide])
            .css({opacity: 0}).show()
            .animate({opacity: 1}, slideshow.transitionTime, function(){ slideshow.inTransition = false; })
            .animate({opacity: '+=0'}, slideshow.slideTimeLength, function(){ advanceSlide(slideshow, 'next') } );

    // if track numbered buttons, do it.
        if( slideshow.trackNumberButtons ){
         // Remove class "current" from numbered buttons
            $('a.'+slideshow.containerid+'-btn[href="'+slideshow.previousSlide+'"]').removeClass('current');
         // Add class "current" to current button.
            $('a.'+slideshow.containerid+'-btn[href="'+slideshow.currentSlide+'"]').addClass('current');
        }
    }


    function slideBtnClick(slideshow, action){
        if( slideshow.inTransition == false ){
         // No current slide transition, advance slide
            $("div", slideshow.container).stop(true, false);
            advanceSlide(slideshow, action);
        }
    }


/*__MODALWINDOWS
==========================================================*/

    function showmodal($html){
        //Set height and width to mask to fill up the whole screen
        $('#modalmask').css({'width':$(document).width(),'height':$(document).height()});
        //transition effect
        $('#modalmask').fadeIn(250);
        //load html if set
        if($html)
            $('#modalcontent').html($html);
        //Set the popup window to center
        $('#modalwindow').css("top", ( $(window).height() - $('#modalwindow').height() ) / 2+$(window).scrollTop() + "px");
        $('#modalwindow').css("left", ( $(window).width() - $('#modalwindow').width() ) / 2+$(window).scrollLeft() + "px");
        $('#modalwindow').css("width", $('#modalwindow').width() ) ;
        //transition effect
        $('#modalwindow').fadeIn(500);
    }

    function refreshmodal($html){
        //transition effect
        $('#modalwindow').fadeOut(250, function(){
            //load html if set
            if($html)
                $('#modalcontent').html($html);
            //Set the popup window to center
            $('#modalwindow').css("top", ( $(window).height() - $('#modalwindow').height() ) / 2+$(window).scrollTop() + "px");
            $('#modalwindow').css("left", ( $(window).width() - $('#modalwindow').width() ) / 2+$(window).scrollLeft() + "px");
            //transition effect
            $('#modalwindow').fadeIn(500);
        });
    }

    function closemodal(){
        // fade both mask and modal window
        $('#modalmask, #modalwindow').fadeOut(250);
    }

    $(function(){
        //if close button is clicked, hide mask and modal window
        $('#modalclose').click(function (e) {
            e.preventDefault();
            closemodal();
        });

        //if mask is clicked, hide mask and modal window
        $('#modalmask').click(function () {
            closemodal();
        });
    });

/*__MISC
==========================================================*/

// Convert HTML5 Placeholders in old IE versions
$(function() {
    // check placeholder browser support
    if (!Modernizr.input.placeholder) {
        // set placeholder values
        $(this).find('[placeholder]').each(function() {
            $(this).val( $(this).attr('placeholder') );
        });
        // focus and blur of placeholders
        $('[placeholder]').focus(function() {
            if ($(this).val() == $(this).attr('placeholder')) {
                $(this).val('');
                $(this).removeClass('placeholder');
            }
        }).blur(function() {
            if ($(this).val() == '' || $(this).val() == $(this).attr('placeholder')) {
                $(this).val($(this).attr('placeholder'));
                $(this).addClass('placeholder');
            }
        });
        // remove placeholders on submit
        $('[placeholder]').closest('form').submit(function() {
            $(this).find('[placeholder]').each(function() {
                if ($(this).val() == $(this).attr('placeholder')) {
                    $(this).val('');
                }
            })
        });
    }
});

// jQuery Ready
$(document).ready(function(){

    $('#navItems >li').hover(
        function() {
            $('#navItems >li>ul').stop(true, true).slideUp(300);
            $('ul',this).stop( true, true ).slideDown(300);
        },
        function() {
            $('ul',this).delay(300).slideUp(300);
        });

	$("#age").select_skin();

	$('#slideshow').cycle({
		speed:    1000,
    	timeout:  5000,
		pager:  '#slideshow-nav',
		fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});
	$('#testimonial-slide').cycle({
		speed:    1000,
    	timeout:  5000,
		cleartype:  false,
		next:   '#rightBtn',
        prev:   '#leftBtn',
		fx: 'scrollUp' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});
	$('#testimonial-slide').hover(
        function() { $('#testimonial-slide').cycle('pause'); },
        function() { $('#testimonial-slide').cycle('resume'); }
    );

});

$(document).ready(function(){
        $("a[rel^='prettyPhoto']").prettyPhoto();
    });
