(function(window, undefined){
    com = window.com || {};
    com.hirtlecallaghan = window.com.hirtlecallaghan || {};
    com.hirtlecallaghan.Page = window.com.hirtlecallaghan.Page || {}
    
    /** @class */
    com.hirtlecallaghan.Page.Homepage = 
    /**@lends com.hirtlecallaghan.Page.Homepage*/
    new function(){
		var 
		//CONFIGURATION
			WINDOW_WIDTH_THRESHOLD = 1060,
			WINDOW_HEIGHT_THRESHOLD = 800, //This is inactive as of now.
			SPOTLIGHT_HIGHT_FIX_SMALL = 660,
			SPOTLIGHT_HIGHT_FIX_NORMAL = 926,
			SPOTLIGHT_TOP_OFFEST_MIN_SMALL = 350,
			SPOTLIGHT_TOP_OFFEST_MAX_SMALL = 389,
			SPOTLIGHT_TOP_OFFEST_MIN_NORMAL = 516,
			SPOTLIGHT_TOP_OFFEST_MAX_NORMAL = 561,
		//Shared Vars
			css_small_url = "/css/hc-screen-graphical-small.css",
			update_size = false,
			window_height,
			window_width,
			window_is_small = false,
			$all_images,
			$css_link,
			$css_link_small,
			$page,
			$spot_announce;
        
        /** This should be called after the dom is ready */
        this.init = function(){
            window_height = $(window).height();
            window_width = $(window).width();
            $all_images = $('img');
            $css_link = $('link[href="/css/hc-screen-graphical.css"]');
            $css_link_small = $('link[href="/css/hc-screen-graphical-small.css"]');
            $page = $('#page');
            $spot_announce = $('div.spotlight-body, div.home-announcement');
        
            //Set all image's original height and width as well as a smaller resized version.
            $all_images.each(function () {
                var $this = $(this);
                $this.data("original_height", $this.height());
                $this.data("original_width", $this.width());
                $this.data("small_height", Math.ceil($this.height() * 0.68));
                $this.data("small_width", Math.ceil($this.width() * 0.68));
            });
            
            //This is a preload hack.
            //Declared above is the small css, with the media type of print,
            //this is to allow the css to be preloaded.
            //Here i'm just setting it back to screen.
            $css_link_small.attr("media", "screen");
            
            //Call the handleWindowResize to handle the current window size
            handleWindowResize(true, window_width <= WINDOW_WIDTH_THRESHOLD);
        
            //Bind resize event callback to window.
            $(window).resize(handleWindowResize);
        
            //Search
            $btn_search = $('#btnSearch');
            $txt_search = $('#txtSearch');
        
            $txt_search.bind('keypress', function (e) {
                if (e.keyCode === 13) {
                    $btn_search.click();
                }
            });
        
            $btn_search.click(function () {
                var searchString = $txt_search.val();
                window.location = '/search.aspx?search=' + searchString;
            });
        };
        
        /** <p>This is a quick dirty image preloader for the large and small images. If any other images need preloaded stick them in the preloadImages array.</p> */
        this.preloadImages = function(){
            var preloadImages = ["/css/hirtlecallaghan/bkg_opt_home_jh.jpg", "/css/hirtlecallaghan/bkg_opt_home_jh_small.jpg"];
            var i = 0;
            for (; i < preloadImages.length; i++) {
                (new Image()).src = preloadImages[i];
            }
        };
        
        /**
        * <p>This swaps in a link tag containing the small css rules, also makes a call to resize all the images on the page.</p>
        */
        this.resizeContent = function() {
            window_height = $(window).height();
            window_width = $(window).width();
            if (window_is_small) {
                shrinkImages(true);
                $css_link.after($css_link_small);
                $css_link_small.attr({
                    href: css_small_url,
                    rel: "stylesheet",
                    type: "text/css"
                });
            } else {
                shrinkImages(false);
                $css_link_small.remove();
            }
        };
        var resizeContent = this.resizeContent;
        
        /**
        * This will fix the top posotions of the spotlight container
        * @param {bool} forceUpdate [Optional] Use this when you want to force a position update regardless if the page height is greater then the "fix height".
        */
        this.fixSpotlightPos = function() {
            var spotlight_pos,
				page_height = $page.height();
			
			
    
            if (window_is_small) {
                if (window_height <= SPOTLIGHT_HIGHT_FIX_SMALL || page_height < SPOTLIGHT_HIGHT_FIX_SMALL || true === arguments[0]) {
                    $page.height(Math.min(SPOTLIGHT_HIGHT_FIX_SMALL, window_height));
                    spotlight_pos = Math.min(Math.max(((window_height) * (20 / 34)), SPOTLIGHT_TOP_OFFEST_MIN_SMALL), SPOTLIGHT_TOP_OFFEST_MAX_SMALL);
                }
            } else {
                if (window_height <= SPOTLIGHT_HIGHT_FIX_NORMAL || page_height <= SPOTLIGHT_HIGHT_FIX_NORMAL || true === arguments[0]) {
                    $page.height(Math.min(SPOTLIGHT_HIGHT_FIX_NORMAL, window_height));
                    spotlight_pos = Math.min(Math.max(((window_height) * (30 / 50)), SPOTLIGHT_TOP_OFFEST_MIN_NORMAL), SPOTLIGHT_TOP_OFFEST_MAX_NORMAL);
                }
            }
    
            $spot_announce.css('top', spotlight_pos + 'px');
        }
        var fixSpotlightPos = this.fixSpotlightPos;
    
        /**
        * shrink all the images on the page to the appropriate dimension. [hack]
        * @param {bool} trueOrFalse Set weather it should shrink[true] or grow [false]
        */
        this.shrinkImages = function(trueOrFalse) {
            if (trueOrFalse) {
                $all_images.each(function () {
                    var $this = $(this);
                    $this.height($this.data("small_height"));
                    $this.width($this.data("small_width"));
                });
            } else {
                $all_images.each(function () {
                    var $this = $(this);
                    $this.height($this.data("original_height"));
                    $this.width($this.data("original_width"));
                });
            }
        }
        var shrinkImages = this.shrinkImages;
        
         /**
        * Event Handler for the window resize event
        * @param {Object} e           If this is being called as a bound event this will be the event object,
        *                             however if set equal to ture this will force the window size to update.
        * @param {bool} forceSmall  Force the smaller sized display
        */
        this.handleWindowResize = function(e) {
            var force_update_size = true === e;
            var force_small_window = arguments[1] ? true === arguments[1] : false;
    
            var prev_window_height = window_height;
            var prev_window_width = window_width;
            window_height = $(window).height();
            window_width = $(window).width();
    
            var update_size_shrink = (prev_window_width >= WINDOW_WIDTH_THRESHOLD && window_width <= WINDOW_WIDTH_THRESHOLD) || force_small_window;
            var update_size_grow = (prev_window_width <= WINDOW_WIDTH_THRESHOLD && window_width >= WINDOW_WIDTH_THRESHOLD);
            update_size = update_size_shrink || update_size_grow || force_update_size;
    
            if (update_size) {
                window_is_small = update_size_shrink;
                resizeContent();
                update_size = false;
                fixSpotlightPos(true);
                Cufon.refresh();
                return;
            }
    
            fixSpotlightPos();
        }
        var handleWindowResize = this.handleWindowResize;
        
    }
    
    //DOM Ready
    $(document).ready(function () {
        com.hirtlecallaghan.Page.Homepage.preloadImages();
        com.hirtlecallaghan.Page.Homepage.init()
    });
    
}(window))
