/*
 * Copyright 2010 Matrixonline.hu Ltd.
 * For more information please visit: http://www.matrixonline.hu
 */

function MXTOOLBAR() {
  this.base = 'http://gateway.matrixonline.hu/_external/_toolbar/';
  this.param_jQuery_ver = 132;
  this.variable_timeout_id = null;
  this.toolbarHTML =
    '<div id="mx_toolbar"><div class="mx_toolbar_inner">' +
    '<span class="mx_toolbar_cl"></span>' +
    '<span class="mx_toolbar_cc">' +
    '<a href="javascript:void(window.open(\'http://gateway.matrixonline.hu/\'));" class="mxlogo"></a>' +
    '<span class="mx_toolbar_spacer"></span>' +
    '<a href="javascript:void(window.open(\'http://www.matrixonline.hu/index_index.html\'));" class="mx_toolbar_link">Honlapkészítés, CMS, CRM fejlesztés</a>' +
    '<span class="mx_toolbar_spacer"></span>' +
    '<a href="javascript:void(window.open(\'http://www.webshopisland.com/\'));" class="mx_toolbar_link">Webáruház készítés</a>' +
    '<span class="mx_toolbar_spacer"></span>' +
    '<a href="javascript:void(window.open(\'http://www.dragondjteam.eu/\'));" class="mx_toolbar_link">Rendezvényszervezés, hangosítás</a>' +
    '</span>' +
    '<span class="mx_toolbar_cr"></span>' +
    '<div class="clear"></div></div></div>';
}

MXTOOLBAR.prototype.loadExternalFile = function(filename, filetype) {
  var fileref;
  if(filetype == "js") {
    fileref = document.createElement("script");
    fileref.setAttribute("type","text/javascript");
    fileref.setAttribute("src", filename);
  }
  else if(filetype == "css") {
    fileref = document.createElement("link");
    fileref.setAttribute("rel", "stylesheet");
    fileref.setAttribute("type", "text/css");
    fileref.setAttribute("href", filename);
  }
  if(typeof fileref != "undefined") {
    document.getElementsByTagName("head")[0].appendChild(fileref);
  }
}

MXTOOLBAR.prototype.checkJqueryVersion = function() {
  return parseInt(jQuery.fn.jquery.replace(/\./gi,'').substring(0,3));
}

MXTOOLBAR.prototype.checkIsMobileBrowser = function() {
  var isMobile = false;
  var mobiles_ua = ["iphone","ipod","ipad","series60","symbian","android","windows ce",
                    "blackberry","palm","avantgo","docomo","vodafone","j-phone",
                    "xv6850","htc","lg;","lge","mot","nintendo","nokia","samsung","sonyericsson"];
  var current_ua = navigator.userAgent.toLowerCase();
  for(var i = 0; i < mobiles_ua.length; i++) {
    if(current_ua.match(mobiles_ua[i]) != null){
      isMobile = true;
      break;
    }
  }
  return isMobile;
}

MXTOOLBAR.prototype.setJqueryLoadStatus = function() {
  var _this = this;
  if(typeof jQuery == "function") {
    if(this.checkJqueryVersion() >= this.param_jQuery_ver) {
      clearTimeout(this.variable_timeout_id);
      this.loadToolbar();
    } else {
      this.variable_timeout_id = setTimeout(function() {_this.setJqueryLoadStatus();}, 200);
    }
  } else {
    this.variable_timeout_id = setTimeout(function() {_this.setJqueryLoadStatus();}, 200);
  }
}

MXTOOLBAR.prototype.loadToolbar = function() {
  jQuery.noConflict();
  var isUnsupportedBrowser = ((jQuery.browser.msie) && (jQuery.browser.version.substr(0,1) < 7)) || this.checkIsMobileBrowser();
  if(!isUnsupportedBrowser) {
    this.loadExternalFile(this.base + 'toolbar.css', 'css');
    jQuery('body').toggleClass('mxtoolbar').append(this.toolbarHTML);
    jQuery('div#mx_toolbar').hide().fadeIn(2000);
  }
}

MXTOOLBAR.prototype.init = function() {
  if(typeof jQuery != "function") {
    this.loadExternalFile(this.base + 'jquery-1.4.3.min.js', 'js', 'head');
  } else if(this.checkJqueryVersion() < this.param_jQuery_ver) {
    this.loadExternalFile(this.base + 'jquery-1.4.3.min.js', 'js', 'head');
  }
  this.setJqueryLoadStatus();
}

var MXTOOLBAR_ = new MXTOOLBAR();
    MXTOOLBAR_.init();
