/**
 * JavaScript code for the MediaPlayer PHP class
 * @author Chris MacPherson
 * @todo this code should be namespaced correctly 
 */

var popup = null;

/**
 * Tell flash which content to load
 * @param string path URI of item to load
 * @param string title Title of item being displayed
 * @return boolean False on error
 */
function loadTheContent(path, title) {
    
    if (title && $.trim(title) != "") {
        setWindowTitle(title);
    }
    
    var flash;

    if(navigator.appName.indexOf("Microsoft") != -1) {
        flash = window.hdFlash;
    } else {
        flash = window.document.hdFlash;
    }

    if (typeof flash.loadContent == 'function') {
        flash.loadContent(path);
    } else {
        return false;
    }
}

/**
 * Tell flash which content to load in the pusher
 * @param string path URI of item to load
 * @param int selected The position selected item is in pusher
 * @return boolean False on error
 */
function loadPusherContent(path, selected) {
    var flash;

    if(navigator.appName.indexOf("Microsoft") != -1) {
        flash = window.hdFlash;
    } else {
        flash = window.document.hdFlash;
    }

    if (typeof flash.loadPusherContent == 'function') {
        flash.loadPusherContent(path, selected);
    } else {
        return false;
    }
}

function setWindowTitle(title) {
    document.title = title;
}

/**
 * Set a cookie with age for age gate
 * @param int age
 * @return void
 */
function setAgeGate(age)
{
    var expireDate = new Date();
    expireDate.setDate(expireDate.getDate() + 30);
    document.cookie = "sega_agegate=" + escape(age) + ";expires=" + expireDate.toUTCString();
}

/**
 * Helper function to get top of window
 * @return int
 */
function windowTop() {
    return window.screenTop != undefined ? window.screenTop : window.screenY;
}

/**
 * Helper function to get left of window
 * @return int
 */
function windowLeft() {
    return window.screenLeft != undefined ? window.screenLeft : window.screenX;
}

/**
 * Helper function to get width of window
 * @return int
 */
function windowWidth() {
    return typeof window.innerWidth == 'number' ? window.innerWidth : document.body.offsetWidth;
}

/**
 * Helper function to get height of window
 * @return int
 */
function windowHeight() {
    return typeof window.innerHeight == 'number' ? window.innerHeight : document.body.offsetHeight;
}

/**
 * Close the popup window we created
 * @return void
 */
function closePopup()
{
    if(popup != null && false == popup.closed)
    {
        popup.close ();
    }
}

/**
 * Activate a popup window with content found at URI
 * @param uri Where to fetch popup content from
 * @param options Optional attributes used to customise the new window
 * @return boolean
 */
function showPopup(uri, options) {

    if (options === null || options == '') {
        options = Array();
    }

    var popupName = 'name' in options ? options['name'] : 'popup';
    var height = 'height' in options ? options['height'] :'400';
    var width = 'width' in options ? options['width'] :'400';

    // Work out the X, Y offset to align the pop-up centrally in the PARENT WINDOW.
    var top = windowTop() + ((windowHeight() - height) / 2);
    var left = windowLeft() + ((windowWidth() - width) / 2);

    //Check if popup already open and close so we can re-open with correct dimensions
    closePopup();

    // Create a pop-up window for the bookmarking provider
    popup = window.open(uri, 'save_bookmark', 'height=' + height + ',width=' + width + ',top=' + top + ',left=' + left);

    if (window.focus) {
        popup.focus();
    }
    return false;
}

/**
 * Show the social bookmark popup
 * @param string provider Determines the popup content we load
 * @param uri The URI to share, in this case for the media item
 * @return boolean False if error
 */
function socialBookmarkPopup(provider, uri) {

    var location = '';

    // Set defaults
    var height = 400;
    var width = 400;

    switch (provider) {
        case 'twitter':
            location = 'http://twitter.com/home?status=' + escape(uri);
            width = 780;
            height = 390;
            break;
        case 'facebook':
            location = 'http://www.facebook.com/sharer.php?u=' + escape(uri);
            width = 600;
            break;
        case 'delicious':
            location = 'http://www.delicious.com/save?url=' + escape(uri);
            width = 840;
            height = 630;
            break;
        case 'digg':
            location = 'http://www.digg.com/submit?url=' + escape(uri);
            width = 940;
            height = 630;
            break;
        case 'myspace':
            location = 'http://www.myspace.com/index.cfm?fuseaction=postto&u=' + escape(uri);
            width = 440;
            height = 500;
            break;
        default:
            // Invalid provider given
            return false;
    }

    showPopup(location, {'name': 'save_bookmark','height': height, 'width': width});
}

/**
 * Create the Facebook 'like' button iframe and add to document
 * @param string uri The URI to 'like
 * @return void
 */
function createLikeButton(uri) {
    var iframe = '<iframe src="http://www.facebook.com/plugins/like.php?href=' + uri;
    iframe += '&amp;locale=en_EN&layout=box_count&amp;show_faces=false&amp;width=200&amp;';
    iframe += 'action=like&amp;font&amp;colorscheme=dark&amp;height=21"scrolling="no" frameborder="0"';
    iframe += 'allowTransparency="true"style="border:none; overflow:hidden; width:60px; height:65px;">';
    iframe += '</iframe>';

    var hdFlashW = $('#hdFlash').width();
  
    $('#facebookLike').html(iframe);
    $('#facebookLike').css('left', (hdFlashW - $('#facebookLike').width() - 10) +'px');
}

/**
 * Hide the Facebook like button iframe
 * Delay and opacity set to match Flash animation
 * @return void
 */
function hideLikeButton() {
    $('#facebookLike').stop().delay(1000).animate({opacity:0}, 500);
}

/**
 * Show the Facebook like button iframe
 * Opacity set to match Flash animation
 * @return void
 */
function showLikeButton() {
    $('#facebookLike').stop().animate({opacity:1}, 500);
}
