﻿var OFFLINE_QUEUE_KEY = "offlineQueue";

function AddedToQueueInit(){
    $jq('#addedtoqueuedialog').dialog({
        autoOpen: false,
        width: 360,
        modal: true, //there is no overlay (.ui-widget-overlay) if modal is false or not set.
        autoResize: true,
        closeText: 'close X',
        open: function (event, ui) {
            addedtoqueuedialog = true;
            if (typeof (hideFlashPlayer) != "undefined")
                hideFlashPlayer();
        },
        close: function (event, ui) {
            addedtoqueuedialog = false;
            if (typeof (showFlashPlayer) != "undefined")
                showFlashPlayer();
        },
        buttons: {
            "Close": function () {
                $jq(this).dialog("close");
            }
        }
    });
}
//This function is used when a user is not logged in, and wants to add
//something to their queue.  Rather than prompt them to login we will
//create a cookie based queue, and merge it with their real queue
//when they login or join
function AddToOfflineQueue(mediaItemId) {
    //first we check if they have an existing offline queue
    var offlineQueueCookie = $jq.cookie(OFFLINE_QUEUE_KEY);
    
    if (offlineQueueCookie != null && offlineQueueCookie.length > 0) {
        //we have an existing offlineQueue cookie, append the mediaItemId to the end
        //alert("Adding to existing queue: " + offlineQueueCookie);
        //first make sure this media id isn't already in the queue
        if (offlineQueueCookie.indexOf(mediaItemId) == -1) 
            offlineQueueCookie += ',' + mediaItemId;
    } else {
        //there is no offlineQueue cookie, so we create a new one
        //alert("creating new queue: " + offlineQueueCookie);
        offlineQueueCookie = mediaItemId;        
    }
    //now we update the cookie with the new addition
    $jq.cookie(OFFLINE_QUEUE_KEY, offlineQueueCookie, { path: '/', domain: '.crackle.com', expires: 365 });
    
    $jq('#addedtoqueuedialog').dialog('open');
}


//View the offline queue - allows for playing and deleting items from the queue
function OfflineQueueInit() {
    //alert('about to show queue: ' + offlineQueueCookie);
    $jq("#viewofflinequeue").dialog({
        autoOpen: false,
        modal: true,
        height: 500,
        width: 600,
        closeText: 'close X',
        open: function (event, ui) {
            var offlineQueueCookie = $jq.cookie(OFFLINE_QUEUE_KEY);
            $jq.ajax({
                url: '/play/panels/ViewOfflineQueue.aspx?queue=' + offlineQueueCookie,
                cache: false,
                success: function (r) {
                    $jq("#viewofflinequeue").html(r);
                    offlinequeuedialog = true;
                    if (typeof (hideFlashPlayer) != "undefined")
                        hideFlashPlayer();
                }
            });
        },
        close: function (event, ui) {
            offlinequeuedialog = false;
            if (typeof (showFlashPlayer) != "undefined")
                showFlashPlayer();
        },
        buttons: {
            "Close": function () {
                $jq(this).dialog("close");
            }
        }
    });	
}
function ViewOfflineQueue() {
    $jq("#viewofflinequeue").dialog('open');
}
//Refresh the offline queue - used after deleting items from the queue
function RefreshOfflineQueue() {

    var offlineQueueCookie = $jq.cookie(OFFLINE_QUEUE_KEY);
    
    $jq.ajax({
        url: '/play/panels/ViewOfflineQueue.aspx?queue=' + offlineQueueCookie,
        cache: false,
        success: function(r){
            $jq("#viewofflinequeue").html(r);
            if(typeof(hideFlashPlayer)!= "undefined")
                hideFlashPlayer();
        }
    });
		
}

function RemoveFromOfflineQueue(mediaItemId) {
    //first we get their offline queue cookie
    var offlineQueueCookie = $jq.cookie(OFFLINE_QUEUE_KEY);
    //alert("removing from existing queue: " + offlineQueueCookie + " - media id " + mediaItemId);
    if (offlineQueueCookie != null && offlineQueueCookie.length > 0) {
        //we have an existing offlineQueue cookie, remove the mediaItemId from the queue
        //alert("removing from existing queue: " + offlineQueueCookie);
        
        //remove the media item id and the following comma if there is one
        offlineQueueCookie = offlineQueueCookie.replace(mediaItemId + ',', '');
        offlineQueueCookie = offlineQueueCookie.replace(mediaItemId, '');
        
        //now save the queue to the cookie again
        $jq.cookie(OFFLINE_QUEUE_KEY, offlineQueueCookie, { path: '/', domain: '.crackle.com', expires: 365 });
    }
    //and reload the offline queue view
    RefreshOfflineQueue();
}

//this function is used to remove the offline queue cookie once a user logs in or joins crackle
function ClearOfflineQueue () {
    //now save the queue to the cookie again
    $jq.cookie(OFFLINE_QUEUE_KEY, null, { path: '/', domain: '.crackle.com', expires: 5 });
    $jq("#viewofflinequeue").dialog("close");
}

function GetOfflineQueue() {
    return $jq.cookie(OFFLINE_QUEUE_KEY);
}


////This is the dialog for the added to offline queue message.
////it has some fancy jquery code to close the dialog when a user clicks
////outside the dialog
//$jq(document).ready(function(){
//    // Dialog
//    $jq('#addedtoqueuedialog').dialog({
//        autoOpen: false,
//        width: 400,
//        modal: true, //there is no overlay (.ui-widget-overlay) if modal is false or not set.
//        buttons: {
//            "Close": function() {
//                $jq(this).dialog("close");
//            }
//        }
//    });

//    //closes the dialog window if the user clicks outside of the window.
//    $jq(".ui-widget-overlay").live("click", function() { $("#addedtoqueuedialog").dialog("close"); } );

//});
