








var EPJSON = new Object();
var EPBANNER = new Object();
var EPFILTER = new Object();

EPJSON.login = '';
EPJSON.passHash = '';
EPJSON.language = 'ru';
EPJSON.codepage = 'UTF8';
EPJSON.windowSize = 340;
EPJSON.mvObj = null;

EPJSON.isOsel = false;

EPJSON.callServer = function (callMethod, callBackFunc, data) {
  var d = (typeof data == 'string') ? data : $(data).serializeArray();

  var entryURL = "http://ws.expopromoter.local/json/v1/?callMethod=" + callMethod +
                 "&callBackFunc=?" +
                 "&login=" + this.login +
                 "&passHash=" + this.passHash +
                 "&data=" + d +
                 "&lang=" + this.language +
                 "&cp=" + EPJSON.codepage +
                 "&rnd=" + Math.random()*1000000;

  $.getJSON(entryURL, callBackFunc);
}

// Callback functions

EPJSON.ScheduleCallback = function(result) {
  switch (result.callMethod) {
    case "delExhibitionFromSchedule":
    case "delCompanyFromSchedule":
    case "delCompanyServiceFromSchedule":
    case "delExpocenterFromSchedule":
      EPJSON.hideMessageWindow();
      window.location.reload(true);
      break;
    default:
      EPJSON.hideMessageWindow();
  }
}




EPJSON.getSubCategoriesList = function(id) {
  if (!document.getElementById('subcategory')) return;
  var data = '{"id":' + id + '}';
  this.callServer('getSubCategoriesList', EPJSON.ExhibitionSubCategoriesCallback, data);
  this.writeUpdatingPopUp();
}

EPJSON.getCountriesList = function(id) {
  var data = '{"id": ' + id + '}';
  this.callServer('getCountriesList', EPJSON.ExhibitionCountriesCallback, data);
  this.writeUpdatingPopUp();
}

EPJSON.getCitiesList = function(id) {
  var data = '{"id": ' + id + '}';
  this.callServer('getCitiesList', EPJSON.ExhibitionCitiesCallback, data);
  this.writeUpdatingPopUp();
}


EPJSON.ExhibitionSubCategoriesCallback = function(result) {
  //No action must be redefined
}

EPJSON.ExhibitionCountriesCallback = function(result) {
  //No action must be redefined
}

EPJSON.ExhibitionCitiesCallback = function(result) {
  //No action must be redefined
}



//Extra functions

EPJSON.writeMessageWindow = function(mess, w) {
  $('#EPMessPopUpContent').html(mess);
  this.alignMessageWindow(w);
  this.showMessageWindow(w);
}

EPJSON.writeInfoWindow = function(mess) {
  $('#EPMessPopUpContent').html(mess);
  this.alignMessageWindow();
  this.showMessageWindow();
}


EPJSON.writeUpdatingPopUp = function() {
  this.writeInfoWindow('<center><i>Обновление...</i></center>');
}


EPJSON.popupLinkHandler = function () {
  EPJSON.hideMessageWindow();
  return true;
}


EPJSON.showMessageWindow = function(width) {
  var w = width ? width : EPJSON.windowSize;
  $("#EPMessPopUp").css('width', w + 'px').fadeIn("fast");
  $("#EPMessPopUpContent a").not("a[onclick]").click(EPJSON.popupLinkHandler);
}


EPJSON.hideMessageWindow = function() {
  $('#EPMessPopUp').hide();
}


EPJSON.initMessageWindow = function() {
  var win_ie_ver = parseFloat(navigator.appVersion.split("MSIE")[1]);

  var dynamic_html = '<div id="EPMessPopUp" style="padding:0; position:absolute; border:4px solid #999; background-color:#efefef; font-size:12px; display:none; width:'+EPJSON.windowSize+'px;"></div>';

  if (win_ie_ver >= 5.5) {
    this.isOsel = true;
    $(dynamic_html).appendTo("body");

    $(window).scroll(EPJSON._alignMessageWindow);
  } else {
    this.isOsel = false;
    $(dynamic_html).appendTo("body");
    $("#EPMessPopUp").css('position', 'fixed');
  }

  $('<div style="text-align:right; background:#999; padding:4px;"><a style="text-decoration:none; font-size:10px; font-weight:bold; color:#efefef;" onclick="EPJSON.hideMessageWindow(); return false;" style="cursor:hand;">X</a></div><div id="EPMessPopUpContent" style="padding:8px 10px 10px 10px;"></div>').appendTo('#EPMessPopUp');

  this.mvObj = $('#EPMessPopUp');
}

EPJSON._alignMessageWindow = function() {
  EPJSON.alignMessageWindow(0);
}


EPJSON.alignMessageWindow = function(w) {
  var t = EPJSON.mvObj;

  var ppWidth = w ? w : EPJSON.windowSize;

  if (t) {
    var wHeight = 780;
    var wWidth = $(window).width();

    // alert(wHeight + ':' + wWidth);

    if (EPJSON.isOsel) {
      w_top   = wHeight / 4 + $(window).scrollTop();
      w_right = wWidth / 2 - ppWidth / 2;
    } else {
      w_top   = wHeight / 4;
      w_right = wWidth / 2 - ppWidth / 2;
    }

    t.css('top', w_top + "px");
    t.css('right', w_right + "px");
  }
}

EPJSON.setSizeMessageWindow = function(width) {
  EPJSON.mvObj.width(width);
}

EPJSON.hidePageElementById = function(id) {
  $("#" + id).hide();
}


//Баннера

EPBANNER.data = null;
EPBANNER.lastClickURL = null;
EPBANNER.banners = new Array();

EPBANNER.baseExhibitionUrl = '/Event/lang/ru/';
EPBANNER.baseEventUrl = '/Event/lang/ru/';

EPBANNER.baseSocialOrganisationsUrl = '/SocialOrganisations/lang/ru/';
EPBANNER.baseExpoCentersUrl = '/ExpoCenters/lang/ru/';
EPBANNER.baseServiceCompaniesUrl = '/ServiceCompanies/lang/ru/';

EPBANNER.show = function(data, bannerId, elementID) {
  var dataOut = '';
  var i = 0;

  if (data.exhibition) {
    exhib = data.exhibition;
    url = this.getGoUrl('exhibition', exhib);

    var dstart = exhib.startDate.split('-');
    var dend   = exhib.endDate.split('-');
    dstart = 'ru' == 'ru' ? (dstart[2] + '.' + dstart[1]) : (dstart[1] + '/' + dstart[2]);
    dend   = 'ru' == 'ru' ? (dend[2] + '.' + dend[1] + '.' + dend[0]) : (dend[1] + '/' + dend[2] + '/' + dend[0]);

    ds = dstart[0] + dstart[1] + dstart[2];
    de = dend[0] + dend[1] + dend[2];

    dataOut = '<td width="140" style="white-space:nowrap;"><div class="date"><strong>' + dstart + " - " + dend +
      '</strong></td><td class="body"><h3><a href="' + url + '">' + exhib.name +
      '</a></h3></td><td width="120" style="white-space:nowrap;"><div class="location">' + exhib.countryName + ' | ' + exhib.cityName + '</div></td>' +
      '<td style="cursor:pointer; width:15px;" align="center" onclick="EPJSON.addExhibitionToCalendars(' + exhib.id + ', \'' + exhib.name + '\', \'' + ds + '\', \'' + de + '\', \'' + exhib.countryName + ', ' + exhib.cityName + '\'); return false;"><img src="/images/icon_r4.gif" alt="Add to calendar" /></td>';

    if ($("#" + elementID).length) {
      $("#" + elementID).html(dataOut);
    }
  }
}

EPBANNER.getGoUrl = function(type, data) {
  var url = data.url + "_dummy?go=http://" + document.location.hostname;

  switch (type) {
    case "exhibition":
      url += EPBANNER.baseExhibitionUrl + 'id/' + data.id + '/';
      break;
  }

  return url;
}

EPBANNER.redirectToTarget = function(data) {
  EPJSON.hideMessageWindow();

  window.location.href = EPBANNER.lastClickURL;
}





// Flash
function AC_AddExtension(src, ext)
{
  if (src.indexOf('?') != -1)
    return src.replace(/\?/, ext+'?');
  else
    return src + ext;
}

function AC_Generateobj(objAttrs, params, embedAttrs)
{
  var str = '<' + 'object ';
  for (var i in objAttrs)
    str += i + '="' + objAttrs[i] + '" ';
  str += '>';
  for (var i in params)
    str += '<param name="' + i + '" value="' + params[i] + '" /> ';
  str += '<' + 'embed ';
  for (var i in embedAttrs)
    str += i + '="' + embedAttrs[i] + '" ';
  str += ' ></embed></object>';

  document.write(str);
}

function AC_FL_RunContent(){
  var ret =
    AC_GetArgs
    (  arguments, ".swf", "movie", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
    , "application/x-shockwave-flash"
    );
  AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}

function AC_SW_RunContent(){
  var ret =
    AC_GetArgs
    (  arguments, ".dcr", "src", "clsid:166B1BCA-3F9C-11CF-8075-444553540000"
    , null
    );
  AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}

function AC_GetArgs(args, ext, srcParamName, classid, mimeType){
  var ret = new Object();
  ret.embedAttrs = new Object();
  ret.params = new Object();
  ret.objAttrs = new Object();
  for (var i=0; i < args.length; i=i+2){
    var currArg = args[i].toLowerCase();

    switch (currArg){
      case "classid":
        break;
      case "pluginspage":
        ret.embedAttrs[args[i]] = args[i+1];
        break;
      case "src":
      case "movie":
        args[i+1] = AC_AddExtension(args[i+1], ext);
        ret.embedAttrs["src"] = args[i+1];
        ret.params[srcParamName] = args[i+1];
        break;
      case "onafterupdate":
      case "onbeforeupdate":
      case "onblur":
      case "oncellchange":
      case "onclick":
      case "ondblClick":
      case "ondrag":
      case "ondragend":
      case "ondragenter":
      case "ondragleave":
      case "ondragover":
      case "ondrop":
      case "onfinish":
      case "onfocus":
      case "onhelp":
      case "onmousedown":
      case "onmouseup":
      case "onmouseover":
      case "onmousemove":
      case "onmouseout":
      case "onkeypress":
      case "onkeydown":
      case "onkeyup":
      case "onload":
      case "onlosecapture":
      case "onpropertychange":
      case "onreadystatechange":
      case "onrowsdelete":
      case "onrowenter":
      case "onrowexit":
      case "onrowsinserted":
      case "onstart":
      case "onscroll":
      case "onbeforeeditfocus":
      case "onactivate":
      case "onbeforedeactivate":
      case "ondeactivate":
      case "type":
      case "codebase":
        ret.objAttrs[args[i]] = args[i+1];
        break;
      case "width":
      case "height":
      case "align":
      case "vspace":
      case "hspace":
      case "class":
      case "title":
      case "accesskey":
      case "name":
      case "id":
      case "tabindex":
        ret.embedAttrs[args[i]] = ret.objAttrs[args[i]] = args[i+1];
        break;
      default:
        ret.embedAttrs[args[i]] = ret.params[args[i]] = args[i+1];
    }
  }
  ret.objAttrs["classid"] = classid;
  if (mimeType) ret.embedAttrs["type"] = mimeType;
  return ret;
}





var default_country = 0;

EPJSON.targetElement = false;



EPJSON.ExhibitionSubCategoriesCallback = function(result) {
  var resLen = result.data[0].data.length;
  var obj = EPJSON.targetElement;

  if (!obj) return;

  if (resLen > 1) {
    obj.disabled = false;
    obj.options.length = 0;
    obj.options[0]=new Option('- select subcategory -');
    obj.options[0].value = '';

    for (i=0; i < resLen; i++) {
      obj.options[i+1]=new Option(result.data[0].data[i].name);
      obj.options[i+1].value = result.data[0].data[i].id;
    }
  } else {
    obj.options.length = 0;
    obj.options[0]=new Option('- select category first -');
  }

  EPJSON.hideMessageWindow();
}



EPJSON.ExhibitionCountriesCallback = function(result) {
  var resLen = result.data.length;
  var obj = EPJSON.targetElement;

  if (!obj) return;

  if (resLen > 1) {
    obj.disabled = false;
    obj.options.length = 0;
    obj.options[0]=new Option('- select country -');
    obj.options[0].value = '';

    for (i=0; i < resLen; i++) {
      obj.options[i+1]=new Option(result.data[i].name);
      obj.options[i+1].value = result.data[i].id;
    }
  } else {
    obj.disabled = true;
    obj.options.length = 0;
    obj.options[0]=new Option('- select country -');
  }

  EPJSON.hideMessageWindow();
}

EPJSON.ExhibitionCitiesCallback = function(result) {
  var resLen = result.data.length;
  var obj = EPJSON.targetElement;

  if (!obj) return;

  if (resLen>0) {
    obj.disabled = false;
    obj.options.length = 0;
    obj.options[0]=new Option('- select city -');
    obj.options[0].value = '';

    for (i=0; i < resLen; i++) {
      obj.options[i+1]=new Option(result.data[i].name);
      obj.options[i+1].value = result.data[i].id;
    }
  } else {
    obj.disabled = true;
    obj.options.length = 0;
    obj.options[0]=new Option('- select city -');
  }

  EPJSON.hideMessageWindow();
}



jQuery.preloadImages = function() {
  for(var i = 0; i < arguments.length; i++) {
    jQuery("<img/>").attr("src", arguments[i]);
  }
}



$(document).ready(
  function () {
    $.preloadImages("/mcard/images/icons.png");

    EPJSON.initMessageWindow();

    $("#filter_form").submit(
      function () {
        return false;
      }
    );

    $("#filter_form select").change(
      function () {
        var url = document.filter_form.url.value;

        if (this.id == "category") {
          $("#subcategory").val('');
        }

        if (this.id == "country") {
          $("#city").val('');
        }

        if (this.id == "year") {
          $("#month").val('');
        }

        if ($("#country").val())  url += 'country/' + $("#country").val() + '/';
        if ($("#city").val())    url += 'city/'    + $("#city").val() + '/';
        if (!$("#country").val() && !$("#city").val()) url += 'country/all/';

        if ($("#category").val())    url += 'category/' + $("#category").val() + '/';
        if ($("#subcategory").val()) url += 'subcategory/' + $("#subcategory").val() + '/';

        if ($("#year").val())  url += 'year/'  + $("#year").val()  + '/';
        if ($("#month").val()) url += 'month/' + $("#month").val() + '/';

        // alert(url);

        location.href = url;
      }
    );


    $("#category1").change(
      function () {
        $("#filter_form")[0].submit();
        return false;
        ///////////////////////////////////////////////////////////////////////////

        var category = $(this).val();

        if (category) {
          $("#subcategory").html('<option value="">- select category first -</option>');

          EPJSON.targetElement = $("#subcategory")[0];
          EPJSON.getSubCategoriesList(category);
        } else {
          $("#subcategory").html('<option value="">- select category first -</option>');
        }
      }
    );

    $("#subcategory1").change(
      function () {
        $("#filter_form")[0].submit();
        return false;
        ///////////////////////////////////////////////////////////////////////////

        var subcategory = $(this).val();

        location.href="/Json/lang/ru/subcategory/%%%/".replace('%%%', subcategory);
        return false;
      }
    );

    $("#country1").change(
      function () {
        $("#filter_form")[0].submit();
        return false;
        ///////////////////////////////////////////////////////////////////////////

        var country = $(this).val();

        location.href="/Json/lang/ru/country/%%%/".replace('%%%', country);
        return false;

        ///////////////////////////////////////////////////////////////////////////

        if (country) {
          EPJSON.targetElement = $("#city")[0];
          EPJSON.getCitiesList(country);
        } else {
          $("#city").html('<option value="">- select country first -</option>');
        }
      }
    );

    $("#city1").change(
      function () {
        $("#filter_form")[0].submit();
        return false;
        ///////////////////////////////////////////////////////////////////////////

        var city = $(this).val();

        location.href="/Json/lang/ru/city/%%%/".replace('%%%', city);
        return false;
      }
    );

  }
);

