var Magazine = new Class({
  Implements:[Events,Options],
  flash:null,
  container:null,
  loaded:false,
  ready:false,
  options:{
    demo:false,
    resize:false,
    width:"100%",
    height:"100%",
    debug:false
  },

    ////////////////////////////////////////////
   //    Constructor and instance Methods    //
  ////////////////////////////////////////////

  initialize:function(name,vars,options){
    this.setOptions(options);
    this.vars = vars;
    this.log("Magazine.constructor()");
  },
  setSize:function(width,height){
    this.container.setStyles({
      "width":width,
      "height":height
    });
  },
  setPosition:function(x,y){
    this.container.setStyles({
      "left":x,
      "top":y
    });
  },

    /////////////////////////
   //    Flash Methods    //
  /////////////////////////

  setPage:function(number){
    this.flashMethod("setPage",number);
    this.log("Magazine.setPage("+number+")");
  },
  setZoom:function(mode){
    this.flashMethod("setZoom",mode);
    this.log("Magazine.setZoom("+mode+")");
  },
  setVideo:function(id){
    this.flashMethod("setVideo");
    this.log("Magazine.seVideo()");
  },
  fadeModalIn:function(){
    this.flashMethod("fadeModalIn");
    this.log("Magazine.fadeModalIn()");
  },
  fadeModalOut:function(){
    this.flashMethod("fadeModalOut");
    this.log("Magazine.fadeModalOut()");
  },
  showModal:function(){
    this.flashMethod("showModal");
    this.log("Magazine.showModal()");
  },
  hideModal:function(){
    this.flashMethod("hideModal");
    this.log("Magazine.hideModal()");
  },
  openTab:function(name){
    this.flashMethod("openTab",name);
    this.log("Magazine.openTab("+name+")");
  },
  flashMethod:function(methodname,params){
    return Swiff.remote(this.flash.toElement(),methodname,params);
  },

    //////////////////
   //    EVENTS    //
  //////////////////

  load:function(){ //this Event fires when the flash App has loaded and can recieve commands
    this.fireEvent("load");
    this.log("Magazine.load()");
  },
  preload:function(){ //this Event fires when the flash App has enter it's preload state
    this.fireEvent("preload");
    this.log("Magazine.onInitLoad()");
  },
  magReady:function(){ //this Event fires when the flash App is ready to recieve API instruction
    this.fireEvent("magReady");
    this.log("Magazine.magReady()");
  },

    //////////////////
   // MODAL EVENTS //
  //////////////////

  modalPress:function(){ //this Event fires when themouse button is pressed over the Modal
    this.fireEvent("modalPress");
    this.log("Magazine.modalPress()");
  },
  modalRelease:function(){ //this Event fires when the Mouse button is released over the Modal
    this.fireEvent("modalRelease");
    this.log("Magazine.modalRelease()");
  },
  modalVisible:function(){ //this Event fires when the modal is at maximum opacity
    this.fireEvent("modalVisible");
    this.log("Magazine.modalVisible()");
  },
  modalHidden:function(){ //this Event fires when the modal is completely transparent
    this.fireEvent("modalHidden");
    this.log("Magazine.modalHidden()");
  },

    ///////////////////////
   // NAVIGATION EVENTS //
  ///////////////////////

  page:function(number){
  	this.pageNumber = number;
    this.fireEvent("page",number);
    this.log("Magazine.page("+number+")");
  },
  zoom:function(value){
    this.fireEvent("zoom",value);
    this.log("Magazine.zoom("+value+")");
  },
  video:function(id){
    this.fireEvent("onVideo",id);
    this.log("Magazine.onVideo("+id+")");
  },
  tab:function(name){
    this.fireEvent("tab",id);
    this.log("Magazine.tab("+name+")");
  },

    ///////////////////////
   // STATISTICS EVENTS //
  ///////////////////////

  state:function(object){
    this.state = object;
    this.fireEvent("state",object);
    this.log("Magazine.state()");
  },

  embedVideoPlayer:function(filename,container,width,height){
    this.videoplayer = new Swiff("/panels/player.swf",{
        id: 'VideoPlayer',
        container: container,
        width: width || '100%',
        height: height || '100%',
        params: {
          wmode:"transparent",
          wMode:"transparent",
          scale:"no-scale",
          salign:"TL",
      		swliveconnect:"true"
        },
        vars:{
          file:filename,
          bufferlength:15,
          dock:false,
          autostart:true
        }
    });
  },

    //////////////////////////////////
   //    Make JuJu. Go BoomBoom    //
  //////////////////////////////////

  embedFlash:function(debug){
	$(document.body).setStyle("overflow","hidden");
    if(debug != undefined)this.options.debug = debug;
    this.vars.debug = this.options.debug;

    if(!$chk($("MainContainer"))){
      this.container = new Element("div",{
        "id": "MainContainer",
        "styles":{
          "width": this.options.width,
          "height": this.options.height,
          "left": 0,
          "top": 0,
          "z-index":"-10"
        }
      });
    }

    this.container.inject($(document.body));
	
	if(Browser.Plugins.Flash.version < 8){
		Panel.get("/panels/flashdetection/express.html");
		return;
	}
    
	this.flash = new Swiff('loader.swf',{
      id: 'Application',
      container: 'MainContainer',
      width: '100%',
      height: '100%',
      params: {
        wmode:"transparent",
        wMode:"transparent",
        scale:"no-scale",
        salign:"TL",
    		swliveconnect:"true"
      },
      vars: this.vars,
      callBacks: {
        onLoad:this.load.bind(this),
        onPreload:this.preload.bind(this),
        onMagReady:this.magReady.bind(this),
        onPage:this.page.bind(this),
        onZoom:this.zoom.bind(this),
        onModalPress:this.modalPress.bind(this),
        onModalRelease:this.modalRelease.bind(this),
        onModalVisible:this.modalVisible.bind(this),
        onModalHidden:this.modalHidden.bind(this),
        onDebug:this.log.bind(this),
        onOpenTab:this.openTab.bind(this),
        onState:this.state.bind(this)

      }
    });

    this.fireEvent("onEmbed",this.flash.toElement());
    this.log("Magazine.onEmbed()");
  },

    /////////////////////////////////////////
   //    Console Loggind and debugging    //
  /////////////////////////////////////////

  log:function(log){
    if (window.console!=undefined && this.options.debug) {
		if(window.console.log!=undefined){
			console.log(log);
		}
	}
  }

});
