﻿/*This is the main IgluSearch control*/
var IgluSearch =
{
    /*Version Number*/
    Version: '0.1',

    /*This is the search control that gets set by the next adapter, i.e. Cruise*/
    Control: null,

    /*This is the container where we will render out the HTML for the Search Control*/
    Container: null,

    /*The Referrer*/
    Referrer: null,

    /*This is the data, that gets set by the Server*/
    Data: null,

    /*Return an Element */
    get: function (el) {
        return document.getElementById(el);
    },

    /**
    * Appends an HTML fragment to the given element.
    */
    append: function (el, html) {
        if (el.insertAdjacentHTML) {
            el.insertAdjacentHTML('BeforeEnd', html);
        }
        else if (el.lastChild) {
            var range = el.ownerDocument.createRange();
            range.setStartAfter(el.lastChild);
            var frag = range.createContextualFragment(html);
            el.appendChild(frag);
        }
        else {
            el.innerHTML = html;
        }
    },

    clearSelect: function (el) {
        while (el.hasChildNodes())
            el.removeChild(el.firstChild);
    },

    getDaysInMonth: function (m) {
        switch (m) {
            case '01':
                return 31;
            case '02':
                return 29;
            case '03':
                return 31;
            case '04':
                return 30;
            case '05':
                return 31;
            case '06':
                return 30;
            case '07':
                return 31;
            case '08':
                return 31;
            case '09':
                return 30;
            case '10':
                return 31;
            case '11':
                return 30;
            case '12':
                return 31;
        }
    },

    /*A Callback method that can be set by an included Adapter, if they need to do anything before we render the control to the page*/
    PreRender: null,

    /*This is the function that MUST be called, it sets the container*/
    Setup: function (targetContainer, site, referrer, styled) {

        //Check to see if there is a control rendered..
        if (this.Control == null)
            throw 'Cannot Create Search Control, no adapter specified, please include an adapter.';

        if (site == null)
            throw 'Cannot Create Search Control, no site specified.';

        if (referrer == null)
            throw 'Cannot Create Search Control, Referrer Not Provided.';
        else
            this.Referrer = referrer;

        //Check to see if the target container exists
        if (this.get(targetContainer) == null)
            throw 'Cannot find Target Container (' + targetContainer + ') in document';
        else
            this.Container = this.get(targetContainer);

        //We have a site, and a control, lets write out the JavaScript and CSS Includes, if this is to be styled.
        var includes = ''
        if (styled != false) {
            includes += '<link href="' + IgluSearch.Control.IgluComponentsSite + '/' + site + '/' + site + '.css" rel="stylesheet" type="text/css" />';
        }

        //Put the Includes into the Page, forcing the browser to load them
        document.writeln(includes);

        //Put the HTML into the Container, hidden for now, so we can hide/show what we need to.
        this.append(this.Container, this.Control.html);

        /*Does the control have a Pre-Render event registered? If so, call it. If not, go straight to Render*/
        if (this.PreRender != null) 
            this.PreRender();
        
    },

    /*This is what gets called when we want to Search*/
    Search: function () {
        var urlToGet = this.Control.Search() + '&ReferrerSiteId=' + this.Referrer.toString();
        window.location = urlToGet.toLowerCase();
    }
};
