// Cookie to track player
function setPlayerCookie() {
document.cookie="musicplayer=off"
//window.alert(document.cookie);
}

// Detect iframes and jump to main page 
function detectIframe(page) {
	
	theUrl = "http://cantinero.com/index.php?p="
	
	if (top.location.href == window.location.href) {
	 window.parent.location.href = theUrl+page; 
	} 
}

// Fit Win to a minimum size
// From http://www.dotnethero.com/hero/javascript/ImageNewWindow.aspx?nmx=9_1
function fitToWin() {
	
	xtra_offest = 0;
	
	if ((screen.width >= 1200) && (screen.height >= 900))
	{
		xtra_offest = 50;
	}	
	
	if ((screen.width >= 1024) && (screen.height >= 768))
	{	
		if (window.innerWidth){
			iWidth = window.innerWidth;
			iHeight = window.innerHeight;
		}else{
			iWidth = document.body.clientWidth;
			iHeight =document.body.clientHeight;
		}
		
		if ((iWidth < 950) || (iHeight < 625 ))
		{
			iWidth = (950+xtra_offest) - iWidth;
			iHeight = (625+xtra_offest) - iHeight;
			
			window.resizeBy(iWidth, iHeight);
		}
	}
}

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		news_over = newImage("i/news-over.jpg");
		music_over = newImage("i/music-over.jpg");
		videos_over = newImage("i/videos-over.jpg");
		photos_over = newImage("i/photos-over.jpg");
		store_over = newImage("i/store-over.jpg");	
		about_over = newImage("i/about-over.jpg");
		media_over = newImage("i/media-over.jpg");
		link_over = newImage("i/link-over.jpg");
		contact_over = newImage("i/contact-over.jpg");
		preloadFlag = true;
	}
}

function openMusicPlayer() 
{
	var musicPlayerWindow = window.open('/music_player_pop.php','musicPlayerWindow','width=350, height=150');
	if (window.focus) {musicPlayerWindow.focus()}
	
	var popWinW = 350;
	var iFrameW = 250; // Offset for the iframe that launches the player
	
	// Moz
	if((window.screenX))
	{
		xPos = window.screenX - iFrameW;
		musicPlayerWindow.moveTo(xPos,window.screenY);//+(window.outerHeight-window.innerHeight));
	}
	// IE
	if((window.screenLeft > popWinW)) 
	{
		xPos = window.screenLeft - popWinW - iFrameW;
		musicPlayerWindow.moveTo(xPos,parent.window.screenTop);
	}	
	
	return false;
}

function closeMusicPlayer()
{
	var musicPlayerWindow = window.open('/music_player_pop.php','musicPlayerWindow','width=350, height=150');
	musicPlayerWindow.close();
}

// These are the links updated in the control_player.php file that's included on the content iframes

var stopLink='<a href="#" onClick="closeMusicPlayer(); togglePlayerLink();"><span class="player-indicator-on">&#8226;</span> stop music player</a>';
var startLink='<a href="#" onClick="openMusicPlayer(); togglePlayerLink();"><span class="player-indicator-off">&#8226;</span> launch music player<span class="popup"><!--Open a small popup window that lets the music follow you through the site.--></span></a>';

function togglePlayerLink()
{
	// The div & the indicator dot
	elemDiv = document.getElementById('control-player');
	elemIndc = document.getElementById('player-indicator');
	
	if (document.cookie == 'musicplayer=off')
	{ 
		document.cookie='musicplayer=on';
		// StopLink
		elemDiv.innerHTML=stopLink;
	}
	else
	{
		document.cookie='musicplayer=off';
		// StartLink
		elemDiv.innerHTML=startLink;
	}
}

function showPlayerLink()
{		
	elemDiv = document.getElementById('control-player');
	
	if (document.cookie == 'musicplayer=on')
	{ 
		//StopLink
		elemDiv.innerHTML=stopLink;
	}
	else
	{
		document.cookie = 'musicplayer=off'
		// StartLink
		elemDiv.innerHTML=startLink;
	}
}

function toggleOpenerPlayerLink()
{
	// If the player window is closed w/ the browser buttons this updates the link on the parent page
	if(window.opener.frames.musicContent) 
	{ 
		elemDiv = window.opener.frames.musicContent.document.getElementById('control-player'); 
	}
	else
	{
		elemDiv = window.opener.document.getElementById('control-player');
	}			

	document.cookie='musicplayer=off';
	//startLink
	elemDiv.innerHTML=startLink;
}
