var PlayerDisplayed = false;
var currPlayClip = null, currDownClip = null, targetMode;
function IniFramePage() {
	if (!CheckTop()) {
		return false;
	}
	//FireHandles(window, "onload", window.event);
	try {
		parent.RegisterCW(window);
		parent.MMSelectItemFromFrame(MenuArgs);
		parent.ShowBanners(GetBannerArr());
	}
	catch(e) {}
}



function BannerInfo(id, typeName, url, width, height) {
	this.id = id;
	this.typeName = typeName;
	this.url = url;
	if (isNaN(width)) this.width = -1;
	else this.width = parseInt(width);
	if (isNaN(height)) this.height = -1;
	else this.height = parseInt(height);
}
function AddLangToUrl(lang, url) {
	var pos = url.lastIndexOf(".");
	if (pos == -1) return url;
	var s = url.substring(0, pos + 1) + lang + url.substring(pos);
	return s;
}
function GetStringFromResource(s, arr) {
	var r = FindStringResource();
	if (r) return r.getString(s, arr);
	else return s;
}
function FindStringResource(w) {
	var r = null;
	if (!w) {
		w = window;
	}
	if (w.document.getElementById("scriptStringRes")) {
		if (w._stringRes) {
			return w._stringRes;
		}
		try {
			r = w.InitializeStringResource();
			w._stringRes = r;
		} catch (e) {alert("err 1: " + e.description)}
		return r;
	}
	if (w.parent != w.self) {
		try {
			return FindStringResource(w.parent);
		} catch (e) {alert("err 2: " + e.description)}
	}
}
function InitializeStringResource() {
	var res = IniStringRes();
	res.getString = function (s, arr) {
		if (this[s]) {
			if (!arr) return this[s];
			var s2 = this[s];
			for (var i = 0; i < arr.length; i++) {
				var re = new RegExp("\\{" + i + "\\}", "gi");
				s2 = s2.replace(re, arr[i]);
			}
			return s2;
		}
		else return s;
	};
	return res;
}


function SwapImage2(me, ev) {
	var img = GetLinkImage(me);
	var en = ev.type;
	var s = img.src;
	var p = s.lastIndexOf(".");
	var ext = s.substring(p);
	var s1 = s.substring(0, p);
	if (en == "mouseover" || en == "focus") {
		var s2 = s1.substring(s1.length - 4);
		if (s2 == "_off") {
			var s3 = s1.substring(0, s1.length - 4) + "_on" + ext;
			img.src = s3;
		}
	}
	else if (en == "mouseout" || en == "blur") {
		var s2 = s1.substring(s1.length - 3);
		if (s2 == "_on") {
			var s3 = s1.substring(0, s1.length - 3) + "_off" + ext;
			img.src = s3;
		}
	}
}
function GetInvalidString(res) {
	return GetStringFromResource('ERROR_INVALID_CHARACTER', [res.pos, res.str, "\n"]);
}
function ShowSecureMessage(me, ev) {
	CancelEvent(ev);
	DoBlur(me);
	var w = window.open(WebRoot + "public/SecureServer." + UIL + ".html", "BarcelonaSecureServerPopup", "width=400,height=188,history=no,resizable=yes,status=yes,menubar=no");
	if (w) {
		w.focus();
	}
	else {
		return true;
	}
	return false;
}
function hbxPT() {
	var s = document.title;
	s =  repJS(s);
	return s;
}
function hbxCC() {
	var cc = 0;
	var s = ""
	try {
		cc = MenuArgs[0];
	} catch (e) {}
	if (cc > 0) {
		try {
			s = parent.GetMenuName(cc);
		} catch (e) {}
	}
	s = repJS(s);
	return s
}
function repJS(s) {
	var re1 = /'|\\/g;
	var ss = s.replace(re1, "");
	return ss;
}
function InsertCategorisedAdd(club, network, placement, keywords) {
	InsertAdTechBanner(club, network, placement, keywords);
}
function InsertAdTechBanner(club, network, placement, keywords) {
	document.write('<IFRAME WIDTH="468" HEIGHT="60" SCROLLING="No" FRAMEBORDER="0" MARGINHEIGHT="0" MARGINWIDTH="0" ' +
		'SRC="http://adserver.adtech.de/adiframe|3.0|' + network + '|'+ placement +'|0|1|ADTECH;target=_blank;key='+ 
		keywords + ';grp=1"></IFRAME>');		
}
function DoSearchFocus(me, ev) {
	var val = trim(me.value)
	var bl = GetStringFromResource('SEARCH_FIELD');
	if (val == bl) me.value = "";
}
function ShowRatings(ratings_) {
	if (!ratings_) {
		try {
			ratings_ = ratings;
		} 
		catch(e) {}
		if (!ratings_) return;
	}
	for (var i = 0; i < ratings_.ratings.length; i++) {
		ShowRating(ratings_.ratings[i]);
	}
}
function ShowRating(rating) {
	var elid = $get("elids_" + rating[0] + "_" + rating[1]);
	if (elid) {
		elid.className = "ratingspan";
		elid.innerHTML = rating[2];
	}
	var elid = $get("elid_" + rating[0] + "_" + rating[1]);
	if (elid) {
		ShowStars(elid, rating[2]);
	}
}
function ShowStars(el, rating) {
	var span = null;
	for (var i = 0; i < el.childNodes.length; i++) {
		var el2 = el.childNodes[i];
		if (el2.tagName == "SPAN") {
			span = el2;
			break;
		}
	}
	if (!span) return;
	var cnt = 0;
	for (var i = 0; i < span.childNodes.length; i++) {
		var el2 = span.childNodes[i];
		if (el2.tagName == "A") {
			cnt ++;
			var cn = el2.className;
			if (cnt <= rating) {
				if (cn == "white")
					SwapImage(el2, "star_whitebg_on.gif");
				else
					SwapImage(el2, "star_on.gif");
			}
			else {
				if (cn == "white")
					SwapImage(el2, "star_whitebg_off.gif");
				else
					SwapImage(el2, "star_off.gif");
			}
		}
	}
}
function CheckRating(elType, elId) {
	RemoveOldRatings();
	var cookie = ReadCookie("userRatings");
	if (!cookie) return true;
	var s2 = "|" + elType + "," + elId + ",";
	if (cookie.indexOf(s2) != -1) return false;
	return true;
}
function RemoveOldRatings() {
	var cookie = ReadCookie("userRatings");
	if (!cookie) return true;
	var ss = cookie.split("|");
	var d1 = new Date();
	d1.setUTCDate(d1.getUTCDate() - 3);
	var st = GetDateStrForRating(d1);
	var res = "";
	for (var i = 0; i < ss.length; i++) {
		if (!ss[i]) continue;
		var ss2 = ss[i].split(",");
		if (ss2.length != 4) continue;
		if (ss2[3] < st) continue;
		res += "|" + ss2[0] + "," + ss2[1] + "," + ss2[2] + "," + ss2[3];
	}
	if (res == cookie) return;
	WriteCookie("userRatings", res, true, 24);
}
function GetDateStrForRating(d) {
	var yy = d.getUTCFullYear();
	var mm = d.getUTCMonth() + 1;
	var dd = d.getUTCDate();
	var res = String(yy).substr(2) + (mm < 10 ? "0" : "") + mm + (dd < 10 ? "0" : "") + dd;
	return res;	
}
function RegisterRating(elType, elId, rating) {
	var cookie = ReadCookie("userRatings");
	var s = "|" + elType + "," + elId + "," + rating + "," + GetDateStrForRating(new Date());
	if (!cookie) cookie = "";
	cookie += s;
	WriteCookie("userRatings", cookie, true, 24);
	var frm = $get("ifrmRatingPost");
	var s = WebRoot + "public/rate.aspx?type=" + elType + "&itemid=" + elId + "&rating=" + rating;
	frm.src = s;
}
function RateElement(me, ev, elType, elId, rating) {
	DoBlur(me);
	CancelEvent(ev);
	if (!CheckRating(elType, elId)) {
		alert("You have already rated this clip recently.");
		return false;
	}
	RegisterRating(elType, elId, rating);
	return false;
}
function SwapArticleSets(me, ev, dir) {
	CancelEvent(ev);
	DoBlur(me);
	
	var articles = [];
	var parent = $get("hparticles");
	var first = -1, cnt = 0;

	for (var i = 0; i < parent.childNodes.length; i ++) {
		var el = parent.childNodes[i];
		if (el.tagName == "DIV") {
			articles[articles.length] = el;
			if (first == -1) {
				if (!(el.style.display == "none")) {
					first = cnt;
				}
			}
			cnt++;
		}
	}
	var newFirst = (dir == 0 ? first - 2 : first + 2);
	if (newFirst < 0)
		newFirst = 0;
	if ((newFirst + 1) >= articles.length)
		newFirst = articles.length - 1;
	if (newFirst == 0) {
		SwapImage($get("aHpScrollLeft"), "home_clip_leftarrow_fade.gif");
	}
	else {
		SwapImage($get("aHpScrollLeft"), "home_clip_leftarrow_off.gif");
	}
	if ((newFirst + 2) >= articles.length) {
		SwapImage($get("aHpScrollRight"), "home_clip_rightarrow_fade.gif");
	}
	else {
		SwapImage($get("aHpScrollRight"), "home_clip_rightarrow_off.gif");
	}
	for (var i = 0; i < articles.length; i++) {
		articles[i].style.display = "none";
	}
	articles[newFirst].style.display = "block";
	if ((newFirst + 1) < articles.length) articles[newFirst + 1].style.display = "block";
	return false;
}
function IniHomePlayer() {
	var img = $get("divVideoImg");
	img.style.backgroundImage = "url(" + WebRoot + "dynamic/images/" + _hpArticleCollection.image + ")";
	img.style.display = "block";
	SwapArticleSets(null, null, 0);
	var clip = GetClipFromId(_hpArticleCollection.firstArticle);
	HpShowClipInfo(clip);
	if (_hpArticleCollection.autostart == "1" && HpCanPlay(clip)) {
		HpPlayClip(clip);
	}
}
function HpShowClipInfo(clip) {
	MediaPlayer_Stop(null);
	MediaPlayer_ResetControls();
	MP = null;
	$get("divVideoPlayer").style.display = "none";
	$get("divPlayerError").style.display = "none";
	$get("divVideoImg").style.display = "block";
	HpHideMessages();
	var canPlay = HpCanPlay(clip);
	if (canPlay) $get("divVideoDoPlay").style.display = "block";
	else if (login_status == "") $get("divVideoNeedToLogin").style.display = "block";
	else $get("divVideoNeedToSubscribe").style.display = "block";
	if (mp == "wmp7" || mp == "wmp6") {
		$get("divVideoControls").style.display = "block";
		$get("divVideoTitle").innerHTML = clip.title;
	}
	else {
		$get("divVideoControlsEmbed").style.display = "block";
		$get("divVideoControlsEmbed").innerHTML = clip.title;
	}
	currPlayClip = clip;
}
function HpHideMessages() {
	$get("divVideoDoPlay").style.display = "none";
	$get("divVideoNeedToLogin").style.display = "none";
	$get("divVideoNeedToSubscribe").style.display = "none";
}
function HpCanPlay(clip) {
	if (login_status == "S") {
		return true;
	}
	else if (login_status == "R" || login_status == "P") {
		if (clip.et == "S")
			return false;
		return true;
	}
	else if (clip.et == "F") {
		return true;
	}
	return false;
}
function HpDoPlayClip(me, ev) {
	CancelEvent(ev);
	DoBlur(me);
	HpPlayClip(currPlayClip);
	return false;
}
function HpPlayClip(clip) {
	currPlayClip = clip;
	$get("divVideoImg").style.display = "none";
	$get("divVideoPlayer").style.display = "block";

	if (IsLicenseRequired2(clip)) {
		targetMode = "P";
		$get("MediaPlayerFrame").src = GetLicensePage(clip);
	}
	else {
		HpShowHideAudio();
		ChangeFrameSource();
		RegisterPlayerGuid();
		//parent.PauseAudio();
	}
	MediaPlayer_ResetControls();
	return false;
}
function IsLicenseRequired2(clip) {
	if (isMac) return false;
	var ec = _encTypes[clip.et];
	var cn = "ec" + ec;
	var v = ReadCookie(cn);
	if (v == "1") 
		return false;
	return true;
}
function GetLicensePage(clip) {
	var res = LicensePage + "?ec=" + _encTypes[clip.et];
	return res;
}
function HpShowHideAudio() {
	var d1 = $get("divVideoImg");
	var f = $get("MediaPlayerFrame");
	if (currPlayClip.is_audio == 1) {
		if (mp == "wmp6" || mp == "wmp7") {
			f.style.height = "1px";
			f.height = 1;
		}
		else {
			$get("divVideoControlsEmbed").style.display = "none";
			f.style.height = "66px";
			f.height = 66;
		}
		HpHideMessages();
		d1.style.display = "block";
	}
	else {
		d1.style.display = "none";
		if (mp == "wmp6" || mp == "wmp7") {
			f.style.height = "240px";
			f.height = 240;
		}
		else {
			$get("divVideoControlsEmbed").style.display = "none";
			f.style.height = "284px";
			f.height = 284;
		}
	}
}
