// 24-7 data object
var fBall247 = {
	// default value: number of live tonight events displayed on home page
	"noOfEventsToday": 6,
	// default value: number of promotions displayed on home page by default
	"noOfPromotions": 1,
	// default value: number of regions displayed on news page
	"noOfNewsRegions": 4,
	// default value: number of characters displayed as a summary of the news article
	"noOfSummaryChars": 350 };

var setStreamingFlashHeight = function(newH) {
	$("#fBall247 .navigation.right #streamingRN").css("height", newH+"px");
}

$(document).ready(function() {

	// IE6 transparent png fix
	if(typeof(DD_belatedPNG)!='undefined') {
		DD_belatedPNG.fix(".navigation.left .globe");
	}

	// Left nav - even/odd classes for table rows
	$("#fBall247 .navigation.left ul li:even").addClass("even");
	
	// Home page - Sing up button
	if($("#fBall247 .navigation.top .signUp a").length>0) $("#fBall247 .navigation.top .signUp a").attr("href", $("#loginBox #registerButton a").attr("href")).click(function() {
		$("#loginBox #registerButton a").click();
		return false;
	});
	
	// Home page - Live tonight table	
	$.get("/misc/webServices/inPlay/NewXmlWebService.ashx",
		{ action: "schedule", xml: "<schedule/>" },
		function(data1) {	
			$.get("/s/flash/promotion/24-7/conf/setup_247.xml",
				function(data2) {
					$(data1).find("today sport[id='102'] event:lt("+fBall247.noOfEventsToday+")").each(function() {
						$("#fBall247.home .tabContent .box.sub table.eventsToday tbody")
							.append($("<tr>")
								.append("<td class=\"time\"><b>"+$(this).attr("startTime").substring(8, 10)+":"+$(this).attr("startTime").substring(10, 12)+"</b></td>")
								.append("<td class=\"date\">"+(($(this).attr("inrunning")=="1")?$(data2).find("settings item[name='mainButton_inPlayNow']").text():$(data2).find("settings item[name='mainButton_comingUp']").text())+"</td>")
								.append("<td class=\"event\">"+$(this).attr("name")+"</td>")
								.append("<td class=\"betNow\"><a class=\"rightSmall\" href=\"/t/inPlay/inPlay.aspx?eventid="+$(this).attr("id")+"\"></a></td>")
							);
					});
					$(data1).find("tomorrow sport[id='102'] event:lt("+(fBall247.noOfEventsToday-$(data1).find("today sport[id='102'] event:lt("+fBall247.noOfEventsToday+")").length)+")").each(function() {
						$("#fBall247.home .tabContent .box.sub table.eventsToday tbody")
							.append($("<tr>")
								.append("<td class=\"time\"><b>"+$(this).attr("startTime").substring(8, 10)+":"+$(this).attr("startTime").substring(10, 12)+"</b></td>")
								.append("<td class=\"date\">"+($(data2).find("settings item[name='dateformat']").text()).replace(/d/, $(this).attr("startTime").substring(6, 8)).replace(/m/, $(this).attr("startTime").substring(4, 6)).replace(/y/, $(this).attr("startTime").substring(0, 4))+"</td>")
								.append("<td class=\"event\">"+$(this).attr("name")+"</td>")
								.append("<td class=\"betNow\"><a class=\"rightSmall\" href=\"/t/inPlay/inPlay.aspx?eventid="+$(this).attr("id")+"\"></a></td>")
							);
					});
					if($("#fBall247.home .tabContent .box.sub table.eventsToday tr").length > 0) {
						$("#fBall247.home .tabContent .box.live").css("display", "block");
						$("#fBall247.home .tabContent .box.sub table.eventsToday td.betNow a").text($("#fBall247.home .tabContent .hide a.betNow").text());
							// Whole row clickable
						$("#fBall247.home .tabContent .box.sub table.eventsToday tr").each(function() {
							$(this).click(function() {
								location.href=$(this).find("td.betNow a.rightSmall").attr("href");
								return false;
							});
							$(this).hover(
								function() {
									$(this).find("td.betNow a.rightSmall").addClass("hovered");
									$(this).find("td").addClass("hovered");
								},
								function() {
									$(this).find("td.betNow a.rightSmall").removeClass("hovered");
									$(this).find("td").removeClass("hovered");
								}
							);
						});
					}
					// Even/odd classes for table rows
					$("#fBall247.home .tabContent .box.sub table.eventsToday tr:even").addClass("even");
				},
			"xml");
		},
		"xml");
	
	// Home page - Promotions
		// Open in pop up layer if there is no url and there is content in insider fulltext
	$("#fBall247.home .tabContent ul.promotions li a.more").each(function() {
		if($(this).attr("href")=="#" && $(this).parents("li").eq(0).find(".termsAndConditions").html()!="" && $(this).parents("li").eq(0).find(".termsAndConditions").html()!=" ") {
			$(this).click(function() {
				$("#popupBox #popLayer1").html("<div id=\"popBox1\" class=\"popup promo247\">"
					+"<div class=\"popupHead\"></div>"
					+"<div class=\"popupBody\"><div class=\"container\"><div class=\"content\">"
					+"</div></div></div>"
					+"<div class=\"popupFoot\"><span></span></div>"
					+"</div>");
				$("#popupBox #popLayer1 #popBox1.promo247 .popupBody .content").append($(this).parents("li").eq(0).find(".termsAndConditions").eq(0).html());
				$("#popupBox #popLayer1 #popBox1.promo247 .popupHead").append($("#fBall247 .tabContent .hide a.closePopUp").eq(0).clone()).append($("#popupBox #popLayer1 #popBox1.promo247 .popupBody .content h2").eq(0));
				SB.base.divPopUp(1, "show");
				$("#popupBox #popLayer1 #popBox1.promo247 .popupHead a").click(function() {
					SB.base.divPopUp(1,"hide");
					$("#popupBox #popLayer1 #popBox1.promo247").remove();
					return false;
				});
				return false;
			});
			// Whole promotions box clickable
			$(this).parents("li").eq(0).click(function() {
				$(this).find("a.more").eq(0).click();
				return false;
			});
		}
		else{
			$(this).parents("li").eq(0).click(function() {
				location.href = $(this).find("a.more").eq(0).attr("href");
				return false;
			});
		}
			// Whole promotions hover-able
		$(this).parents("li").eq(0).hover(
			function() { $(this).find("a.more").addClass("hovered"); },
			function() { $(this).find("a.more").removeClass("hovered"); }
		);
	});
		// Display 1st X promotions and More link
	if($("#fBall247.home .tabContent .box.sub .promotions > li").length > 0) {
		$("#fBall247.home .tabContent .box.promo").css("display", "block");
		$("#fBall247.home .tabContent .box.sub .promotions > li:lt("+fBall247.noOfPromotions+")").css("display", "list-item");
		if($("#fBall247.home .tabContent .box.sub .promotions > li:gt("+(fBall247.noOfPromotions-1)+")").length > 0) {
			$("#fBall247.home .tabContent .box a.morePromo").css("display", "block").click(function() {
				$("#fBall247.home .tabContent .box.sub .promotions > li:gt("+(fBall247.noOfPromotions-1)+")").slideDown("fast");
				$(this).remove();
				return false;
			});
		}
	}
	
	// News
	$("#fBall247.news .tabContent .box .articles > li .txt .subtitle").append($("#fBall247.news .tabContent .box .hide a.more").eq(0).clone());
	$("#fBall247.news .tabContent .box .articles > li").each(function(index) {
		$(this).find(".txt .subtitle").prepend("<p>"+$(this).find(".fullText .txt").text().replace(/\s{2,}/g, " ").substring(0, fBall247.noOfSummaryChars).replace(/[\.,;\-_]*\s+\S+\s?$/, "")+"...</p>");
		$(this).click(function() {
			$("#fBall247.news .tabContent .box").hide();
			$("#fBall247.news .tabContent").append("<div id=\"articleDetail\" class=\"box sub\">"
				+"<h3><strong>"+$(this).find(".txt h4 a").html()+"</strong></h3>"
				+"<div class=\"txt\">"+$(this).find(".fullText .txt").html()+"</div>"
				+"</div>");			
			$("#fBall247.news .tabContent #articleDetail").prepend($("#fBall247.news .tabContent .box .hide a.back").eq(0).clone().click(function() {
				$("#fBall247.news .tabContent #articleDetail").remove();
				$("#fBall247.news .tabContent .box .articles > li").hide();
				$("#fBall247.news .tabContent .box .articles > li.initShow").show();
				$("#fBall247.news .tabContent .box .articles").parents(".box").eq(0).children("a.back").remove();
				$("#fBall247.news .tabContent .box").show();
				return false;
			})).slideDown("fast");
			// Create archive button if more then one article available in region
			if($("#fBall247.news .tabContent .box .articles > li."+$("#fBall247.news .tabContent .box .articles > li").get(index).className.split(" ")[0]).length > 1) $("#fBall247.news .tabContent #articleDetail h3").after($("#fBall247.news .tabContent .box .hide a.archive").eq(0).clone().click(function() {
				$("#fBall247.news .tabContent #articleDetail").remove();
				$("#fBall247.news .tabContent .box .articles > li").hide();
				$("#fBall247.news .tabContent .box .articles > li."+$("#fBall247.news .tabContent .box .articles > li").get(index).className.split(" ")[0]).show();
				// Back to articles link
				if($("#fBall247.news .tabContent .box .articles").parents(".box").eq(0).children("a.back").length < 1) $("#fBall247.news .tabContent .box .articles").parents(".box").eq(0).prepend($("#fBall247.news .tabContent .box .hide a.back").eq(0).clone().click(function() {
					$("#fBall247.news .tabContent .box .articles > li").hide();
					$("#fBall247.news .tabContent .box .articles > li.initShow").show();
					$(this).remove();
					$("#fBall247.news .tabContent .box").show();
					return false;
				}));
				$("#fBall247.news .tabContent .box").show();
				return false;
			}));
			$("#fBall247.news .tabContent #articleDetail h3").after($(this).find(".author").clone()).after($(this).find(".fullText .photo").clone());
			return false;
		}).hover(
			function() { $(this).find("a.more").addClass("hovered"); $(this).find("h4 a").addClass("hovered"); },
			function() { $(this).find("a.more").removeClass("hovered"); $(this).find("h4 a").removeClass("hovered"); }
		);
		$(this).find("a.more").click(function() {
			$(this).parents('li').eq(0).click();
			return false;
		});
		$(this).find("h4 a").click(function() {
			$(this).parents('li').eq(0).click();
			return false;
		});
	});
		// Add class identifying region based on the headline image
	$("#fBall247.news .tabContent .box .articles > li").each(function() {
		$(this).addClass($(this).find(".img").eq(0).css("background-image").match(/\/([^\/]+)\.[^\/]+$/)[1]);
	});
		// Display 1st four articles, one for each region
	for(var i=0, j=0; i<$("#fBall247.news .tabContent .box .articles > li").length && j<fBall247.noOfNewsRegions; i++) {
		if($("#fBall247.news .tabContent .box .articles > li."+$("#fBall247.news .tabContent .box .articles > li").get(i).className.split(" ")[0]+".initShow").length<1) {
			$("#fBall247.news .tabContent .box .articles > li").eq(i).addClass("initShow");
			j++;
		}
	}
	$("#fBall247.news .tabContent .box .articles > li:not(.initShow)").hide();
	$("#fBall247.news .tabContent .box .articles").css("display", "block");
	
	// Leagues
	$("#fBall247.leagues .tabContent a.stats").click(function() {
		(window.open(this.href, "stats_"+$("#fBall247").attr("class").split("leagues ")[1].split(" ")[0], "width=1015,height=700,resizable,scrollbars")).focus();
		return false;
	});
	
	// Stats
	$("#fBall247.stats .tabContent ul.flags li a").click(function() {
		(window.open(this.href, "stats_"+$(this).attr("class").split(" ")[0], "width=1015,height=700,resizable,scrollbars")).focus();
		return false;
	});
	
	// Insert right nav In play flash
	fo_streaming_rightNav.write("streaming_rightNav");
	$("#fBall247 .navigation.right #streaming_rightNav").css("display", "block");
	
	// oddsTables

if($("#fBall247.leagues .tabContent #upcomingFixtures table tr").length > 0)
{
	$("#fBall247.leagues .tabContent #upcomingFixtures").css("display", "block");
}

});

