/*
    JS DIRECTORY
        __FORMS
        __SLIDESHOW
        __PRETTYPHOTO
        __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( !validate_field(theform.name, REGEXP_NAME, 'Not a valid name!') ) return false;
        if( !validate_field(theform.phone, REGEXP_PHONE, 'Not a valid phone number!') ) return false;
        if( !validate_field(theform.email, REGEXP_EMAIL, 'Not a valid email address!') ) return false;
        /* the following first check if field exists then make sure they are not blank */
        if( theform.email_confirm && (theform.email_confirm.value != theform.email.value) ){ alert('Your emails do not match!'); 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 if Ajax hidden field present
        if( !theform.noajax ){
            $.ajax({
                url: "/form-handler.php",
                type: "POST",
                data: 'json=true&' + $(theform).serialize(),
                dataType: 'json',
                cache: false,
                error: function(a,b,c){ alert(' An Error Occured??  Try again?  Maybe the server is down?  That should be rare, like 1 in a million, so maybe you should go play the lottery, then come back.'); },
                success: function(json){

                    if(json.error){

                        showmodal(json.error);

                    }else{

                        $(theform).html(json.success);

                    }

                }
            });

            return false;
        }


	}




/*__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.slide', container);
        
     // Add Mask
        container.prepend('<div class="slideshowmask"></div>');
        slideshow.mask = $('div.slideshowmask', container);

     // 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
        slides.show();
        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;
        slideshow.container.append( $(slideshow.slides[newSlide-1]) );
        slideshow.mask
            .css({opacity: 1}).show()
            .animate({opacity: 0}, slideshow.transitionTime, function(){ slideshow.inTransition = false; $(this).hide(); })
            .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);

        }

    }





/*__PRETTYPHOTO
==========================================================*/
    $(document).ready(function(){
        $("a[rel^='prettyPhoto']").prettyPhoto();
    });

    function prettyLaunch(linkurl, title) {
    document.getElementById("prettyLink").href = linkurl;
    document.getElementById("prettyLink").title = title;
    $("#prettyLink").trigger('click');
    }

    $("#prettyLink").trigger('click');

	function prettyLaunchTrain(linkurl, title) {
    document.getElementById("prettyLinkTrain").href = linkurl;
    document.getElementById("prettyLinkTrain").title = title;
    $("#prettyLinkTrain").trigger('click');
    }

    $("#prettyLinkTrain").trigger('click');



/*__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
==========================================================*/

    function sendtomaps(form){

        window.open( 'http://maps.google.com/maps?q='+encodeURIComponent('"'+form.query.value+'" '+form.zip.value) );
        return false;

    }

    $(function(){
    
    	$('#retailerlist .com-content').hide();
    	$('#retailerlist .com-links a').click(function(event){
    		event.preventDefault();
    		$('.com-content').hide('500');
    		$( $(event.target).attr('href') ).show('500');
    	});
    	
    });

	function goToByScroll(id){
	$('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');
	}

	$(function(){
	   $('.go_top').click(function(event) {
	   event.preventDefault();
	   goToByScroll('page_top');
      });
	 });


     $(function(){
	 $(".toggle_container").hide(); 

		$("p.trigger").click(function(){
			$(this).toggleClass("active").next().slideToggle("slow");
			return false;
		});
	 });

