﻿$('html').addClass('js');

jQuery(function() {

jQuery.fn.pngFix = function(settings) {

    // Settings
    settings = jQuery.extend({
        blankgif: 'blank.gif'
    }, settings);

    var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
    var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);

    if (jQuery.browser.msie && (ie55 || ie6)) {

        //fix images with png-source
        jQuery(this).find("img[src$=.png]").each(function() {

            jQuery(this).attr('width', jQuery(this).width());
            jQuery(this).attr('height', jQuery(this).height());

            var prevStyle = '';
            var strNewHTML = '';
            var imgId = (jQuery(this).attr('id')) ? 'id="' + jQuery(this).attr('id') + '" ' : '';
            var imgClass = (jQuery(this).attr('class')) ? 'class="' + jQuery(this).attr('class') + '" ' : '';
            var imgTitle = (jQuery(this).attr('title')) ? 'title="' + jQuery(this).attr('title') + '" ' : '';
            var imgAlt = (jQuery(this).attr('alt')) ? 'alt="' + jQuery(this).attr('alt') + '" ' : '';
            var imgAlign = (jQuery(this).attr('align')) ? 'float:' + jQuery(this).attr('align') + ';' : '';
            var imgHand = (jQuery(this).parent().attr('href')) ? 'cursor:hand;' : '';
            if (this.style.border) {
                prevStyle += 'border:' + this.style.border + ';';
                this.style.border = '';
            }
            if (this.style.padding) {
                prevStyle += 'padding:' + this.style.padding + ';';
                this.style.padding = '';
            }
            if (this.style.margin) {
                prevStyle += 'margin:' + this.style.margin + ';';
                this.style.margin = '';
            }
            var imgStyle = (this.style.cssText);

            strNewHTML += '<span ' + imgId + imgClass + imgTitle + imgAlt;
            strNewHTML += 'style="position:relative;white-space:pre-line;display:inline-block;background:transparent;' + imgAlign + imgHand;
            strNewHTML += 'width:' + jQuery(this).width() + 'px;' + 'height:' + jQuery(this).height() + 'px;';
            strNewHTML += 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + jQuery(this).attr('src') + '\', sizingMethod=\'scale\');';
            strNewHTML += imgStyle + '"></span>';
            if (prevStyle != '') {
                strNewHTML = '<span style="position:relative;display:inline-block;' + prevStyle + imgHand + 'width:' + jQuery(this).width() + 'px;' + 'height:' + jQuery(this).height() + 'px;' + '">' + strNewHTML + '</span>';
            }

            jQuery(this).hide();
            jQuery(this).after(strNewHTML);

        });

        // fix css background pngs
        jQuery(this).find("*").each(function() {
            var bgIMG = jQuery(this).css('background-image');
            if (bgIMG.indexOf(".png") != -1) {
                var iebg = bgIMG.split('url("')[1].split('")')[0];
                jQuery(this).css('background-image', 'none');
                jQuery(this).get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iebg + "',sizingMethod='scale')";
            }
        });

        //fix input with png-source
        jQuery(this).find("input[src$=.png]").each(function() {
            var bgIMG = jQuery(this).attr('src');
            jQuery(this).get(0).runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + bgIMG + '\', sizingMethod=\'scale\');';
            jQuery(this).attr('src', settings.blankgif)
        });

    }

    return jQuery;

};

    // Watermark
    jQuery.watermark = function(o) {
        o.el = jQuery(o.el).wrap("<div style='position:relative;'/>");
        var l = jQuery("<label/>");
        if (o.html) { l.html(o.html); };
        if (o.cls) { l.addClass(o.cls); };
        if (o.css) { l.css(o.css); };

        l.css({ position: "absolute", left: "6px", top: "6px", display: "inline", cursor: "text" });
        if (o.el[0].tagName != "TEXTAREA") { l.css({ top: "4px" }); };

        if (!o.cls && !o.css) {
            l.css("color", "#bbb");
        };

        var focus = function() {
            l.css("text-indent", "-10000px");
        };

        var blur = function() {
            if (o.el.val() == "") {
                l.css("text-indent", "0px");
            } else {
                l.css("text-indent", "-10000px");
            }
        };

        var click = function() {
            o.el.focus();
        };

        o.el.focus(focus).blur(blur);
        l.click(click);

        o.el.before(l).focus().blur();

        return o.el;
    };

    jQuery.fn.watermark = function(o) {
        return this.each(function() {
            if (typeof (o) == "string") {
                try { o = eval("(" + o + ")"); } catch (ex) { o = { html: o }; };
            };
            o.el = this;
            return jQuery.watermark(o);
        });
    };
});

jQuery().ready(function() {
    jQuery("[watermark]").each(function(num, el) {
        jQuery(el).watermark(jQuery(el).attr("watermark"));
    });

    // Tabs
    $('#tabs').tabs({
        select: function(e, ui) {
            $("[id$=hidSelectedTab]").val(ui.index);
        },
        fx: { opacity: 'toggle', duration: 'fast' }
    });
    var itab = parseInt($("[id$=hidSelectedTab]").val());
    $("#tabs").tabs('select', itab);


    // Bedrooms
    $('#slideBeds').slider({
        range: "max",
        min: 0,
        max: 6,
        change: function(e, ui) {
            $("#minBeds").text(ui.value == 0 ? "Any" : ui.value);
            $("[id$=hidBedsMin]").val(ui.value);
        }
    });
    var ibed = $("[id$=hidBedsMin]").val();
    $("#slideBeds").slider('value', ibed);

    // Sale Price
    $('#slidePrice').slider({
        range: true,
        min: 0,
        max: 600,
        step: 10,
        change: function(e, ui) {
            var min = $("#slidePrice").slider("values", 0);
            var max = $("#slidePrice").slider("values", 1);
            if (min == 0 && max == 600) {
                $("#salePriceTxt").text('Any');
            } else if (min == 0) {
                $("#salePriceTxt").text('Up to £' + max + 'K');
            } else if (max == 600) {
                $("#salePriceTxt").text('' + min + 'K and above');
            } else {
                $("#salePriceTxt").text('£' + min + 'K - £' + max + 'K');
            }

            $("[id$=hidPriceMax]").val(max);
            $("[id$=hidPriceMin]").val(min);

        }
    });

    var imin = parseInt($("[id$=hidPriceMin]").val());
    var imax = parseInt($("[id$=hidPriceMax]").val());
    $("#slidePrice").slider('values', 1, imax);
    $("#slidePrice").slider('values', 0, imin);

    // Rental Prices
    $('#rentPrice').slider({
        range: true,
        min: 200,
        max: 1400,
        step: 10,
        change: function(e, ui) {
            var min = $("#rentPrice").slider("values", 0);
            var max = $("#rentPrice").slider("values", 1);
            if (min == 200 && max == 1400) {
                $("#rentPriceTxt").text('Any');
            } else if (min == 200) {
                $("#rentPriceTxt").text('Up to £' + max + ' pcm');
            } else if (max == 1400) {
                $("#rentPriceTxt").text('£' + min + ' pcm and above');
            } else {
                $("#rentPriceTxt").text('£' + min + ' pcm - £' + max + ' pcm');
            }

            $("[id$=hidRentMax]").val(max);
            $("[id$=hidRentMin]").val(min);

        }
    });

    var iminR = $("[id$=hidRentMin]").val();
    var imaxR = $("[id$=hidRentMax]").val();
    $("#rentPrice").slider('values', 1, imaxR);
    $("#rentPrice").slider('values', 0, iminR);
    $("INPUT[id$=tbFindInAddress]").watermark("Anywhere");

    //Highlight
    function highlight(elemId) {
        var elem = $(elemId);
        setTimeout(function() { elem.css("backgroundColor", "#41924C"); elem.animate({ backgroundColor: "#E51900" }, 500) }, 500);
        setTimeout(function() { elem.animate({ backgroundColor: "#41934D" }, 1500) }, 4200);
        setTimeout(function() { elem.css("backgroundColor", "transparent") }, 7000);
    }
    highlight(".result");

    $('#tabs').fadeOut(10, function() { $('#tabs').css("visibility", "inherit"); $('#tabs').fadeIn('fast'); });

    $(document).pngFix();

});

