//Environment Settings
var app           = 'http://www.paddypower.com/bet';
var app_secure    = 'https://www.paddypower.com/bet';
var web           = 'http://www.paddypower.com';


//
// Function for getting cookies
//

function getCookie(name) 
	{
    var prefix = name + "=";
    var begin = document.cookie.indexOf(prefix);
    if (begin == -1) return null;
    var end = document.cookie.indexOf(";", begin);

    if (end == -1) end = document.cookie.length;
    return (unescape(document.cookie.substring(begin + prefix.length, end)));
	} 

function set_cookie(name,value,days)	{
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/; domain=.paddypower.com";

}	


function getUserPref() {
  
  var bp_cookie = getCookie('BP_STATUS');

  var userType = "New User"
  
  if(bp_cookie == 'Y') userType = 'Bingo Player: BP_STATUS = ' + bp_cookie + '\n';
  else if(bp_cookie == 'N') userType = 'Bingo Player: BP_STATUS = ' + bp_cookie + '\n';
  
  
  return userType;
}



function go_register(AFFILIATE_IDS){
 window.open(app_secure + "?action=go_register_init&bingo=1&bus_channel=BI&"+ AFFILIATE_IDS, "pp_registration" , "width=642, height=600, scrollbars=no,  menubar=no, status=no, scrollbars=no, resizable=yes,screenX=5, screenY=5, left=5, top=5");
} 

function popAlias(){
	var vars=popAlias.arguments[0];
	popup(app_secure + '?action=go_bingo_create_alias'+vars,'width=400,height=560');
	return false;
}


function popup(url, features) {
	var defaultFeatures = '\,location=0,statusbar=0,menubar=0';
    features += defaultFeatures;
  var target = 'GameWindow';
	var theWindow = window.open(url, target, features);
	theWindow.focus();
	return theWindow;
}

function popRoom(url, features) {
	var defaultFeatures = '\,location=0,statusbar=0,menubar=0';
    features += defaultFeatures;
  var target = 'BingoRoom';
	var theWindow = window.open(url, target, features);
	theWindow.focus();
	return theWindow;
}


function show(id) {
var d = document.getElementById(id);
for (var i = 1; i<=10; i++)
  {
    if (document.getElementById('smenu'+i))
      {
        document.getElementById('smenu'+i).style.display='none';
      }
  }
if (d)
  {
    d.style.display='block';
  }
}



function popBingo(form, popName){
	allowResize = "no";
	allowResize = "yes";
	
	// Start at top left
	LeftPosition = 0;
	TopPosition = 0;
	
	// Go almost full-screen for Flash Bingo
	myHeight = (screen.height) ? (screen.height-145) : 0;
	myWidth = myHeight * 4 / 3;
	
	// Set the left and top window position to zero if logic above has produced a value less than zero
	if (LeftPosition < 0){
		LeftPosition = 0;
	}
	
	if (TopPosition < 0){
		TopPosition = 0;
	}
	
	// Configure other window properties
	windowprops = "left="+LeftPosition+",top="+TopPosition+",width="+myWidth+",height="+myHeight+",location=0,scrollbars=0,menubar=0,toolbar=0,resizable=" + allowResize;
	
	// Open window as object
	var bingoWindow = window.open("", popName, windowprops);
	
	// Set form target to bingo window name
	form.target = popName;
	
	// Submit form
	form.submit();
	
	// Focus on bingo window by object reference
	bingoWindow.focus();
	
	return true;
}


function popbingo(url) {
	var allowResize = "yes";

//	 Start at top left
	var LeftPosition = 0;
	var TopPosition = 0;

//	 Go almost full-screen for Flash Bingo
	var myHeight = (screen.height) ? (screen.height-145) : 0;
	var myWidth = myHeight * 4 / 3;

//	 Set the left and top window position to zero if logic above has produced a value less than zero
	if (LeftPosition < 0) {	LeftPosition = 0;}
	if (TopPosition < 0) {	TopPosition = 0;}

//	 Configure other window properties
	var windowprops = "left="+LeftPosition+",top="+TopPosition+",width="+myWidth+",height="+myHeight+",location=0,scrollbars=0,menubar=0,toolbar=0,resizable=" + allowResize;

//	 Open window as object
	var bingoWindow = window.open(url, '_blank', windowprops);
	bingoWindow.focus();
	return bingoWindow;
}

// Open Game
/*
function open_game(name, game, thirdparty, width, height) {

	if(thirdparty == 1) {
		var url = 'http://www.paddypower.com/bet?action=go_thirdparty_url&redirect=' + game + '&promo=game_' + name + '&crea=java';
		var login_url = encode_url("https://www.paddypower.com/bet?action=go_thirdparty_login");
		url = url + "&login_url=" + login_url;
	} else {
		var url = 'http://www.paddypower.com/iframe/games/' + game + '?promo=game_' + name + '&crea=java';
	}
	var options = 'resizable=1, scrollbars=0, menubar=0, status=0, width=' + width + ', height=' + height;
	game_window = window.open(url, name, options); 
}
*/

function bookmarksite(title,url){
if (window.sidebar) // firefox
	window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
	var elem = document.createElement('a');
	elem.setAttribute('href',url);
	elem.setAttribute('title',title);
	elem.setAttribute('rel','sidebar');
	elem.click();
} 
else if(document.all)// ie
	window.external.AddFavorite(url, title);
}


// to show chat rules 
function expand(){
//close all rules
var allTags = document.getElementsByTagName('*');
 for(i=0; i<allTags.length; i++){
  if(allTags[i].className == 'more') allTags[i].style.display = 'none';
 }

	//open wanted chat rule
	for (var i=0; i<expand.arguments.length; i++) {
    var element = document.getElementById(expand.arguments[i]);
		element.style.display = (element.style.display != 'none') ? 'none' : 'block';
	}
}



// to hide and show bingo schedule 
function expandCollapse(){
	for (var i=0; i<expandCollapse.arguments.length; i++) {
    		var element = document.getElementById(expandCollapse.arguments[i]);
		element.style.display = (element.style.display != 'none') ? 'none' : 'block';
		//element.style.display = (element.style.display != 'none') ? 'none' : 'table';
	}
}



// to hide and show bingo schedule 
function expandCollapseTable(){
	for (var i=0; i<expandCollapseTable.arguments.length; i++) {
    		var element = document.getElementById(expandCollapseTable.arguments[i]);
		element.style.display = (element.style.display != 'none') ? 'none' : '';
		//element.style.display = (element.style.display != 'none') ? 'none' : 'table';
	}
}



// to hide and show terms
function expandCollapseTerms(){

//if not clicking on the current expanded link set all backgrounds back to yellow and close all open terms
var element1 = document.getElementById(expandCollapseTerms.arguments[0]);
if (element1.style.display != 'block')
resetTerms();

for (var i=0; i<expandCollapseTerms.arguments.length; i++) {
    	var element = document.getElementById(expandCollapseTerms.arguments[i]);
	if(i==3){
		element.className = (element.className == 'yellowboxgameholdercurrent') ? 'yellowboxgameholder' : 'yellowboxgameholdercurrent';
	}else{
		element.style.display = (element.style.display != 'none') ? 'none' : 'block';
	}
}
	
}


// to hide and show promo terms
function expandCollapsePromoTerms(){

//if not clicking on the current expanded link set all backgrounds back to yellow and close all open terms
var element1 = document.getElementById(expandCollapsePromoTerms.arguments[0]);
if (element1.style.display != 'block')
resetTerms();

for (var i=0; i<expandCollapsePromoTerms.arguments.length; i++) {
    	var element = document.getElementById(expandCollapsePromoTerms.arguments[i]);
	if(i==3){
		element.className = (element.className == 'yellowboxpromoholdercurrent') ? 'yellowboxpromoholder' : 'yellowboxpromoholdercurrent';
	}else{
		element.style.display = (element.style.display != 'none') ? 'none' : 'block';
	}
}
	
}

function resetTerms(){
//this should set all backgrounds back to yellow and close all open terms
var allTags = document.getElementsByTagName('*');
 	for(i=0; i<allTags.length; i++){
  	if(allTags[i].className == 'termsleft more left') allTags[i].style.display = 'none';
  	if(allTags[i].className == 'termsright more right') allTags[i].style.display = 'none';
	if(allTags[i].className == 'link') allTags[i].style.display = 'block';
  	if(allTags[i].className == 'link2') allTags[i].style.display = 'none';
  	if(allTags[i].className == 'yellowboxgameholdercurrent') allTags[i].className = "yellowboxgameholder";
  	
  	
  	//for new promo big box
  	if(allTags[i].className == 'yellowboxpromoholdercurrent') allTags[i].className = "yellowboxpromoholder";
 	}
}



function flickItOpen(tab, id, type){

	switchTab(tab);

for(var i=0; i<3; i++) {
var name;
	if(i==0){ name = 'more';}
	if(i==1){ name = 'link';}
	if(i==2){ name = 'less';}
	var element = document.getElementById(name + id );
	element.style.display = (element.style.display != 'none') ? 'none' : 'block';	
}

if(document.getElementById('gameholder' + id)){
  document.getElementById('gameholder' + id).className = "yellowboxgameholdercurrent"; 
}else if(document.getElementById('promoholder' + id)){
  //for new promo big box
  document.getElementById('promoholder' + id).className = "yellowboxpromoholdercurrent";
}
  
  
}



//switch bingo tabs on games/promotions pages
function switchTab(){
	var i=1;
    for (i=1; i<10; i++){
		if (document.getElementById('l'+i)!=null){
        	element = document.getElementById('l'+i);
        	if (i==switchTab.arguments[0]){
        		element.className='current';
        	}else{
            		if (element.className=='inactive'){
            			element.className='inactive';
            		}else{
            			element.className='';
            		}
          	}
      	}
    }

    for (i=1; i<10; i++){
		if (document.getElementById('tab'+i)!=null){
        	element = document.getElementById('tab'+i);
        	element.style.display = (i==switchTab.arguments[0]) ? 'block' : 'none';
		}
    }
}


//for pp channel jump menu
function menu_goto( menuform )
{
    selecteditem = menuform.newurl.selectedIndex ;
    newurl = menuform.newurl.options[ selecteditem ].value ;
    if (newurl.length != 0) {
      location.href = newurl ;
    }
}




//leading zero formating...
function LZ(x) {
	return (x >= 10 || x < 0 ? "" : "0") + x;
}

function countdown_clock(target_miliSeconds, format, divname){
	/*html_code = '<div id="'+ divname + '" class="cntdiv"><\/div>';
	document.write(html_code);*/
	countdown(target_miliSeconds, format, divname);
}

function countdown(target_miliSeconds, format, divname){
    var countDiv=document.getElementById(divname);
    // substract 1 sec
    var Time_Left = Math.round((target_miliSeconds - 1000));
    if(Time_Left < 0){
    	Time_Left = 0;
    }

    switch(format)
    {
	case 0:
			countDiv.innerHTML = Time_Left / 1000 + ' seconds';
			break;
	case 1:
			//More datailed.
			// convert time into seconds
			Time_Lef = Math.round(Time_Left / 1000);
			days = Math.floor(Time_Lef / (60 * 60 * 24));
			Time_Lef %= (60 * 60 * 24);
			hours = Math.floor(Time_Lef / (60 * 60));
			Time_Lef %= (60 * 60);
			minutes = Math.floor(Time_Lef / 60);
			Time_Lef %= 60;
			seconds = Time_Lef;

			countDiv.innerHTML = LZ(hours) + ':' + LZ(minutes) + ':' + LZ(seconds);
			break;
	default:
			countDiv.innerHTML = Time_Left + ' seconds';
    }

    //Recursive call, keeps the clock ticking.
    if (Time_Left>0){
		setTimeout('countdown('+ Time_Left + ',' + format + ', \''+divname+'\');', 1000);
	}
	else{
		//countDiv.innerHTML = 'already started';
		countDiv.innerHTML = 'Started';
	}
}



//working
function queryString(parameter) {
  var loc = location.search.substring(1, location.search.length);
  var param_value = false;

  var params = loc.split("&");
  for (i=0; i<params.length;i++) {
      param_name = params[i].substring(0,params[i].indexOf('='));
      if (param_name == parameter) {
          param_value = params[i].substring(params[i].indexOf('=')+1)
      }
  }
  if (param_value) {
      return param_value;
  }
  else {
      return false; //Here determine return if no parameter is found
  }
}



//Added for PPY search engine referral tracking

var seo_ref = document.referrer.toLowerCase();
	
if ((seo_ref.indexOf("google") > 0) && (!getCookie('AFF_ID')) ) {
 
	if ((seo_ref.indexOf("paddy") < 0)) {
    	set_cookie('AFF_ID', '60011', '');
  	}
  	else {
  	  		set_cookie('AFF_ID', '1707', '');
  	}
} else if ((seo_ref.indexOf("yahoo") > 0) && (!getCookie('AFF_ID'))) { 
  if((seo_ref.indexOf("paddy") < 0) )
    set_cookie('AFF_ID', '60013', '');
  else
    set_cookie('AFF_ID', '1709', '', '/');
    
} else if ( ((seo_ref.indexOf("bing") > 0) || (seo_ref.indexOf("msn") > 0)) && (!getCookie('AFF_ID')) ) {
  	if((seo_ref.indexOf("paddy") < 0) )
    	set_cookie('AFF_ID', '60012', '');
  	else
    	set_cookie('AFF_ID', '1708', '');
}

 
function unicaEventHandler(section, button_tag)
{
  	ntptAddPair("button", button_tag);
	  ntptEventTag("ev=Bingo_" + section);	
}





