/**
 * @author Ralph Sledge
 * copyright 2009 AccuRadio, LLC
 *
 * @requires jQuery
 * @requires json_parse
 *
 */

var IFRPlayer =
{
    divId: "div_ifr_player"
}

var ChannelForm =
{
    divId: "div_form_channel",
    formId: "form_channel",
    updateStatusId: "span_update_status",
    dropped: false,
    
    // Should be able to do something smarter than these
    jqDivObj: false,
    jqDiv: function()
    {
        if (ChannelForm.jqDivObj == false) {
            jqDivObj = $("#" + ChannelForm.divId);
        }
        return jqDivObj;
    },
    
    jqFormObj: false,
    jqForm: function()
    {
        if (ChannelForm.jqFormObj == false) {
            jqFormObj = $("#" + ChannelForm.formId);
        }
        return jqFormObj;
    },
    
    jqUpdateStatusObj: false,
    jqUpdateStatus: function()
    {
        if (ChannelForm.jqUpdateStatusObj == false) {
            jqUpdateStatusObj = $("#" + ChannelForm.updateStatusId);
        }
        return jqUpdateStatusObj;
    },
    
    setBYOCookie: function(callback)
    {
        $.ajax({
            type: "POST",
            data: ChannelForm.jqForm().serialize(),
            url: '/player/set_byo_cookie/',
            success: callback
        })
    },

    drop: function()
    {
        if (!this.dropped) {
            // Drop the form
            var d = ChannelForm.jqDiv();
            var playerDiv = $("#" + IFRPlayer.divId);
            var addY = parseInt(playerDiv.height()) + 5;
            
            d.animate({
                "top": "+=" + addY
                }, 1200, "easeOutBounce", function() {
                    // Submit the form once the animation is finished
                    ChannelForm.setBYOCookie(function(msg) {
                        ChannelForm.jqForm().submit();
                    });
                }
            );
        } else {
            // Update the form
            ChannelForm.setBYOCookie(function(msg) {
                ChannelForm.updateAlert("Player updated.");
            });
        }
        
        this.dropped = true;
    },
    
    setCellBg: function(o)
    {
        var parentTd = $(o).parent("td");
        if (o.checked) {
            parentTd.css("background-color", "#035281");
        } else {
            var normalBgColor = $("#" + ChannelForm.divId).css("background-color");
            parentTd.css("background-color", normalBgColor);
        }
    },
    
    scanAndSetBg: function()
    {
        var inputs = ChannelForm.jqForm().find("input");
        inputs.each(function() {
            if (this.checked) ChannelForm.setCellBg(this);
        });
    },
    
    updateAlert: function(alertText)
    {
        var u = ChannelForm.jqUpdateStatus();
        u.html(alertText);
        u.show();
        setTimeout("ChannelForm.jqUpdateStatus().fadeOut(2000)", 2000);
    },
    
    setCellAsPlaying: function(o)
    {
        $(o).css('border-color', '#FFFFFF');
    },
    
    setCellAsNotPlaying: function(o)
    {
        var normalBgColor = ChannelForm.jqDiv().css("background-color");
        $(o).css("border-color", normalBgColor);
    },
    
    scanAndSetAsPlaying: function(cnames)
    {
        var jnames = false;
        
        try {
            jnames = json_parse(cnames);
        } catch (e) {}
        
        if (jnames) {
            var labels = ChannelForm.jqForm().find("label");
            labels.each(function() {
                for (var i=0; i<jnames.length; i++) {
                    var t = $(this).parent("td");
                    if (this.innerHTML.indexOf(jnames[i]) > -1) {
                        ChannelForm.setCellAsPlaying(t);
                    } else {
                        ChannelForm.setCellAsNotPlaying(t);
                    }
                }
            });
        }
    }
}