Helper = {

  jumptvURL: "",
  jumptvSSLEnabled: null,
  languageCode: "",
  parnerCode: "",
  location: "",
  sessionCreateTime: "",

  getSecureJumpTVUrl: function()
  {
    if (this.jumptvSSLEnabled)
      return this.jumptvURL.replace(/http:/, "https:");
    else
      return this.jumptvURL.replace(/https:/, "http:");
  },

  getJumpTVUrl: function()
  {
    return this.jumptvURL.replace(/https:/, "http:");
  },

  getLang: function()
  {
    return this.languageCode;
  },

  getLocation: function()
  {
    return this.location;
  },

  getBrowser: function()
  {

    var str = navigator.userAgent;

    if (str.match(/Firefox\//))
      return 'FF';
    else
      if (str.match(/Opera\//))
        return 'OP';
      else
        if (str.match(/Safari\//))
          return 'SA';
        else
          if (str.match(/Konqueror /))
            return 'KO';
          else
            if (str.match(/MSIE /))
              return 'IE';
            else
              return 'UNKNOWN';
  },


  getOS: function()
  {
    var str = navigator.userAgent;

    if (str.match(/Linux/))
      return 'LX';
    else
      if (str.match(/Windows/))
        return 'MS';
      else
        return 'UNKNOWN';
  },



  refresh: function(url)
  {
    var new_url = '';

    if (typeof url != 'undefined' && url)
      new_url = url;
    else
      new_url = document.location.href;

    new_url = new_url.replace(/#.*/, '');

    document.location.href = new_url;
  },


  getCookie: function(cookieName)
  {
    var theCookie = "" + document.cookie;
    var ind = theCookie.indexOf(cookieName);
    if (ind == -1 || cookieName == "")
      return "";
    var ind1 = theCookie.indexOf(';', ind);
    if (ind1 == -1)
      ind1 = theCookie.length;
    return unescape(theCookie.substring(ind + cookieName.length + 1, ind1));
  },

  setCookie: function(name, value, expires, path, domain, secure)
  {
    var today = new Date();

    today.setTime(today.getTime());

    if (expires) {
      expires = expires * 1000 * 60 * 60 * 24;
    }

    var expires_date = new Date(today.getTime() + (expires));

    document.cookie = name + '=' + escape(value) +
    ((expires) ? ';expires=' + expires_date.toGMTString() : '') + //expires.toGMTString()
    ((path) ? ';path=' + path : '') +
    ((domain) ? ';domain=' + domain : '') +
    ((secure) ? ';secure' : '');
  },

  addGetParam: function(main_url, param_name, param_value)
  {
    var param_separator = '?';
    if (main_url.indexOf('?') > 0)
      param_separator = '&';

    if (typeof(param_value) == 'object' && param_value === null)
      return main_url;

    return main_url + param_separator + param_name + "=" + encodeURIComponent(param_value);
  },

  calculatePopUpPosition: function(width, height, horizontal_align, vertical_align)
  {
    if (horizontal_align != 'center' || vertical_align != 'middle')
      return;


    // Calculate windows size and position
    var window_params = {
      x: 0,
      y: 0,
      width: width,
      height: height
    }

    if (Helper.getBrowser() == 'IE') {
      window_params.x = window.screenLeft + window.document.documentElement.clientWidth / 2;
      window_params.y = window.screenTop + window.document.documentElement.clientHeight / 2;
    }
    else {
      window_params.x = window.screenX + window.innerWidth / 2 + (window.outerWidth - window.innerWidth - 4);
      window_params.y = window.screenY + window.innerHeight / 2 + (window.outerHeight - window.innerHeight - 25);
    }

    window_params.x = parseInt(window_params.x - window_params.width / 2);
    window_params.y = parseInt(window_params.y - window_params.height / 2);

    return window_params;
  },


  getPartnerCode: function()
  {
    return this.parnerCode;
  },


  waiter: function(element)
  {
    $(element).update('<img src="/img/waiter.gif" alt="waiting"/>');
  },

  getElementPosition: function(obj)
  {
    var curleft = 0;
    var curtop = 0;

    obj = $(obj);

    if (typeof obj.offsetParent !== "unknown" && obj.offsetParent) {
      curleft = obj.offsetLeft;
      curtop = obj.offsetTop;
      while (obj = obj.offsetParent) {
        curleft += obj.offsetLeft - (obj.tagName != 'HTML' ? obj.scrollLeft : 0);
        curtop += obj.offsetTop - (obj.tagName != 'HTML' ? obj.scrollTop : 0);
      }
    }
    return {
      left: curleft,
      top: curtop
    };
  },

  riseClickEvent: function(object)
  {
    if (Prototype.Browser.IE) {
      object.fireEvent('onClick');
    }
    else {
      var evt = document.createEvent("MouseEvents");
      evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
      object.dispatchEvent(evt);
    }
  },

  onButtonClick: function(event)
  {
    var target = Event.element(event);

    if (target.tagName != 'A' && target.tagName != 'BUTTON') {
      var tBody = target.parentNode.parentNode;

      var links = tBody.getElementsByTagName('a');

      if (typeof(links[0]) != "undefined") {
        this.riseClickEvent(links[0]);
        return;
      }

      var buttons = tBody.getElementsByTagName('button');

      if (typeof(buttons[0]) != "undefined")
        this.riseClickEvent(buttons[0]);
    }
  },

  detectPlayerType: function()
  {
  	if(this.getCookie('mena_player_type'))
		return;

	if(Player.is_wmp_plugin_installed())
		this.setCookie('mena_player_type', 'application/x-ms-wmp', 30*24*3600*1000, '/');
  },

	populate: function( link, destination, options )
	{
    new Ajax.Updater( destination, link, options );
	},

	selectPaymentOption: function( option )
  {
	  $('paymentOptionsForm').getElements().each(function(element){
	    var parent = $(element).parentNode;
			$( parent ).removeClassName('selected');
	  });

		var parent = $(option).parentNode;

	  $( parent ).addClassName('selected');
  }
};
