/*global window document alert google aObj JSONscriptRequest $ */
function countChildElements(parent) {
    return $(parent).children().size();
}
	
function homesHQ_callback(jsonData) {
    if ( jsonData.hotHome.length > 0 )
    {
        if ( typeof(newspaperDomain) != 'undefined' )
        {
            CreatorDomain = newspaperDomain;
        }        

        var objCount = jsonData.hotHome.length;

        var active_image_path;
        switch(CreatorDomain)
        {
             case "aberdeennews.com":
                 active_image_path = "active_images/aberdeen";
                 break;
             case "thedickinsonpress.com":
                 active_image_path = "active_images/dickinson";
                 break;
             case "duluthnewstribune.com":
                 active_image_path = "active_images/duluth";
                 break;
	     case "findabluegrasshome.com":
                 active_image_path = "active_images/lexington";
                 break;
             case "inforum.com":
                 active_image_path = "active_images";
                 break;
             default:
                 active_image_path = "active_images";
         }    

        // insert stylesheet into the head
        $('head').append('<link rel="stylesheet" href="http://widgets.fccinteractive.com/homeshq/homesHQ.css" type="text/css" media="screen" />');

        var hotHomeHeader = $('<div></div>')
                .attr("id", "homesHQheader");
			
            $(hotHomeHeader).html('<h3><a href="http://www.homeshq.com/" title="Visit homesHQ.com">homes<em><strong>HQ</strong></em><img src="http://widgets.fccinteractive.com/homeshq/homeshqLogo.png" alt="homesHQ" class="noBorder" /></a></h3>');
            // append header to widget holder
            $('#homes_widget').append(hotHomeHeader);
				
           var wrapper = $('<div></div>')
                   .attr("id", "homesHQwrapper");


            /* ---------------------------------------------------
             * we need to grab a random hothome from the list of
             * hothomes created by homesHQs.php
               -------------------------------------------------*/
            var objRandom = Math.floor(Math.random() * objCount);
            
            var list_number = jsonData.hotHome[objRandom].list_number;
            var price = jsonData.hotHome[objRandom].price;
            var city = jsonData.hotHome[objRandom].city;
            var state = jsonData.hotHome[objRandom].state;
            var agent_id = jsonData.hotHome[objRandom].agent_id;
            var agent_name = jsonData.hotHome[objRandom].agent_name;
            var agency_name = jsonData.hotHome[objRandom].agency_name;
            var phone = jsonData.hotHome[objRandom].phone;
            var photo = jsonData.hotHome[objRandom].photo;
            
            var content = $('<div></div>')
                    .attr("id", "homesHQcontent");
                
                var content_p = $('<p></p>');
                    $(content_p).append('<a href="http://www.homeshq.com/detail/' + list_number + '/" title="Click for more information about MLS# ' + list_number + '"><img src="http://www.homeshq.com/' + active_image_path + '/hothomes/' + list_number.substring(0,4) + '/' + list_number + '_1.jpg" alt="Click for more information about MLS# ' + list_number + '" class="noBorder" width="96%" /></a><br />');
                    $(content_p).append('<strong><a href="http://www.homeshq.com/detail/' + list_number + '/" title="Click for more information about MLS# ' + list_number + '">' + price + '</a></strong><br />');
                    $(content_p).append(city + ', ' + state);
                    if (phone != "")
                        $(content_p).append('<br />' + phone);
                $(content).append(content_p);
            $(wrapper).append(content);
        $('#homes_widget').append(wrapper);

            var agent = $('<div></div>')
                    .attr("id", "homesHQagent");
                
                var agent_p = $('<p></p>');
                    if ( photo != '' )
                    {
                        $(agent_p).append('<img src="http://www.homeshq.com/images/agents/' + photo + '" alt="' + agent_name + '" title="' + agent_name + '" width="100%" /><br />');
                    }
                    $(agent_p).append('<span>' + agent_name + '</span><br />');
                $(agent).append(agent_p);

               var agency_p = $('<p></p>').text(agency_name);
               $(agent).append(agency_p);
 
        $('#homes_widget').append(agent);

            var footer = $('<div></div>')
                    .attr("id", "homesHQfooter");

                $(footer).html('<p><a href="http://www.homeshq.com/detail/' + list_number + '/" title="Click for more information about MLS# ' + list_number + '">' + String.fromCharCode(171) + ' More Info</a> | <a href="http://www.homeshq.com/viewAll/hothomes/" title="View All HotHomes">View All ' + String.fromCharCode(187) + '</a></p>');

        $('#homes_widget').append(footer).addClass('widget_box');
    }
}
