var FeaturedPropertyFormClass = Class.create();

FeaturedPropertyFormClass.prototype = {

    initialize: function(options)
    {
    },

    getStartDate: function()
    {
        var dY = parseInt($('select[name="start_date[Y]"]').val());
        var dM = parseInt($('select[name="start_date[M]"]').val());
        var dD = parseInt($('select[name="start_date[d]"]').val());
        if (isNaN(dY) || isNaN(dM) || isNaN(dD)) {
            return false;
        }
        return sprintf('%04d-%02d-%02d', dY, dM, dD);
    },

    getEndDate: function()
    {
        var dY = parseInt($('select[name="end_date[Y]"]').val());
        var dM = parseInt($('select[name="end_date[M]"]').val());
        var dD = parseInt($('select[name="end_date[d]"]').val());
        if (isNaN(dY) || isNaN(dM) || isNaN(dD)) {
            return false;
        }
        return sprintf('%04d-%02d-%02d', dY, dM, dD);
    },

    getCurrentDate: function()
    {
        var d = new Date();
        var y = d.getYear();
        if (y < 1900) {
            y += 1900;
        }
        return sprintf('%04d-%02d-%02d', y, d.getMonth() + 1, d.getDate());
    },

    updateStatus: function()
    {
        var sd = this.getStartDate();
        var ed = this.getEndDate();
        var cd = this.getCurrentDate();

        var status = '<span class="inactive">Inactive</span>';
        if (sd && ed) {
            if (sd <= cd && ed >= cd) {
                status = '<span class="active">Showing on homepage</span>';
            } else if (sd > cd) {
                status = '<span class="inactive">Inactive - will be shown in future</span>'
            } else if (ed < cd) {
                status = '<span class="inactive">Inactive - was shown before</span>';
            }
        }
        $('#id_active').html(status);
    }
};

var featuredPropertyForm = new FeaturedPropertyFormClass();

// ---------------------------------------------------------------------------------------------------------------

var slideTimer = null;
var slideWidth = 0;
var slidesWidth = 0;
var slideCurrentOffset = 0;
var slideMaxOffset = 0;
var frameSize = null; //5;
var centerFrame = 3;
var slidesData = null;
var slidesMoving = false;
var shortTimeout = 7000;
var longTimeout = 20000;

function slideTimeoutStart(timeout)
{
    timeout = parseInt(timeout) || shortTimeout;
    slideTimer = setTimeout("prevSlides();", timeout);
    return true;
}

function slideTimeoutStop()
{
    if (slideTimer != null) {
        clearTimeout(slideTimer);
        slideTimer = null;
    }
    return true;
}

function slideShowActive()
{
    var slideId = $(".slide_list div:eq(2)").attr('id');
    slideShowItem(slideId);
}

function nextSlides(timeout, step)
{
    if (typeof step == 'undefined') {
        step = frameSize;
    }

    slideTimeoutStop();

    if (slideMaxOffset > step && !slidesMoving) {
        var duration = 1000;
        if (step < frameSize) {
            duration = duration / (frameSize - step);
        }

        slidesMoving = true;

        var appendFrame = $('div.slide_list div[id^=property_slide_]:lt(' + step + ')');
        $(appendFrame).each(function () {
            $('div.slide_list').append('<div id="' + $(this).attr('id') + '" class="' + $(this).attr('class') + '">' + $(this).html() + '</div>');
        });

        $('div.slide_list').animate({marginLeft: '-=' + (slideWidth * step) + 'px'}, duration, 'linear', function () {
            $(appendFrame).remove();
            $('div.slide_list').css('margin-left', 0);
            slideShowActive();
            slidesMoving = false;
        });

        sliderBindClick();
    }

    slideTimeoutStart(timeout);
}

function prevSlides(timeout, step)
{
    if (typeof step == 'undefined') {
        step = frameSize;
    }

    slideTimeoutStop();

    if (slideMaxOffset > step && !slidesMoving) {
        var duration = 1000;
        if (step < frameSize) {
            duration = duration / (frameSize - step);
        }

        slidesMoving = true;

        var lastFrame = slideMaxOffset - step - 1;
        var prependFrame = $('div.slide_list div[id^=property_slide_]:gt(' + lastFrame + ')');
        $('div.slide_list').css('margin-left', -(slideWidth * step));
        var elements = [];
        $(prependFrame).each(function () {
            //elements.push($(this).clone(true));
            elements.push('<div id="' + $(this).attr('id') + '" class="' + $(this).attr('class') + '">' + $(this).html() + '</div>');
        });
        for (var i = elements.length - 1; i >= 0; i--) {
            //$(elements[i]).prependTo('div.slide_list');
            $('div.slide_list').prepend(elements[i]);
        }

        $('div.slide_list').animate({marginLeft: '+=' + (slideWidth * step) + 'px'}, duration, 'linear', function () {
            $(prependFrame).remove();
            slideShowActive();
            slidesMoving = false;
        });

        sliderBindClick();

    }

    slideTimeoutStart(timeout);
}

function slideShowItem(id)
{
    var idSlide = id.replace(/property_slide_/, '');
    if (typeof slidesData[idSlide] != 'undefined') {
        var container = $('div.slide_details');
        if ($(container).size()) {
            var html = '<table width="100%" celpadding="0" cellspacing="0">'
                     + '<tr valign="top">'
                     + '    <td>'
                     + '        <img src="' + slidesData[idSlide].photo_full + '" width="145" height="109"  />';
            /*if (slidesData[idSlide].be_seen_url) {
                html += '<br /><center><a href="' + slidesData[idSlide].be_seen_url + '" target="_blank">Be Seen Here</a></center>';
            }*/
            html += '<br /><center><a href="resource/marketing/featuredproperty" target="_blank">Be Seen Here</a></center>';

            html += '    </td>'
                  + '    <td style="padding-left:10px">'
                  + '        <div class="feat_name">' + slidesData[idSlide].property_name + '</div>'
                  + '        <div class="feat_title">' + slidesData[idSlide].availability_title + '</div>'
                  + '        <div class="feat_addr">' + slidesData[idSlide].property_address + '<br />' + slidesData[idSlide].property_city + ', ' + slidesData[idSlide].property_state + ' ' + slidesData[idSlide].property_zip + '</div>'
                  + '        <div class="feat_type"><b>Type:</b> ' + slidesData[idSlide].property_type + '</div>'
                  + '        <div class="feat_brok"><b>Contact:</b> ' + slidesData[idSlide].contact_name + ', ' + slidesData[idSlide].company + '</div>'
                  + '        <a class="feat_prop_details" href="featured_property_click?id=' + parseInt(slidesData[idSlide].id) + '" target="_blank">VIEW DETAILS</a>'
                  + '    </td>'
                  + '</tr>'
                  + '</table>';
            $(container).html(html).show();
        }
    }
}

function sliderBindClick()
{
    $('div.slide_list div.slide_item').each(function() {
        $(this).unbind('click');
        $(this).click(function() {
            var id = $(this).attr('id');
            var i = null;
            $(".slide_list div").each(function (index) {
                if ($(this).attr('id') == id) {
                    i = index;
                    return false;
                }
            });

            var move = (centerFrame - 1) - i;

            if (move < 0) {
                nextSlides(longTimeout, Math.abs(move));
            } else {
                prevSlides(longTimeout, move);
            }
        });
    });
}

$(document).ready(function() {

    slideMaxOffset = parseInt($('div.slide_list div.slide_item').size());

    if (slideMaxOffset) {

        //$('div.slide_list').mouseover(function(){ slideTimeoutStop(); }).mouseout(function(){ slideTimeoutStart(); });

        slideWidth = parseInt($('div.slide_list div.slide_item').css('width')) || 0;
        slidesWidth = slideWidth * slideMaxOffset;

        if (count(slidesData) > frameSize) {
            $('div.properties_slider div.slide_navi div.slide_back img').click(function() {
                nextSlides(longTimeout, 1);
            });

            $('div.properties_slider div.slide_navi div.slide_next img').click(function() {
                prevSlides(longTimeout, 1);
            });
        } else {
            $('div.properties_slider div.slide_navi div.slide_back img').css('visibility', 'hidden');
            $('div.properties_slider div.slide_navi div.slide_next img').css('visibility', 'hidden');
        }

        sliderBindClick();

        slideShowActive();
        slideTimeoutStart();
    }
});

