﻿//<![CDATA[

/* Document ready:
******************/
$(document).ready(function() {
	enableShowHideCalendar();
	enableShowHideLeagueTable();
	enableHomepageImageCycler();
	//enableTruncateHeader();
});

function getCookieVal (offset) 
{
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) 
{
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return null;
}

function SetCookie(name, value, expires, path, domain, secure) 
{
    var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
      
  document.cookie = curCookie;
}

function DeleteCookie (name,path,domain) 
{
    if (GetCookie(name)) 
    {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function FixCookieDate (date) 
{
  var base = new Date(0);
  var skew = base.getTime(); // dawn of (Unix) time - should be 0
  if (skew > 0)              // Except on the Mac - ahead of its time
    date.setTime (date.getTime() - skew);
}

//Function to change the url of the window that opened a pop (TopTipster and BetSlip popups, nav to signup etc)
function NavigateFromPopup(url) 
{
    var target;
    target = top.opener;
    target.window.location.href = url;
    target.window.focus(); 
    window.close();
}

function OpenPopup(url, w, h, strpos) 
{
    wndPopup = "eopop";
    if (strpos == "middle") 
    {
        with (Math) { t = round(screen.availHeight * .5 - h / 2); }
    }
    else { t = 0; l = 0; }
    switch (strpos) 
    {
        case "middle": with (Math) { t = round(screen.availHeight * .5 - h / 2); l = round(screen.availWidth * .5 - w / 2); break; }
        case "midright": with (Math) { t = round((screen.availHeight * .75) - h); l = round((screen.availWidth * .75) - w); break; }
        case "midleft": with (Math) { t = round((screen.availHeight * .25) - h); l = round((screen.availWidth * .25) - w); break; }
        default: t = 0; l = 0;
    }
    newFeatures = "height=" + h + ",width=" + w + ",top=" + t + ",left=" + l + ",channelmode=0,dependent=yes,directories=0,fullscreen=0,location=0, menubar=0,resizable=0,scrollbars=no,status=0,toolbar=0,title=0";
    popupWin = window.open(url, wndPopup, newFeatures)
}

function OpenGateway(url, referral, w, h) 
{    
    wndPopup = "eopop";
    newFeatures = "height=" + h + ",width=" + w + ",top=0,left=0,channelmode=0,dependent=yes,directories=0,fullscreen=0,location=0, menubar=0,scrollbars=yes,resizable=yes,status=0,toolbar=0,title=0";
    popupWin = window.open(url+"&referral="+referral, wndPopup, newFeatures)
}

function CreateMail(toAddr, asubject) {
    var a = "info";
    var d = "easyodds.com";
    var s = "";
    if (toAddr != undefined) {
        a = toAddr;
    }
    if (asubject != undefined) {
        s = "?subject=" + asubject;
    }
    document.write("<a href='mailto:" + a + "@" + d + s + "'>");
    document.write(a + "@" + d + "</a>");
}

/* Show / hide racing calendar
******************************/
function enableShowHideCalendar() {
	if ($("#racingCalendarAction").length == 0)
		return;
		
	var calendar = $("#racingCalendar");
	calendar.hide();

	$("#racingCalendarAction").click(function() {
		if (calendar.is(":visible")) {
			calendar.hide();
			$(this).text("Select a date");
		} else {
			calendar.show();
			$(this).text("Hide calendar");
		}
		return false;
	});
}

function enableShowHideLeagueTable() {
	if ($(".leagueTableList").length == 0)
		return;
	
	$(".leagueTableList ul").hide();
	$(".leagueTableList h4").text("Show all league tables");

	$(".leagueTableList h4").click(function() {
		if ($(".leagueTableList ul").is(":visible")) {
			$(".leagueTableList ul").slideUp();
			$(this).text("Show all league tables");
		} else {
			$(".leagueTableList ul").slideDown();
			$(this).text("Hide all league tables");
		}
	});
}

function enableHomepageImageCycler() {
	if ($(".slideshow").length == 0) { 
	  return;
	}

	$(".slideshow > h1, .slideshow > h2").hide();
	$(".slideshow > h1:first-child, .slideshow > h2:first-child").show();
	
  setInterval("cycleSlideshowPanels()", 5000);
}

function cycleSlideshowPanels() {
  var $current = $(".slideshow > h1:visible, .slideshow > h2:visible");
	var $next = $current.next();
	if ($next.length == 0) {
		$next = $(".slideshow > h1:first-child, .slideshow > h2:first-child");
	}
	$current.fadeOut('medium', function() {
	    $next.fadeIn('medium');
	});
	
	//
}

function enableTruncateHeader() {
	if ($(".home .normal").length == 0)
		return;

	$(".home .normal h3 span").each(function() {
		if ($(this).text().length > 15) {
			var text = $(this).text().substring(0, 12) + "...";
			$(this).text(text);
		}
	});

	$(".home .panel ul li a").each(function() {
		if ($(this).text().length > 21) {
			var text = $(this).text().substring(0, 18) + "...";
			$(this).text(text);
		}
	});
}

// ]]>
