String.prototype.contains = function(it) { return this.indexOf(it) != -1; };

function getWidth(target) {
    var classStr = target.attr("class")
    var widthStrIndex = classStr.search(/sys-width/);
    if (widthStrIndex != -1) {
        classStr = classStr.slice(widthStrIndex);
        var widthStr = classStr.split()[0];
        var width = widthStr.replace("sys-width", "");
        return width;
    } else {
        return 900;
    }
}

function qTip(target, text, classes, show, type, delay) {
    target.addClass("hasTooltip" + type);
    target.qtip({
        content: {
            text: text,
        },
        show: show,
        position: {
            my: 'bottom center',
            at: 'top center',
        },
        style: {
            classes: classes
        },
        hide: {
            delay: delay
        }
    });
}

function qTipDOM(target, source, classes, show, type, delay) {
    qTip(target, source.html(), classes, show, type, delay);
}

function qTipText(target, text, classes, show, type, delay) {
    qTip(target, text, classes, show, type, delay);
}


function qTipDOMError(target, source, show) {
    qTipDOM(target, source, 'ui-tooltip-red', show, "Error", 1000);
}

function qTipDOMInfo(target, source, show) {
    qTipDOM(target, source, 'ui-tooltip-blue', show, "Info", 0);
}

function qTipDOMHelp(target, source, show) {
    qTipDOM(target, source, 'ui-tooltip-green', show, "Help", 1000);
}

function qTipTextError(target, text, show) {
    qTipText(target, text, 'ui-tooltip-red', show, "Error", 1000);
}

function qTipTextInfo(target, text, show) {
    qTipText(target, text, 'ui-tooltip-blue', show, "Info", 0);
}

function qTipTextHelp(target, text, show) {
    qTipText(target, text, 'ui-tooltip-green', show, "Help", 1000);
}

function qTipFix(target) {
    target.qtip('option', 'hide.fixed', true);
}



function dialogBase(dialogBox, width) {
    dialogBox.dialog({
        modal: true,
        minWidth: 400,
        width: width
    });
}

function dialoxBoxCreate() {
    var dialogId = "dialog";
    var dialogBox = jQuery("#"+dialogId);
    if (dialogBox.length == 0) {
        jQuery("body").append("<div id='"+dialogId+"' class='hidden'></div>");
        dialogBox = jQuery("#"+dialogId);
    }
    return dialogBox
}

function dialogInit(title, text, width, slide) {
    var dialogBox = dialoxBoxCreate();
    dialogBox.attr("title", title);
    dialogBox.html(text);
    dialogBase(dialogBox, width);
    if (slide) {
        dialogBox.slideto({
            target : ".ui-dialog",
            speed  : 1000,
            once: true
        });
        dialogBox.trigger('click');
    }
    jQuery(".ui-widget-overlay").one("click", function(){
        dialogBox.dialog('close');
    });
}

function dialogAjaxContent(target, action) {
    var url = target.attr("href");
    var title = target.attr("title");
    var slide = target.hasClass("sys-dialogSlide");
    var width = getWidth(target);
    target.bind(action, function(){
        var jqxhr = jQuery.getJSON(url, {}, function(data) {
            dialogInit(title, data.html, width, slide);
            return false;
        })
        return false;
    });
    return false;
}

function dialogAjaxContentMultiple(targets, action) {
    targets.each(function(){
        dialogAjaxContent(jQuery(this), action)
    });
    return false;
}

function dialogDOMContent(target, action) {
    var sourceClass = target.attr("rel");
    var title = target.attr("title");
    var slide = target.hasClass("sys-dialogSlide");
    var width = getWidth(target);
    target.bind(action, function(){
        html = "";
        jQuery("."+sourceClass).each(function(){
            html = html + jQuery(this).html();
        });
        dialogInit(title, html, width, slide);
        return false;
    });
    return false;
}

function dialogDOMContentMultiple(targets, action) {
    targets.each(function(){
        dialogDOMContent(jQuery(this), action)
    });
    return false;
}

function qTipTextInfoHover(targets) {
    targets.each(function(){
        var item = jQuery(this);
        qTipTextInfo(item, item.attr("title"), { target: item });
    });
}

function nextbox(fldobj, nbox) {
    if (fldobj.value.length==fldobj.maxLength) {
        jQuery("#"+nbox).focus();
    }
}

function slideTo(target) {
    target.slideto({
        target : target.find("a").attr("href"),
        speed  : 1000
    });
}

function slideToMultiple(targets) {
    targets.each(function() {
        slideTo(jQuery(this));
    });
}

function wl_redirect() {
    var url = window.location.protocol + "//" + window.location.host + window.location.pathname;
    if (window.location.search) {
        url = url + window.location.search + "&";
    } else {
        url = url + "?";
    }
    if (window==window.top && wl==true) {
        window.location = url + "wl=False";
        return false;
    }
    if (window!=window.top && wl==false) {
        window.location = url + "wl=True";
        return false;
    }
}
wl_redirect();

function isNumeric(input){
    var RE = /^-{0,1}\d*\.{0,1}\d+$/;
    return (RE.test(input));
}

function sleep(ms)
{
    var dt = new Date();
    dt.setTime(dt.getTime() + ms);
    while (new Date().getTime() < dt.getTime());
}

jQuery(document).ready(function(){
    dialogAjaxContentMultiple(jQuery(".sys-dialogAjax"), "click");
    dialogDOMContentMultiple(jQuery(".sys-dialogDOM"), "click");
    qTipTextInfoHover(jQuery(".sys-qTipTextInfo"));
    slideToMultiple(jQuery(".sys-slideTo"));

    jQuery('.sys-gallery').colorbox({
        opacity: 0.85,
        current: "",
        rel: "sys-gallery"
    });
    
    jQuery('.sys-floorplan').colorbox({
        opacity: 0.85,
        current: "",
        rel: "sys-floorplan"
    });

    jQuery("sys-disablePropragation").live("click", function(){
        jQuery(this).stopPropagation();
    });
});

