﻿var dimension = DetectDimension();
var tempPropSearchImgSrc;
var linkLocation;
var keepProperties = false;

$(function () {
    $(".searchButton").button({
        icons: {
            primary: 'ui-icon-search'
        }
    });
    $(".shortlistButton").button({
        icons: {
            primary: 'ui-icon-home'
        }
    });
    $(".printButton").button({
        icons: {
            primary: 'ui-icon-print'
        }
    });
    $(".infoButton").button({
        icons: {
            primary: 'ui-icon-comment'
        }
    });
    $(".editButton").button({
        icons: {
            primary: 'ui-icon-wrench'
        }
    });
    $(".deleteButton").button({
        icons: {
            primary: 'ui-icon-trash'
        }
    });
    $(".button").button();
});

$(window).scroll(function () {
    var scrollpos = 0;
    if (($(window).scrollTop() - 250) > 0)
    {
        scrollpos = $(window).scrollTop() - 250;
    }

    $("#search-panel").stop().animate({ "marginTop": scrollpos + "px" }, "slow");
})

$(document).ready(function () {

    //Speed of the slideshow
    var speed = 9000;

    $('img[data-hover]').hover(function () {
        $(this).attr('tmp', $(this).attr('src')).attr('src', $(this).attr('data-hover')).attr('data-hover', $(this).attr('tmp')).removeAttr('tmp');
    }).each(function () {
        $('<img />').attr('src', $(this).attr('data-hover'));
    });

    //You have to specify width and height in .slider CSS properties
    //After that, the following script will set the width and height accordingly
    $('.mask-gallery, .gallery li').width($('.slider').width());
    $('.gallery').width($('.slider').width() * $('.gallery li').length);
    $('.mask-gallery, .gallery li, .mask-excerpt, .excerpt li').height($('.slider').height());

    //Assign a timer, so it will run periodically
    var run = setInterval('newsscoller(0)', speed);


    $('.gallery li:first, .excerpt li:first').addClass('selected');

    //Mouse over, pause it, on mouse out, resume the slider show
    $('.slider').hover(

		function () {
		    clearInterval(run);
		},
		function () {
		    run = setInterval('newsscoller(0)', speed);
		}

	);
    if (navigator.userAgent.match(/Android/i) ||
             navigator.userAgent.match(/webOS/i) ||
             navigator.userAgent.match(/iPhone/i) ||
             navigator.userAgent.match(/iPod/i) ||
             navigator.userAgent.match(/BlackBerry/)) {

        $('#slide-menu').click(function () {

            if (!slideMenu) {
                // open the menu
                $('#slide-closed').fadeOut("slow", function () {
                    $('#menu-area').animate({ "height": 123 + "px" }, "slow", function () {
                        $('#slide-closed').hide();
                        $('#slide-open').fadeIn("slow");
                    });
                });
            }
        });

    }
    else {

        $('#slide-menu').mouseenter(function () {

            if (!slideMenu) {
                // open the menu
                $('#slide-closed').fadeOut("slow", function () {
                    $('#menu-area').animate({ "height": 123 + "px" }, "slow", function () {
                        $('#slide-closed').hide();
                        $('#slide-open').fadeIn("slow");
                    });
                });
            }
        });
    }

    $('#slide-menu').mouseleave(function () {

        if (!slideMenu) {
            // open the menu
            $('#slide-open').fadeOut("slow", function () {
                $('#menu-area').animate({ "height": 25 + "px" }, "slow", function () {
                    $('#slide-open').hide();
                    $('#slide-closed').fadeIn("slow");
                });
            });
        }
    });

    $('.expandable-search').mouseenter(function () {

        // open the menu
        $('.expandable-search').stop();
        $('.expandable-search').animate({ "height": 220 + "px" }, "slow");
    });

    $('.expandable-search').mouseleave(function () {

        // close the menu
        $('.expandable-search').stop();
        $('.expandable-search').animate({ "height": 68 + "px" }, "slow");
    });


    $('#slide-open').toggle();

    $('#repairForm').validate({

        rules: {

            FirstName: {

                required: true,
                minlength: 2

            },

            LastName: {

                required: true,
                minlength: 2

            },

            EMail: {

                required: true,
                minlength: 2

            },

            Mobile: {

                required: true,
                minlength: 2

            },

            Address: {

                required: true,
                minlength: 2

            },

            Request: {

                required: true,
                minlength: 2

            }
        },

        messages: {

            FirstName: "",
            LastName: "",

            Mobile: "",
            Address: "",

            EMail: "",
            Request: ""
        }
    });

    $('.date').datepicker({ dateFormat: "mm/dd/yy" });

    $('.ImageCard').click(function () {

        var currentLarge = $('#primaryImageBox').css('background-image');
        currentLarge = currentLarge.replace("_display", "_thumb")
        var myImage = $(this).css('background-image');
        myImage = myImage.replace("_thumb", "_display")
        $('#primaryImageBox').css({
            "background-image": myImage
        });

    });


});


function DetectDimension() {
    if ($.browser.msie) {
        dim_win_width = $(window).width();
        dim_win_height = $(window).height();
    } else {
        dim_win_width = window.innerWidth;
        dim_win_height = window.innerHeight; ;
    }
    dim_win = [dim_win_width, dim_win_height];
    return dim_win;
}

function newsscoller(prev) {

    //Get the current selected item (with selected class), if none was found, get the first item
    var current_image = $('.gallery li.selected').length ? $('.gallery li.selected') : $('.gallery li:first');
    var current_excerpt = $('.excerpt li.selected').length ? $('.excerpt li.selected') : $('.excerpt li:first');

    //if prev is set to 1 (previous item)
    if (prev) {

        //Get previous sibling
        var next_image = (current_image.prev().length) ? current_image.prev() : $('.gallery li:last');
        var next_excerpt = (current_excerpt.prev().length) ? current_excerpt.prev() : $('.excerpt li:last');

        //if prev is set to 0 (next item)
    } else {

        //Get next sibling
        var next_image = (current_image.next().length) ? current_image.next() : $('.gallery li:first');
        var next_excerpt = (current_excerpt.next().length) ? current_excerpt.next() : $('.excerpt li:first');
    }

    //clear the selected class
    $('.excerpt li, .gallery li').removeClass('selected');

    //reassign the selected class to current items
    next_image.addClass('selected');
    next_excerpt.addClass('selected');

    //Scroll the items
    $('.mask-gallery').scrollTo(next_image, 800);
    $('.mask-excerpt').scrollTo(next_excerpt, 800);

}


Cufon.replace('h1', {
    color: '-linear-gradient(#555, #000)'
});
Cufon.replace('h2', {
    color: '-linear-gradient(#555, #000)'
});
Cufon.replace('h3', {
    color: '-linear-gradient(#555, #000)'
});
Cufon.replace('h4', {
    color: '-linear-gradient(#555, #000)'
});
Cufon.replace('h5', {
    color: '-linear-gradient(#555, #000)'
});
Cufon.replace('h6', {
    color: '-linear-gradient(#555, #000)'
});
Cufon.replace('p', {
    color: '-linear-gradient(#555, #000)'
});
Cufon.replace('td', {
    color: '-linear-gradient(#555, #000)'
});
Cufon.replace('label', {
    color: '-linear-gradient(#555, #000)'
});
Cufon.replace('.pager, #shortlistcount', {
    color: '-linear-gradient(#fff, #ccc)'
});
Cufon.replace('.feature', {
    color: '-linear-gradient(#555, #000)'
});
Cufon.replace('.searchButton', {
    color: '-linear-gradient(#555, #000)'
});
Cufon.replace('#slide-closed', {
    color: '-linear-gradient(#fff, #ccc)'
});
Cufon.replace('#nav li:hover ul a, #nav li a', {
    color: '-linear-gradient(#555, #000)'
});
Cufon.replace('.pager-link', {
    hover: true,
    color: '-linear-gradient(#fff, #ccc)'
});
Cufon.replace('.property-card-details', {
    color: '-linear-gradient(#555, #000)'
});

Cufon.replace('.property-card-title-text', {
    color: '-linear-gradient(#fff, #ccc)'
});
Cufon.replace('#footer-content', {
    color: '-linear-gradient(white, #888)'
});
Cufon.replace('#slide-open', {
    color: '-linear-gradient(white, #888)'
});
Cufon.replace('.property-card-info-box, #shortlist-box', {
    color: '-linear-gradient(#555, #000)'
});
Cufon.replace('.front-property-card-title, .front-property-card-info-box, #phone', {
    color: '-linear-gradient(#fff, #ccc)'
});
var slideMenu = false;

function GoToProperty(id) {

    window.location = "/Rental/Details/" + id;

}

function AddToShortlist(id)
{
    $.ajax({
        url: "/AddToShortlist/" + id,
        type: "POST",
        error: function (jqXHR, textStatus, errorThrown) {
            alert("There was an error, " + jqXHR + "\n\n" + textStatus + "\n\n" + errorThrown);
            $('#error').html("There was an error, " + jqXHR + "<br />" + textStatus + "<br />" + errorThrown);
        },
        success: function (result) {

            $('#shortlistcount').fadeOut("slow", function () {
                $('#shortlistcount').html(result.shortlistcount + " properties shortlisted.");
                Cufon.replace('#shortlistcount', {
                    color: '-linear-gradient(#fff, #ccc)'
                });
                $('#shortlistcount').fadeIn("slow");
            });


        }
    });
}

function ShareShortlistFacebook() {
    $.ajax({
        url: "/SaveShortlist/",
        type: "POST",
        error: function (jqXHR, textStatus, errorThrown) {
            alert("There was an error, " + jqXHR + "\n\n" + textStatus + "\n\n" + errorThrown);
            $('#error').html("There was an error, " + jqXHR + "<br />" + textStatus + "<br />" + errorThrown);
        },
        success: function (result) {

            window.open('http://www.facebook.com/sharer.php?u=http://www.tomlinsonestates.co.uk/List/' + result.shortlistID + '&t=TomlinsonEstates%20-%20My%20Shortlist', 'open_window', 'width=640, height=480, left=0, top=0');

        }
    });

}


function ShareShortlistTwitter() {
    $.ajax({
        url: "/SaveShortlist/",
        type: "POST",
        error: function (jqXHR, textStatus, errorThrown) {
            alert("There was an error, " + jqXHR + "\n\n" + textStatus + "\n\n" + errorThrown);
            $('#error').html("There was an error, " + jqXHR + "<br />" + textStatus + "<br />" + errorThrown);
        },
        success: function (result) {

            window.open('http://twitter.com/home?status=Check%20out%20my%20new%20shortlist%20on%20Tomlinson%20Estates%20-%20http://www.tomlinsonestates.co.uk/List/' + result.shortlistID + '', 'open_window', 'width=640, height=480, left=0, top=0');

        }
    });

}

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-21483444-8']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

