﻿var replaceme;
var mouseIn = false;
var nextSlideTo;
function menuIn(t) {
    $('.slider ul li a').removeClass('selected');
    var anc = $(t).children('a');
    var hoverImgCta = $(anc).addClass('selected').attr('rel');
    $('.' + hoverImgCta).fadeIn();
    $('.ctaimage').not('.' + hoverImgCta).fadeOut();
}

function timeSlider() {
    if (!mouseIn) {
        nextSlideTo = $('.slider ul li a.selected').parent().next();
        if ($(nextSlideTo).length == 0) { nextSlideTo = $('.slider ul li:first'); }
        menuIn($(nextSlideTo));
    }
    setTimeout("timeSlider()", 2500);
}

function GetBackgroundImagePos(v) {
    var vResult = $(v).css("background-position");
    if (typeof vResult == "string") {
        var a = vResult.split(" ");
    } else {
        var a = [$(v).css("background-position-x"),
                 $(v).css("background-position-y")];
    }
    return {
        x: parseFloat(a[0]),
        xUnit: a[0].replace(/[0-9-.]/g, ""),
        y: parseFloat(a[1]),
        yUnit: a[1].replace(/[0-9-.]/g, "")
    };
}

function styleButtons() {
    $('.sbutton').button();
}

$(document).ready(function () {

    if ($('.slider').length > 0) {
        $('.slider ul li').mouseenter(function () { mouseIn = true; menuIn($(this)); });
        $('.slider ul li').mouseleave(function () { mouseIn = false; });

        nextSlideTo = $('.slider ul li:first');

        setTimeout("timeSlider()", 3500);
    }

    $('.showdefault').each(function () { $(this).val($(this).attr('title')); });
    $('.showdefault').focus(function () {
        if ($(this).val() == $(this).attr('title')) { $(this).val(''); }
    });
    $('.showdefault').blur(function () {
        if ($(this).val() == '') { $(this).val($(this).attr('title')); }
    });

    $('.stickynav').hover(function () {
        $(this).prev().css('background-position-y', 92);
    }, function () {
        $(this).prev().css('background-position-y', 0);
    });


    $('.jsjaxpopup').live('click', function () {
        var rel = $(this).attr('rel');
        var displayTitle = $(this).attr('title');
        if (rel == "") {
            replaceme = $(this).parent();
        } else {
            replaceme = $('#' + rel);
        }
        var url = this.href;
        var dialog = $("#dialog");
        if ($("#dialog").length == 0) {
            dialog = $('<div id="dialog" style="display:hidden" class="profile"></div>').appendTo('body');
        }
        dialog.load(
                url,
                function (responseText, textStatus, XMLHttpRequest) {
                    dialog.dialog({ modal: true, title: displayTitle, width: "auto", height: "auto" });
                    styleButtons();
                }
            );
        return false;
    });

    styleButtons();

    $('.canceldialog').live('click', function () { $("#dialog").dialog('close'); return false; });


    $('.addfav').live('click', function () {
        var template = '<tr class="newfav" id="favli{0}"><td><img width="85" src="{4}" alt="thumbnail of property" /></td><td>{1}<br />&pound;{3}<br /><a href="{2}">View details</a><img src="/content/images/red_heart-del.png" class="delfav" id="delfav|{0}"></td></tr>';
        var parts = $(this).attr('id').split('|');
        var propertyId = parts[0];
        var propName = parts[1];
        var propUrl = parts[2];
        var cont = true;
        $('#favorites tr td').each(function () {
            if ($(this).html().indexOf(propName) > -1) { cont = false; return; }
        });
        if (cont) {
            $('#nofavs').slideUp();
            template = stringFormat(template, parts);
            $.ajax({
                url: '/properties/addfavorite/' + propertyId,
                type: 'POST'
            });
            $(this).effect('transfer', { to: '#favorites', className: "ui-effects-transfer" }, 500, callback);
        }
        function callback() {
            //$('#favorites ul').append('<li class="newfav" id="favli' + propertyId + '"><img src="/content/images/red_heart-del.png" class="delfav" id="delfav|' + propertyId + '"><a href="' + propUrl + '">' + propName + '</a></li>');
            $('#favtab > tbody:last').append(template);
            $('.newfav').fadeIn().slideDown().removeClass('newfav');
        }
    });

    function stringFormat(str, arr) {
        return str.replace(
      /\{([0-9]+)\}/g,
      function (_, index) { return arr[index]; });
    }

    $('.delfav').live('click', function () {
        var parts = $(this).attr('id').split('|');
        var propertyId = parts[1];
        var favli = '#favli' + propertyId;
        $.ajax({
            url: '/properties/DeleteFavorite/' + propertyId,
            type: 'POST'
        });
        $(favli).effect('transfer', { to: '#found', className: "ui-effects-transfer" }, 500, callback);
        function callback() {
            $(favli).slideUp().fadeOut(function () { $(this).remove(); if ($('#favorites tr').length == 0) { $('#nofavs').slideDown(); } });
        }
    });

    $('.date_input').datepicker({
        dateFormat: "dd/mm/yy"
        , minDate: 0
        //,minDate: minDate
    });

    $("fieldset [title]").tooltip({
        open: function () {
            var tooltip = $(this).tooltip("widget");
            $(document).mousemove(function (event) {
                tooltip.position({
                    my: "left center",
                    at: "right center",
                    offset: "25 25",
                    of: event
                });
            })
            // trigger once to override element-relative positioning
				.mousemove();
        },
        close: function () {
            $(document).unbind("mousemove");
        }
    });

});


