/** 
 * -----------------------------------------------------------
 *  return dimensions of a block
 *
 * -----------------------------------------------------------
 */
function getDimensions(element) {
    element = document.getElementById(element);
    if (element.clientWidth && element.clientHeight) {
        return {width: element.clientWidth, height: element.clientHeight};
    }
    var els = element.style;
    var originalVisibility = els.visibility;
    var originalPosition = els.position;
    var originalDisplay = els.display;
    els.visibility = 'hidden';
    els.position = 'absolute';
    els.display = 'block';
    var originalWidth = element.clientWidth;
    var originalHeight = element.clientHeight;
    els.display = originalDisplay;
    els.position = originalPosition;
    els.visibility = originalVisibility;
    return {width: originalWidth, height: originalHeight};
}
/** 
 * -----------------------------------------------------------
 * build an element block
 *
 * -----------------------------------------------------------
 */
function buildCalendarElement(data)
{
    if (data.type == undefined) return;

    var obj = document.createElement(data.type);
    if (data.name != undefined) {
        var newAttr = document.createAttribute("name")
        newAttr.value = "localSearch"
        obj.setAttributeNode(newAttr);
    }

    if (data.id != undefined) {
        obj.id = data.id;
    }
    if (data.ftype != undefined) {
        obj.type = data.ftype;
    }
    if (data.value != undefined) {
        obj.value = data.value;
    }
    if (data.text != undefined) {
        obj.innerHTML = data.text;
    }
    if (data.className != undefined) {
        obj.className = data.className;
    }
    if (data.size != undefined) {
        obj.size = data.size;
    }

    return obj;
}
/** 
 * -----------------------------------------------------------
 *  load a javascript json request
 *
 * -----------------------------------------------------------
 */
function loadCalendarJSON(url)    
{
    url += '&noCacheIE=' + (new Date()).getTime();

    var headID = document.getElementsByTagName("head")[0];         
    var newScript = document.createElement('script');
    newScript.type = 'text/javascript';
    newScript.src = url;
    headID.appendChild(newScript);
}
/** 
 * -----------------------------------------------------------
 *
 *
 * -----------------------------------------------------------
 */
function okWidget(init, containerId, name) {
    this.initialize(init, containerId, name);
};
/** 
 * -----------------------------------------------------------
 *
 *
 * -----------------------------------------------------------
 */
function _CSAT(init, containerId, name) { 
    this.initialize(init, containerId, name);
}
/** 
 * -----------------------------------------------------------
 *
 *
 * -----------------------------------------------------------
 */
_CSAT.prototype = {
    /** Variables declaration */
    name                : "_csat",
    containerId         : "_csatListing",
       
    calendarId          : "",           /** calendar group id in wimgo            */
    venueId             : "",           /** venue id in wimgo                     */
    zips                : "",           /** zip code to search for                */
    distance            : 0,            /** distance from zip code or venue       */
    partnerId           : "",           /** wimgo partner id                      */
    sdate               : "",
    smonth              : "",
    monthsAhead         : 0,
    daysAhead           : 0,

    /** Style Information */
    eventClass          : "event",
    maxTitleLength      : 250,           /** maximum length of title               */

    
    counter             : 1, /** internal use only */
    data                : null,

    /** Don't change anything below this line */

    calendarListURL : "http://wimgo.com/ajax/okla365/eventList.php?", 
    detailEventURL  : "http://wimgo.com/events/", 

    initialize: function (init, containerId, name)
    {
        if(typeof(containerId) != 'undefined')          { this.containerId      = containerId;              }
        if(typeof(name) != 'undefined')                 { this.name             = name;                     }

        if(typeof(init.monthsAhead) != 'undefined')     { this.monthsAhed       = init.monthsAhead;         }
        if(typeof(init.daysAhead) != 'undefined')       { this.daysAhead        = init.daysAhead;           }
        if(typeof(init.calendarId) != 'undefined')      { this.calendarId       = init.calendarId;          }
        if(typeof(init.venueId) != 'undefined')         { this.venueId          = init.venueId;             }
        if(typeof(init.zips) != 'undefined')            { this.zips             = init.zips;                }
        if(typeof(init.partnerId) != 'undefined')       { this.partnerId        = init.partnerId;           }
        if(typeof(init.distance) != 'undefined')        { this.distance         = init.distance;            }
        if(typeof(init.eventClass) != 'undefined')      { this.eventClass       = init.eventClass;          }
        if(typeof(init.maxTitleLength) != 'undefined')  { this.maxTitleLength   = init.maxTitleLength;      }
        if(typeof(init.calendarListURL) != 'undefined') { this.calendarListURL  = init.calendarListURL;     }
        if(typeof(init.detailEventURL) != 'undefined')  { this.detailEventURL   = init.detailEventURL;      }
    },
    loadMonth: function(search) 
    {
        var url =  this.calendarListURL;

        url += "sdate="         + escape(this.sdate);
        url += "&smonth="       + escape(this.smonth);
        url += "&calendarId="   + escape(this.calendarId);
        url += "&venue_id="     + escape(this.venueId);
        url += "&zips="         + escape(this.zips);
        url += "&distance="     + escape(this.distance);
        url += "&monthsAhead="  + escape(this.monthsAhead);
        url += "&daysAhead="    + escape(this.daysAhead);
        if (search != undefined) {
            url += "&search=" + escape(search);
        }
        if (this.partnerId.length) {
            url += "&partner_id=" + escape(this.partnerId);
        }
        url += "&callBack="+this.name+'.display';

        loadCalendarJSON(url); 

        return false;
    },
    eventDetails: function(blockId, eventListId) 
    {
        document.getElementById(blockId).style.display = (document.getElementById(blockId).style.display == 'block') ? 'none' : 'block';
    },
    buildHTML: function (eventListId, id, title, description, date, happens, venue, address, city, zip)
    {
        var blockId = '_eventDetails' + this.counter++;
        var dateArray = new Array();
        dateArray = date.split(" "); 
        var html = "";
        html += "<div class='date_holder'><div class='month'>" + dateArray[2] + "</div>";
        html += "<div class='date'>" + dateArray[1] + "</div></div>";
        html += "<div class='info_holder'>";
        html += "<div class='title'>";
        html += "<a href='"+this.detailEventURL+id+"' "
        html += " title='"+title+"'>";
        html += title.substr(0,this.maxTitleLength)
          if (title.length > this.maxTitleLength) {
                html += "...";
          }
        html += "</a>"
        html += "</div>"
          if (dateArray[4]) {
               time = " | "+dateArray[4]+" "+dateArray[5];
          } else {
               time = "";
          }
        html += "<div class='venue'>"+venue+time+"</div></div>";
        html += "</div><div class='clear'></div>";
        return html;
    },
    display: function(data) 
    {
        this.data = data;
        var container = document.getElementById(this.containerId);
        if (container == undefined) {
            alert("Event list container ("+this.containerId+") not found.");
            return false;
        }

        while (container.firstChild) 
        {
            container.removeChild(container.firstChild);
        }

        if (data.events.length == 0) {
            html = 'There are no events found.'
            container.appendChild(buildCalendarElement({"type": "div", "text" : html, "className":this.eventClass}));
        }
        for(var i=0; i < data.events.length; i++)
        {
            var html = this.buildHTML(
                                        i,
                                        data.events[i].id, 
                                        data.events[i].title,  
                                        data.events[i].description,    
                                        data.events[i].date,    
                                        data.events[i].happens,    
                                        data.events[i].venue,
                                        data.events[i].address,
                                        data.events[i].city,
                                        data.events[i].zip
                                );
            container.appendChild(buildCalendarElement({"type": "div", "text" : html, "className":this.eventClass}));
        }
        
    }
}
/** 
 * -----------------------------------------------------------
 *
 *
 * -----------------------------------------------------------
 */
okWidget.prototype = {
    containerId : "_okWidgetId",
    name        : "_okWidget",
    _CSAT       : "",
    title       : "Calendar",
    searchable  : false,
    calClass    : false,
    initialize: function(init, containerId, name) 
    {
        if(typeof(containerId) != 'undefined')      { this.containerId      = containerId;  }
        if(typeof(name) != 'undefined')             { this.name             = name;         }

        if(typeof(init.title) != 'undefined')       { this.title        = init.title;       }
        if(typeof(init.searchable) != 'undefined')  { this.searchable   = init.searchable;  }
        if(typeof(init.calClass) != 'undefined')    { this.calClass     = init.calClass;    }

        this._CSAT  = new _CSAT(init, this.containerId + "Listings", this.name+"._CSAT" );
        this.buildCalendar();
        this._CSAT.loadMonth();
    },
    buildCalendar: function()
    {
        var searchString = ""
        var container = document.getElementById(this.containerId);
        if (container == undefined) {
            alert("okWidget ID not found.");
            return false;
        }
        while (container.firstChild) 
        {
            container.removeChild(container.firstChild);
        }
        var calDiv = buildCalendarElement({"type" : "div", "className" : this.calClass});
        container.appendChild(calDiv);

        if (this.title.length) {
            calDiv.appendChild(buildCalendarElement({"type" : "div", "className" : "header", "text" : this.title }));
        }
        if (this.searchable) {
            var calSearch = buildCalendarElement({"type" : "div", "className":"search"});
            var myForm = buildCalendarElement({"type" : "form", "id":"Form"+this.name});
            myForm.onsubmit = function() {
                try 
                {
                    if (this.localSearch == undefined) {
                        searchString = this.elements[0].value;
                    } else {
                        searchString = this.localSearch.value
                    }
                    listings = this.id.replace(/Form/, "") + "._CSAT.loadMonth('"+searchString+"');"
                    eval(listings);
                } catch (e) { 
                }
            
                return false;
            }

            var myLocalSearch = buildCalendarElement({"type":"input", "ftype":"text", "name" : "localSearch", "size" : "15", "id":"localSearch"})
            myForm.appendChild(myLocalSearch);
            myForm.appendChild(buildCalendarElement({"type":"input", "ftype":"submit", "className":"search_button", "value":"GO" }));
            calSearch.appendChild(myForm);
            calDiv.appendChild(calSearch);
        }
        calDiv.appendChild(buildCalendarElement({"type" : "div", "id":this.containerId+"Listings", "className":"eventListing"}));
    }
}

/** 
 * -----------------------------------------------------------
 *
 *
 * -----------------------------------------------------------
 */
function buildEventCalendar(divId, init)
{
    var html = "";

    if(typeof(divId) == 'undefined') { 
        alert("Need div id of calendar container.");
        return;
    }

    var calDiv = document.getElementById(divId);
    var ad = "<a href='http://promos.newsok.com/adclick.php?n=a2f2a004' target='_blank'><img src='http://promos.newsok.com/adview.php?what=zone:1254&amp;n=a2f2a004' border='0' alt=''></a>";

    html  = "<div id='wimgoSatelliteEmbed'>";
    html += "    <div id='wimgoSatellite'>";
    html += "        <div id='header_graphics'>";
    html += "           <div class='topLeft'></div>";
    html += "           <div class='topRight'></div>";
    html += "        </div>";
    html += "        <div id='wimgoListings'>";
    html += "        </div>";
    html += "        <div class='clear'></div>";
    html += "        <div id='footer_graphics'>";
    html += "           <div class='bottomLeft'></div>";
    html += "           <div class='bottomRight'></div>";
    html += "        </div>";
    html += "        <div class='clear'></div>";
    html += "   </div>";
    html += "   <div class='wimgo_credits'>Powered by <a href='http://wimgo.com/'>wimgo.com</a></div>";
    html += "</div>";

    calDiv.innerHTML = html;

    _okWidget1               = new okWidget(init, "wimgoListings", "_okWidget1");
    
    _okWidget1._CSAT.eventDetails = function(blockId, eventListId)
    {
        if (document.getElementById(blockId).style.display == 'block') {
            document.getElementById(blockId).style.display = 'none';
        } else {
            document.getElementById(blockId).style.display = 'block';
        }
    }

    
}
