﻿
/// <reference path="../jquery-1.2.6.js" />

/// <reference path="../jquery-1.2.6-vsdoc.js" />
/// <reference path="Footinet.Objects.js" />
/// <reference path="../json2.min.js" />

var ClientService = {
    GetWithDataAndContext: function(serviceName, data, onsucceeded, onerror, context) {
        ajaxcall3(serviceName, data, onsucceeded, onerror, context);
    },
    GetWithData: function(serviceName, data, onsucceeded, onerror) {
        ajaxcall2(serviceName, data, onsucceeded, onerror);
    },
    Get: function(serviceName, onsucceeded, onerror) {
        ajaxcall1(serviceName, onsucceeded, onerror);
    },
    ServiceNames: {
        GetGrounds: "/AjaxService/ClientWcfService.svc/GetGrounds"
    }
}

var TimerControl = {
    GetTimerObject: function() {
        if (this.TimerObject == null)
            this.TimerObject = new Object();

        return this.TimerObject;
    },
    TimerObject: null
}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function PredictiveText(serviceName, textBoxID, hiddenFieldID, resultDivID, dataArgs, dataTextField, dataValueField) {

    if (trim(dataArgs.searchText).length > 0) {
        $("#" + hiddenFieldID).val("");

        var context = {
            TextBoxID: textBoxID,
            HiddenFieldID: hiddenFieldID,
            ResultDivID: resultDivID,
            DataTextField: dataTextField,
            DataValueField: dataValueField
        };

        clearTimeout(TimerControl.GetTimerObject().PredictiveTextTimer);

        TimerControl.GetTimerObject().PredictiveTextTimer = setTimeout(
            function() {
                ClientService.GetWithDataAndContext(serviceName,
            dataArgs,
            function(data) {
                onPredictiveTextComplete(data, context)
            },
            onPredictiveTextError,
            context);
            }
        , 250);
    }
}

function onPredictiveTextComplete(data, context) {

    $("#" + context.ResultDivID).empty();
    var container = document.createElement("div");
    var closer = document.createElement("div");
    var list = document.createElement("ul");
    for (var e in data.d) {

        var item = document.createElement("li");
        var link = document.createElement("a");
        $(link).data("objectValue", data.d[e][context.DataValueField]);
        $(link).data("objectText", data.d[e][context.DataTextField]);
        $(link).attr("href", "#");
        link.innerHTML = data.d[e][context.DataTextField];
        $(link).click(function() {
            $("#" + context.TextBoxID).val($(this).data("objectText"));
            $("#" + context.HiddenFieldID).val($(this).data("objectValue"));
            $("#" + context.ResultDivID).hide("fast");
        });

        $(item).append(link);
        $(list).append(item);
    }



    var closelink = document.createElement("a");
    closelink.innerHTML = "close";
    $(closelink).attr("href", "#").click(function() {
        $("#" + context.ResultDivID).hide("fast");
    });
    $(closelink).css({
        "float": "right"
    });
    $(closer).append(closelink);
    $(container).append(list);

    $(container).css({
        "max-height": 150
    });

    $("#" + context.ResultDivID).append(container);
    $("#" + context.ResultDivID).append(closer);
    $("#" + context.ResultDivID).append('<br clear="all"/>');
    $("#" + context.ResultDivID).css({
        "width": $("#" + context.TextBoxID).width(),
        "position": "absolute",
        "left": $("#" + context.TextBoxID).offset().left,
        "top": parseInt($("#" + context.TextBoxID).offset().top) + 20
    });
    if (data.d.length > 0 && data.d[0] != "")
        $("#" + context.ResultDivID).show("fast");
    else
        $("#" + context.ResultDivID).hide();
}

function onPredictiveTextError(error) {
    throw new Error();
}

function ajaxcall3(methodName, jsondata, onsucceeded, onerror, context) {
    $.ajax(
        {
            type: "GET",
            url: methodName,
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: jsondata,
            success: onsucceeded,
            error: onerror,
            context: context
        });
}

function ajaxcall2(methodName, jsondata, onsucceeded, onerror) {
    $.ajax(
        {
            type: "GET",
            url: methodName,
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: jsondata,
            success: onsucceeded,
            error: onerror
        });
}

function ajaxcall1(methodName, onsucceeded, onerror) {
    $.ajax(
        {
            type: "GET",
            url: methodName,
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: onsucceeded,
            error: onerror
        });
}




function ShowAddMedia(id) {

    var windowHeight = $(window).height();
    var windowWidth = $(window).width();
    var objectWidth = $("#" + id).width();
    var objectHeight = $("#" + id).height();
    var newWidth = (windowWidth / 2) - (objectWidth / 2);
    var newHeight = (windowHeight / 2) - (objectHeight / 2);

    $("#" + id).css({
        position: "absolute",
        left: newWidth,
        top: newHeight
    }).show("fast");

    $(window).bind("resize", function() {
        var windowHeight1 = $(window).height();
        var windowWidth1 = $(window).width();
        var objectWidth1 = $("#" + id).width();
        var objectHeight1 = $("#" + id).height();
        var newWidth1 = (windowWidth1 / 2) - (objectWidth1 / 2);
        var newHeight1 = (windowHeight1 / 2) - (objectHeight1 / 2);

        $("#" + id).css({
            position: "absolute",
            left: newWidth1,
            top: newHeight1
        })
    });
}

function RaisePostBack() {

    var controlID = "";

    if (arguments.length > 1) {
        for (var i = arguments.length - 1; i > 0; i--) {
            controlID += arguments[i] + "$";
        }
        controlID += arguments[0];
    }
    else {
        controlID += arguments[0];
    }
    __doPostBack("ctl00_ctl00_ContentPlaceHolder1_ContentPlaceholderMidddle_UpdatePanelMedia", "");
}

function HideAddMedia(id) {
    $("#" + id).hide("fast");
}

function GetUserID() {
    $.ajax({
        type: "POST",
        url: "/Handlers/AjaxHelper.ashx",
        data: "Function=GetUserID",
        success: function(msg) {
            ReturnUserID(msg);
        },
        error: function(e) {

        }
    });
}

//ShowUploadProgress('uploadProgress');

function ShowUpload(arg,field) {
    $("#uploadProgressDiv").css({
        "position": "absolute",
        "z-index": 1000,
        left: 0,
        top: 0,
        width: $(document).width(),
        height: $(document).height(),
        "background-color": "#CCCCCC",
        opacity: 0.6
    }).show();

    $("#fnetvalsummary").css({
        "position": "absolute",
        "z-index": 1001,
        left: ($(window).width() - $("#fnetvalsummary").width()) / 2,
        top: ($(window).height() - $("#fnetvalsummary").height()) / 2
    }).show("fast");

    if (arg != null) {
        $("#" + field).val(arg.toString());
    } 
}

function HideUpload() {

    $("#fnetvalsummary").hide("fast");
    $("#uploadProgressDiv").hide("fast");
}

function ShowUploadProgress() {
    $("#uploadProgressDiv").css({
        "position": "absolute",
        "z-index": 1000,
        left: 0,
        top: 0,
        width: $(document).width(),
        height: $(document).height(),
        "background-color": "#CCCCCC",
        opacity: 0.6
    }).show();

    $("#upoadProgressInner").css({
        "position": "absolute",
        "z-index": 1001,
        width: 600,
        height: 150,
        left: ($(window).width() - 600) / 2,
        top: ($(window).height() - 150) / 2,
        "background-color": "#FCFCFC",
        "border": "#7DA9E4 5px Solid",
        opacity: 1,
        "font-size": 14,
        "font-weight": 600,
        "color": "#7DA9E4"
    }).show("fast");

    $("span", "#upoadProgressInner").css({
        "padding-left":200,
        "padding-top":30
    });
}

function ReturnUserID(userID) {
    $get("UploaderInterface").ReturnUserID(userID);
}

function ShowDialogue(message, title) {
    $(".popup a[id='okbutton']").click(function() {
        $(".popup").hide("fast");
    });

    $(".popup #popuptitle").text(title);

    $(".popup #popupmessage").text(message);

    var windowHeight = $(window).height();
    var windowWidth = $(window).width();
    var objectWidth = $(".popup:first").width();
    var objectHeight = $(".popup:first").height();
    var newWidth = (windowWidth / 2) - (objectWidth / 2);
    var newHeight = (windowHeight / 2) - (objectHeight / 2);

    $(".popup").css({
        position: "absolute",
        left: newWidth,
        top: newHeight,
        "z-index": "100"
    }).show("fast");
}