function showPage(id,jsonObj) {
	var html = '';
	var index = 0;
	// Builds Photos HTML

	for (var i=0; i<jsonObj["pages"][id].length; i++) 
    {
		//html += '<a href="'+collectionPath+photos[index][i]+'/" title="View This Photo">';
        
		html += '<img src="http://images.ringtv.com/images' + padID(jsonObj["pages"][id][i].image_id) + '_80_80.jpg" width="80" height="80" border="0" onclick="showImageInfo(' + jsonObj["pages"][id][i].image_id + ',jsonObj); return false;"';
        /*
        if (jsonObj["pages"][id][i].image_id == selected_id) 
        {
            html += 'class="selected" ';
        }
        */
		html += '/>';
		//html += '</a>';
	}
	
	// Assigns Previous Page Button
	
    if(jsonObj["pages"].length > 1)
    {
        var ppImage = document.getElementById('PreviousPageImage');
	    if (id == 0){
        
		    ppImage.src = imageServerPath+'/images/left_arrow_off.gif';
            
		    ppImage.title = '';
            
		    ppImage.onclick = function() {}
            
	    } else {
            index = Number(id)-1;
		    ppImage.src = imageServerPath+'/images/left_arrow_on.gif';
		    ppImage.title = '< Previous Page';
		    ppImage.onclick = function() {
			    showPage(index,jsonObj);
		    }
		    
	    }
	    
	    // Assigns Next Page Button
        var maxPages = jsonObj["pages"].length - 1;
	    var npImage = document.getElementById('NextPageImage');
	    if (id < maxPages){		
            index = Number(id) + 1;
		    npImage.src = imageServerPath+'/images/right_arrow_on.gif';
		    npImage.title = 'Next Page >';
		    npImage.onclick = function() {
			    showPage(index,jsonObj);
		    }
            
	    } else {
		    npImage.src = imageServerPath+'/images/right_arrow_off.gif';
		    npImage.title = '';
		    npImage.onclick = function() {}
		    
	    }
    }
    else
    {
        document.getElementById('PreviousPageImage').style.visibility="hidden";
        document.getElementById('NextPageImage').style.visibility="hidden";
    }
	
	// Loads Photo HTML
	document.getElementById('Photos').innerHTML = html;
}

function padID(id) {
	var str = new String();
	str = String(id);
	var howMany = 8-str.length;
	for (var i=0; i<howMany; i++) {
		str = '0'+str;
	}
	var path = '/7/';
	for (var i=0; i<4; i++) {
		path += str.substr(i*2,2)+'/';
	}
	path += id;
	return path;
}

function padMainID(id) {
    var str = new String();
    str = String(id);
    var howMany = 8-str.length;
    for (var i=0; i<howMany; i++) {
        str = '0'+str;
    }
    var path = '/7/';
    for (var i=0; i<4; i++) {
        path += str.substr(i*2,2)+'/';
    }
    path += id;
    return path;
}

function flipChart(){
	if (document.getElementById('OthersByUser').style.display == 'block') {
		document.getElementById('OthersByUser').style.display = 'none';
		document.getElementById('RelatedCollections').style.display = 'block';
	} else {
		document.getElementById('OthersByUser').style.display = 'block';
		document.getElementById('RelatedCollections').style.display = 'none';
	}
}

function showImageInfo(image_id,jsonObj)
{

    var url_path = jsonObj["url_path"];
    var gallery_id = jsonObj["gallery_id"];
    var next_id = 0;
    var previous_id = 0;
    var previous_link = '';
    var previous_arrow = '<div class="arrow_prev_end"></div>';
    var next_link = '';
    var next_arrow = '<div class="arrow_next_end"></div>';
    var description = '';
    var currentPage = 0;
    
    parent.location.hash = image_id;
    
    // find next / previous page
    for(page in jsonObj["pages"])
    {
        for(image in jsonObj["pages"][page])
        { 
            
            if(jsonObj["pages"][page][image].image_id == image_id)
            {
            
                next_id = jsonObj["pages"][page][image].next_id;
                previous_id = jsonObj["pages"][page][image].previous_id;
                description = jsonObj["pages"][page][image].name;
                currentPage = page;
                
                if(previous_id != 0)
                {
                    previous_link = '<a href="' + url_path + '/#' + previous_id + '" onclick="showImageInfo(' + previous_id + ',jsonObj); return false">&lt; Previous</a> | ';
                    previous_arrow = '<a class="arrow_prev" href="' + url_path + '/#' + previous_id + '" onclick="showImageInfo(' + previous_id + ',jsonObj); return false"><em></em></a>';
                }
                
                if(next_id != 0)
                {
                    next_link = '<a href="' + url_path + '/#' + next_id + '" onclick="showImageInfo(' + next_id + ',jsonObj); return false">Next &gt;</a>';
                    next_arrow = '<a class="arrow_next" href="' + url_path + '/#' + next_id + '" onclick="showImageInfo(' + next_id + ',jsonObj); return false"><em></em></a>';
                }
                
                document.getElementById("paginationLinks").innerHTML =  previous_link + next_link ;
                
                document.getElementById("photo_arrows_links").innerHTML =  previous_arrow + next_arrow ;
                
            }
        }
    }

    document.getElementById("shareLinksInfo").innerHTML = '<p>SHARE:</p> <iframe src="/share_links.php?url=' + url_path + '/#' + image_id + '&title=' + jsonObj["title"] + '" marginheight="0" marginwidth="0" frameborder="0" height="25" width="250" scrolling="no" /> ';
    
    document.getElementById("selectedImage").innerHTML = '<a href="http://images.ringtv.com/images' + padMainID(image_id) + '_605.jpg" target="_blank"><img src="http://images.ringtv.com/images' + padMainID(image_id) + '_605.jpg" border="0" alt="" name="largeImg" /></a>';
    document.getElementById("image_description").innerHTML = description;
    
    document.share_links.link_path.value = url_path + '/#' + image_id;
    document.share_links.embed_path.value = 'http://images.ringtv.com/images' + padMainID(image_id) + '_m605.jpg';
    if(currentPage != tempPage)
    {
        tempPage = currentPage;
        showPage(currentPage,jsonObj);
    }
    
    document.getElementById('ad_ord').value = 0;
    document.getElementById("LREC").innerHTML = '<iframe src="/ads/doubleclick.php?dart=sportnet.rtv.photos/detail&w_value=300&h_value=250&tile=1" frameborder="0" width="300" height="250" marginwidth="0" marginheight="0"></iframe>';
    document.getElementById("HLREC").innerHTML = '<iframe src="/ads/doubleclick.php?dart=sportnet.rtv.photos/detail&w_value=300&h_value=100&tile=2" frameborder="0" width="300" height="100" marginwidth="0" marginheight="0"></iframe>';
    document.getElementById("BANNER").innerHTML = '<iframe src="/ads/doubleclick.php?dart=sportnet.rtv.photos/detail&w_value=728&h_value=90&tile=3" frameborder="0" width="728" height="90" marginwidth="0" marginheight="0"></iframe>';
}
