/* MooFlow Viewer (extra JS MooFlowViewer.js)
 * v0.1dev useViewer:true
 */

function killTemplate(curId) {
  if ($(curId+'_Elem')) {
    var kk = confirm("Are you sure you want to delete this template?");
    if (kk) {
      new Request({
        url: '/ajax/killCard.php?cardId='+curId+'&rn='+Math.floor(Math.random()*999999999),
        onSuccess: 	function(txt) {
          $(curId+'_Elem').destroy();
        }
      }).send();
    }
  }
}

MooFlow.implement({

	attachViewer: function(){
		return this;
	},

	flowStart: function(){
		this.hideIcon();
		this.clearMyDrags();
	},
	
	flowComplete: function(){
		this.createIconTip();
		this.createIdragger();
	},
	
	createIconTip: function(){
		var cur = this.getCurrent();
		if(!$chk(cur.rel)||!$chk(cur.href)) return;
		this.tipFx = new Fx({link:'cancel'});
		
		if(!$chk(this.icon)) {
			this.icon = new Element('div').addClass('show').setStyles({'display':'none','opacity':'0'}).inject(this.MooFlow);
		}
		this.icon.addEvent('mouseenter', this.showTip.bind(this, cur));
		if(!$chk(this.tip)){
			this.tip 	   = new Element('div').addClass('tooltip').setStyles({'display':'none','opacity':'0'}).inject(this.MooFlow);
		}
		
		myicons  = "<div id='icon_enlarge'>Enlarge</div>";
		if ($('ismyphotos')) {
		  myicons += "<div id='icon_pdelete'>Delete</div>";
		  myicons += "<div id='icon_rotate'>Rotate 90&deg;</div>";
		}
		else if ($('isgraphics')) {
		  myicons += "<div id='icon_pdelete'>Delete</div>";
		  myicons += "<div id='icon_favorites'>&rarr; Favorites</div>";
		}
		else if ($('ismycards')) {
		  myicons += "<div id='icon_create'>Use Card Again!</div>";
		}
		else {
		  myicons += "<div id='icon_create'>Create a Card</div>";
		  myicons += "<div id='icon_favorites'>&rarr; Favorites</div>";
		  if ($('isCMS')) {
		    myicons += '<input type="button" value="x" id="killBtn" />';
		  }
		  if ($('cur_fav_cat_id')) {
			myicons += "<div id='icon_del_favorites'>delete from this category</div>";
		  }
		}
		this.tip.set('html', myicons);
		this.tip.addEvent('mouseleave', this.hideTip.bind(this, cur));
		this.icon.addClass(cur.rel.toLowerCase());
		if (cur.id != "no_cards") {
		  this.icon.setStyle('display','block').fade(0.6);
		}
	},
	
	createIdragger: function(){
//		mooflo_makedrag(this.getCurrent().id);
	},

	clearMyDrags: function(){
//		if (mooflo_last_drag) {
//			mooflo_last_drag.removeEvents('mousedown');
//		}
	},

	showTip: function(cur){
	 	tp = this.icon.getCoordinates().top;
		if ((this.MooFlow) && (!mooflo_full)) {
		  tp = (tp - this.MooFlow.getCoordinates().top);
		}
		tp = (tp - 70); // offset
		this.tip.setStyles({'top':(tp - 50), 'display':'block'});
		this.tipFx = new Fx.Morph(this.tip,{link:'cancel'}).start({'opacity': 0.8, 'top': tp});
		
	    $$('.mf div.tooltip div').each(function(item){
	    	item.removeEvents();
		  	item.addEvent('mouseenter', function(e) {
	  			item.setStyle('color','#95BFFE');
		  	});
		  	item.addEvent('mouseleave', function(e) {
	  			item.setStyle('color','#FFF');
		  	});
	    });
	    
	    $('icon_enlarge').addEvent('click', function(e) {
	    	Slimbox.open(cur.href,cur.title);
	    });

		if ($('icon_create')) {
	      $('icon_create').addEvent('click', function(e) {
	        if ($('ismycards')) {
	          resend_card(cur.id);
	        }
	        else {
	          load_card_template(cur.id)
	        }
	      });
	    }

		if ($('killBtn')) {
		  $('killBtn').addEvent('click',function(e) {
		    killTemplate(cur.id);
		  });
		}

		if ($('cur_fav_cat_id')) {
		    $('icon_del_favorites').addEvent('click', function(e) {
		    	var kk = confirm("Are you sure you want to delete this card from this My Favorites Category?");
		    	if (kk == true) {
		    	  window.location = "?action=del_fav_card&card_id="+cur.id+"&favorite_category_id="+$('cur_fav_cat_id').value;
		    	}
		    });
		}

		if ($('icon_favorites')) {
	      $('icon_favorites').addEvent('click', function(e) {
	        if ($('isgraphics')) {
	    	  var rn = Math.floor(Math.random()*99999999);
		      MOOdalBox_I.open(
		        "/ajax/cms_graphics_category.php?rn="+rn+"&id="+cur.id,
		        "EDIT GRAPHIC CATGORY",
		        "400 250"
		      );
	        }
	        else {
	    	  var rn = Math.floor(Math.random()*99999999);
		      MOOdalBox_I.open(
		        "/ajax/save_to_favorites.php?rn="+rn+"&card_id="+cur.id,
		        "SAVE CARD TO FAVORITES",
		        "400 250"
		      );
		    }
	      });
	    }

		if ($('icon_pdelete')) {
		    $('icon_pdelete').addEvent('click', function(e) {
		    	vvv = "photo";
		    	if ($('isgraphics')) {
		    	   vvv = "graphic";
		    	}
		    	var kk = confirm("Are you sure you want to delete this "+vvv+"?");
		    	if (kk == true) {
		    	  window.location = "?action=delete_photo&photo_id="+cur.id+"&cat_id="+$('cat_id').value;
		    	}
		    });
		}

		if ($('icon_rotate')) {
		    $('icon_rotate').addEvent('click', function(e) {
	    	  dim_screen("Rotating your photo 90&deg;<br />This sometimes takes a few seconds.<br />...ok sometimes several.");
		      var myAjax = new Request.JSON({
		    	url:		'ajax/rotate_photo.php',
		    	data:		'&action=rotate_photo&photo_id='+cur.id+'&cat_id='+$('cat_id').value,
		    	onSuccess:	function(arsp, html) {
    			  if (arsp.status == "SUCCESS") {
				    window.location = '?photo_id='+arsp.photo_id+'&cat_id='+$('cat_id').value;
		    	  }
		    	  else {
		    	    alert('Error completing operation!');
		    	  }
		    	},
		    	onFailure: 	function() {
		    	  alert('Operation Failed!');
		    	}
		      }).get();
	    	  
		    });
		}

	},
	
	hideTip: function(cur){
	 	tp = this.icon.getCoordinates().top;
		if ((this.MooFlow) && (!mooflo_full)) {
		  tp = (tp - this.MooFlow.getCoordinates().top);
		}
		tp = (tp - 70); // offset
		this.tipFx.start({'opacity': 0, 'top': (tp - 50)});
	},

	hideIcon: function(){
		if($chk(this.icon) && $chk(this.icon.getParent())) this.icon.destroy();
		if($chk(this.tip) && $chk(this.tip.getParent())) this.tip.destroy();
		this.icon = this.tip = null;	
	},
	
	onClickView: function(cur){
		switch (cur.rel.toLowerCase()){
			case 'image':
//			this.loadImage(cur);
			Slimbox.open(cur.href,cur.title);
			break;
			case 'link':
			window.open(cur.href, cur.target || '_blank');
			break;
			default:
		}
	},
	
	loadImage: function(cur){
		this.icon.removeClass('image').addClass('viewerload');
		this.image = new Asset.image(cur.href, {onload: this.showImage.bind(this)});
	},
	
	showImage: function(){
		var cur = this.getCurrent();
		var c = cur.con.getFirst().getCoordinates();
		this.image.inject(document.body);
		this.image.addEvent('click', this.hideImage.bind(this));
		this.image.setStyles({'left':c.left,'top':c.top,'width':c.width,'height':c.height,'position':'absolute','z-index':'103'});
		var imageFx = new Fx.Morph(this.image, {transition: Fx.Transitions.Sine.easeOut});
		var to = {x: this.image.get('width'), y: this.image.get('height')};
		var box = document.getSize(), scroll = document.getScroll();
		var pos = {x: scroll.x + ((box.x - to.x) / 2).toInt(), y: scroll.y + ((box.y - to.y) / 2).toInt() };
		var vars = {left: pos.x, top: pos.y, width: to.x, height: to.y};
		imageFx.start(vars);
	},
	
	hideImage: function(){
		this.icon.removeClass('viewerload').addClass('image');
		this.image.dispose();
	}
});

