var JS_PAGE_COBRAND;

if(!window.Msn)
{
	window.Msn={};
}

Msn.Flash = new function()
{
	var me=this;
	var flv=null;
	var d=document;
	var w=window;
	var aad=false;
	
	function E(id)
	{ 
		return d.getElementById(id);
	}

	function flVer()
	{
		if(flv===null)
		{
			flv=0;
			var i,f=null,p=w.navigator.plugins;
			
			if(p&&p.length)
			{
				f=(p["Shockwave Flash"] || p["Shockwave Flash 2.0"]);
				if(f && (i=f.description))
				{
					flv=parseInt(i.substring(i.indexOf('.')-2));
				}
			}
			else if(w.ActiveXObject)
			{
				for(i=15;i>2 && !f;--i)
				{
					eval("try{f=new ActiveXObject('ShockwaveFlash.ShockwaveFlash.'+i);}catch(e){}");
					if(f)
					{
						flv=i;
					}
				}
			}
		}
		return flv;
	}

	this.Version = function()
	{
		return flVer();
	};
	
	this.Build = function(f,v,wf,hf,id,pf,wmode)
	{
		var m=5;
		if(v)
		{
			var p=v.lastIndexOf(' ');
			if(p>0)
			{
				p=parseInt(v.substring(p));
			}
			else
			{
				p=parseInt(v);
			}
			m =(isNaN(p) ? m : p);
		}
		if(m<=flVer())
		{
			var defs = {movie:f,quality:'high'};
			var el='<object id="'+id+'obj" type="application/x-shockwave-flash" width="' + wf + '" height="' + hf + '" data="' + f + '">';
			if(pf)
			{
				for(var r in defs)
				{
					if(!pf[r])
					{
						pf[r]=defs[r];
					}
				}
			}
			else
			{
				pf=defs;
			}
			
			for(var q in pf)
			{
				if(pf[q])
				{
					if (q == "flashvars")
					{
						pf[q] = getCommaHandledQuery(pf[q]);
					}
					el+='<param name="'+q+'" value="'+pf[q]+'"/>';
				}
			}
			
			if(wmode)
			{
				el+= '<param name="wmode" value="' + wmode + '">';	// without this, pop-up menu will go underneath the video player
			}

			el += '</object>';
			var dv=(id?d.getElementById(id):null);
			if(dv)
			{
				dv.innerHTML=el;
			}
			else
			{
				d.write(el);
			}
		}
	}
	;return this;
};

Msn.Video = new function()
{
	var me=this;
	this.OnVideoTitle = function(text,id)
	{
		var thisId = id + "_t";
		if(checkString(text) && checkString(thisId))
		{
			var el=document.getElementById(thisId);
			if(el)
			{
				el.innerHTML=text;
			}
		}
	};
	
	this.OnLinkback = function(text,url,id)
	{
		var thisId = id + "_m";
		var c;
		var el=document.getElementById(thisId);
		var css;
		if(el)
		{
			el.innerHTML="";
			for(c=0;c<=text.length-1;c++)
			{
				if(checkString(text[c]) && checkString(url[c]))
				{
					css=(c==text.length-1) ? "linkback last" : "linkback";
					el.innerHTML=el.innerHTML+'<a class="'+css+'" href="'+url[c]+'">'+text[c]+'</a>';
				}
			}
		}
	};
	
	this.OnAdLoaded=function(adData,id)
	{
		var thisId = id + "_a";
		if(checkObject(adData) && checkString(adData.imageUrl) && checkArray(adData.clickUrls,2) && checkString(thisId))
		{
			var el=document.getElementById(thisId);
			if(el)
			{
				el.innerHTML='<a class="ad" href="'+adData.clickUrls[1]+'"><img src="'+adData.imageUrl+'" width="300" height="60"></a>';
			}
		}
	};
	
	this.Build=function(id,dl,fv,w,h,wmode)
	{
		var el=document.getElementById(id);
		if(el)
		{
			var v='<div class="video1"><h3 id="' + id + '_t"></h3><div id="' + id + '_p"><div>' + (dl ? dl : '') + '</div><a href="http://www.adobe.com/go/getflashplayer">This video requires the Adobe® Flash® Player. Download a free version of the player.</a></div><div id="' + id + '_m"></div><div id="' + id + '_a"></div></div>';
			el.innerHTML = v;
			Msn.Video.BuildPlayer(id,fv,w,h);
		}
	};
	
	this.BuildPlayer=function(id,fv,w,h,wmode)
	{
		var fvs="";
		
		for(var i in fv)
		{
			fvs += i+"="+fv[i]+"&amp;";
		}

		fvs += addOptusBranding(fvs);
		
		//add flashvar for VAP integration
		fvs += addVAPVar(fvs);

		if (JS_SITEID == '2326' || JS_SITEID == '281' || JS_SITEID == '2339' || JS_SITEID == '2320') 
        {
            fvs += addVarforWWOS();
        }		
		
		fvs += "ch=true&amp;playlistmin=2&amp;cbprefix=Msn.Video.&amp;cbdata=" + id;
		Msn.Flash.Build('http://images.soapbox.msn.com/flash/soapbox1_1.swf','8',w?w:300,h?h:269,id+'_p',{allowScriptAccess:'always',allowFullScreen:'true',base:'http://images.soapbox.msn.com/flash/soapbox1_1.swf',flashvars:fvs},wmode);
	};
	
	//Appends the brand for the destination player depending on which copbranded header is running
	function addOptusBranding()
	{
		var additionalFlashVars = "";
		if(JS_PAGE_COBRAND && JS_PAGE_COBRAND.toLowerCase() == "optus")
		{
			additionalFlashVars += "brand=optus&amp;mh=OPTS&amp;";
		}
		else
		{
			additionalFlashVars += "brand=ninemsn&amp;";
		}
		return additionalFlashVars;
	}
	
	function addVAPVar(fvs)
	{
		return "adDivs=videoAdsdiv,300,60&amp;"
	}

	function addVarforWWOS() 
	{
	    return "eap=true&amp;slateTime=0&amp;";
	}	
	
	function checkString(s)
	{
		return((typeof(s)=="string")&&(s.length>0));
	}
	
	function checkObject(o)
	{
		return((typeof(o) == "object") && (o !== null));
	}
	
	function checkArray(array,minLength)
	{
		var arrayCheck=(array instanceof Array);
		if(minLength !== '')
		{
			arrayCheck=arrayCheck&&(array.length>=minLength);
		}
		return arrayCheck;
	}

    
	return this;
};

// This function is taken from msn_video_collection.js ! IMPORTANT
// This functions is called by the video player SWF object when a new ad is loaded
function OnAdLoaded(adData)
{
	var videAdsDiv = document.getElementById("videoAdsDiv");
    if((videAdsDiv!=null) && (adData != null) && adData.clickUrls && (adData.clickUrls.length >= 2)){
        videAdsDiv.innerHTML = '<a href="' + adData.clickUrls[1] + '"><img src="'+adData.imageUrl+'" alt="" /></a>';
    }
}

// return param value from the given query string
function getParamValue(param, query) {
	var value;
	var startPos = query.indexOf(param + "=");
	if(startPos > -1) {
		startPos += param.length+1;
		var endPos = query.indexOf("&",startPos+1);
		if(endPos < 0) {
			endPos = query.length;
		}
		value = query.substring(startPos,endPos);
	}
	return value;
}

// return vcq value without comma
function getCommmaLessVcq(param, vcq)
{
	// check if there is comma in this vcq string
	var paramValue = getParamValue(param, vcq);
	
	// no value for this param, nothing to do
	if(paramValue)
	{
		var paramArray = paramValue.split(",");
		
		// no comma, nothing to do
		if(paramArray.length > 1)
		{
			//  replace the found list having comma with the last item from the list
			vcq = vcq.replace(paramValue, paramArray[paramArray.length-1]);
		}
	}
	return vcq;
}

// return query string after handling comma in vcq
function getCommaHandledQuery(query)
{
	// only need to worry about vcq value in the query
	var vcq = getParamValue("vcq", query);
	
	// no vcq, nothing to do
	if(vcq)
	{
		// decode the vcq value so that we can get value for ns and tag, which we need to handle the comma separated list
		var decodedVcq = unescape(vcq);
		decodedVcq = getCommmaLessVcq("ns", decodedVcq);
		decodedVcq = getCommmaLessVcq("tag", decodedVcq);
		
		// from the query, replace only vcq value with the handled vcq 
		query = query.replace(vcq, escape(decodedVcq));
	}
	return query;
}