var pic = 1;
var doLft = 0;
var doRgt = 0;
var go = null;
var goHndl = null;
var actv = null;

$(document).ready(function() {

  $('.mn, .smn').mouseenter(function(){
    var id = $(this).attr('id');
    if (!$(this).hasClass(id+'s')) $('#'+id+'l').removeClass('hide');
  }).mouseleave(function(){
    var id = $(this).attr('id');
    if (!$(this).hasClass(id+'s')) $('#'+id+'l').addClass('hide');
  });

  $('#bg').height(Math.max($(window).height(), 1000)).width(Math.max($(window).width(), 799));

  $("#earL").click(function(){
    if (doLft != 0 || doRgt != 0) return;
    doLft = 1;
    var nxt = pic - 1 > 0 ? pic - 1 : totalGames;

    var ww = $(window).width();
    var hh = $(window).height();

    $("#box"+nxt).css('left', ww);
    $("#box"+pic).animate({left:-ww},'slow', stopAnimatationLeft);
    $("#box"+nxt).animate({left:0},"slow", stopAnimatationLeft);


    setTimeout('$("#cntr"+'+pic+').css("display", "none");$("#logo"+'+pic+').css("display", "none");$("#cntr"+'+nxt+').css("display", "block");$("#logo"+'+nxt+').css("display", "block");', 400);

    pic = nxt;
  });

  $("#earR").click(function(){
    if (doLft != 0 || doRgt != 0) return;
    doRgt = 1;
    var nxt = pic + 1 <= totalGames ? pic + 1 : 1;

    var ww = $(window).width();
    var hh = $(window).height();

    $("#box"+nxt).css('left', -ww);
    $("#box"+pic).animate({left:ww},"slow", stopAnimatationRight);
    $("#box"+nxt).animate({left:0},"slow", stopAnimatationRight);

    setTimeout('$("#cntr"+'+pic+').css("display", "none");$("#logo"+'+pic+').css("display", "none");$("#cntr"+'+nxt+').css("display", "block");$("#logo"+'+nxt+').css("display", "block");', 400);

    pic = nxt;
  });

  $('#screen1,#screen2,#screen3,#screen4').mouseenter(function(){
    $(this).removeClass('sbg1').addClass('sbg2');
  }).mouseleave(function(){
    $(this).removeClass('sbg2').addClass('sbg1');
  });

  $('#play').mouseenter(function(){
    $(this).removeClass('play').addClass('play_o');
  }).mouseleave(function(){
    $(this).removeClass('play_o').addClass('play');
  });

  $('.earL').mouseenter(function(){
    $(this).removeClass('earLn').addClass('earLo');
  }).mouseleave(function(){
    $(this).removeClass('earLo').addClass('earLn');
  });

  $('.earR').mouseenter(function(){
    $(this).removeClass('earRn').addClass('earRo');
  }).mouseleave(function(){
    $(this).removeClass('earRo').addClass('earRn');
  });

  $('.btn').mouseenter(function(){
    if ($(this).hasClass('btn_play')) $(this).removeClass('btn_play').addClass('btn_play_o');
    if ($(this).hasClass('btn_video')) $(this).removeClass('btn_video').addClass('btn_video_o');
  }).mouseleave(function(){
    if ($(this).hasClass('btn_play_o')) $(this).removeClass('btn_play_o').addClass('btn_play');
    if ($(this).hasClass('btn_video_o')) $(this).removeClass('btn_video_o').addClass('btn_video');
  });

  $('#screen1,#screen2,#screen3,#screen4').mouseenter(function(){
    $(this).removeClass('sbg1').addClass('sbg2');
  }).mouseleave(function(){
    $(this).removeClass('sbg2').addClass('sbg1');
  });

  $('#gcont').mousemove(function(e){

    var old = go;
    var h = $('#gcont').height();
    var p = $('#gcont').offset();

    //if(e.pageY - p.top > h / 2) go = 1; else go = -1;
    go = (e.pageY - p.top - h / 2) / 100;

      if (go > -0.3 && go < 0.3) go = 0;
      go = Math.ceil(go * Math.abs(go)  * 2);

    if (old == null) goHndl = setInterval("scroll();", 10);
  }).mouseleave(function(){
    go = null;
    clearInterval(goHndl);
  });

});


$(window).resize(function() {
  $('#bg').height(Math.max($(window).height(), 1000)).width(Math.max($(window).width(), 799));
  for (var i=1; i<=totalGames; i++) {
    if (i != pic) $("#box"+i).css('left', -9000);
  }
});

function scroll() {
    var i = parseInt($('#gline').css('top'));
    var hh = (totalGames - 4) * 90;
    $('#debug').html(hh+ " :: " + i);
    if (i - go > 0) {$('#gline').css('top', 0); return;}
    if (i - go < -hh) {$('#gline').css('top', -hh); return;}
    $('#gline').css('top', i - go);
}
function showEars(i) {

  var p = $('.rel.page').offset();
  $('#earL'+i).css('left', p.left - 110).css('display', 'block');
  $('#earR'+i).css('left', p.left + 821 + 35).css('display', 'block');
}

function scrollbg() {
  $("#earR").click();
  actv = setInterval('$("#earR").click();', 15000);
}

function dbg(val) {
  $("#debug").html(val);
}

function stopAnimatationLeft() {
  doLft++;
  if (doLft == 3) doLft = 0;
}

function stopAnimatationRight() {
  doRgt++;
  if (doRgt == 3) doRgt = 0;
}

function doVideo() {

    //$('#txtCont').css('display', 'none');
    //$('#videoCont').css('display', 'block');


    $f("player", "/img/flowplayer-3.0.7.swf", {

        clip: {
            // use first frame of the clip as a splash screen
            autoPlay: false,
            autoBuffering: true
        }
    });

    $f().play($('#player').get(0).getAttribute("href", 2));
    //$f().play();
    return false;
}

(function ($) {
  $.preLoadImages = function(list,callback) {
    var pic = [], i, total, loaded = 0;

      if ($.isArray(list)) {

        total = list.length;

        for (i=0; i < total; i++) {

          pic[i] = document.createElement('img');
          //pic[i] = new Image();
          $(pic[i]).bind('error load', function (e) {
            if (e.type == 'load') {
              loaded++;
              if (loaded == total) if ($.isFunction(callback)) callback();
            }
            else if (e.type == 'error') {
              if (typeof(console) == 'object') console.log('incorrect url preloading picture!');
            }
          });
          pic[i].src = list[i];
        }
      }

    pic = null;
  };
  /*
  function cnt(e, img) {

    //if (!img) img = e.data;
    //alert(dump(img, 0));
    var s = e.type+' ';

    if(e.type == 'readystatechange' &&  img.readyState === undefined ) { //not for msie
      s += img.readyState;
      $(img).attr('src',$(img).attr('src')); // re-fire load event
    }
    else if (e.type == 'load') {

    }
    alert(s);    
  }
  */
})(jQuery);

function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;

	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";

	if(typeof(arr) == 'object') { //Array/Hashes/Objects
		for(var item in arr) {
			var value = arr[item];

			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				//dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}
