// Clips in page code
// ******************
var CurrentPlayImage;
var timeHandle = -1;
var skipFlag = 0;

function StopPlayer()
{
	if(GE('ClipPlayer') != null)
	{
		GE('ClipPlayer').controls.stop();
		GE('ClipPlayer').settings.setMode("loop", wmplp);
		GE('ClipPlayer').settings.setMode("shuffle", wmpsh);
	}
		
	if(CurrentPlayImage != null)
	{
		var item = CurrentPlayImage;

		var tmp = CurrentPlayImage.children[0].alt;
		tmp = tmp.replace("Stop", "Play");

		CurrentPlayImage.children[0].alt = tmp;
		
		CurrentPlayImage = null;
		clpot(item);
	}
}

function StopPlayerNow()
{
	skipFlag = 1;
	
	if(GE('ClipPlayer') != null)
	{
		GE('ClipPlayer').controls.stop();
		GE('ClipPlayer').settings.setMode("loop", wmplp);
		GE('ClipPlayer').settings.setMode("shuffle", wmpsh);
	}
}

function ClipPlayState(NewState)
{
	if(skipFlag == 1)
		return;
		
	if(timeHandle != -1)
	{
		window.clearTimeout(timeHandle);
		timeHandle = -1;
	}
	
	if(NewState == 3 && CurrentPlayImage != null)
	{
		CurrentPlayImage.children[0].isLoading = false;
		CurrentPlayImage.children[0].src = imgPre + "/bt_stop.gif";

		var tmp = CurrentPlayImage.children[0].alt;
		tmp = tmp.replace("Play", "Stop");

		CurrentPlayImage.children[0].alt = tmp;
	}
	
	if(NewState == 1)
		StopPlayer();
		
	if(NewState == 10 && CurrentPlayImage != null)
	{
		timeHandle = window.setTimeout(StopPlayer, 1000);
	}
}

function CreatePlayer(redir)
{
	if(GE('ClipPlayer') == null)
	{
		try
		{
			var doc = GE("clipSpan");

			var tag = '<object id="ClipPlayer" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" width="0" height="0">';
			tag += '<param name="autoStart" value="false">';
			tag += '<param name="uimode" value="invisible">';
			tag += '<param name="enableContextMenu" value="false">';
			tag += '</object>';
			tag += '<script for="ClipPlayer" event="PlayStateChange(NewState)" language="javascript">ClipPlayState(NewState);</script>';
			tag += '<script for="ClipPlayer" event="error()" language="javascript">StopPlayer();</script>';
			
			doc.innerHTML = tag;
			doc.style.display = 'none';
			
			var idx = -1;
			var ver = GE('ClipPlayer').versionInfo;

			if(ver != null && ver.length > 0)
			{
				idx = ver.indexOf(".");
			}
			else
			{
				throw "wrong version";
			}

			if(idx != -1 && parseInt(ver.substr(0, idx)) < 7)
			{
				throw "wrong version";
			}
		}
		catch(e)
		{
			if(redir == true)
				location.href = "/help/requirements.aspx";
				
			return;
		}
	}
}

var wmplp = false;
var wmpsh = false;

function PlayClip(url, id)
{
	if(CurrentPlayImage.children[0]) 
	{
		CurrentPlayImage.children[0].isLoading = true;
		CurrentPlayImage.children[0].src = imgPre + "/bt_load.gif";
	}

	if(GE('ClipPlayer') == null)
	{
		CreatePlayer(true);
	}
	
	if(!CheckWmp10())
		window.onbeforeunload = StopPlayerNow;

	if(GE('ClipPlayer') != null)
	{
		wmplp = GE('ClipPlayer').settings.getMode("loop");
		wmpsh = GE('ClipPlayer').settings.getMode("shuffle");
		GE('ClipPlayer').settings.setMode("loop", false);
		GE('ClipPlayer').settings.setMode("shuffle", false);
		GE('ClipPlayer').URL = url;
		GE('ClipPlayer').controls.play();
	}
}

function clpov(item)
{
	if(item == CurrentPlayImage)
	{
		if(CurrentPlayImage.children[0].isLoading != true)
			CurrentPlayImage.children[0].src = imgPre + "/bt_stop_o.gif";
	}
	else if(item.children[0]) 
		item.children[0].src = imgPre + "/bt_play_o.gif"; 
}

function clpot(item)
{
	if(item == CurrentPlayImage)
	{
		if(CurrentPlayImage.children[0].isLoading != true)
			CurrentPlayImage.children[0].src = imgPre + "/bt_stop.gif";
	}
	else if(item.children[0])
		item.children[0].src = imgPre + "/bt_play.gif";
}

function clpdn(item)
{
	if(event != null && event.button > 1)
		return;

	if(item == CurrentPlayImage)
	{
		if(CurrentPlayImage.children[0].isLoading != true)
			CurrentPlayImage.children[0].src = imgPre + "/bt_stop_d.gif";
	}
	else if(item.children[0]) 
		item.children[0].src = imgPre + "/bt_play_d.gif";
}

function clpup(item, url, id)
{
	if(event != null && event.button > 1)
		return;

	if(item.clipid == null)
	{
		var len = document.all.length;
		var fnd = false;

		for(var x = 0; x < len; x++)
		{
			var ele = document.all[x];
			
			if(ele.tagName.toLowerCase() == "a" && ele.clipid == id)
			{
				item = ele;
				fnd = true;
				break;
			}
		}
		
		if(fnd == false)
			return;
	}

	if(item == CurrentPlayImage)
	{

		var tmp = CurrentPlayImage.children[0].alt;
		tmp = tmp.replace("Stop", "Play");

		CurrentPlayImage.children[0].alt = tmp;

		GE('ClipPlayer').controls.stop();
		GE('ClipPlayer').settings.setMode("loop", wmplp);
		GE('ClipPlayer').settings.setMode("shuffle", wmpsh);

		CurrentPlayImage = null;
		clpot(item);
		
		return;
	}
	
	if(CurrentPlayImage != null)
	{
		var tmp = CurrentPlayImage;
		var tmps = CurrentPlayImage.children[0].alt;
		tmps = tmps.replace("Stop", "Play");

		GE('ClipPlayer').settings.setMode("loop", wmplp);
		GE('ClipPlayer').settings.setMode("shuffle", wmpsh);

		CurrentPlayImage.children[0].alt = tmps;
		CurrentPlayImage = null;
		clpot(tmp);
	}

	CurrentPlayImage = item;
	clpot(item);
	PlayClip(url, id);
}

// --------------------- // 

function CheckWmp10()
{
	try
	{
		var ver = GetWmpVer();
		var idx = -1;
		
		if(ver != null && ver.length > 0)
			idx = ver.indexOf(".");

		if(idx != -1 && ver.substr(0, idx) == "10")
			return true;
	}
	catch(e)
	{
	}
			
	return false;
}
function GetWmpVer()
{
	try
	{
		var obj = new ActiveXObject("WMPlayer.OCX");

		if(obj != null)
			return obj.versionInfo;
	}
	catch(e)
	{
	}
		
	return null;
}
function InWmp()
{
	try
	{
		if(window.external != null)
			return (typeof(window.external.NavigateTaskPaneURL) != 'undefined');
	}
	catch(e)
	{
	}
			
	return false;
}

// --------------------- //

function Cookie(document, name, hours, path, domain, secure)
{
	this.$document = document;
	this.$name = name;
	
	if( hours ) {
		this.$expiration = new Date((new Date()).getTime() + hours * 3600000);
	} else {
		this.$expiration = new Date(2049, 1, 1, 1, 1, 1, 1);
	}
	
	if( path ) { this.$path = path;	} else { this.$path = "/";	}
	if( domain ) { this.$domain = domain; } else { this.$domain = domain; }
	if( secure ) { this.$secure = true; } else { this.$secure = false; }
}
function _Cookie_store()
{
	var cookieval = "";
	for( var prop in this ) {
		if( (prop.charAt(0) == '$') || ((typeof this[prop]) == 'function') )
			continue;
		if( cookieval != "" )
			cookieval += '&';
		cookieval += prop + '=' + escape(this[prop]);
	}
	var cookie = this.$name + '=' + cookieval;
	if( this.$expiration ) cookie += '; expires=' + this.$expiration.toGMTString();
	if( this.$path ) cookie += '; path=' + this.$path;
	if( this.$domain ) cookie += '; domain=' + this.$domain;
	if( this.$secure ) cookie += '; secure';
	this.$document.cookie = cookie;
}
function _Cookie_load()
{
	var allcookies = this.$document.cookie;
	if( allcookies == "" )
		return false;
	var start = allcookies.indexOf(this.$name + '=');
	if( start == -1 )
		return false;
	start += this.$name.length + 1;
	var end = allcookies.indexOf(';', start);
	if( end == -1 )
		end = allcookies.length;
	var cookieval = allcookies.substring(start, end);
	var a = cookieval.split('&');
	var i = a.length;
	while( i-- )
	{
		if(a[i].indexOf(':') != -1)
			a[i] = a[i].replace(':', '=');
			
		a[i] = a[i].split('=');
	}
	i = a.length;
	while( i-- )
		this[a[i][0]] = unescape(a[i][1]);
	return true;
}
function _Cookie_remove()
{
	var cookie;
	cookie = this.$name + '=';
	if( this.$path ) cookie += '; path=' + this.$path;
	if( this.$domain ) cookie += '; domain=' + this.$domain;
	cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';
	this.$document.cookie = cookie;
}

new Cookie();
Cookie.prototype.store = _Cookie_store;
Cookie.prototype.load = _Cookie_load;
Cookie.prototype.remove = _Cookie_remove;

function GetCkVal(sName)
{
	var aCookie = document.cookie.split("; ");
	
	for (var i=0; i < aCookie.length; i++)
	{
		var aCrumb = aCookie[i].split("=");
		
		if (sName == aCrumb[0]) 
			return unescape(aCrumb[1]);
	}
	
	return null;
}

// --------------------- //

// Buy buttons and logic
// *********************

// buy globals
var frzBuy = false;
var buyImages;
var npbid;

// support script for ImageButtonCtrl
function chkbb()
{
	if(meetsReqs)
	{
        // preload the images
        __PreloadBuyImages();

		var bid = GetCkVal("bbuy");

		if(bid != null && bid.length > 0 && bid != "undefined")
		{
            if( GetCkVal("secauth") != "1" && GetCkVal("secauth") != "2" && NeedSecure())
            {
                // Do secure login - DO NOT CLEAR the Buy cookie
                // Set secauth cookie to 2 to avoid loops when auth is aborted / fails
                expSet = new Date((new Date()).getTime() + (10 * 60 * 1000)).toGMTString();
                document.cookie = "secauth=2;Path=/; expires=" + expSet + "; domain=" + __getDocDomain();
                TrigLogIn("musicstore", true);
            }
            else
            {
                
                if( GetCkVal("secauth") == "2" )
                {   				
                    document.cookie = "secauth=;Path=/; expires=" + expDel + "; domain=" + __getDocDomain();

					if(	!IndefIsLoggedIn() )
					{				
						// If user refused to log in once redirected to	passport sign in page, we should give up
						// with	trying to complete the purchase	- otherwise	user may be	redirected to secured 
						// passport	sign in	page again when	continuing browsing	music pages.
						document.cookie	= "bbuy=;Path=/; expires=" + expDel	+ "; domain=" +	__getDocDomain();
						return;
					}
                }

                if( (GE(bid) != null && GE(bid).pctrl == true && pcontr == true) )
    			{
                    document.cookie = "bbuy=;Path=/; expires=" + expDel + "; domain=" + __getDocDomain();
    				return;
    			}
    			var frmnp = GetCkVal("frmnp");
    			
    			if(GE(bid) != null && (frmnp != null && frmnp.length > 0 && frmnp != "undefined"))
    			{
    				document.cookie = "frmnp=;Path=/; domain=" + __getDocDomain();
    				npbid = bid;
    				__ShowNotifyDialog(GE(bid));
    			}
			else if(IndefIsLoggedIn() && GE(bid) != null)
    			{
    				var top = 0, lft = 0;
    				var ele = GE(bid);	
    				// determine pixel location of the button on the page
    				do {
    					top += ele.offsetTop;
    					lft += ele.offsetLeft;
    								
    					ele = ele.offsetParent;
    				} while (ele != null)
    
    				// only scroll to the button if it's off the page				
    				if(top > document.body.clientHeight)
    					GE(bid).scrollIntoView(true);
    
    				document.cookie = "bbuy=;Path=/;domain=" + __getDocDomain() + ";expires=" + (new Date((new Date()).getTime() - (10 * 60 * 1000)).toGMTString());
    				bc(GE("cn" + bid.substr(2)), GE(bid));
    			}
            }
		}
	}
}

function tk(t,i,p) { return t+":"+i+":"+p;}


function bbi(t, i, p, o)
{
    if(event != null && event.button > 1)
        return;
    // if the user mouses over an image before the onload scripts have run (possible!)
    if (!buyImages) {
        __PreloadBuyImages();
	}
    var k = tk(t,i,p);
    if (!buyImages[k])
    {
        __AddBuyImageToCache(t,i,p);
    }
    if(o == null) 
        event.srcElement.src = buyImages[k].src;
    else 
        o.src = buyImages[k].src;   
}

// preload confirm button images for snappy buy button perf
function __PreloadBuyImages()
{
   if (!buyImages)
   {
        buyImages = new Object();
        // if nohassle is not enabled preload the confirm states
        //if (IndefIsLoggedIn() && noHassle != 1)
        //{
            var pT = ["al","so"];
            var pI = ['up','dn','hv'];
            var pP = ['con'];

            for(var tp in pT)
            {
                var t = pT[tp];

                for(var ti in pI)
                {
                    var i = pI[ti];

                    for(var tp in pP)
                    {
                        var p = pP[tp];
                        __AddBuyImageToCache(t, i, p);
                    }
                }
            }
        //}

        // also add purchased images
        __AddBuyImageToCache('al', 'up', 'pur');
        __AddBuyImageToCache('so', 'up', 'pur');
    }
}

// add an image to the image cache (used by preload and bbi)
function __AddBuyImageToCache(t, i, p)
{
    var k = tk(t,i,p);

    // need to size the image exactly for the preload to be persistent
    if (p == 'can')
        buyImages[k] = new Image(14,14);
    else if (t == 'al')
        buyImages[k] = new Image(89,14);
    else 
        buyImages[k] = new Image(59,14);

    buyImages[k].src = imgPre + "/" + t + "_" + p + "_" + i + ".gif";
}

function bs(s, o) 
{ 
	if(event != null && event.button > 1)
		return;

	if(o == null) 
		event.srcElement.src = s; 
	else 
		o.src = s; 
}

function nbs(s, o) 
{ 
	if(event != null && event.button > 1)
		return;

	if(o == null && s !== null) 
	{
		if(s.complete == true)
			event.srcElement.src = s.src; 
	}
	else if(s !== null)
	{
		if(s.complete == true)	
			o.src = s.src; 
	}		
}

function bc(ele, obj)
{
	return; // FOR NOW ###
	
	if(event != null && event.button > 1)
		return;

	if(frzBuy == true)
		return;

    // IndefIsLoggedIn is not 100% accurate as it relies on a value set in the entertainment page
    // however, it does not require a roundtrip and if the user is not logged in, the buy code can
    // handle this and initiate the login at that point
    var sec = NeedSecure();
	if( !IndefIsLoggedIn() || (GetCkVal("secauth") != "1" && GetCkVal("secauth") != "2" && sec) )
	{
		var url = document.URL;

        // Secure login anyone who may need to go through computer enrollment
        // to save a redirect later
        if (url.indexOf("?") == -1) 
            url = url + "?"+ (sec ? SLIT : LIT);
        else 
            url = url + "&"+ (sec ? SLIT : LIT);
	
		url = url + "&ppcb=musicstore";
        expSet = new Date((new Date()).getTime() + (10 * 60 * 1000)).toGMTString();
		document.cookie = "bbuy=" + event.srcElement.parentElement.id + ";Path=/; expires=" + expSet + "; domain=" + __getDocDomain();
		document.cookie = "buyurl=" + escape(document.URL) + "; Path=/; expires=" + expSet + "; domain=" + __getDocDomain();
        document.cookie = "secauth=2;Path=/; expires=" + expSet + "; domain=" + __getDocDomain();
		
		if(window.ctTrackUrl != null)
		{
			try
			{
				ctTrackUrl(url, "cm=" + event.srcElement.parentElement.cmvl + "&ce=BUYPP" + event.srcElement.parentElement.cevl);
			}
			catch(e)
			{
				location.href = url;
                redirPending = true;
			}
		}
		else
		{	
			location.href = url;
            redirPending = true;
		}
		
		return;
	}
			
	var o = (event == null ? null : event.srcElement);
	
	if(obj != null)
		o = obj.children[0];
    
    // if bst dne or is not set, set it now
    // this assures cancel will operate correctly
    // (note repeat purchase cancels can occur even in noHassle)
    if (o != null && !o.bst)
    {
        o.bst = 1;

		o.baksr = o.src;
		o.bakov = o.onmouseover;
		o.bakot = o.onmouseout;
		o.bakdn = o.onmousedown;
		o.bakup = o.onmouseup;
		o.bakkdn = o.onkeydown;
		o.bakkup = o.onkeyup;
		
        // noHassle is -1, 0 or 1 (unknown, no, yes)
        __CheckNoHassleCookie();
        if(noHassle > 0)
        {
			if(window.ctTrackUrl != null && event != null)
			{
				try
				{
					ctTrackDirect("cm=" + event.srcElement.parentElement.cmvl + "&ce=BUYONE" + event.srcElement.parentElement.cevl);
				}
				catch(e)
				{
				}
			}
            // is noHassle buy and not repeat buy cancel
            __DoBuy(o);
        }
        else
        {
			if(window.ctTrackUrl != null && event != null)
			{
				try
				{
					ctTrackDirect("cm=" + event.srcElement.parentElement.cmvl + "&ce=BUY" + event.srcElement.parentElement.cevl);
				}
				catch(e)
				{
				}
			}
            // is standard buy and not repeat buy cancel
            eval("bbi('" + o.parentElement.bytp + "','up','con',o);");
		
            o.onmouseover = bm;
            o.onmouseout = bm;
            o.onmousedown = bm;
            o.onmouseup = bm;
            o.onkeydown = bm;
            o.onkeyup = bm;
		
            ele.style.visibility = 'visible';
            o.bst = 1;
        }
    }
	else if(o != null)
	{
        // is cancel click, revert to buy button
		o.src = o.baksr;
		o.onmouseover = o.bakov;
		o.onmouseout = o.bakot;
		o.onmousedown = o.bakdn;
		o.onmouseup = o.bakup;
		o.onkeydown = o.bakkdn;							
		o.onkeyup = o.bakkup;
		
		ele.style.visibility = 'hidden';
		o.bst = 0;
	}
}

function bkchk(toExecute)
{
    if(event.keyCode == 13 || event.keyCode == 32 )
    eval(toExecute);
}

function bn(ele)
{
	bc(event.srcElement.parentElement, ele);
}

// handle all cancel button events
function bcan()
{
	var o = event.srcElement;
    var par = o.parentElement;
	switch(event.type)
	{
		case 'mouseover': eval("bbi('" + par.bytp + "','hv','can',o);"); break;
		case 'mouseout': eval("bbi('" + par.bytp + "','up','can',o);"); break;
		case 'mousedown': eval("bbi('" + par.bytp + "','dn','can',o);"); break;
		case 'mouseup': 
			eval("bbi('" + par.bytp + "','up','can',o);"); 
			eval("bn(by" + par.item + ");"); 

			break;
		case 'keydown': if(event.keyCode == 13 || event.keyCode == 32 ){eval("bbi('" + par.bytp + "','dn','can',o);");} break;
		case 'keyup':   
			if(event.keyCode == 13 || event.keyCode == 32 )
			{
                eval("bbi('" + par.bytp + "','up','can',o);"); 
                eval("bn(by" + par.item + ");"); 
            }
            break;
    }
}

// handle all confirm button events
function bm()
{
	var o = event.srcElement;
	
	switch(event.type)
	{
		case 'mouseover': eval("bbi('" + o.parentElement.bytp + "','hv','con',o);"); break;
		case 'mouseout': eval("bbi('" + o.parentElement.bytp + "','up','con',o);"); break;
		case 'mousedown': eval("bbi('" + o.parentElement.bytp + "','dn','con',o);"); break;
		case 'mouseup': 
			eval("bbi('" + o.parentElement.bytp + "','up','con',o);"); 
			if(window.ctTrackUrl != null)
			{
				try
				{
					ctTrackDirect("cm=" + event.srcElement.parentElement.cmvl + "&ce=CONFIRM" + event.srcElement.parentElement.cevl);
				}
				catch(e)
				{
				}
			}
            __DoBuy(o);
			
			break;
		case 'keydown': if(event.keyCode == 13 || event.keyCode == 32 ){eval("bbi('" + o.parentElement.bytp + "','dn','con',o);");} break;
		case 'keyup':   
			if(event.keyCode == 13 || event.keyCode == 32 )
			{
                eval("bbi('" + o.parentElement.bytp + "','up','con',o);"); 
				if(window.ctTrackUrl != null)
				{
					try
					{
						ctTrackDirect("cm=" + event.srcElement.parentElement.cmvl + "&ce=CONFIRM" + event.srcElement.parentElement.cevl);
					}
					catch(e)
					{
					}
				}
                __DoBuy(o);
			} 
			break;
	}
}

function __DoBuy(o)
{	
    var retval = eval(o.parentElement.conck);
			
    if(retval == 0) 
        bdn(o);
    else if(retval == 2)
        frzBuy = true;
    else if(retval > 0 || retval == -5)
        bc(GE('cn' + o.parentElement.id.substring(2)), o.parentElement);
	
	__dNoHassleClearBBUrl(retval);
}

function bdn(ele)
{
    var par = ele.parentElement;
	bFly(par);
	ele.onmouseover = null;
	ele.onmouseout = null;
	ele.onmousedown = null;
	ele.onmouseup = null;
	ele.onclick = null;
	ele.onkeydown = null;
	ele.onkeyup = null;
	ele.style.cursor = "default";
    // set the purchase image (should be preloaded)
    bbi(par.bytp, 'up', 'pur', ele);
	ele.title = "Purchased";
	document.all["cn" + par.id.substring(2)].style.visibility = "hidden";

    if(window.inPostLic == null || window.inPostLic != true) 
		__ShowNoHassleDialog(par);
}

// --------------------- //

function sHS()
{
	if(document.all.sbs1) {
		var iWidth = document.body.clientWidth;
		document.all.sbs1.style.display = (iWidth > 773 ? 'inline' : 'none');
		document.all.sbs2.style.display = (iWidth > 773 ? 'inline' : 'none');
	}
}

var sdd = true;
	
// Safe window.onload function
var gsl = new Array(); 

function SOL(f) 
{ 
	if(window.onload) 
	{ 
		if(window.onload != SOLF) 
		{ 
			gsl[0] = window.onload; 
			window.onload = SOLF; 
		} 
		
		gsl[gsl.length] = f; 		
	} 
	else 
	{ 
		window.onload = f; 
	} 
} 

function SOLF() 
{ 
	for(var i=0; i < gsl.length; i++) 
		gsl[i](); 
}

