/*jslint white: true, browser: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, strict: false, newcap: true, immed: true */

var Meeks = window.Meeks || {};

(function ($) {
    
    (function () { 
        
        var self = this;
        
        var initializeModule = function (module) {
            $.each(module, function (i) {
                var submodule = this;
                if ($.isPlainObject(submodule)) {
                    initializeModule(submodule);
                }
            });
            
            if (module.hasOwnProperty('initialize')) {
                module.initialize();
            }
        };
        
        this.setup = function () {
            initializeModule(self);
        };
        
    }).call(Meeks);
    $(document).ready(function () {
        
        Meeks.setup();
        $('a[rel=external], a[href^=http], a[href*=meeks.com]').live('click.external', function (event) {
            var link = $(this);
            window.open(link.attr('href'), 'meeks');
            return false;
        });
    });
    
}(jQuery));
/*jslint white: true, browser: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, strict: false, newcap: true, immed: true */

(function ($) {
    
    $(document).ready(function (event) {
        
        $('form.ask-expert').live('submit', function (event) {
            var form = $(this),
                url = form.attr('action');
            
            $.ajax({
                url: url,
                cache: false,
                data: form.serialize(),
                dataType: 'html',
                type: 'POST',
                success: function (data, status, xhr) {
                    form.replaceWith($(data));
                },
                error: function (xhr, status, error) {
                    alert('Sorry, there was an error sending your message.');
                }
            });
            
            return false;
        });
        
        $('a.ask-expert, a[href="/contact-us/expert/"]').bind('click', function (event) {
            var link = $(this),
                url = link.attr('href');
            
            if ((!$.browser.msie && event.button == 0) || ($.browser.msie && event.button == 1)) {
                
                $.ajax({
                    url: url,
                    cache: false,
                    dataType: 'html',
                    success: function (data, status, xhr) {
                        var content = $(data);
                        content.dialog({
                            modal: true,
                            hide: 'fade',
                            show: 'fade',
                            resizable: false,
                            width: 520
                        });
                    },
                    error: function (xhr, status, error) {
                        alert('Sorry, there was an error loading the contact form.');
                    }
                });
                
                return false;
                
            }
        });
        
    });
    
})(jQuery);/*jslint white: true, browser: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, strict: false, newcap: true, immed: true */

(function ($) {
    
    $(document).ready(function (event) {
        
        $('form.co-op').live('submit', function (event) {
            var form = $(this),
                url = form.attr('action');
            
            $.ajax({
                url: url,
                cache: false,
                data: form.serialize(),
                dataType: 'html',
                type: 'POST',
                success: function (data, status, xhr) {
                    form.replaceWith($(data));
                },
                error: function (xhr, status, error) {
                    alert('Sorry, there was an error sending your message.');
                }
            });
            
            return false;
        });
        
        $('a.co-op, a[href="/contact-us/co-op/"]').bind('click', function (event) {
            var link = $(this),
                url = link.attr('href');
            
            if ((!$.browser.msie && event.button == 0) || ($.browser.msie && event.button == 1)) {
                
                $.ajax({
                    url: url,
                    cache: false,
                    dataType: 'html',
                    success: function (data, status, xhr) {
                        var content = $(data);
                        content.dialog({
                            modal: true,
                            hide: 'fade',
                            show: 'fade',
                            resizable: false,
                            width: 520
                        });
                    },
                    error: function (xhr, status, error) {
                        alert('Sorry, there was an error loading the contact form.');
                    }
                });
                
                return false;
                
            }
        });
        
    });
    
})(jQuery);/*
(function ($) {
    
    $(document).ready(function (event) {
        var MediaPlayer = {};
        
        (function () {
            var self = this;
            
            this.create = function (content) {
                var wrapper = $('<div>asdf</div>');
                wrapper.append($(content));
                wrapper.dialog({
                    modal: true,
                    resizable: false,
                    width: 520,
                    open: function (event, ui) {
                        $('.ui-dialog .media .items a').live('click.mediaItem', function (event) {
                            var url = $(this).attr('href');
                            
                            $.ajax({
                                url: url,
                                dataType: 'html',
                                cache: false,
                                success: function (data, status, xhr) {
                                    $('.ui-dialog .media').replaceWith($(data));
                                }
                            });
                            
                            return false;
                        });
                    },
                    close: function (event, ui) {
                        $('.ui-dialog .media .items a').die('click.mediaItem');
                    }
                });
            }
            
        }).call(MediaPlayer);
        
        $('.media-page #primary-content a').live('click', function (event) {
            var url = $(this).attr('href');
                
            $.ajax({
                url: url,
                dataType: 'html',
                cache: false,
                success: function (data, status, xhr) {
                    MediaPlayer.create(data);
                }
            });
            
            return false;
        });
        
    });
    
})(jQuery);
*/
/*jslint white: true, browser: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, strict: false, newcap: true, immed: true */

var Meeks = window.Meeks || {};
Meeks.Products = Meeks.Products || {};
Meeks.Products.Specials = Meeks.Products.Specials || {};

(function () {
    
    this.initialize = function () {
        
        $('a.special').bind('click', function (event) {
            var link = $(this),
                url = link.attr('href');
            
            $.ajax({
                cache: false,
                dataType: 'html',
                success: function (data, status, xhr) {
                    var content = $(data),
                        actionList = $('<ul class="actions">' + 
                            '<li class="print"><a>Print</a></li>' +
                            '<li class="all"><a>See All Specials</a>'  +
                            '</li></ul>');
                    
                    actionList.find('li.print a').attr('href', url + 'print/');
                    actionList.find('li.all a').attr('href', Meeks.Settings.PRODUCT_SPECIALS_LIST_URL);
                    content.append(actionList);
                    
                    content.dialog({
                        modal: true,
                        dialogClass: 'special-dialog',
                        hide: 'fade',
                        show: 'fade',
                        resizable: false,
						draggable: false,
                        width: 520
                    });
                },
                error: function (xhr, status, error) {
                    // TODO: Add error modal.
                },
                url: url
            });
            
            // TODO: Make this bookmarkable using BBQ for hash states.
            
            return false;
        });
        
    };
    
}).call(Meeks.Products);

(function () {
    var self = this;
    
    this.equalizeGridHeights = function (grid) {
        var grid = $(grid),
            items = grid.children(),
            heights = $.makeArray(items.map(function () {
                        return $(this).innerHeight();
                    })),
            maxHeight = Math.max.apply(Math, heights);
        
        items.height(maxHeight);
    };
    
    this.initialize = function () {
        var grid = $('body.products.specials.list ul.specials');
        
        if (grid.length > 0) {
            self.equalizeGridHeights(grid);
        }
        
        var resultCountForm = $('.results-per-page'),
            resultSelect = resultCountForm.find('select'),
            resultButton = resultCountForm.find('.button');
        
        resultSelect.bind('change', function (event) {
            resultCountForm.submit();
        })
        
        resultButton.hide();
    };
    
}).call(Meeks.Products.Specials);/*jslint white: true, browser: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, strict: false, newcap: true, immed: true */

var Meeks = window.Meeks || {};
Meeks.Search = Meeks.Search || {};

(function () {
    
    this.initialize = function () {
        var headerSearch = $('#headerSearch fieldset:first');
        headerSearch.find('input').defaultValue(headerSearch.find('label').text());
    };
    
}).call(Meeks.Search);/*jslint white: true, browser: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, strict: false, newcap: true, immed: true */

var Meeks = window.Meeks || {};
Meeks.Stores = Meeks.Stores || {};

(function () {
    
    var self = this;
    
    this.checkStorePreference = function () {
        if ($('body').is(':not(.stores.search)')) {
            if ($('#selectedLocation').is(':has(.none)')) {
                var currentURL = window.location.pathname + window.location.search + window.location.hash;
                //window.location = Meeks.Settings.STORE_SEARCH_URL + '?next=' + escape(currentURL);
                window.location = 'http://midwest.meeks.com/stores/default/?store_id=39&next=' + escape(currentURL);
            }
        }
    };
    
    this.initialize = self.checkStorePreference;
    
}).call(Meeks.Stores);
/*jslint white: true, browser: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, strict: false, newcap: true, immed: true */

var Meeks = window.Meeks || {};
Meeks.Stores = Meeks.Stores || {};
Meeks.Stores.Search = Meeks.Stores.Search = {};

(function () {
    
    var self = this;
    
    var getBounds = function (data) {
        var bounds = new google.maps.LatLngBounds();
        $.each(data, function (i) {
            var location = this;
            bounds.extend(location['geopoint']);
        });
        return bounds;
    }
    
    var createMap = function (data) {
        var bounds = getBounds(data),
            canvas = $('<div id="map-canvas"></div>'),
            map = new google.maps.Map(canvas[0], {
                zoom: 8,
                center: bounds.getCenter(),
                mapTypeId: google.maps.MapTypeId.ROADMAP
            });
        
        map.fitBounds(bounds);
        
        $.each(data, function (i) {
            var location = this,
                marker = new google.maps.Marker({
                    position: location['geopoint'],
                    map: map
                });
        });
            
        return canvas;
    };
    
    this.getLocationData = function () {
        var locationList = self.getLocationList(),
            data = [];
        
        locationList.find('li').each(function (i) {
            var location = $(this),
                latitude = location.find('.latitude').text(),
                longitude = location.find('.longitude').text();
            
            data[data.length] = {
                'location': location,
                'geopoint': new google.maps.LatLng(latitude, longitude)
            };
        });
        
        return data;
    }
    
    this.getLocationList = function () {
        return $('form .stores');
    }
    
    this.initialize = function () {
        var data = self.getLocationData();
            canvas = createMap(data);
        $('form .stores').after(canvas);
    };
    
}).call(Meeks.Stores.Search);/*jslint white: true, browser: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, strict: false, newcap: true, immed: true */

var Meeks = window.Meeks || {};
Meeks.Featured = Meeks.Featured || {};

(function () {
    
    var self = this;

    this.initialize = function () {
		$('body.home div#feature').scrollable({ 
		    size: 1,
			vertical: false,
			next: '#featureNext',
			prev: '#featurePrevious',
			circular: true,
			keyboard: false
		}).navigator({
			navi: 'ul.tabs'
		}).autoscroll({ 
		    autoplay: true,
			interval: 10000
		});
		
		$('body.home #featureNext, body.home #featurePrevious').click(function(e) {
			e.preventDefault();
		});
	};
    
}).call(Meeks.Featured);/*jslint white: true, browser: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, strict: false, newcap: true, immed: true */

var Meeks = window.Meeks || {};
Meeks.StoreSpecials = Meeks.StoreSpecials || {};

(function () {
    
    var self = this;

    this.initialize = function () {
		$('body.home div#in-store-specials').scrollable({ 
		    size: 3,
			vertical: false,
			next: '#storeSpecialsNext',
			prev: '#storeSpecialsPrevious',
			circular: true,
			clickable: true
		});
		$('body.home #storeSpecialsNext, body.home #storeSpecialsPrevious').click(function(e) {
			e.preventDefault();
		});
	};
    
}).call(Meeks.StoreSpecials);

