/*
 *	media-play-wmv.js  - GridNetworks Javascript library supporting 
 *                         the simple playing of media using Windows
 *                         Media Player in both embedded and standalone
 *                         mode.  
 *						
 *	NOTE:  This library is provided both as a "default" 
 *    implementation, used in our "white label" 
 *    download/install/verify/play implementation, and as a start
 *	in developing your own specialized functions to support more 
 *    complex web experiences.
 *	
 *	For most purposes, this library will not require any 
 *    customization or change	beyond assigning the variables defined at 
 *    the top of the file.  For additional information about changing
 *    or adding to these functions, consult the GridNetworks Javascript
 *    API documentation on the GridNetworks Customer Portal.
 *	
 *	
 *	THE FOLLOWING VARIABLES MUST BE CUSTOMIZED ON A PER-CUSTOMER     
 *    BASIS:   
 *	
 *		The "defaultCustomerMediaLibrary" variable is used in the 
 *		one-argument form	of both launchMovie() and playMovie().
 *		If you are mixing media on the same	page or site from 
 *		multiple media libraries, then you'll need the two-argument
 *		form, passing in both the media lookup key and the media
 *		library.  
 *	
 *		In general, each customer account will have an 
 *		"assignedGCRAddress", so this	does not need to be passed
 *		around; it merely needs to be assigned here so it
 *		is properly used in each of the following functions.
 *	
 */

//var defaultCustomerMediaLibrary = 261;
var defaultCustomerMediaLibrary = 441;
var assignedGCRAddress = "web-gcr.grid1.gridnetworks.com";

/* END OF REQUIRED CUSTOMIZATION -- DO NOT CHANGE BELOW THIS LINE WITHOUT A
   GOOD UNDERSTANDING OF THE GRIDNETWORKS JAVASCRIPT API */

// always create our browser object
browser = new GRID.util.Browser();
gridcast = new GRID.media.wmv(browser);

var det = gridcast.detectWmp();
isScriptable = det.scriptable;

testfullscreen();

function launchMovie(mediaFile) 
{
	whiteLabelCheckOrLaunch(defaultCustomerMediaLibrary,mediaFile,false);
}

function launchMovieWLibrary(library,mediaFile) 
{
	whiteLabelCheckOrLaunch(library,mediaFile,false);
}

function playMovie(mediaFile) 
{
	whiteLabelCheckOrLaunch(defaultCustomerMediaLibrary,mediaFile,true);
}

function playMovieWLibrary(library,mediaFile)
{
	whiteLabelCheckOrLaunch(library,mediaFile,true);
}

function whiteLabelCheckOrLaunch(library,mediaFile,embedBool) 
{
	if(gridcast.isGridcastInstalled()) {
		var url = gridcast.getURL(library,assignedGCRAddress,mediaFile);
		if(embedBool == true) {
			playMovieEmbedWMV(url);
		} else {
			playMovieStandaloneWMV(url);
		}
	} else {
		downloadPanel.show();
		var frm = document.getElementById("frmDownload");
		frm.MEDIA_ID.value = mediaFile;
		frm.CONTENT_URL.value = window.location;
	}
}

function playMovieEmbedWMV(url) 
{
	gridcast.setMovieURL(url);
	gridcast.play();
}

function playMovieStandaloneWMV(url) 
{
	gridcast.launchPlayer(url);
}

/*********
 * Utility functions
 */

/*
 * Function which checks to see if the GridCast Connector is installed, 
 * and if not, unhides a block with an "Install Notice", and hides the 
 * embedded player.  If the Connector is installed, it hides the 
 * installation notice, and exposes the embedded player.  This is used 
 * on the SuperbowlAds demo site, for example, to good effect.  
 *
 */

function checkInstall(){

	if (gridcast.isGridcastInstalled()){
		document.getElementById('installNotice').style.display = 'none';
		document.getElementById('player').style.display = 'block';

		var MEDIA_ID = getQueryVariable("MEDIA_ID");
		/*
        if (MEDIA_ID) {
           playMovie(MEDIA_ID);
        }*/
		gridcast.setOptionValue("streamrate","unlimited");
	} else {
		document.getElementById('installNotice').style.display = 'block';
		document.getElementById('player').style.display = 'none';

	}
}


//find an elements position
function findPos(obj) 
{
	var curleft = curtop = 0;
	if (obj.offsetParent) 
	{
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) 
		{
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}


/*
 * Function which detects if you are in firefox, which is useful for 
 * situations where we want to warn about FF-specific issues, such as 
 * needing to close the browser to activate plugins,
 * for example.
 */
function detectFireFox() {
	if(navigator.userAgent.indexOf("Firefox")!=-1){
		var versionindex=navigator.userAgent.indexOf("Firefox")+8
		if (parseInt(navigator.userAgent.charAt(versionindex))>=1) {
			return true;
		} else {
			return false;
		}
	}
}

/*
 * Function which hides or displays alternative HTML elements/blocks 
 * depending upon whether the Windows version is XP or Vista.  This 
 * can be very useful in displaying alternate download or customer
 * support instructions (and screenshots!) based on the viewer's 
 * operating system.  This can also be easily extended to show 
 * alternate content based on a variety of browser/OS combinations.
 *
 */
function showXPorVistaBlocks() {
	var is_vista = navigator.userAgent.match("Windows NT 6.0");
	var is_xp = navigator.userAgent.match("Windows NT 5.1");
	
	if(is_vista != null) {
		document.getElementById('vista').style.display = 'block';
		document.getElementById('xp').style.display = 'none';
	} else {
		document.getElementById('vista').style.display = 'none';
		document.getElementById('xp').style.display = 'block';
	}
}

/*
 * Function which turns on embedded player controls for Windows Media
 * if the plugin available supports it.  The original (and default on 
 * XP)Firefox plugin, for example, does not support scripting and is a 
 * very simple player (as opposed to the new Firefox/Port25 plugin)
 *
 */

function toggleControls()
{
	if(!isScriptable) return;

	var id = document.getElementById("playerStyle");
	if(!id) return;
	var type = id.innerHTML;
	if(type == "none") type = "full";
	else type = "none";
	gridcast.setUIStyle(type);
	id.innerHTML = type;
}


function gofullscreen()
{
	gridcast.setFullScreen(true);
}

function testfullscreen()
{
	var id = document.getElementById("fullScreen");
	if(id)
	{
		if(!browser.is_mac && isScriptable)
		{
			if(gridcast.getStatusCode() == 2)
			{
				//id.innerHTML = "<img src='images/fullscreen.jpg' border='0'>";
				id.innerHTML = "Pantalla completa";
				return;
			}
		}
		id.innerHTML = "";
	}
	if(isScriptable) setTimeout('testfullscreen()', 1000);
}

/* Funciones propias */
function TamVentana() {
  var Tamanyo = [0, 0];
  if (typeof window.innerWidth != 'undefined')
  {
    Tamanyo = [
        window.innerWidth,
        window.innerHeight
    ];
  }
  else if (typeof document.documentElement != 'undefined'
      && typeof document.documentElement.clientWidth !=
      'undefined' && document.documentElement.clientWidth != 0)
  {
 Tamanyo = [
        document.documentElement.clientWidth,
        document.documentElement.clientHeight
    ];
  }
  else   {
    Tamanyo = [
        document.getElementsByTagName('body')[0].clientWidth,
        document.getElementsByTagName('body')[0].clientHeight
    ];
  }
  return Tamanyo;
}
function PantallaCompleta() {
	var id = document.getElementById("fullScreen");
	if(id)
	{
		if(!browser.is_mac && isScriptable)
		{
			if(gridcast.getStatusCode() == 2)
			{
				gofullscreen();
				return;
			} else {
				if(isScriptable) setTimeout('PantallaCompleta()', 1000);
			}
		}
		id.innerHTML = "";
	}
}
