/**
* @class SureHits
* Base namespace for all SureHits javascript.
* @author David Sterling
* @Modified by Manish Deshmukh
*/
var SureHitsBannerList = function() {
    /**
    * Constructor method.
    * @author David Sterling
    * @constructor
    */
    this.initialize = function() {
    };

    this.currentTime = function() {
        var dateObj = new Date();
        var curTime = dateObj.getHours() + ":" + dateObj.getMinutes() + ":" + dateObj.getMilliseconds();
        return curTime;
    }

    //this.debug = false;
    this.initialize();
};

/******************************************************************************
* Define namespaces
*****************************************************************************/
SureHitsBannerList.MiniListing = {};


/******************************************************************************
* MiniListing Listings
*****************************************************************************/

/**
* Javascript object to wrap the Listings functionality
* @class SureHits.MiniListing.Listings
* @author David Sterling
* @Modified by Manish Deshmukh
*/
SureHitsBannerList.MiniListing.Listings = function(config) {
    //config consists of "src, ssc, zip, var1, gs, dsp, age, ma, hm, ci"

    this.initialize = function() {

        __this.__initialSrc = __this.__config.src;
        __this.__initialSSC = __this.__config.ssc;
        __this.__initialZip = __this.__config.zip;
        __this.__var1 = __this.__config.var1;
        __this.__generateState = __this.__config.gs;
        __this.__initialAge = __this.__config.age;
        __this.__initialMarried = __this.__config.ma;
        __this.__initialHomeowner = __this.__config.hm;
        __this.__initialInsured = __this.__config.ci;
        __this.__searchLoc = "http://www.nextinsure.com";
        __this.__refURL = window.location;
        __this.__userAgent = navigator.userAgent;
        __this.__displayType = __this.__config.dsp;
        __this.__aff = __this.__config.aff;
        __this.getInitialListings();
    },

    this.getInitialListings = function() {

        var __url = __this.__searchLoc + "/display_listings.asp?json=1&src=" + __this.__initialSrc + "&zc=" + __this.__initialZip + "&ssc=" + __this.__initialSSC + "&ref=" + escape(__this.__refURL) + '&rnd=' + this.currentTime()+'&callback=?';    

		 if(__this.__aff != undefined){
			 if(__this.__var1 != undefined){
				__url +="&ni_var1="+encodeURIComponent("var1=" +__this.__var1 +"&aff="+__this.__aff);			
			 }else{
				__url +="&ni_var1="+encodeURIComponent("aff="+__this.__aff) + "&var1=" + escape(__this.__aff);	 
			 }
		}else{
			__url +="&var1=" + escape(__this.__var1);
		}
         
        //check the user specific parameters
        if (__this.__initialAge != undefined)
        { __url += "&ni_seg_a=" + __this.__initialAge; }
        if (__this.__initialMarried != undefined)
        { __url += "&ni_seg_m=" + __this.__initialMarried; }
        if (__this.__initialHomeowner != undefined)
        { __url += "&ni_seg_h=" + __this.__initialHomeowner; }
        if (__this.__initialMarried != undefined)
        { __url += "&ni_seg_ci=" + __this.__initialInsured; }

        //generate waiting progress image
        var loaderImg = new Image();
        var loaderDiv = '';
        var imgWrapper = $('<div></div>').css({ 'text-align': 'center', 'margin': '25px 0' })
        if (__this.__displayType == 1)
        { loaderDiv = $('#listingsDiv') }
        if (__this.__displayType == 2)
        { loaderDiv = $('#listingsDivSM') }

        $(loaderImg).attr({ 'src': '/images/ajax-loader.gif', 'border': '0', 'alt': 'Loading Results...', 'title': 'Loading Results...' }).appendTo($(imgWrapper));
        $('<p>Loading Results...</p>').appendTo($(imgWrapper));
        $(imgWrapper).appendTo($(loaderDiv));
        $.getJSON(__url, function(data) { __this.showInitialListings(data) })
    },
	
	
	
    this.showInitialListings = function(data) {

        //we need to check and see if the feed is trying to escape single quotes with \', 
        //in JSON single quotes are valid, double quotes need to be escaped using \"
        //since the feed is already constructed i have to check for quirks here
        //var newData = data.replace(/\\'/g, "'");

        //turn returned text into objects
        var listingsObj = data;
        //get listings array object
        var listings = listingsObj.listings;
           if (listings.length > 0) {

                $('#listingsDivSM').empty();

                var i = 0;
                var __listingsContainer = $('<div></div>').css('width', '300px');
                var max = 3;

                if (__this.__generateState) {
                    $('#customLocSM').text(" in " + listings[0].state);
                }

                if (listings.length < max) {
                    max = listings.length
                }

                for (i = 0; i < max; i++) {

                    var __listingTable = $('<table></table>').attr({ 'class': 'listingTableSM', 'cellspacing': '0', 'cellpadding': '0', 'title': 'Click for a Quote' }).appendTo($(__listingsContainer));
                    if (i == 0) {
                        $(__listingTable).css('margin-top', '4px');
                    }

                    var __listingTBody = $('<tbody></tbody>').appendTo($(__listingTable));
                    var __tr = $('<tr></tr>').appendTo($(__listingTBody));

                    var __td1 = $('<td></td>').attr('class', 'listingLogoSM').appendTo($(__tr));
                    var __imageLink = $('<a></a>').appendTo($(__td1));

                    var __image = new Image();
                    $(__image).attr('src', listings[i].logo).appendTo($(__imageLink));
                    
                    //add ad title
                    var __td2 = $('<td></td>').attr('class', 'listingTitleSM').appendTo($(__tr));
                    var __textLink = $('<span></span>').html(listings[i].listingTitle).css('font-size', '13px').appendTo($(__td2));

                    var __tr2 = $('<tr></tr>').appendTo($(__listingTBody));
                    var __td3 = $('<td colspan="2"></td>').attr({ 'class': 'listingDescriptionSM', 'colspan': '2' }).appendTo($(__tr2));
                    var __divDesc = $('<div></div>').attr('class', 'listingDescriptionBodySM').html(listings[i].listingDescription).css('font-size', '12px').appendTo($(__td3));

                    var __quoteLinkDiv = $('<div></div>').attr('class', 'quote-linkSM').appendTo($(__td3));
                    var __quoteLink = $('<img></img>').attr({'src':'/blog/img/rtk_main_quote_button.jpg','align':'middle'}).appendTo($(__quoteLinkDiv));
                    
                    
                    //if (i < max - 1)
                   // { $('<hr />').appendTo($(__listingsContainer)) }
                }

                //add the ads to the display div in the HTML
                $(__listingsContainer).appendTo($('#listingsDivSM'));
               // $('#listingsDivSM').hide().slideDown(700);

                //generate the footer
                if ($('.listingsFooterSM') !== undefined) {
                    var __ftr = $('<div></div>').attr('id', 'listingsFooterSM').text('Listings Provided by SureHits').appendTo($('#listingsDivSM'));
                }

                //find listings and add effects to them
                var listingTables = $("table.listingTableSM");
                listingTables.each(function(i, el) {

                    $(el).mouseenter(function() {
						$(this).css('background-color', '#FFFFD4');
						$(el).children().children('tr:eq(1)').show();
                        $(el).children().children().children('.listingDescriptionSM').css('background-color', '#FFFFD4');
                        $(el).css('border', 'solid 1px #78A900');
                        $(el).click(function() {
                            window.open(listings[i].url, '');
                        });
                    });

                    $(el).mouseleave(function() {
						$(this).css('background-color', '#FFFFFF');							  
						$(el).children().children('tr:eq(1)').hide();
                        $(el).children().children().children('.listingDescriptionSM').css('background-color', '#FFFFFF');
                        $(el).css('border', 'solid 1px #FFFFFF');
                        $(el).unbind('click');
                    });
                });

            }
			
			$('#listingsDivSM').hide().slideDown(700);
			$(".listingTableSM tr:not(:first-child)").hide();		
			$("#listingsDivSM div:first").addClass('startdiv');		
        
    }

    this.__config = $.extend({/* set defaults here if needed */}, (config || {}));

var __this = this;
this.initialize();

};
SureHitsBannerList.MiniListing.Listings.prototype = new SureHitsBannerList();

