/**
 * Updates the article in the Gallery tab.
 * $(..) is a shortcut to document.getElementById(..)
 * Parameters:
 * imageUrl - The link to the image to render
 * headline - The headline text
 * link - The url to the article
 */
function changeGalleryArticle(imageUrl, headline, description, link)
{
    changeImage(imageUrl);
    setInnerText("gallery-article-link", headline.unescapeHTML().unescapeHTML());
    var z = $("gallery-article-link");
    z.href = link;
    setInnerText("gallery-article-description", description.unescapeHTML().unescapeHTML());
}

/**
 * Changes the image with the id 'mainimage'.
 *
 * Parameters:
 * imageUrl - the URL of the image to display
 */
function changeImage(imageUrl)
{
    document.mainimage.src = imageUrl;
    document.mainimage.width = 320;
    document.mainimage.height = 240;
}

/**
 * Updates the headline/description of a video selected in the carousel.
 * Replaces the text within the 'video-article-link' and 'video-article-description'
 * div id's.
 * 
 * Parameters:
 * -----------
 * videoUrl - The RTSP url to call the netventures player with
 * holdingImageUrl - The image to display in the player before playback.
 *                 Note that the holding image will only be displayed 
 *                 if there is a video to play.
 * headline - The headline text to replace
 * articleUrl - The link to the related article
 * description - The description text to replace
 * hitTrackUrl - The URL to call as a result of the media being selected and 
 *               installed in the media player.  This is typically used to record a 
 *               hit.
 * isBulletinOrWrap - True | False.  If true, then the hyperlink is disabled
 */ 
function updateHeadlineAndPlayVideo(videoUrl, holdingImageUrl, headline, articleUrl, description, hitTrackUrl, isBulletinOrWrap)
{
    cr4PlayContent({
        'url':videoUrl,
        'holdingImg':holdingImageUrl,
        'bDuration':'0',
        'autoStart':'1',
        'propertyName':'BPTV',
        'targetLevel':'VIDEOS.NEWS'
    });
    var z = $('video-article-link');
    
    z.setAttribute("href_bak", articleUrl);
    z.setAttribute("href", articleUrl);
    setInnerText("video-article-link", headline.unescapeHTML().unescapeHTML());
    setInnerText("video-article-description", description.unescapeHTML().unescapeHTML());

    // use ajax call to record hit tracking
    callRequestUrl(hitTrackUrl);

    disableAnchor(z, isBulletinOrWrap);
}

/**
 * Calls the given URL.
 * url - The URL to call
 */
function callRequestUrl(url) {
    var myAjax = new Ajax.Request(
        url,
        {
            method:'post',
            onComplete: showComplete,
            onFailure: showFailure,
            onSuccess: showSuccess
        }
    );
}


/**
 * Called when Ajax.Request completes.
 */
function showComplete(transport, json)
{
	//alert(json ? Object.inspect(json) : "no JSON object");
}

/**
 * Called when Ajax.Request succeeds.
 */
function showSuccess(originalRequest)
{
	//alert('succeeded');
}

/**
 * Called when Ajax.Request fails.
 */
function showFailure(originalRequest)
{
	//alert('failed');
}

/**
 * Sets the inner text of the given element id.
 *
 * Parameters:
 * -----------
 * elementId - The ID of the element to set the inner text
 * text - The text to set on the element with the given id
 */
function setInnerText (elementId, text) 
{
	var element;
	if (document.getElementById) 
	{
		element = $(elementId);
	}
	else if (document.all) 
	{
		element = document.all[elementId];
	}
	if (element) 
	{
		if (typeof element.textContent != 'undefined') 
		{
			element.textContent = text;
		}
		else if (typeof element.innerText != 'undefined') 
		{
			element.innerText = text;
		}
		else if (typeof element.removeChild != 'undefined') 
		{
			while (element.hasChildNodes()) 
			{
				element.removeChild(element.lastChild);
			}
			element.appendChild(document.createTextNode(text));
		}
	}
}

/**
 * This function will disable an anchor tag (ie., remove the href).
 * The disabled anchor href value is placed into the 'href-bak' attribute 
 * of the element node passed in.
 *
 * Parameters:
 * -----------
 * obj - The DOM node to disable (expects an anchor node)
 * disable - boolean TRUE to disable, FALSE to enable/restore
 */ 
function disableAnchor(obj, disable){
  if(disable){
    var href = obj.getAttribute("href");
    if(href && href != "" && href != null){
       obj.setAttribute('href_bak', href);
    }
    obj.setAttribute("href", "#");
    obj.removeAttribute('href');
    obj.style.color="#0094c2";
    document.getElementById("video-article-link").setAttribute("class", "mediaHeaderDisable");
  }
  else{
  	if(obj.attributes['href_bak']) {
    	obj.setAttribute('href', obj.attributes['href_bak'].nodeValue);
    }
    obj.style.color="#0094c2";
    document.getElementById("video-article-link").setAttribute("class", "none");
  }
}

