// disable background flickering on hover in project selectors
try {
  document.execCommand("BackgroundImageCache", false, true);
}
catch(err) {}

// preload hovered images for projects and initialize event handlers
(function() {

    addEvent(window, "load", function() {

        var betcruise = document.getElementById("proj-betcruise").getElementsByTagName("img")[0];
        (new Image(74, 48)).src = "/i/projects_betcruise_hover.gif";

        betcruise.onmouseover = function() {
            this.src = "/i/projects_betcruise_hover.gif";
        };
        betcruise.onmouseout = function() {
            this.src = "/i/projects_betcruise.gif";
        };

        var betraiser = document.getElementById("proj-betraiser").getElementsByTagName("img")[0];
        (new Image(72, 48)).src = "/i/projects_betraiser_hover.gif";

        betraiser.onmouseover = function() {
            this.src = "/i/projects_betraiser_hover.gif";
        };
        betraiser.onmouseout = function() {
            this.src = "/i/projects_betraiser.gif";
        };

        var betliner = document.getElementById("proj-betliner").getElementsByTagName("img")[0];
        (new Image(60, 48)).src = "/i/projects_betliner_hover.gif";

        betliner.onmouseover = function() {
            this.src = "/i/projects_betliner_hover.gif";
        };
        betliner.onmouseout = function() {
            this.src = "/i/projects_betliner.gif";
        };
    });
})();


var DECOR = {
  NO: 0,
  YES: 1
};
var DECORATED_WINDOW = 0;
var NOT_DECORATED_WINDOW = 1;

window.onload = function() {
  bindGameSelectorEvents();
};


function bindGameSelectorEvents() {

  var gameSelector = document.getElementById("game-selector");
	if ( !gameSelector ) return;

	var games = gameSelector.getElementsByTagName("img");

  for (var i = 0; i < games.length; i++) {

    games[i].onmouseover = function() {
      this.src = this.src.replace("_off", "_on");
    };

    games[i].onmouseout = function() {
      this.src = this.src.replace("_on", "_off");
    };
  }
}


/**
 * Open link in new window
 * @returns true if new window was opened, false if not (maybe pop-up blocker?)
 * @param {string} url address of page in new window
 * @param {enumerated} decoration decorated window or not (must be DECOR.YES or DECOR.NO)
 * @param {int} width default browser width if not specified
 * @param {int} height default browser height if not specified
 * @param {string} container name of the window, in which opens specified url. if empty, will open brand new window w\o name
 * @param {object} winconfig additional parameters for new window decoration
 */
function openWin(url, decoration, width, height, container, winconfig) {

  if ( !url ) return true;

  if ( container != 'mail' && container != 'chat' && container != 'history' )
    container = '_blank';

  // Check winconfig for default parameters
  if ( winconfig ) {

    if ( winconfig['statusbar'] != 'no' )
      winconfig['statusbar'] = 'yes';

    if ( winconfig['resizable'] != 'no' )
      winconfig['resizable'] = 'yes';

    if ( winconfig['scrollbars'] != 'yes' )
      winconfig['scrollbars'] = 'no';
  }
  else
    winconfig = {'statusbar': 'yes', 'resizable': 'yes', 'scrollbars': 'no'};

  var new_win = null;

  if ( decoration == DECOR.YES ) {

    new_win = window.open(url);
  }
  else if ( decoration == DECOR.NO ) {

    var win_properties = '';

    if ( winconfig )
      for ( var property in winconfig )
        if ( winconfig[property] == 'yes' || winconfig[property] == 'no' )
          win_properties += ',' + property + '=' + winconfig[property];

    if ( width )
      win_properties += ',width=' + width;
    if ( height )
      win_properties += ',height=' + height;

    if ( url.match(/(gif|jpg|jpeg|png)$/) ) {

      new_win = window.open('', container, win_properties, false);
      new_win.document.write('<body style="margin: 0; padding: 0;">');
      new_win.document.write('<img src="' + url + '">');
      new_win.document.write('</body>');
      new_win.document.close();
    }
    else {

      new_win = window.open(url, container, win_properties, false);
    }
  }

  if ( new_win ) {
    new_win.focus();
    return false;
  }
  else {
    return true;
  }
}


/*
** Browser incompatibilities and verbosity removed
*/

function addEvent(target, event, handler) {
    if (target.addEventListener) {
        target.addEventListener(event, handler, false);
    }
    else if (target.attachEvent) {
        target.attachEvent('on' + event, handler);
    }
}

function removeEvent(target, event, handler) {
    if (target.removeEventListener) {
        target.removeEventListener(event, handler, false);
    }
    else if (target.detachEvent) {
        target.detachEvent('on' + event, handler);
    }
}

function getCookie(cookieName) {
    var allCookies = document.cookie;

    var cookiePos = allCookies.indexOf(cookieName + '=');
    if (cookiePos == -1)
        return null;

    var valueStart = cookiePos + cookieName.length + 1;

    var valueEnd = allCookies.indexOf(';', valueStart);
    if (valueEnd == -1)
        valueEnd = allCookies.length;

    var value = decodeURIComponent(allCookies.substring(valueStart, valueEnd));

    return value;
}

var XMLHttpFactories = [
  function () {return new XMLHttpRequest();},
  function () {return new ActiveXObject("Msxml2.XMLHTTP");},
  function () {return new ActiveXObject("Msxml3.XMLHTTP");},
  function () {return new ActiveXObject("Microsoft.XMLHTTP");}
];

function createXMLHTTPObject() {
    var xmlhttp = false;
    for (var i=0;i<XMLHttpFactories.length;i++) {
        try{
            xmlhttp = XMLHttpFactories[i]();
        }
        catch (e) {
            continue;
        }
        break;
    }
    return xmlhttp;
}

/*
** old functions
*/
function showURL(url,name,str) {
	ph = window.open(url,name,str);
	ph.focus();
	return false;
}
