﻿ 

function LoadAutomaticBillboardChange(object, dataIds, linkIds, defaultDataId, defaultLinkId, delay, id)
{
    window['rotationDataIds' + id] = dataIds;
    window['rotationLinkIds' + id] = linkIds;
    window['rotationDelay' + id] = delay;

    clearInterval(window['rotationIntervalTimeoutId' + id]);
    
    window['rotationIntervalTimeoutId' + id] = setInterval(function() { RotateBillboard(defaultDataId, defaultLinkId, id); }, window['rotationDelay' + id]);

  //  DisplayRandomFlipper(defaultDataId, defaultLinkId, id);
 
}

function RotateBillboard(defaultDataId, defaultLinkId, id)
{
    var rotationDataIds = window['rotationDataIds' + id];
    var rotationLinkIds = window['rotationLinkIds' + id];
    var currentRotationIndex = window['currentRotationIndex' + id];
    
    if(window['RunBillboardSwitching' + id] == null || window['RunBillboardSwitching' + id])
    {
        if(rotationDataIds != null && rotationLinkIds != null)
        {
            if(currentRotationIndex < rotationDataIds.length - 1)
            {
                currentRotationIndex++;
            } 
            else 
            {
                currentRotationIndex = 0;
            }

            var divId = rotationDataIds[currentRotationIndex];
            var linkId = rotationLinkIds[currentRotationIndex];
            
            window['currentRotationIndex' + id] = currentRotationIndex;
               
            DisplayFlipperData(divId, linkId, defaultDataId, defaultLinkId, id);
        }
    }
}

function DisplayFlipperData(dataID, linkID, defaultDataID, defaultLinkID, id)
{
   
    var currentItem = window['currentVisibleFlipperItem' + id];
    var currentLink = window['currentVisibleFlipperLink' + id];
   
    var newItem = $(dataID);
    var newLink = $(linkID);
    
    if(currentItem == null)
    {
        currentItem = $(defaultDataID);
    }
    
    if(currentLink == null)
    {
        currentLink = $(defaultLinkID);
    }   
    
    if(currentItem != null && newItem != null && currentLink != null && newLink != null)
    {
        if(linkID != currentLink.id)
        {
            currentItem.fade({duration: 0.3 });
            newItem.appear({duration: 0.3, queue: 'end' });
            
            currentLink.className = '';
            newLink.className = 'Selected';
            
            window['currentVisibleFlipperItem' + id] = newItem;
            window['currentVisibleFlipperLink' + id] = newLink;
            
            var rotationIntervalTimeoutId = window['rotationIntervalTimeoutId' + id];
            var rotationDelay = window['rotationDelay' + id];
            
            clearInterval(rotationIntervalTimeoutId);
            
            window['rotationIntervalTimeoutId' + id] = setInterval(function() { RotateBillboard(defaultDataID, defaultLinkID, id); }, rotationDelay);
        }
    }
}
 


function DisplayRandomFlipper(defaultDataId, defaultLinkId, id) {
    
    var boardCookie = readCookie("DnDBillboard");    
    var newDiv = 0;
    
    if (boardCookie == null || boardCookie == ""){
        createCookie("DnDBillboard","seen",1);
    } else {
        var newDiv = rand(5);
    }  
    
    var rotationDataIds = window['rotationDataIds' + id];
    var rotationLinkIds = window['rotationLinkIds' + id];
    var divId = rotationDataIds[newDiv];
    var linkId = rotationLinkIds[newDiv];
    window['currentRotationIndex' + id] = newDiv;

    if (newDiv == 0) {
        document.getElementById(defaultDataId).style.display = "block";
        document.getElementById(defaultLinkId).className = "Selected";
        DisplayFlipperData(divId, linkId, defaultDataId, defaultLinkId, id);
    } else {
        document.getElementById(divId).style.display = "block";
        DisplayFlipperData(divId, linkId, defaultDataId, defaultLinkId, id);
    }

}







// ===||| RANDOM NUMBERS |||================

rnd.today=new Date();
rnd.seed=rnd.today.getTime(); 

function rnd() {
	rnd.seed = (rnd.seed*9301+49297) % 233280;
	return rnd.seed/(233280.0);
}

function rand(number) {
	return Math.floor(rnd()*number);
}

function dice(number) {
	return Math.floor(rnd()*number)+1;
}


// =======================================================

function createCookie(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=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

 


