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

	attachViewer: function(ip){
        this.myIP = ip;
		return this;
	},
    
    resetButtons: function() {
            this.hideTip(null);
            this.iconDown.className = "showDown";
  //          this.icon.className = "show";

			this.icon.className = "showUp";
            this.icon.addClass("image");
            this.icon.setStyle('display','block').fade(0.9);
            this.iconDown.addClass("image");
            this.iconDown.setStyle('display','block').fade(0.9);
    },
    
	flowStart: function() {    
		this.fireEvent('pictureInFocus', this.getCurrent());
        if ($chk(this.lastIndexFocus) && this.lastIndexFocus != -1)
            {
            var el = $H(this.getCurrent(this.lastIndexFocus));
            el.div.style.cursor = 'default';
            }
	},
	
	flowComplete: function() {
		this.createIconTip();
		var el = $H(this.getCurrent());
        el.div.style.cursor = 'pointer';
        this.lastIndexFocus = this.index;
	},
	
	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('a').addClass('showUp').setStyles({'display':'none','opacity':'0'}).inject(this.MooFlow);
            this.iconDown = new Element('a').addClass('showDown').setStyles({'display':'none','opacity':'0'}).inject(this.MooFlow);
            this.iconDownClick = false;
//            this.iconUpClick = false;

			this.iconClick = false;
            this.iconDown.className = "showDown";
            this.icon.className = "showUp";
            this.icon.addClass(cur.rel.toLowerCase());
            this.icon.setStyle('display','block').fade(0.9);
            this.iconDown.addClass(cur.rel.toLowerCase());
            this.iconDown.setStyle('display','block').fade(0.9);
            this.icon.addEvent('click', this.onClickViewUp.bind(this, cur));
            this.iconDown.addEvent('click', this.onClickViewDown.bind(this, cur));
            }
//		this.icon.addEvent('mouseenter', this.showTip.bind(this, cur));
//		this.icon.addEvent('mouseleave', this.hideTip.bind(this, cur));
		if(!$chk(this.tip)){ this.tip = new Element('div').addClass('tooltip').setStyles({'display':'none','opacity':'0'}).inject(this.MooFlow); }
		this.tip.set('html', "");
        
	},

	showTip: function(cur){
		this.tip.setStyles({'top':100,'display':'block', 'opacity': 0.8});
//		this.tipFx = new Fx.Morph(this.tip,{link:'cancel'}).start({'opacity': 0.8, 'top': this.icon.getCoordinates().top-this.tip.getSize().y-100});
	},
	
	hideTip: function(cur){
		this.tip.setStyles({'top':100,'display':'block', 'opacity': 0});
//		this.tipFx.start({'opacity': 0, 'top': 100});
	},

	hideIcon: function(){
		if($chk(this.icon) && $chk(this.icon.getParent())) this.icon.destroy();
		if($chk(this.iconDown) && $chk(this.iconDown.getParent())) this.iconDown.destroy();
		if($chk(this.tip) && $chk(this.tip.getParent())) this.tip.destroy();
		this.iconDown = this.icon = this.tip = null;	
	},

	onClickViewDown: function(cur){
        var curNow = this.getCurrent();
		switch (cur.rel.toLowerCase())
            {
			case 'image':
                this.tip.set('html', "Posting 'Dislike' Vote...");
                this.showTip(curNow);
                this.iconDown.className = "showDownNeg";
                this.iconDown.addClass(cur.rel.toLowerCase());
                this.iconDown.setStyle('display','block').fade(0.9);
                this.stop();
                var drawingId = curNow.src.substring(curNow.src.indexOf("id=")+3);
                var ajaxurl = '/coverflow/ajaxserver/vote.php?id='+drawingId+'&ip=' +this.myIP+'&type=dislike&duh='+Math.random();
                loadXMLDoc(ajaxurl);
			break;
			case 'link':
			window.open(cur.href, cur.target || '_blank');
			break;
			default:
		}
	},
	
	onClickViewUp: function(cur){
        var curNow = this.getCurrent();
		switch (cur.rel.toLowerCase()){
			case 'image':
                    this.tip.set('html', "Posting 'Like' Vote...");
                    this.showTip(curNow);
                    this.icon.className = "showUpPos";
                    this.icon.addClass(cur.rel.toLowerCase());
                    this.icon.setStyle('display','block').fade(0.9);
                    this.stop();
                    var drawingId = curNow.src.substring(curNow.src.indexOf("id=")+3);
                    var ajaxurl = '/coverflow/ajaxserver/vote.php?id='+drawingId+'&ip=' +this.myIP+'&type=like&duh='+Math.random();
                    loadXMLDoc(ajaxurl);
			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();
	}
});

