
// *********
//
// this js module depends on stars.js (configVoteStars).
// tries to be compat. w/ use for links and buttons. For links there will be only 1 pic
// in the series row.
//
// *********

var _picCount;
var _currentPic=1; //1-based
var _picsArray;
var _buttonArray;

// init fn. setup picsArray values
function prepareButtonsAndInitPics() {	
	_currentPic=1; // on page load pic #1 is first for display
	var parent = document.getElementById("seriesPicRow"); //div container that holds pics
	_picsArray = parent.childNodes;
	_picCount = _picsArray.length;
	//printDOMTree(elem);
	
	if (_picCount > 1) {
		parent = document.getElementById("navLinks"); //span that holds buttons
		_buttonArray = parent.childNodes;
	} else {
		_buttonArray = null;
	}
	absolutePic(1);
}

// pic numbering is 1-based for html use, which lines up with button numbering,
// but 0-based internally because its an array
function get(elem) {
	return _picsArray[elem-1];
}

function getButton(elem) {
	return _buttonArray[elem-1];
}

function nextPic() {
	absolutePic(_currentPic+1);	
}

function prevPic() {
	absolutePic(_currentPic-1);	
}

// all page (photo) selections and page changes go thru this fn
function absolutePic(toPic) {
	hide(get(_currentPic));
	//alert("id="+ getId());
	_currentPic = toPic;
	show(get(_currentPic));
	checkButtons();
	configVoteStars(document.getElementById(getId()));
	sendGetReqToServer("/PopularPics/Stats?pid=" + getId());
	updateCopyLink(getId());
}

//re-do the copyLink with the current pic id. called when user clicks next or
//prev photo button or link. The copylink is an anchor-img inside a div.
//do this in js because if we are using nav button the page will not get
//refreshed for next-pic so the server will not get called
function updateCopyLink(id) {
	var tbox = document.getElementById('copyLinkTxtBox');
	 tbox.value="<img src="+ "\"" +  "www.Popular-Pics.com/PopularPics/Photo?photoId=" +id + "\"" +"><a href=\"http://www.popular-pics.com\">More popular and funny pictures at Popular-Pics.com</a>";
	 //tbox.cols = 50;
	 //tbox.rows = 2;
	 tbox.readOnly = true;
}

function show(elem) {
	elem.style.display="block";
}

function hide(elem) {
	elem.style.display="none";
}

function hideAll() {
	for (var i=0;i<_picCount;i++) {
		hide(get(i));
	}
}

// highlight button for curr pic
// enable,disable next,prev as needed
function checkButtons() {
	if (_buttonArray == null)
		return;
	
	var prev= 0; // idx to 1st button in array
	var next= _buttonArray.length-1; //idx to last button in array
	
	for(var i=0;i< _buttonArray.length; i++) {
		_buttonArray[i].style.backgroundColor="#ffffff";		
	}		
	_buttonArray[_currentPic].style.backgroundColor="#ddd";
	
	if ((_currentPic-1) <= prev)
		_buttonArray[prev].disabled= true;
	else
		_buttonArray[prev].disabled= false;
	
	if ((_currentPic) >= next-1)
		_buttonArray[next].disabled= true;
	else
		_buttonArray[next].disabled= false;
}

// get id of current pic. Children of seriesPicList all have a 'pid_' or 'psid_' ID naming
function getId() {
	return get(_currentPic).id;
}
