/*
function mediaAction(dlg, mediaId, title, url) {
    var handleCancel = function() {
        this.cancel();
    }
    var handleSubmit = function() {
        this.submit();
    }


    dlgDialog = new YAHOO.widget.Dialog(dlg+"_"+mediaId, { modal:true, visible:false, width:"350px", fixedcenter:true, constraintoviewport:true, draggable:true });

    dlgDialog.setHeader(title);

    dlgDialog.setBody('<p>Please leave a comment explaining your action</p> \
                         <form name="'+dlg+'" action="'+url+'" method="post"> \
                         <textarea name="userComment" id="userComment"></textarea> \
                         <input type="hidden" name="mediaId" value="'+mediaId+'" /> \
                         <input type="hidden" name="action" value="enableMedia" /> \
                         </form>');

    var listeners = new YAHOO.util.KeyListener(document, { keys : 27 }, {fn:handleCancel,scope:dlgDialog,correctScope:true} );

    dlgDialog.cfg.queueProperty("keylisteners", listeners);
    dlgDialog.cfg.queueProperty("buttons", [ { text:"Submit", handler:handleSubmit },
                                               { text:"Cancel", handler:handleCancel }
                                             ]);

    dlgDialog.render(document.body);

    dlgDialog.cfg.setProperty("postmethod", "form");
    dlgDialog.show();
}
*/

function reportMedia(mediaId) {
    _$('#reportMedia').fetch('/ajax/media/reportForm.php?mediaId=' + mediaId, {
        before: function(el) {
            spinner.show();
        },
        after: function(resp) {
            this.setContent(resp);
            
            spinner.hide();
        }
    });
    
    var reasonGroup = new YAHOO.widget.ButtonGroup({ id: "reasongroup", name: "reason", container:"reasonContainer" });

    reasonGroup.addButtons([

        { label:"Underage", value:"Underage" },
        { label:"Abusive", value:"Abusive" }, 
        { label:"Beastiality", value:"Beastiality" }, 
        { label:"Other TOU Violation", value:"TOU_Violation" }

    ]);

    reasonGroup.addListener("checkedButtonChange", function(e) {
        var checkedReason = this.get("checkedButton");
        submitReport(mediaId, checkedReason.get('value'));
    });
    
    _$('#reportMedia').show();
}

function submitReport(mediaId, reason) {
    _$('#reportMedia').fetch('/ajax/media/report.php?mediaId=' + mediaId + '&reason=' + reason, {
        before: function() {
            spinner.show();
        },
        after: function(resp) {
            spinner.hide();
            
            eval(resp);
            
            killDiv('reportMedia');
        }
    });
}

function rateMedia(mediaId, rating) {
    _$('#ratingContainer').fetch('/ajax/media/rate.php?mediaId=' + mediaId + '&rating=' + rating, {
        before: function() {
            spinner.show();
        },
        after: function(resp) {
            spinner.hide();
            
            this.setContent(resp);
        }
    });
}

function addFavorite(mediaId, loggedIn) {

	if (mediaId == "") {
		return false;
	}
	
	if (loggedIn == 0) {
		alert('You must be logged in.');
	} else {
    
		_$('#modFavorite').fetch('/ajax/media/favorite.php?mediaId=' + mediaId + '&action=add', {
			before: function() {
				//spinner.show(); 
			},
			after: function(resp) {
				//spinner.hide();
				eval(resp);
			}
		});
	}
}

function deleteFavorite(mediaId) {
	if (mediaId == "") {
		return false;
	}
    
    _$().fetch('/ajax/media/favorite.php?mediaId=' + mediaId + '&action=delete', {
        before: function() {
            //spinner.show(); 
        },
        after: function(resp) {
            //spinner.hide();
            eval(resp);
        }
    });
}

function quickFav(mediaId) {
	if (mediaId == "") {
		return false;
	}
    
    _$().fetch('/ajax/media/favorite.php?mediaId=' + mediaId + '&action=add&quick=true', {
        before: function() {
            spinner.show();
        },
        after: function(resp) {
            spinner.hide();
            eval(resp);
        }
    });
}

function removeMedia(mediaId, reason) {
    if (mediaId == "") {
        return false;
    }
    
    _$().fetch('/ajax/media/remove.php?mediaId=' + mediaId + '&reason=' + reason, {
        before: function() {
            spinner.show();
        },
        after: function(resp) {
            spinner.hide();
            eval(resp);
        }
    });
}

/* ====================================================================================== */
/* ajaxPopups - "netflix" style info popups using YUI/DED|Chain - stephen bazemore - 2007 */
/* START ================================================================================ */

YAHOO.widget.ajaxPopups = function() {
    this.triggers = [];
    this.oEls = _$('.ajaxPopup').el;
    this.init();
};

YAHOO.widget.ajaxPopups.prototype.init = function() {
    $D.batch($D.get(this.oEls), this.setup, this, true);
    
    $E.on(this.triggers, 'mouseover', this.show, this, true);
    $E.on(this.triggers, 'mouseout', this.hide, this, true);
    
    this.Popup = new YAHOO.widget.Overlay("ajaxPopupContainer", {visible:false});
    this.Popup.render(document.body);
    this.Popup.cfg.setProperty("visible", false);
}

YAHOO.widget.ajaxPopups.prototype.setup = function(oEl) {
    if (!oEl.getAttribute('rel') || 
        oEl.getAttribute('rel') == null || 
        oEl.getAttribute('rel') == '' ) {
        return;
    }
    
    if (oEl.getAttribute('title')) {
        oEl.removeAttribute('title');
    }
    
    if (oEl.getAttribute('alt')) {
        oEl.setAttribute('alt', '');
    }

    this.triggers.push(oEl);
}

YAHOO.widget.ajaxPopups.prototype.show = function(e) {
    this.curPop  = $E.getTarget(e);
    this.xy = $E.getXY(e);
    
    this.dataUrl = this.curPop.getAttribute('rel');
    
    var that = this;
    
    aTimer = setTimeout(
        function() {
            that.loadData();
        }
    , 500);
}

YAHOO.widget.ajaxPopups.prototype.hide = function() {
    this.Popup.hide();
    window.clearTimeout(aTimer);
}

YAHOO.widget.ajaxPopups.prototype.loadData = function() {
    var that = this;
    
    this.Popup.cfg.setProperty("width", null);
    this.Popup.cfg.setProperty("zIndex", 990);
    this.Popup.cfg.setProperty("visible", true);
    this.Popup.cfg.setProperty("constraintoviewport", true);
    this.Popup.cfg.setProperty("monitorresize", true);
    this.Popup.cfg.setProperty("effect", {effect:YAHOO.widget.ContainerEffect.FADE, duration:0.3});
    
    if (this.xy[0] > ($D.getViewportWidth() / 2) + 300) {
        var popupPosLeft = this.xy[0] - 100;
    } else {
        var popupPosLeft = this.xy[0] + 50;
    }

    var popupPosTop = this.xy[1] - 15;

    this.Popup.cfg.setProperty("x", popupPosLeft);
    this.Popup.cfg.setProperty("y", popupPosTop);
    
    _$().fetch(this.dataUrl, {
        before: function() {
            that.Popup.setBody('<p style="font-weight: bold;">Loading..</p>');
            that.Popup.render();
        },
        after: function(resp) {
            that.aResponse = resp;
            that.animate();
        }
    });
}

YAHOO.widget.ajaxPopups.prototype.animate = function() {
    if (this.xy[0] > ($D.getViewportWidth() / 2) + 300) {
        var popupPosLeft = this.xy[0] - 275;
    } else {
        var popupPosLeft = this.xy[0] + 50;
    }

    var popupPosTop = this.xy[1] - 35;

    this.aPopupXY = [popupPosLeft, popupPosTop];
    
    var that = this;
    
    var attributes = {
        points: {
            to: that.aPopupXY
        },
        width: {
            to: 200,
            unit: 'px'
        },
        opacity: {
            to: 0.8
        }
    };
    
    var shift = new $M('ajaxPopupContainer', attributes, 0.2, YAHOO.util.Easing.easeOut);
    
    shift.onStart.subscribe(
        function() {
            _$('#ajaxPopupContainer').addClass('ajaxPopupMotion');
        }
    );
    
    shift.onComplete.subscribe(
        function() {
            that.Popup.cfg.setProperty("width", "200px");
            that.Popup.cfg.setProperty("x", that.aPopupXY[0]);
            that.Popup.cfg.setProperty("y", that.aPopupXY[1]);
            
            that.Popup.setBody(that.aResponse);
            that.Popup.render();
            
            _$('#ajaxPopupContainer').removeClass('ajaxPopupMotion');                
        }
    );
    
    shift.animate();
}


/* ====================================================================================== */
/* ajaxPopups - "netflix" style info popups using YUI/DED|Chain - stephen bazemore - 2007 */
/* END ================================================================================== */

