﻿var m_Splitter ;
var m_RightContainer;
var m_RightContent;
var m_LeftContent;
var m_LeftContainer;
var m_curent_list_item;



function show_propsHeader(obj) {
    var result = '<thead  style="display: fixed;" ><tr>';
    var id = "";
    var first = true;

    for (var i in obj) {
        if (first == true) {
            first = false;
        }
        else {
            result += '<th>' + obj[i] + '</th>';
        }
    }
    return result + '</tr></thead>';
}

function show_props(obj, target) {
    var result='';
    var id = "";
    var first = true;

    for (var i in obj) {
        if (first == true) {
            first = false;
            id = obj[i];
             result +='<tr  id="LItem_' + id.toString() + '" onclick=LoadDetail("' + target + id.toString() + '",' + id.toString() + ') class="tdclickitem" >';
        }
        else {
            result += '<td  >' + obj[i] + '</td>';
        }
    }
    return result + '</tr>';
}


function GetJsonList(obj, target) {
  $.getJSON(obj + '?' + Math.random(), function (data) {
    var table = $('#GenericList');
    if (table != null) {
      table.empty();
      if (data != null) {
        try {
          var tmp = jQuery.parseJSON(data);

          var vAppend = '';
          if (tmp.length > 0) {
            table.append(show_propsHeader(tmp[0]));
          }
          for (x = 1; x < tmp.length; x++) {
            table.append(show_props(tmp[x], target));
          };
          table.tablesorter({});
        }
      catch (err) { m_RightContent.html(err) };
      }

    }
});
SetWindow();
}

function GetMessage() {
    $.ajaxSetup({ async: true });
    $("#message").load('/Home/Message', null);
}

function PostForm(target) {
    $.ajaxSetup({ async: false });
        $.post($('form').attr('action'), $('form').serialize(), function (data) {
          m_RightContent.html(data);
        });

      $('#LItem_' + m_curent_list_item ).css('background-color', '#666666');

      if (target.indexOf('Translation') == -1) {
          LoadList(target);
      }
      else {
          $('#LItem_' + m_curent_list_item).css('background-color', '#666666');
      }
      GetMessage();
}

function Delete(obj, AName) {
    $.ajaxSetup({ async: false });
    $.post(obj);
    LoadList(AName);
    GetMessage();
}

function Load(obj) {
    $("#maincontent").empty();
    $("#maincontent").load(obj + '?' + Math.random(), null);
    GetMessage();
}

function LoadList(obj) {
   m_LeftContent.empty();
   m_LeftContent.load(obj + '?' + Math.random(), null);
   GetMessage();
   
}

function LoadDetail(obj, Aid) {
    m_RightContent.empty();
    m_curent_list_item = Aid;
    if (obj.indexOf('TranslationsLoad') != -1) {
        document.location = obj + '?' + Math.random();
    }
    else {
        m_RightContent.load(obj + '?' + Math.random(), null);
    }
    GetMessage();
}



function SetWindow() {
    m_Splitter = $("#listcontainer_resize");
    m_RightContainer = $("#rightcontent_container");
    m_RightContent = $('#rightcontent');
    m_LeftContent = $('#leftcontent');
    m_LeftContainer = $('.listcontainer_list');
    m_RightContent.height($(window).height() - 102);
  m_RightContainer.css('margin-left', '406px');
  m_RightContainer.width($(window).width() - 406);
  m_RightContainer.height($(window).height() - 102);
  m_Splitter.css('left', 406);
  m_Splitter.height($(window).height() - 100);
  m_LeftContent.width(406);
  m_LeftContent.height($(window).height() - 125);
  m_LeftContainer.height($(window).height() - 125);
}


$(document).ready(function () {
    var resizing = false;
    m_Splitter = $("#listcontainer_resize");
    m_RightContainer = $("#rightcontent_container");
    m_RightContent = $('#rightcontent');
    m_LeftContent = $('#leftcontent');
    m_LeftContainer = $('.listcontainer_list');
    var origWidthFrame = m_LeftContent.height();
    var origPosXGrip = m_LeftContent.width();
    var gripWidth = m_Splitter.height();

    SetWindow();

    $(window).resize(function () {
        SetWindow()
    });

    $('.menuitem').unbind();
    jQuery('.menuitem').click(function () { if (this.id.toString().length > 0) { LoadList(this.id); } });
    $('.menuitemnorm').unbind();
    jQuery('.menuitemnorm').click(function () { if (this.id.toString().length > 0) { Load(this.id); } });


    m_LeftContent.mouseup(function (e) {
        resizing = false;
    });
    m_RightContent.mouseup(function (e) {
        resizing = false;
    });

    //RESIZE PART
    function resizeAll(e) {

        m_RightContainer.width(document.body.clientWidth - (origPosXGrip + (e.pageX - gripWidth)));
        m_RightContainer.css('margin-left', 3 + origPosXGrip + (e.pageX - gripWidth) + 'px');
        m_Splitter.css('left', origPosXGrip + (e.pageX - gripWidth));
        m_LeftContent.width(origPosXGrip + (e.pageX - gripWidth));

    }

    m_RightContent.mousemove(function (e) {
        if (resizing) {
            resizeAll(e);
        }
    });



    m_LeftContent.mousemove(function (e) {
        if (resizing) {
            resizeAll(e);
        }
    });

    m_Splitter.mouseup(function (e) {
        resizing = false;
    });

    m_Splitter.mousedown(function (e) {
        origPosXGrip = m_LeftContent.width();
        gripWidth = e.pageX;
        resizing = true;
    });

    m_Splitter.mousemove(function (e) {
        if (resizing) {
            resizeAll(e);
        }
    });


});



 
