/*loading the livequery.minified*/
(function($){$.extend($.fn,{livequery:function(type,fn,fn2){var self=this,q;if($.isFunction(type))fn2=fn,fn=type,type=undefined;$.each($.livequery.queries,function(i,query){if(self.selector==query.selector&&self.context==query.context&&type==query.type&&(!fn||fn.$lqguid==query.fn.$lqguid)&&(!fn2||fn2.$lqguid==query.fn2.$lqguid))return(q=query)&&false;});q=q||new $.livequery(this.selector,this.context,type,fn,fn2);q.stopped=false;q.run();return this;},expire:function(type,fn,fn2){var self=this;if($.isFunction(type))fn2=fn,fn=type,type=undefined;$.each($.livequery.queries,function(i,query){if(self.selector==query.selector&&self.context==query.context&&(!type||type==query.type)&&(!fn||fn.$lqguid==query.fn.$lqguid)&&(!fn2||fn2.$lqguid==query.fn2.$lqguid)&&!this.stopped)$.livequery.stop(query.id);});return this;}});$.livequery=function(selector,context,type,fn,fn2){this.selector=selector;this.context=context||document;this.type=type;this.fn=fn;this.fn2=fn2;this.elements=[];this.stopped=false;this.id=$.livequery.queries.push(this)-1;fn.$lqguid=fn.$lqguid||$.livequery.guid++;if(fn2)fn2.$lqguid=fn2.$lqguid||$.livequery.guid++;return this;};$.livequery.prototype={stop:function(){var query=this;if(this.type)this.elements.unbind(this.type,this.fn);else if(this.fn2)this.elements.each(function(i,el){query.fn2.apply(el);});this.elements=[];this.stopped=true;},run:function(){if(this.stopped)return;var query=this;var oEls=this.elements,els=$(this.selector,this.context),nEls=els.not(oEls);this.elements=els;if(this.type){nEls.bind(this.type,this.fn);if(oEls.length>0)$.each(oEls,function(i,el){if($.inArray(el,els)<0)$.event.remove(el,query.type,query.fn);});}else{nEls.each(function(){query.fn.apply(this);});if(this.fn2&&oEls.length>0)$.each(oEls,function(i,el){if($.inArray(el,els)<0)query.fn2.apply(el);});}}};$.extend($.livequery,{guid:0,queries:[],queue:[],running:false,timeout:null,checkQueue:function(){if($.livequery.running&&$.livequery.queue.length){var length=$.livequery.queue.length;while(length--)$.livequery.queries[$.livequery.queue.shift()].run();}},pause:function(){$.livequery.running=false;},play:function(){$.livequery.running=true;$.livequery.run();},registerPlugin:function(){$.each(arguments,function(i,n){if(!$.fn[n])return;var old=$.fn[n];$.fn[n]=function(){var r=old.apply(this,arguments);$.livequery.run();return r;}});},run:function(id){if(id!=undefined){if($.inArray(id,$.livequery.queue)<0)$.livequery.queue.push(id);}else $.each($.livequery.queries,function(id){if($.inArray(id,$.livequery.queue)<0)$.livequery.queue.push(id);});if($.livequery.timeout)clearTimeout($.livequery.timeout);$.livequery.timeout=setTimeout($.livequery.checkQueue,20);},stop:function(id){if(id!=undefined)$.livequery.queries[id].stop();else $.each($.livequery.queries,function(id){$.livequery.queries[id].stop();});}});$.livequery.registerPlugin('append','prepend','after','before','wrap','attr','removeAttr','addClass','removeClass','toggleClass','empty','remove');$(function(){$.livequery.play();});var init=$.prototype.init;$.prototype.init=function(a,c){var r=init.apply(this,arguments);if(a&&a.selector)r.context=a.context,r.selector=a.selector;if(typeof a=='string')r.context=c||document,r.selector=a;return r;};$.prototype.init.prototype=$.prototype;})(jQuery);
/*document ready and other ports*/       
$(document).ready(function()
{
    var ja = window.location.pathname;
    var o = ja.split('/');
    var js = o[2];
    var c = o[3];
    jQuery.fn.exists = function()
    {
        return jQuery(this).length > 0;
    };
    $('#keyword').focus(function()
    {
        $(this).val('').css(
        {
            'color': 'black'
        }).addClass('ui-state-highlight');
    }).blur(function()
    {
        $(this).css(
        {
            'color': 'black'
        }).removeClass('ui-state-active');
    });
    $("#district").jqSelecta(
    {
        closeButton: true,
        autoScrollOverflowX: false,
        autoScrollOverflowY: false,
        autoSelected: true,
        titleSetFrom: 'text',
        height: '250px',
        width: '750px',
        columns: 2,
        title: 'Select districts',
        onClose: function()
        {
            set_districts();
        }
    });
    $(".fg-button:not(.ui-state-disabled)").hover(function()
    {
        $(this).addClass("ui-state-hover");
    }, function()
    {
        $(this).removeClass("ui-state-hover");
    }).mousedown(function()
    {
        $(this).parents('.fg-buttonset-single:first').find(".fg-button.ui-state-active").removeClass("ui-state-active");
        if ($(this).is('.ui-state-activer.fg-button-toggleable, .fg-buttonset-multi .ui-state-active')) 
            {
            $(this).removeClass("ui-state-active");
        }
        else 
            {
            $(this).addClass("ui-state-active");
        }
    }).mouseup(function()
    {
        if (!$(this).is('.fg-button-toggleable, .fg-buttonset-single .fg-button,  .fg-buttonset-multi .fg-button')) 
            {
            $(this).removeClass("ui-state-active");
        }
    });
    $(".fg-button:not(.ui-state-disabled)").livequery(function()
    {
        $(this).hover(function()
        {
            $(this).addClass("ui-state-hovere");
        }, function()
        {
            $(this).removeClass("ui-state-hovere");
        }).mousedown(function()
        {
            $(this).parents('.fg-buttonset-single:first').find(".fg-button.ui-state-active").removeClass("ui-state-active");
            if ($(this).is('.ui-state-active.fg-button-toggleable, .fg-buttonset-multi .ui-state-active')) 
                {
                $(this).removeClass("ui-state-active");
            }
            else 
                {
                $(this).addClass("ui-state-active");
            }
        }).mouseup(function()
        {
            if (!$(this).is('.fg-button-toggleable, .fg-buttonset-single .fg-button,  .fg-buttonset-multi .fg-button')) 
                {
                $(this).removeClass("ui-state-activer");
            }
        });
    });
    $('#tabs').tabs();
    $('.main_nav ul').hide();
    $('#legacy').hide();
    $('.r_search_a').click(function()
    {
        $('#legacy').toggle();
    });
    $("#slider-range-max").slider(
    {
        range: "max",
        min: 1,
        max: 10,
        value: 1,
        slide: function(event, ui)
        {
            $("#rooms").val(ui.value);
            $("#rooms_t").val(ui.value);
        }
    });
    $("#rooms").val($("#slider-range-max").slider("value"));
    var jn = set_price(js, c);
    $("#slider-range-min").slider(jn);
    $("#price").val('$ ' + format_price($("#slider-range-min").slider("value")));
    $("#slider-size").slider(
    {
        range: "min",
        value: 300,
        min: 100,
        max: 20000,
        step: 500,
        slide: function(event, ui)
        {
            $("#size").val(format_price(ui.value) + ' sqft');
        }
    });
    $("#size").val(format_price($("#slider-size").slider("value")) + ' sqft');
    ni = $('table#report');
    ni.livequery(function()
    {
    });
    $('.odd').live('click', function()
    {
        no = $('.odd').index(this);
        var elem = $('.even')[no];
        if (elem.style.display == 'none') 
            {
            $(this).next('tr').show();
        }
        else 
            {
            $(this).next('tr').hide();
        }
        $(this).find('.ui-icon-plus').toggleClass("ui-icon-minus");
    });
    var $url = '';
    var $link = '';
    var $b = null;
    $("#divId").livequery(function()
    {
        $(this).dialog(
        {
            autoOpen: false,
            title: '<span class="ui-icon ui-icon-mail-closed" style="float:left;margin-right:.3em"></span>Got a Question?',
            buttons: 
            {
                "Send": function()
                {
                    $.ajax
                    ({
                        type: "POST",
                        url: $url + '/send',
                        data: 
                        {
                            name: $(this).find("input[name*='name']").val(),
                            number: $(this).find("input[name*='number']").val(),
                            email: $(this).find("input[name*='email']").val(),
                            preferred: $(this).find("#preferred option:selected").val(),
                            message: $(this).find("textarea[name*='message']").val(),
                            link: $link					   	
                        },
                        success: function($data)
                        {
                            $(this).dialog("close");
                            $("#divId").html($data).dialog('open');
                        }
                    });

                    $(this).dialog("close");
                    $('#divId').html('<p>Sending...</p>').dialog('open');
                },
                "Close": function()
                {
                    $(this).dialog("close");
                    $("#divId").html();
                }
            }
        });
    });
    $('.converse').livequery(function()
    {
        $(this).click(function()
        {
            $link = ($(this).attr('rel').length > 3) ? $(this).attr('rel') : '';
            $url = $(this).attr('href');
            $.ajax(
            {
                type: "POST",
                url: $url,
                data: "",
                success: function($data)
                {
                    $("#divId").html($data).dialog('open');
                }
            });
            return false;
        });
    });
    $ajax = '';
    $('form#slf').submit(function(event)
    {
        event.preventDefault();
        $('#ajax_box').html('<div style="margin-top:100px">'+
        '<img src="http://privateproperty.com.sg/realestate/css/images/ajax-loader.gif" >'+
        '</div>');   
        $url = $(this).attr('action');
        $query = '?ajax=1&price='+$('#price').val()+'&district='+$('#district').val()+
        '&size='+$('#size').val()+'&rooms='+$('#rooms').val()+'&keyword='+$('#searchfield').val();
        $.get($url+$query, 
        function(data)
        {
            $('#ajax_box').empty().fadeIn('slow').html(data);
        });     
        $ajax = 1;            
    });

    $('p.pagination a').live('click', function(event)
    {
        if($ajax == 1)
            {
            event.preventDefault();
            $('#ajax_box').html('<div style="margin-top:100px">'+
            '<img src="http://privateproperty.com.sg/realestate/css/images/ajax-loader.gif" >'+
            '</div>');   
            $url = $(this).attr('href');
            $.get($url, 
            function(data)
            {
                $('#ajax_box').empty().fadeIn('slow').html(data);
            });             
        }

    });
});
function format_price(num, decpoint, sep)
{
    if (arguments.length == 2) 
        {
        sep = ",";
    }
    if (arguments.length == 1) 
        {
        sep = ",";
        decpoint = ".";
    }
    num = num.toString();
    a = num.split(decpoint);
    x = a[0];
    y = a[1];
    z = "";
    if (typeof(x) != "undefined") 
        {
        for (i = x.length - 1; i >= 0; i--) 
            {
            z += x.charAt(i);
        }
        z = z.replace(/(\d{3})/g, "$1" + sep);
        if (z.slice(-sep.length) == sep) 
            {
            z = z.slice(0, -sep.length);
        }
        x = "";
        for (i = z.length - 1; i >= 0; i--) 
            {
            x += z.charAt(i);
        }
        if (typeof(y) != "undefined" && y.length > 0) 
            {
            x += decpoint + y;
        }
    }
    return x;
}
function get_map(se, nn, jr, nc, nj)
{
    $.getJSON(nc, 
    {
        street: se,
        project: nn,
        type: nj
    }, function(data)
    {
        var l;
        var jo;
        var ji;
        var ns;
        var jl = new GLatLng(data.lat, data.long);
        var ne = 
        {
            yaw: 370.64659986187695,
            pitch: -20
        };
        ji = new GStreetviewClient();
        l = new GMap2(document.getElementById('m' + jr));
        l.setCenter(jl, 14);
        l.setUIToDefault();
        l.addOverlay(new GMarker(jl));
        GEvent.addListener(l, "click", function(overlay, latlng)
        {
            ji.getNearestPanorama(latlng, showPanoData);
        });
        jo = new GStreetviewPanorama(document.getElementById('p' + jr));
        jo.setLocationAndPOV(jl);
        GEvent.addListener(jo, "error", handleNoFlash);
        ji.getNearestPanorama(jl, showPanoData);
        function showPanoData(panoData)
        {
            if (panoData.code != 200) 
                {
                $('#p' + jr).html('<div id="no_map">We couldn\'t find a Street View for this property. Try zooming in the <span class="enlarge">Map</span> and clicking on a nearby road.</div>');
                return;
            }
            else 
                {
                var nr = '#p' + jr;
                $(nr).text('');
            }
            ns = panoData.links[0].panoId;
            var nl = '<table id="description">' + se + '</table>';
            l.openInfoWindowHtml(panoData.location.latlng, nl);
            jo.setLocationAndPOV(panoData.location.latlng);
        }
        function next()
        {
            ji.getPanoramaById(ns, showPanoData);
        }
        function handleNoFlash(errorCode)
        {
            if (errorCode == 603) 
                {
                alert("Error: Flash doesn't appear to be supported by your browser");
                return;
            }
        }
    });
}
function set_price(js, nj)
{
    var jn;
    switch (js)
    {
        case 'rent':
            jn = 
            {
                range: "min",
                value: 6000,
                min: 1500,
                max: 30000,
                step: 500,
                slide: function(event, ui)
                {
                    $("#price").val('$ ' + format_price(ui.value));
                    $("#price_t").val('$ ' + format_price(ui.value));
                }
            };
            break;
        case 'sale':
            jn = 
            {
                range: "min",
                value: 2500000,
                min: 300000,
                max: 30000000,
                step: 10000,
                slide: function(event, ui)
                {
                    $("#price").val('$ ' + format_price(ui.value));
                    $("#price_t").val('$ ' + format_price(ui.value));
                }
            };
            break;
    }
    return jn;
}
function set_districts()
{
    var jc = '';
    $('#district > :selected').each(function(i)
    {
        var na = jQuery(this);
        jc += na.attr('value') + ',';
    });
    jc = jc.substring(0, jc.length - 1);
    return jc;
}
