/*****************************************************************************************
/  SET ALL VARIABLES
/*****************************************************************************************/

    var mlsid;
    var address;
    var beds;
    var baths;
    var bathhalf;
    var sqft;
    var sqftcost;
    var city;
    var state;
    var ptype;
    var price;
    var sqftcost;
    var propimg;
    var remarks;
    var loopCount = 1;
    var timerId = null;
    var featuredIndex = -1;
    var currentPage = getFileName();
    var strDebug = '';
    var currentProperty = '';
    var loadProperty = '';
    var firstProperty = '';
    var loadMLSID = '';
    var tempHTML = '';
    var StartItem = 1;
    var propCount = 25;
    var IsIE6 = false;
    var CarouselHTML = '<div class="gdtabs"><div id="CarouselContainer" class="contentPanelWhite" style="background-color:transparent"><div id="Carousel" class="jcarousel-skin-tango gdtabNavigation"><ul></ul></div></div></div>';
    var carouselControl = '<div class="carousel-controls"><div id="PropCount"></div><div id="CarouselNavigation"><a href="#" id="mycarousel-prev">&laquo;</a><span id="carouselPages" class="jcarousel-control"></span><a href="#" id="mycarousel-next">&raquo;</a><img src="images/loading2.gif" id="ajaxLoading"></div></div>';
    var listingArea = '<div id="gdtab" class="contentPanel2 fLeft">' +
                            '<div id="gdtabCnt"><img src="images/close.gif" id="closeTip">' +
                                '<div id="ImgContainer">' +
                                    '<a href="" id="varLink"><img border="0" src="" id="propimg" width="255" alt=""></a>' +
                                '</div>' +
                                '<div id="greatPropDealDisplay" class="fRight txt12w gray">' +
                                    '<div id="ToolTipImg"><img src="images/logo-tooltip.gif"></div>' +
                                    '<p><span id="varPrice"></span></p>' +
                                    '<p><span id="varSqFtPrice"></span></p>' +
                                    '<p><span id="varBeds"></span>&nbsp;<span id="varBaths"></span>&nbsp;<span id="varBathHalf"></span></p>' +
                                    '<p></p>' +
                                    '<p><span id="varSqFt"></span></p>' +
                                    '<p><span id="varRemarks"></span></p>' +
                                '</div>' +
                            '</div>' +
                        '</div>';
    CarouselLightboxBg = '<div id="CarouselLightboxBg"></div>';

/*****************************************************************************************
/  LOAD FUNCTIONS
/*****************************************************************************************/

    MM_preloadImages('images/loading2.gif', 'images/tooltipBg.gif', 'images/arrow_left.gif', 'images/arrow_right.gif', 'images/logo-tooltip.gif', 'images/bullet2.gif', 'images/previewsPanelBox-Bttm.gif', 'images/previewsPanelBox-Top.gif', 'images/previewPriceBG.gif');
    
    jQuery(document).ready(function() {

        $("body").append('<div id="JT"><div class="JT_arrow"></div><div class="JT_close"></div><div id="JT_copy">' + listingArea + '<div class="JT_loader"><div></div></div>');

        if ($.browser.msie && $.browser.version == '6.0') {
            IsIE6 = true;
            $('#JT').show().css('top', '-9000px');
        }

        $('#JT_copy').html(listingArea);

        if (currentPage == "/" || currentPage == "/index.asp" || currentPage == "/index-josh.asp") {
            $('#luxPropPreviewsPanel').html(CarouselHTML).prepend(carouselControl);
            CarouselStart();
        }

        $('.previewsPanelBox').live("click", function() {
            //document.location = $('#surl' + this.id).val();
            JT_show(this.id);
            return false;

        }).live("mouseover", function() {
            //JT_show(this.id);
            return false;

        }).live("mouseout", function() {

        });
        
        $('#closeTip').live('click',function() {

        if (IsIE6) {
            $('#JT').css('top', '-9000px');
        } else {
            $('#JT').hide();
        }

        return false;
        })


    });
    
/*****************************************************************************************
/  LOAD CAROUSEL XML
/*****************************************************************************************/

    function mycarousel_itemLoadCallback(carousel, state) {

        if (carousel.has(carousel.first, carousel.last)) {
            return;
        }

        if (!IsIE6) {
            showLoader();
        }
        
        jQuery.ajax({
            url: "inc-luxprop-previews.asp?Page=" + carousel.first,
            cache: false,
            datatype: "xml",
            success: function(xml) {
                mycarousel_itemAddCallback(carousel, carousel.first, carousel.last, xml, state);
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert('XMLHttpRequest: ' + XMLHttpRequest + '\ntextStatus: ' + textStatus + '\nerrorThrown: ' + errorThrown);
            }
        });
    };


/*****************************************************************************************
/  CHECK IF USER CAROUSEL STATE EXISTS
/*****************************************************************************************/

    function historyLoad(CurrentPage, mlsID) {
        $.get("inc-luxprop-previews.asp?action=historyLoad&CurrentPage=" + CurrentPage);
    }


/*****************************************************************************************
/  ADD CAROUSEL OBJECT TO PAGE
/*****************************************************************************************/
    
    function CarouselStart() {
        jQuery.ajax({
            url: "inc-luxprop-previews.asp?action=historyInit",
            dataType: "script",
            cache: false,
            success: function(response) {
                $('#greatdeals').html(CarouselHTML).append(listingArea);
                $('#Carousel').jcarousel({
                    initCallback: mycarousel_initCallback,
                    itemLoadCallback: mycarousel_itemLoadCallback,
                    itemVisibleOutCallback: { onAfterAnimation: function(carousel, item, i, state, evt) {  } },
                    start: StartItem,
                    scroll: 1,
                    buttonNextHTML: null,
                    buttonPrevHTML: null
                });
            }
        });
    }


/*****************************************************************************************
/  PROCESS SERVER'S CAROUSEL XML RETURN
/*****************************************************************************************/

    function mycarousel_itemAddCallback(carousel, first, last, xml, initstate) {
        
        var xmlCount = parseInt($('total', xml).text());
        
        var pageNumber = $('total', xml).attr('pagenumber');
        
        $('#PropCount').html(xmlCount + ' Properties available&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')

        $('#carouselPages').html('');
        var xmlPages = Math.round(xmlCount / 25);
        
        for (i = 1; i <= xmlPages; i++) {
            $('#carouselPages').html($('#carouselPages').html() + '&nbsp;<a id="Page' + parseInt(i) + '" href="#">' + parseInt(i) + '</a>')
        }
    
        $('#carouselPages').html($('#carouselPages').html() + '&nbsp;')
        $('#carouselPages a').css('color', 'white');
        $('#carouselPages a#Page' + pageNumber).css('color', '#ffa500');

        jQuery('listing', xml).each(function(i) {

            var objThis = $(this);

            propimg = objThis.attr('propimg');
            if (!IsIE6) {
                MM_preloadImages(propimg);
            }

            mlsid = objThis.attr('mlsid');
            address = objThis.attr('address');
            beds = objThis.attr('beds');
            baths = objThis.attr('baths');
            bathhalf = objThis.attr('bathhalf');
            sqft = objThis.attr('sqft');
            surl = objThis.attr('surl');
            city = objThis.attr('city');
            reqinfo = objThis.attr('reqinfo');
            state = objThis.attr('state');
            ptype = objThis.attr('ptype');
            price = objThis.attr('price');
            remarks = objThis.text();
            sqftcost = objThis.attr('sqftcost');
            subdiv = objThis.attr('subdiv');

            tempHTML = tempHTML + mycarousel_getItemHTML(mlsid, address, beds, baths, bathhalf, sqft, city, state, price, ptype, sqftcost, propimg, surl, reqinfo, subdiv, remarks);

        });

        carousel.add(first, tempHTML);
        tempHTML = '';
        pageCount = 0

        if (!IsIE6) {
            hideLoader();
        }
        
        historyLoad(last, firstProperty);
    };


/*****************************************************************************************
/  CAROUSEL CALLBACKS
/*****************************************************************************************/

    function mycarousel_initCallback(carousel) {
        $('.jcarousel-control a').live('click', function() {
            carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
            return false;
        });

        $('.jcarousel-scroll select').bind('change', function() {
            //carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
            return false;
        });

        $('#mycarousel-next').bind('click', function() {
            carousel.next();
            return false;
        });

        $('#mycarousel-prev').bind('click', function() {
            carousel.prev();
            return false;
        });

    };


/*****************************************************************************************
/  CHANGE PROPERTY INFO IN TOOLTIP
/*****************************************************************************************/

    function ChangeProp(propertyId) {
        
        $('#propimg').attr('src', $('#propimg' + propertyId).val());
        $('#varPrice').text($('#price' + propertyId).val());
        
        sqft        = $('#sqft' + propertyId).val();
        sqftcost    = $('#sqftcost' + propertyId).val();
        beds        = $('#beds' + propertyId).val()
        baths       = $('#baths' + propertyId).val()
        bathhalf    = $('#bathhalf' + propertyId).val();
        remarks     = $('#remarks' + propertyId).val();

        $('#varLink').attr('href', $('#surl' + propertyId).val());
        if (beds >= 1) {
            $('#varBeds').text(beds + ' beds ');
        } else {
            $('#varBeds').text(beds + ' bed');
        }

        if (baths >= 1) {
            $('#varBaths').text(baths + ' baths');
        } else {
            $('#varBaths').text(baths + ' bath');
        }

        if (bathhalf != '0') {
            if (bathhalf >= 1) {
                $('#varBathHalf').html('<br>' + bathhalf + ' half baths');
            } else {
            $('#varBathHalf').html('<br>' + bathhalf + ' half bath');
            }
        } else {
            $('#varBathHalf').text('');
        }

        if (sqftcost != '') {
            $('#varSqFtPrice').text('(' + sqftcost + '/s.f.)').parent().show();
        } else {
            $('#varSqFtPrice').text('(' + sqftcost + '/s.f.)').parent().hide();
        }

        if (remarks != '') {
            $('#varRemarks').text(remarks + '...');
        } else {
            $('#varRemarks').text('');
        }

    }


/*****************************************************************************************
/  GENERATE HTML FOR EACH PROPERTY DIV FOR CAROUSEL PAGE
/*****************************************************************************************/

    function mycarousel_getItemHTML(mlsid, address, beds, baths, bathhalf, sqft, city, state, price, ptype, sqftcost, propimg, surl, reqinfo, subdiv, remarks) {
        var sqftdiv = '<p class="nobackground">&nbsp;&nbsp;</p>';

        if (sqft != '0') {
            sqftdiv = '<p>&nbsp;&nbsp;' + sqft + ' s.f.</p>'
        }
        
        var strHTML = '<div id="' + mlsid + '"  class="previewsPanelBox fLeft jTip">' +
                      '    <div class="txt12w previewTitle center">' + subdiv + '</div>' +
                      '    <div class="previewPrice  txt12w center"><img width="113" height="69" src="' + propimg + '" alt="Property Id: ' + mlsid + '"><br>' + price + '</div>' +
                      '    <input type="hidden" id="address' + mlsid + '" value="' + address + '">' +
                      '    <input type="hidden" id="beds' + mlsid + '" value="' + beds + '">' +
                      '    <input type="hidden" id="baths' + mlsid + '" value="' + baths + '">' +
                      '    <input type="hidden" id="bathhalf' + mlsid + '" value="' + bathhalf + '">' +
                      '    <input type="hidden" id="sqft' + mlsid + '" value="' + sqft + '">' +
                      '    <input type="hidden" id="city' + mlsid + '" value="' + city + '">' +
                      '    <input type="hidden" id="state' + mlsid + '" value="' + state + '">' +
                      '    <input type="hidden" id="price' + mlsid + '" value="' + price + '">' +
                      '    <input type="hidden" id="reqinfo' + mlsid + '" value="' + reqinfo + '">' +
                      '    <input type="hidden" id="surl' + mlsid + '" value="' + surl + '">' +
                      '    <input type="hidden" id="ptype' + mlsid + '" value="' + ptype + '">' +
                      '    <input type="hidden" id="sqft' + mlsid + '" value="' + sqft + '">' +
                      '    <input type="hidden" id="sqftcost' + mlsid + '" value="' + sqftcost + '">' +
                      '    <input type="hidden" id="propimg' + mlsid + '" value="' + propimg + '">' +
                      '    <input type="hidden" id="subdiv' + mlsid + '" value="' + subdiv + '">' +
                      '    <input type="hidden" id="remarks' + mlsid + '" value="' + remarks + '">' +
                      '    <div class="m0 txt12w propInfo">' +
                      '        <p>&nbsp;&nbsp;' + sqftcost + '/s.f</p>' +
                      '        <p>&nbsp;&nbsp;' + beds + ' beds ' + baths + ' baths</p>' +
                      sqftdiv +
                      '    </div>' +
                      '    <div class="propBottom"></div>' +
                      '</div>'

        return strHTML;

    };


/*****************************************************************************************
/  CAROUSEL PAGINATION LOADER PLACEHOLDER 
/*****************************************************************************************/

    function showLoader() {
        $('#ajaxLoading').stop().fadeIn('normal', function() {
            $('#Carousel').fadeOut('fast');
        });
    }

    function hideLoader() {
        $('#ajaxLoading').stop().fadeOut('normal', function() {
            $('#Carousel').fadeIn('fast');
        });
    }


/*****************************************************************************************
/  SHOW PROPERTY TOOLTIP
/*****************************************************************************************/

    function JT_show(linkId) {
        var tooltipWidth = 493;
        var de = document.documentElement;
        var w = self.innerWidth || (de && de.clientWidth) || document.body.clientWidth;
        var hasArea = w - getAbsoluteLeft(linkId);
        var clickElementy = getAbsoluteTop(linkId) - 20; //set y position

        if (hasArea > ((tooltipWidth * 1) + 200)) {
            var arrowOffset = getElementWidth(linkId) + 11;
            var clickElementx = getAbsoluteLeft(linkId) + arrowOffset - 20;
            $('#JT').width(tooltipWidth * 1);
            $('#JT div.JT_arrow').attr('id', 'JT_arrow_left').css('left', '');
            $('#JT div.JT_close').attr('id', 'JT_close_left');
        } else {
            var clickElementx = getAbsoluteLeft(linkId) - ((tooltipWidth * 1) + 15 - 20);
            $('#JT').width(tooltipWidth * 1);
            $('#JT div.JT_arrow').attr('id', 'JT_arrow_right').css('left', ((tooltipWidth * 1) + 1));
            $('#JT div.JT_close').attr('id', 'JT_close_right');
        }
        //$('#JT_copy').html(listingArea);
        if (IsIE6) {
            $('#JT').css({ left: clickElementx + "px", top: clickElementy + "px" });
        } else {
            $('#JT').css({ left: clickElementx + "px", top: clickElementy + "px" }).show();
        }
        ChangeProp(linkId);
    }


/*****************************************************************************************
/  PROPERTY TOOLTIP FUNCTIONS
/*****************************************************************************************/

    function getElementWidth(objectId) {
        x = document.getElementById(objectId);
        return x.offsetWidth;
    }

    function getAbsoluteLeft(objectId) {
        // Get an object left position from the upper left viewport corner
        o = document.getElementById(objectId)
        oLeft = o.offsetLeft            // Get left position from the parent object
        while (o.offsetParent != null) {   // Parse the parent hierarchy up to the document element
            oParent = o.offsetParent    // Get parent object reference
            oLeft += oParent.offsetLeft // Add parent left position
            o = oParent
        }
        return oLeft
    }

    function getAbsoluteTop(objectId) {
        // Get an object top position from the upper left viewport corner
        o = document.getElementById(objectId)
        oTop = o.offsetTop            // Get top position from the parent object
        while (o.offsetParent != null) { // Parse the parent hierarchy up to the document element
            oParent = o.offsetParent  // Get parent object reference
            oTop += oParent.offsetTop // Add parent top position
            o = oParent
        }
        return oTop
    }

    function parseQuery(query) {
        var Params = new Object();
        if (!query) return Params; // return empty object
        var Pairs = query.split(/[;&]/);
        for (var i = 0; i < Pairs.length; i++) {
            var KeyVal = Pairs[i].split('=');
            if (!KeyVal || KeyVal.length != 2) continue;
            var key = unescape(KeyVal[0]);
            var val = unescape(KeyVal[1]);
            val = val.replace(/\+/g, ' ');
            Params[key] = val;
        }
        return Params;
    }

    function blockEvents(evt) {
        if (evt.target) {
            evt.preventDefault();
        } else {
            evt.returnValue = false;
        }
    }


