﻿var replaceme;
var mouseIn = false;
var nextSlideTo;

function theRotator() {
    //Set the opacity of all images to 0
    $('div.rotator ul li').css({ opacity: 0.0 });

    //Get the first image and display it (gets set to full opacity)
    $('div.rotator ul li:first').css({ opacity: 1.0 });

    //Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds

    setInterval('rotate()', 6000);

}

function rotate() {
    //Get the first image
    var current = ($('div.rotator ul li.show') ? $('div.rotator ul li.show') : $('div.rotator ul li:first'));

    if (current.length === 0) current = $('div.rotator ul li:first');

    //Get next image, when it reaches the end, rotate it back to the first image
    var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div.rotator ul li:first') : current.next()) : $('div.rotator ul li:first'));

    //Un-comment the 3 lines below to get the images in random order

    //var sibs = current.siblings();
    //var rndNum = Math.floor(Math.random() * sibs.length );
    //var next = $( sibs[ rndNum ] );


    //Set the fade in effect for the next image, the show class has higher z-index
    next.css({ opacity: 0.0 })
	.addClass('show')
	.animate({ opacity: 1.0 }, 1000);

    //Hide the current image
    current.animate({ opacity: 0.0 }, 1000)
	.removeClass('show');

}



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 styleButtons() {
    $('.sbutton').button();
}

$(document).ready(function () {

    //Load the slideshow
    theRotator();
    $('div.rotator').fadeIn(1000);
    $('div.rotator ul li').fadeIn(1000); // tweek for IE

    $('.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', 130);
    }, 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");
        }
    });

});


