if (typeof(VillaStone) == 'undefined') {
    VillaStone = {};
}

VillaStone.Common = Class.create({
    searchBox: false,
    
    initialize: function() {
        this.searchBox = $('ctl00_txtZipSearch');
        if (!this.searchBox) this.searchBox = $('ctl00_ctl00_txtZipSearch');
        this.searchBox.observe('focus', function(e) { this.onSearchFocus(); }.bind(this));
        this.searchBox.observe('blur', function(e) { this.onSearchBlur(); }.bind(this));
        this.searchBox.observe('keypress', function(e) { this.onSearchKeyPress(e); }.bind(this));
        
        if (this.searchBox.value.strip() == '') {
            this.searchBox.value = 'Zip Code';
        } else if (!this.searchBox.hasClassName('active') && this.searchBox.value.strip() != 'Zip Code') {
            this.searchBox.addClassName('active');
        }
    },
    
    onSearchFocus: function() {
        if (this.searchBox.value.strip() == 'Zip Code' || this.searchBox.value.strip() == '') {
            this.searchBox.value = '';
            this.searchBox.addClassName('active');
        }
    },
    
    onSearchBlur: function() {
        if (this.searchBox.value.strip() == 'Zip Code' || this.searchBox.value.strip() == '') {
            this.searchBox.value = 'Zip Code';
            this.searchBox.removeClassName('active');
        }
    },
    
    onSearchKeyPress: function(e) {
        this.searchBox.addClassName('active');
        if (e.keyCode == Event.KEY_RETURN && this.searchBox.value.strip() != '') {
            document.location = "/DealerSearch.aspx?zipCode=" + this.escapeUrl(this.searchBox.value.strip()) + "&radius=20";
            e.stop(); // Prevent browser from submitting other forms on the page
        }
    },
    
	getUrlParam: function(name) {
        name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
        var regexS = "[\\?&]"+name+"=([^&#]*)";
        var regex = new RegExp( regexS );
        var results = regex.exec( window.location.href );
        if( results == null )
            return "";
        else
            return results[1];
	},
	
	getPageUrl: function() {
	    return document.location.pathname.toLowerCase();
	},
	
	scrollIfBelowFold: function(element, offset, options) {
	    if (!element) { return; }
	    
	    var delay = options.delay || 0;
	
	    var scrollOffsets = document.viewport.getScrollOffsets();
        var yFold = document.viewport.getHeight() + scrollOffsets.top;
        var yElem = element.cumulativeOffset()[1] + element.getHeight();
        
        if (offset) {
            yElem += offset;
        }
                 
        if (yElem > yFold) {
            new Effect.Tween(null, scrollOffsets.top, scrollOffsets.top+(yElem-yFold), 
                {
                    duration: 0.80, 
                    afterFinish: function() {
                        if (options && options.afterFinish) {
                            options.afterFinish();
                        }    
                    },
                    delay: delay
                },
                function(p) { scrollTo(scrollOffsets.left, p.round()) }
            );
        } else if (options && options.afterFinish) {
            options.afterFinish();
        }
	}
});

var homePage;
var hoodBuilder;
var mantelBuilder;
var dealerSearch;

VillaStone.InitializeApplication = function() {
    if (typeof(VillaStone.Common) != 'undefined')
        villaCommon = new VillaStone.Common();
        
    var url = villaCommon.getPageUrl();
        
    if (typeof(VillaStone.HomePage) != 'undefined' && (url == '/home.aspx' || url == '/' || url == ''))
        homePage = new VillaStone.HomePage();
    
    if (typeof(VillaStone.HoodBuilder) != 'undefined' && url.indexOf('/hoods/builder.aspx') >= 0)
        hoodBuilder = new VillaStone.HoodBuilder();
        
    if (typeof(VillaStone.MantelBuilder) != 'undefined' && url.indexOf('/mantels/builder.aspx') >= 0) {
        mantelBuilder = new VillaStone.MantelBuilder();
    }
        
    if (typeof(VillaStone.DealerSearch) != 'undefined' && url.indexOf('/dealersearch.aspx') >= 0)
        dealerSearch = new VillaStone.DealerSearch();
       
    var hoodRotator = $('hoodRotator');
    /*if (hoodRotator) {
        var ss = new Slideshow(hoodRotator, {
            duration:2, 
            uri : [
                '/images/feature_pic_1.jpg',
                '/images/feature_pic_2.jpg',
                '/images/feature_pic_3.jpg',
                '/images/feature_pic_4.jpg',
                '/images/feature_pic_5.jpg',
                '/images/feature_pic_6.jpg'
            ]
        });
    }*/
    
    var mantelRotator = $('mantelRotator');
    /*if (mantelRotator) {
        var mantelRotator = new Slideshow('mantelRotator', {
            duration:2, 
            uri : [
                '/images/mantel/feature_pic_1.jpg',
                '/images/mantel/feature_pic_2.jpg',
                '/images/mantel/feature_pic_3.jpg',
                '/images/mantel/feature_pic_4.jpg'
            ]
        });
    }*/
    
    var printInstructions = $('printInstructions');
    if (printInstructions) {
        var toggleLink = $('toggleInstructions');
        var instructions = $('expandedInstructions');
        
        toggleLink.observe('click', function(e) {
            var link = e.element();
            e.stop();
            
            if (link.hasClassName('open')) {
                link.removeClassName('open');
                link.innerHTML = 'Show instructions';
                Effect.BlindUp(instructions, { duration: 0.5 });
            } else {
                link.addClassName('open');
                link.innerHTML = 'Hide instructions';
                Effect.BlindDown(instructions, { duration: 0.5 });
            }
        });
    }
};

function pad(str, len, pad) {
    str = str + "";
	if (typeof(len) == "undefined") { var len = 0; }
	if (typeof(pad) == "undefined") { var pad = ' '; }
 
	if (len + 1 >= str.length) {
	    str = Array(len + 1 - str.length).join(pad) + str;
	}
 
	return str;
}

function preloadImages(images) {
    for(var i=0; i<images.length; i++) {
        var src = images[i];
        var img = new Image();
        img.src = src;
    }
};

document.observe("dom:loaded", function() {
    VillaStone.InitializeApplication();
});

