





var opened_playerId = null;
var opened_is_wide;
var bubblecast_video_num;
if (typeof(bubblecast_video_num) == 'undefined') {
	bubblecast_video_num = 0;
}
var bubblecastPlayerMovieURL = 'http://bubble-cast.com:80/quickcast/player.swf';

function bubblecastCode(thumbnailWidth, thumbnailHeight,
		playerWidth, playerHeight, language, anchorId, podcastId) {
    var video_id = podcastId;
    var bubblecastThumbUrl = 'http://bubble-cast.com:80/thumb.html';
    var playerMovieURL = bubblecastPlayerMovieURL;

    var default_width = 475;
    var default_height = 375;
    
    var player_width = default_width;
    var player_height = default_height;
    var thumbnail_width = default_width;
    var thumbnail_height = default_height;
    
    var params_valid = thumbnailWidth > 0 && thumbnailHeight > 0
    		&& playerWidth > 0 && playerHeight > 0;
    if (params_valid) {
    	player_width = playerWidth;
    	player_height = playerHeight;
    	thumbnail_width = thumbnailWidth;
    	thumbnail_height = thumbnailHeight;
    }
    
    var is_wide = player_width > thumbnail_width || player_height > thumbnail_height;
    var is_wide_string = is_wide ? 'true' : 'false';
    
    var thumbnail_type = (thumbnail_width > default_width || thumbnail_height > default_height)
    		? 'o' : 'b';
    
    if (!is_wide) {
    	player_width = Math.max(player_width, thumbnail_width);
    	player_height = Math.max(player_height, thumbnail_height);
    }

    var div_width = player_width;
    var div_height = is_wide ? player_height + 30 : player_height; // 30 px for Close button
    
    var play_button_width = 135;
    var play_button_height = 135;
    var play_button_left = (thumbnail_width - play_button_width) / 2;
    var play_button_top = (thumbnail_height - play_button_height) / 2;
    
    var bubblecast_player_style = is_wide ? "bubblecast_player_wide" : "bubblecast_player";
    var siteId = 'null';
    var ep = '<div class="bubblecast_player_wp" style="width:' + thumbnail_width + 'px;height:'+ thumbnail_height +'px">';
    ep += '<div class="bubblecast_fl_wp"><a href="http://bubble-cast.com/" class="bubblecast_site_link">http://bubble-cast.com</a></div>';
    ep += '<div class="bubblecast_fl_wp_thumb" id="t' + video_id + '_' + bubblecast_video_num + '"><img src="' + bubblecastThumbUrl + '?podcastId=' + video_id + '&type=' + thumbnail_type + '&forceCheckProvider=true" width="' + thumbnail_width + '" height="' + thumbnail_height + '"/><a class="bubblecast_play_btn" style="left: ' + play_button_left + 'px; top: ' + play_button_top + 'px;" onclick="bubblecastShowPlayer(\'' + video_id + '_' + bubblecast_video_num + '\',' + is_wide_string + ');return true;"><img src="' + 'http://bubble-cast.com:80/images/play.png"  alt="Play"/></a></div>';
    var flash_obj = bubblecast_flash_object(player_width, player_height, video_id, bubblecast_video_num, playerMovieURL, siteId, language);
    var flash_div_open = '<div class="' + bubblecast_player_style + '" id="p' + video_id + '_' + bubblecast_video_num + '" style="width: ' + div_width + 'px; height: ' + div_height + 'px;">';
    var flash_div_close;
    if (!is_wide) {
        flash_div_close = '</div>';
    } else {
        flash_div_close = '<div class="bubblecast_ws_close_btn" align="center"><a href="#" onclick="bubblecastHidePlayer(\'' + video_id + '_' + bubblecast_video_num + '\',' + is_wide_string + '); return false">' + "Close" + '</a></div>';
        flash_div_close += '</div>';
    }
    ep += flash_div_open + flash_obj + flash_div_close;
    ep += '</div>';

	appendStylesheetIfNotAppendedYet("bubblecastEmbedStyles", "http://bubble-cast.com:80/css/embedjs.css");
    bubblecastInsertToAnchor(anchorId, ep);
    
    bubblecast_video_num++;
}

function bubblecast_flash_object(width, height, video_id, videoNum,
        playerMovieURL, siteId, languages) {
    var flash_obj = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0"                width="' + width + '" height="' + height + '" id="quickcast' + video_id + '_' + videoNum + '" align="middle">            <param name="allowScriptAccess" value="always" />            <param name="movie" value="' + playerMovieURL + '" />            <param name="flashvars" value="siteId=' + siteId + '&amp;recordEnabled=false&amp;autoPlay=false&amp;isVideo=true&amp;languages=' + languages + '&amp;pluginMode=embed&amp;streamName=' + video_id + '" />            <param name="quality" value="high" />            <param name="allowfullscreen" value="true"/>            <param name="bgcolor" value="#ededed" />                <embed src="' + playerMovieURL + '" quality="high" bgcolor="#ededed" width="' + width + '" height="' + height + '" name="quickcast' + video_id + '_' + videoNum + '" flashvars="siteId=' + siteId + '&amp;recordEnabled=false&amp;autoPlay=true&amp;isVideo=true&amp;languages='  + languages + '&amp;pluginMode=embed&amp;streamName=' + video_id + '" allowfullscreen="true"                       align="middle" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />        </object>';
    return flash_obj;
}

function bubblecastInsertToAnchor(anchorId, code) {
    var elem = document.getElementById(anchorId);
    elem.innerHTML = code;
}

function appendStylesheetIfNotAppendedYet(linkId, href) {
    var linkElem = document.getElementById(linkId);
    if (linkElem == null) {
        var head = getHeadElement();
        linkElem = document.createElement("link");
        linkElem = head.appendChild(linkElem);
        linkElem.setAttribute("type", "text/css");
        linkElem.id = linkId;
        linkElem.setAttribute("rel", "stylesheet");
        linkElem.setAttribute("media", "all");
        linkElem.setAttribute("href", href);
    }
}
function getHeadElement() {
    var head = null;
    var headArr = document.getElementsByTagName("head");
    if (headArr.length == 0) {
        head = document.createElement("head");
        head = document.appendChild(head);
    }
    else {
        head = headArr[0];
    }
    return head;
}

function bubblecastShowPlayer(playerId, is_wide){
	if (opened_playerId && playerId == opened_playerId) {
		// trying to open an already opened player: short circuit
		return;
	}
    if (opened_playerId != null){
        bubblecastHidePlayer(opened_playerId, opened_is_wide);
    }
    
    bubblecastHideFlashes('p' + playerId);

    var telem = document.getElementById('t' + playerId);
    if (!is_wide) {
        bubblecastHideElement(telem);
    }
    var pelem = document.getElementById('p' + playerId);

    if(is_wide){
        pelem.parentNode.removeChild(pelem);
        document.body.appendChild(pelem);
        bubblecastShowElement(pelem);
        bubblecastPositionElementAtScreenCenter(pelem);
    }
    else{
        bubblecastShowElement(pelem);
    }
    
    opened_playerId = playerId;
    opened_is_wide = is_wide;
}

function bubblecastHidePlayer(playerId, is_wide){
	bubblecastShowFlashes();
    var telem = document.getElementById('t' + playerId);
    var pelem = document.getElementById('p' + playerId);
    bubblecastHideElement(pelem);
    if (!is_wide) {
        bubblecastShowElement(telem);
    }
    bubblecastRecreateElement(pelem);
    opened_playerId = null;
}

function bubblecastHideElement(elem) {
    elem.style.display = 'none';
}

function bubblecastShowElement(elem) {
    elem.style.display = 'block';
}

function bubblecastRecreateElement(parentNode) {
    var oldInnerHTML = parentNode.innerHTML;
    parentNode.innerHTML = oldInnerHTML;

    var open = oldInnerHTML.indexOf('<object');
    if (open < 0) {
	   open = oldInnerHTML.indexOf('<OBJECT');
    }
    var close = oldInnerHTML.indexOf('</object>');
    if (close < 0) {
	   close = oldInnerHTML.indexOf('</OBJECT>');
    }
    var objectCode = oldInnerHTML.substring(open, close + '</object>'.length);

    var width = objectCode.match(/\bwidth=\"?(\d+)\D/)[1];
    var height = objectCode.match(/\bheight=\"?(\d+)\D/)[1];
    var videoId = objectCode.match(/\bid=\"?quickcast(\d+)_/)[1];
    var videoNum = objectCode.match(/\bid=\"?quickcast\d+_(\d+)\D/)[1];
    var siteId = objectCode.match(/\bsiteId=(\d*)/)[1];
    var languages = objectCode.match(/\blanguages=([^&]*)/)[1];
    code = bubblecast_flash_object(width, height, videoId, videoNum, bubblecastPlayerMovieURL, siteId, languages);
    var newInnerHTML = oldInnerHTML.substring(0, open) + code + oldInnerHTML.substring(close + '</object>'.length);
    parentNode.innerHTML = newInnerHTML;
}

function bubblecastPositionElementAtScreenCenter(elem) {
    var windowWidth;
    var windowHeight;
    if (window.innerWidth) {
        windowWidth = window.innerWidth;
        windowHeight = window.innerHeight;
    } else if (document.documentElement.clientWidth) {
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }
    var left = Math.round((windowWidth - elem.offsetWidth) / 2);
    var top = Math.round((windowHeight - elem.offsetHeight) / 2);
    if (left < 0) left = 0;
    if (top < 0) top = 0;
    left += Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
    top += Math.max(document.documentElement.scrollTop, document.body.scrollTop);
    elem.style.left = left + 'px';
    elem.style.top = top + 'px';
}

function bubblecastChangeFlashesVisibility(visible, idToIgnore) {
	var els = document.getElementsByTagName('object');
	for (var i = 0; i < els.length; i++) {
	    var el = els[i];
        var elToCheck = el;
        var found = false;
        while (elToCheck.parentNode != null && idToIgnore != null) {
            if (elToCheck.id == idToIgnore) {
                found = true;
                break;
            }
            elToCheck = elToCheck.parentNode;
        }
        if (!found || idToIgnore == null) {
            el.style.display = visible ? 'block' : 'none';
        }
	}
}

function bubblecastShowFlashes() {
	bubblecastChangeFlashesVisibility(true, null);
}

function bubblecastHideFlashes(idToIgnore) {
	bubblecastChangeFlashesVisibility(false, idToIgnore);
}