function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return null;
}

function toggle_view(element, obj) {
	var el = document.getElementById(element);
	if (el.style.display != 'none') {
		//el.style.display = 'none';
		ChatBox.close();
		obj.innerHTML = 'show';
	}
	else {
		//el.style.display = 'block';
		ChatBox.open();
		obj.innerHTML = 'hide';
	}
	
}

function toggle_display(element) {
	var el = document.getElementById(element);
	el.style.display = (el.style.display == 'none') ? 'block' : 'none';

}

var CardMixin = {
  isCard: true,
  moveTo: function(x, y, z) {
    var ix, iy, iz;
    if(!isNaN(ix = parseInt(x))) {
      this.style.left = ix + "px";
      this._x = x;
    }
    if(!isNaN(iy = parseInt(y))) {
      this.style.top = iy + "px";
      this._y = y;
    }
    if(!isNaN(iz = parseInt(z))) c.style.zIndex = iz;
  },
  mix: function(domElement) {
    return Object.extend($(domElement), this);
  },
  turn: function(side) {
    side = (side == 'front' || side == 'back') ? side : (this.hasClassName('front') ? 'back' : 'front');
    var front = side == 'front';
    var other = front ? 'back' : 'front';
    this.removeClassName(other);
    this.addClassName(side);
    this.image.src = this.deck.images[ front ? this.ordinal : 0 ].src;
    this.title = this.image.alt = (front ? this._label : "???");
  },
  resetDim: function(factor) {
  
    [this, this.image].each(function(o) {
      o.style.width = "";
      o.style.height = "";
      if(factor) {
        var d = Element.getDimensions(o);
        o.style.width =  parseInt(d.width * factor) + "px";
        o.style.height = parseInt(d.height * factor) + "px";
      }
      
    });
  }
}

var CardsDeck = {};

CardsDeck.Base = {
  
  // seeds_en: ["Clubs", "Cups", "Coins", "Swords"],
  seeds: [ "Denari", "Coppe", "Bastoni", "Spade"],
  images: null,
  parent: null,
  imagesPath: null,
  initialize: function(parent, imagesPath, callback) {
    this.parent = parent;
    this.images = [];
    var cards = this;
    this.preloader = new IAPreloader(function() { cards.createDOM() ; });
    if(callback) this.preloader.chainCallback(callback);
    imagesPath = getCookie('card_set');
    this.setImagesPath(imagesPath || "napoletane");
  },
  
  prepareImages: function() {
    if((!this.preloader) || this.preloader.ready) {
      this.preloader = new IAPreloader(function() { this.updateImages(); });
    }
    for(var j = 0; j <= 40; j++) {
      this.images[j] = this.preloader.add(this.imagesPath + '/' + j + '.jpg');
    }
  },
  
  updateImages: function() {
    for(var j = 40; j-- > 0;) {
      c.image.src = this.preloader.images[j].src;
    };
  },
  
  setImagesPath: function(imagesPath) {
    if(this.imagesPath != imagesPath) {
      this.imagesPath = "cards/"+imagesPath;
      this.prepareImages();
    }
  },
  
  getImagesPath: function() {
    return imagesPath;
  },

  preload: function(additionalImages) {
    for(var j = 0; j < arguments.length; j++) {
      this.preloader.add(arguments[j]);
    }
    this.preloader.start();
  },
  
  ord2num: function(o) {
    return o % 10 || 10;
  },
  num2ord: function(num, seed) {
    return num + seed * 10;
  },
  createDOM: function(parent) {
    parent = parent || this.parent;
    if(!parent) return;
    this.parent = $(parent);
  
    for(var j = 0; j <= 40; j++) {
      c = CardMixin.mix(document.createElement("div"));
      c.unselectable = "on";
      c.id = "c" + j;
      c.ordinal = j;
      c.number =this.ord2num(j);
      c.seed = Math.floor((j - 1) / 10);

      c.className = "card " + (j ? "front" : "back");
      c.image = document.createElement("img");
      c.image.src = this.images[j].src;
      
      c.image.unselectable = "on";
      c.appendChild(c.image);
      c.style.visibility = "hidden";
      c.style.zIndex = 40 + j;
      
      if(j)
      {
        switch(c.number)
        {
          case 1:
            c._label = "Asso"; break;
          case 8:
            c._label = "Donna"; break;
          case 9:
            c._label = "Cavallo"; break;
          case 10:
            c._label = "Re"; break;
          default:
            c._label = c.number;
        }
        c.title = c.image.alt = (c._label += " " + this.seeds[c.seed]);
      }
      
      
      this.parent.appendChild(c);
      this[j] = c;
      this.length = j + 1;
      c.deck = this;
    }
    var back = this[0];
    back.style.zIndex = 99;
    var d = back.getDimensions();
    var pd = this.getDimensions();
    this.center = { 
      x: (pd.width - d.width) / 2,
      y: (pd.height - d.height) / 2
    };
    this.cardDimensions = d;
    this.length = 41;

  },
  
  show: function(n, x, y, z) {
    var c = this[n];
    c.moveTo(x, y, z);
    c.style.visibility = "visible";
  },
  
  hide: function(n) {
    this[n].style.visibility = "hidden";
  },
  
  showing: function(n) {
    return c.style.visibility == "visible";
  },
  
  getDimensions: function() {
    return this.parent ? this.parent.getDimensions() : { width: 0, height: 0 };
  },
  
  fxOpts: {
    mode: 'absolute', 
    sync: true,
    queue: { position: 'end', scope: 'cards' }
  },
  
  flock: function(opts) {
    opts = opts || {}; 
   
    var set = ('set' in opts) && $A(opts['set']) || $R(1, 40);
    
    var localOpts = Object.extend(Object.extend({ x: this.center.x, y: this.center.y }, this.fxOpts), opts.local || {});
    var cards = set.map(function(n) { return this[n]; }.bind(this));
    new Effect.Parallel(set.map(function(n) {
      var c = this[n];
      c.style.width = "";
      c.style.height = "";
      return new Effect.Move(c, localOpts);
    }.bind(this)), opts.global);
  }
  

};

CardsDeck.create = function(parent, imagesPath, callback) {
  cd = Object.extend(new Array(), CardsDeck.Base);
  cd.initialize(parent, imagesPath, callback);
  return cd;
}
