var globalCurrStory;var globalTimeout;var globalInterval;var globalBegPage;var globalEndPage;
if (typeof(PHP2Controls) == 'undefined') PHP2Controls = new Object();
PHP2Controls.TopStoriesManager = function(wsUrl, currStory, interval, bPage, ePage)
{this.wsUrl= (wsUrl != null) ? wsUrl : '/wsdl/main.ajax.php';
this.sRootUrl = '/';
this.currStory= (currStory != null) ? currStory : 1;
this.interval = (interval != null)? interval : 5000;
globalCurrStory = this.currStory;
globalInterval= this.interval;
globalBegPage = bPage;
globalEndPage = ePage;
var currentObject = this;
if (document.getElementById('linkPrev'))
{document.getElementById('linkPrev').onclick = function()
{currStory = currStory - 1;
globalCurrStory = currStory;
globalInterval = 0;
topStoriesManager.doShowStory();
}}

 if (document.getElementById('linkNext'))
 {
document.getElementById('linkNext').onclick = function()
{
currStory = currStory + 1;
globalCurrStory = currStory;
globalInterval = 0;
topStoriesManager.doShowStory();
}
 }

globalCurrKind = currKind;
this.setVisiblePages();

if (this.interval > 0)
{
 currentObject.doShowStory();//this.interval
}

for(i = 1; i <= 7; i++)
{
document.getElementById('topStory'+i).onmouseout = function()
{
 var str = new String(this.id);
 i = parseInt(str.substr(8,str.length-8));
 if (i != globalCurrKind)
 {
 this.className = 's_ts_item'+i;
 }
}

document.getElementById('topStory'+i).onmouseover = function()
{
 var str = new String(this.id);
 i = parseInt(str.substr(8,str.length-8));
 this.className = 'h_ts_item'+i;
}

document.getElementById('topStory'+i).onclick = function()
{
 var str = new String(this.id);
 globalCurrKind = parseInt(str.substr(8,str.length-8));
 this.className = 'h_ts_item'+globalCurrKind;
 globalCurrStory = 1;
 currStory = 1;
 globalInterval= currInterval;
 window.clearTimeout(globalTimeout);

 currentObject.doShowStory();
 for(j = 1; j <= 7; j++)
{
 if (j != globalCurrKind)
 {
document.getElementById('topStory'+j).className = 's_ts_item'+j;
 }
}
}
}
}

function clickKind(elem)
{
 var str = new String(elem.id);
 globalCurrKind = parseInt(str.substr(8,str.length-8));
 elem.className = 'h_ts_item'+globalCurrKind;
}
PHP2Controls.TopStoriesManager.prototype.setVisiblePages = function()
{
var currentObject = this;

if (globalBegPage <= 0) globalBegPage = 1;
if (globalCurrStory - visibleLinesCount + 1 > globalBegPage) globalBegPage = globalCurrStory - visibleLinesCount + 1;
if (globalCurrStory < globalBegPage) globalBegPage = globalCurrStory;

if ((countPages - globalBegPage + 1) <= visibleLinesCount)
{
globalEndPage = countPages;
}
else
{
globalEndPage = visibleLinesCount + globalBegPage - 1;
}
 mainDiv1 = document.getElementById('pgnPageNavigator');
 var inner = '<table class="index_nav"><tr id="trNav">123456</tr></table>';
 mainDiv = '';
 if (globalCurrStory > 1)
 {
mainDiv += '<td><a id="linkPrev" href="#nul;" class="link_numb" onclick="javascript:currStory = currStory - 1;globalCurrStory = currStory;globalInterval = 0;/*topStoriesManager.setVisiblePages();setContentDiv(globalBegPage, globalEndPage, globalCurrStory);*/topStoriesManager.doShowStory();"><span><</span></a></td>';
mainDiv += '<td class="navsplitter"></td>';
 }
 for(i = globalBegPage; i <= globalEndPage; i++)
 {
mainDiv += '<td id="div'+i+'"></td>';
mainDiv += '<td class="navsplitter"></td>';
 }
 if (globalCurrStory < countPages)
 {
mainDiv += '<td><a id="linkNext" href="#nul;" class="link_numb" onclick="javascript:currStory = currStory + 1;globalCurrStory = currStory;globalInterval = 0;/*topStoriesManager.setVisiblePages();setContentDiv(globalBegPage, globalEndPage, globalCurrStory);*/topStoriesManager.doShowStory();"><span>></span></a></td>';
 }
 mainDiv1.innerHTML = inner.replace('123456',mainDiv);
 for(i = globalBegPage; i <= globalEndPage; i++)
 {
 if (document.getElementById('div'+i))
 {
 var alt = titleStories[globalCurrKind][i];
 document.getElementById('div'+i).innerHTML = '<a href="#nul;" id="link'+i+'" class="link_numb" alt="'+alt+'" title="'+alt+'" ><span id="spn'+i+'">'+i+'</span></a>';
 document.getElementById('link'+i).onclick = function()
 {
var str = new String(this.id);
globalCurrStory = parseInt(str.substr(4,str.length-4));
currStory = globalCurrStory;
globalInterval = 0;
currentObject.doShowStory();
 };
 }
 }
}
setContentDiv = function(bPage, ePage, currPage)
{
 var currentObject = this;
 for(i = bPage; i <= ePage; i++)
 {
if (i == currPage)
{
 document.getElementById('spn'+i).className = 'numb_block'; //'<span class="numb_block">'+globalCurrStory+'</span>';
 document.getElementById('link'+i).className = ''; //'<span class="numb_block">'+globalCurrStory+'</span>';
}
else
{
 document.getElementById('spn'+i).className = '';
 document.getElementById('link'+i).className = 'link_numb';
}
 }
}
PHP2Controls.TopStoriesManager.prototype.setSRootUrl = function(sRootUrl)
{
this.sRootUrl = sRootUrl;
}
PHP2Controls.TopStoriesManager.prototype.doShowStory = function()
{
this.serverResponse = new PHP2Ajax.JSONRequest(this.wsUrl+'?rand='+Math.random());
this.serverResponse.call('doShowStory');
this.currStory = globalCurrStory;
currStory = globalCurrStory;
this.serverResponse.add("currStory", this.currStory);
this.serverResponse.add("currKind",globalCurrKind);
this.serverResponse.setHandler(this.onStoryChecked);
this.serverResponse.onResponseError = this.onResponseError;
this.serverResponse.currentObject= this;
this.serverResponse.execute();
var currentObject = this;
this.interval = globalInterval;
if (this.interval > 0)
{
 interval = this.interval;
 var func = function () {currentObject.doShowStory();}
 globalTimeout = window.setTimeout(func,interval);
}
else
{
 window.clearTimeout(globalTimeout);
}
}

var timeoutShow;
var img;
var title;
var description;
var link;
var author;
PHP2Controls.TopStoriesManager.prototype.doMorphShow = function()
{
 document.getElementById('topStoriesId').style.display = 'block';
 document.getElementById('imgTopStoryId').src = img;
 document.getElementById('spnTitleTopStoryId').innerHTML = title;
 document.getElementById('txtAuthorTopStoryId').innerHTML = author;
 document.getElementById('txtDescriptionTopStoryId').innerHTML = description;
 document.getElementById('linkDetails').href = link;
 document.getElementById('linkImageDetails').href = link;
 globalDivContent = document.getElementById('topStoriesId').innerHTML;

 return true;
}

 var globalDivContent;
 globalDivContent = document.getElementById('topStoriesId').innerHTML;

PHP2Controls.TopStoriesManager.prototype.onStoryChecked = function()
{
var loginMessage;

img = this.response.Response.Story.image;
title = this.response.Response.Story.title;
author = this.response.Response.Story.author;
description = this.response.Response.Story.description;
link = this.response.Response.Story.url;
countPages = this.response.Response.Story.count;
if(countPages < 2) globalInterval = 0;
globalBegPage = 1;

if (countPages > 0)
{
document.getElementById('topStoriesId').innerHTML = globalDivContent;
if (document.getElementById('topStoriesId').style.display != 'none')
{
}

this.currentObject.doMorphShow();
topStoriesManager.setVisiblePages();
setContentDiv(globalBegPage, globalEndPage, globalCurrStory);
}
else
{
 document.getElementById('topStoriesId').innerHTML = '<h1 class="warning">Not Found Stories <br /><br />There are no stories in this section yet.</h1>';
}

globalCurrStory = parseInt(this.response.Response.nextStory);

return true;
}

PHP2Controls.TopStoriesManager.prototype.onResponseError = function()
{
this.currentObject.alert = new PHP2Controls.Alert("Error: " + this.response.Error.Code + ". " + this.response.Error.Message);
this.currentObject.hideLoading();
}
PHP2Controls.TopStoriesManager.prototype.showLoading = function(){}
PHP2Controls.TopStoriesManager.prototype.hideLoading = function(){}
PHP2Controls.TopStoriesManager.prototype.alert = function(errorMessage, errorCode)
{
this.currentAlert = new PHP2Controls.Alert(errorMessage);
}