var AppChassis = new Class({
    version:'1.0.1',
    author:'Jason Throm',

    initialize: function(ctor){
        if(!ctor) return;
        this.ctor = ctor;    
        this.errors;
        this.dataService = ctor.dataService;
        this.dataAccess = this.loadDataAccess();
        this.validators = new Validators();
    },
    
    location: {
        hash: window.location.hash,
        host: window.location.host,
        hostname: window.location.hostname,
        href: window.location.href,
        pathname: window.location.pathname,
        port: window.location.port,
        protocol: window.location.port,
        search: window.location.search
    },
    
    
    linkClick: function(el){
        //add tracking here
        //this.loadPage(el.pathname);
        
        //disable hrefs
        return true; 
    },
    
    submitContact: function(){
    	var cName = $('cName').value;
		var cEmail = $('cEmail').value;
		var cMsg = $('cMsg').value;
        if(this.contactInfoIsValid(cName, cEmail, cMsg)){
            this.dataAccess.submitContact(cName, cEmail, cMsg);
            $('cName').value="";
            $('cEmail').value="";
            $('cMsg').value="";
        }else{
        
        }
	},
	
	contactInfoIsValid: function(cName, cEmail, cMsg){
	    var isValid=true;
	    var errorMsg="";
	    if(this.validators.isBlank(cName)){
	        isValid&=false;
	        errorMsg+="-Name <br /> \r\n";
	    }
	    if(!this.validators.isEmail(cEmail)){
	        isValid&=false;
	        errorMsg+="-Email <br /> \r\n";
	    }
	    if(this.validators.isBlank(cMsg)){
	        isValid&=false;
	        errorMsg+="-Comment <br /> \r\n";
	    }
	    if(!isValid){
	        $('contactResultMsg').innerHTML="The following fields are missing or invalid: <br /> \r\n" + errorMsg;
	    }
	    return isValid;
	},
    
    loadPage: function(path){
        this.hideAllPages();
        var loadedPage = null;
        
        //ghetto to handle IE bullshit...
        if(window.Browser.Engine.name.indexOf('trident')!= -1 && path!='/'){
            path = '/' + path;
        }
        
        this.pages.each(function(page){
        
            if( path == page.ctor.path ){
                //check to see if page is already loaded
                page.load();
            
                if(!page.isLoaded){
                    //alert(path +" - "+page.ctor.path);
                }
                loadedPage = page;
            }
        }, this);

        if(loadedPage){
            loadedPage.setPageTitle();
            loadedPage.configure();
            loadedPage.show();
        }else{
            
        }
    },
    
    hideAllPages: function(){
        this.pages.each(function(page){
            if($(page.ctor.id)){
                $(page.ctor.id).style.display = "none";
            }
        }, this);
    },
    
    loadDataAccess: function(){
        return new DataAccess({}, this)
    },

    redirect: function(loc)
    {
        window.location=loc;
    },
            
    loadPages: function(){
        return [
            new SitePage({
                title: 'Home',
                path: '/',
                source: '/_system/HTML/Home.html',
                id: 'home',
                config: function(){}
            }, this),
            new SitePage({
                title: 'New Construction',
                path: '/newconstruction',
                source: '/_system/HTML/NewConstruction.html',
                id: 'newConstruction',
                config: function(){}
            }, this),
            new SitePage({
                title: 'Insulated Concrete Form Construction',
                path: '/newconstruction/ICF',
                source: '/_system/HTML/NewConstructionICF.html',
                id: 'newConstructionICF',
                config: function(){}
            }, this),
            new SitePage({
                title: 'Remodeling',
                path: '/remodeling',
                source: '/_system/HTML/Remodeling.html',
                id: 'remodeling',
                config: function(){}
            }, this),
            new SitePage({
                title: 'Kitchens and Baths',
                path: '/remodeling/kitchensbaths',
                source: '/_system/HTML/RemodelKandB.html',
                id: 'remodelKandB',
                config: function(){}
            }, this),
            new SitePage({
                title: 'Kitchens',
                path: '/remodeling/kitchens',
                source: '/_system/HTML/RemodelKitchens.html',
                id: 'remodelKitchens',
                config: function(){}
            }, this),
            new SitePage({
                title: 'Bathrooms',
                path: '/remodeling/baths',
                source: '/_system/HTML/RemodelBaths.html',
                id: 'remodelBaths',
                config: function(){}
            }, this),
            new SitePage({
                title: 'Roofing',
                path: '/remodeling/roofing',
                source: '/_system/HTML/RemodelRoofing.html',
                id: 'remodelRoofing',
                config: function(){}
            }, this),
            new SitePage({
                title: 'Room Additions',
                path: '/remodeling/roomadditions',
                source: '/_system/HTML/RemodelAdditions.html',
                id: 'remodelAdditions',
                config: function(){}
            }, this),                
            new SitePage({
                title: 'Company',
                path: '/company',
                source: '/_system/HTML/Company.html',
                id: 'company',
                config: function(){}
            }, this),
            new SitePage({
                title: 'Testimonials',
                path: '/testimonials',
                source: '/_system/HTML/Testimonials.html',
                id: 'testimonials',
                config: function(){}
            }, this),
            new SitePage({
                title: 'Affiliations',
                path: '/affiliations',
                source: '/_system/HTML/Affiliations.html',
                id: 'affiliations',
                config: function(){}
            }, this),
            new SitePage({
                title: 'Trends',
                path: '/trends',
                source: '/_system/HTML/Trends.html',
                id: 'trends',
                config: function(){}
            }, this),
            new SitePage({
                title: 'Contact Us',
                path: '/contact',
                source: '/_system/HTML/Contact.html',
                id: 'contact',
                config: function(){}
            }, this),
            new SitePage({
                title: 'Terms',
                path: '/terms',
                source: '/_system/HTML/Terms.html',
                id: 'terms',
                config: function(){}
            }, this)
        ]
    }
    
    
});

