var autosugestTimeout;
$(function () {
    $(".savcount").click(function () {
        displaySavedSearch();
    });
    $("#contactform").validate();
    $('.clearField').clearField();
	$("input").blur(function(){
	 	$('#suggestions').fadeOut();
	});
    $('#top-src-btn').click(topSearch);
    $('#top-src-txt').keypress(function (event) {
        if (event.keyCode == '13') {
            topSearch();
        }
    });
	$('#top-src-txt').keyup(function(ev) {
		var text=$('#top-src-txt').val();
		window.clearTimeout(autosugestTimeout);
		autosugestTimeout=window.setTimeout(function(){lookup(text);},200);		
	});
});

function lookup(inputString) {
	if(inputString.length < 2) {
		$('#suggestions').fadeOut(); // Hide the suggestions box
	} else {
		$.post(base_href + "ajax/autocomplete", {name: ""+inputString+""}, function(data) { // Do an AJAX call
			$('#suggestions').fadeIn(); // Show the suggestions box
			$('#suggestions').html(data); // Fill the suggestions box
		});
	}
}

function redirectSearchLink(link) {
	url = base_href + search_surl + '/#' + link;
	window.location = url;
}

function topSearch() {
    txt = $('#top-src-txt').val();
    redirectSearchLink('nm=' + txt);    
}

function oclone(o) {
    so = $.toJSON(o)
    no = $.evalJSON(so);
    return no;
}

function isArray(obj) {
    if (obj.constructor.toString().indexOf("Array") == -1) return false;
    else return true;
}

function unique(a) {
    var r = new Array();
    o: for (var i = 0, n = a.length; i < n; i++) {
        for (var x = 0, y = r.length; x < y; x++) {
            if (r[x] == a[i]) continue o;
        }
        r[r.length] = a[i];
    }
    return r;
}
if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function (value, start) {
        var i;
        if (!start) {
            start = 0;
        }
        for (i = start; i < this.length; i++) {
            if (this[i] == value) {
                return i;
            }
        }
        return -1;
    }
}

function showAllViewed() {
    $(".last_box").show();
}
function scrollTo(elname) {
    $.scrollTo(elname, 500);
}

function AddFavorite(house) {
    var houses = [];
    houses.push(house);
    chouses = $.cookie.get('favs', true);
    if (chouses == null) {
        chouses = [];
    }
    chouses = chouses.concat(houses);
    chouses = unique(chouses);
    $.cookie.set('favs', chouses, {
        json: true,
        path: '/'
    });
    $('#favcount').html("(" + chouses.length + ")");
    $fr = $.cookie.get('f');
    if ($fr == null) {
        $('#favcount').qtip({
            content: {
                text: $('#favcount').attr('tooltip'),
                title: {
                    text: $('#favcount').attr('title'),
                    button: $('#favcount').attr('close')
                }
            },
            position: {
                corner: {
                    target: 'bottomMiddle',
                    tooltip: 'topRight'
                },
                adjust: {
                    scroll: true,
                    screen: true,
                    resize: true
                }
            },
            show: {
                when: false,
                ready: true,
                solo: true
            },
            hide: false,
            style: {
                border: {
                    width: 3,
                    radius: 5,
                    color: '#a9e3f8'
                },
                title: {
                    background: '#d8f0f8',
                    color: '#000000',
                    'font-size': 11
                },
                padding: 10,
                'font-size': 12,
                tip: true
            }
        });
    } else {
        alert(txt_f_added);
    }
    $.cookie.set('f', "1", {
        expires: 0,
        path: '/'
    });
}

function changeLang(lang) {
    $.cookie.set('selectedlanguage', lang, {
        path: '/'
    });
    location.reload(true);
}

function compareHouses() {
    $('.favchk:checked').each(function () {
        did = (this.id.split('-'));
        houses.push(did[1])
    });
}

function openFeedList() {
    $('#qtip-blanket').remove();
    $('.qtip').remove();
    $('<div id="qtip-blanket">').css({
        position: 'absolute',
        top: 0,
        left: 0,
        height: $(document).height(),
        width: '100%',
        opacity: 0.7,
        backgroundColor: '#ffffff',
        zIndex: 5000
    }).appendTo(document.body).hide();
    $(".rss").qtip({
        content: {
            title: {
                text: "RSS - last minutes",
                button: txt_close_window
            },
            text: '',
            url: "feed/"
        },
        position: {
            target: $(window),
            corner: 'center',
            adjust: {
                scroll: true
            }
        },
        show: {
            when: false,
            ready: true,
            solo: true,
            effect: {
                length: 0
            }
        },
        hide: false,
        style: {
            width: {
                max: 700,
                min: 300
            },
            padding: '10px',
            border: {
                width: 5,
                radius: 10,
                color: '#a9e3f8'
            },
            title: {
                background: '#d8f0f8',
                color: '#000000',
                'font-size': 11
            },
            'font-size': 11
        },
        api: {
            beforeShow: function () {
                $('#qtip-blanket').fadeIn(this.options.show.effect.length);
            },
            beforeHide: function () {
                $('#qtip-blanket').fadeOut(this.options.hide.effect.length);
            }
        }
    });
}

function sendContact() {
    if ($("#contactform").valid()) {
        data = $("#contactform").serialize();
        s_url = "ajax/send_contact";
        $.ajax({
            type: "POST",
            url: s_url,
            data: data,
            success: getContactResult
        });
    } else {}
    return false;
}

function getContactResult(data) {
    try {
        jdata = $.evalJSON(data)
    } catch (e) {
        alert(data);
    }
    $("#contactform").html(jdata.response);
}
