$(document).ready(function()
{
    loadPage(page_id);
    transMasthead_init('home');
});

function loadPage(pageID)
{    
    //alert(pageID);
    transMasthead(pageID);
    
    document.getElementById('maincontent').style.visibility = 'hidden';
    window.scroll(0,0);
    
    
    for(i=0;i<pageListArr.length;i++)
    {
        if(document.getElementById(pageListArr[i]))
        {
            document.getElementById(pageListArr[i]).innerHTML = "<a href='javascript:loadPage(&quot;" + pageListArr[i] + "&quot;);'  onmouseover=\"document.images['header_" + pageListArr[i] + "'].src='images/topnav/tnav_" + pageListArr[i] + "_1.png'\" onmouseout=\"document.images['header_" + pageListArr[i] + "'].src='images/topnav/tnav_" + pageListArr[i] + "_0.png'\"><img src='images/topnav/tnav_" + pageListArr[i] + "_0.png' name='header_" + pageListArr[i] + "' alt='' title='' border='0' /></a>";
        }
        
        if(document.getElementById('footer_' + pageListArr[i]))
        {
            document.getElementById('footer_' + pageListArr[i]).style.color = "#ffffff";
            
            $('#footer_' + pageListArr[i]).hover(function () {
	            $(this).css("color", "#f2bf0c");
            },
		        function () {
		        $(this).css("color", "#ffffff");
	        });
        }
    }
    
    if(pageID != 'ux' && pageID != 'ar')
    {
        document.getElementById(pageID).innerHTML = "<img src='images/topnav/tnav_" + pageID + "_1.png' alt='' title='' border='0' />";
    }
    
    if(document.getElementById('footer_' + pageID))
    {
        document.getElementById('footer_' + pageID).style.color = "#f2bf0c";
    }
    $('#footer_' + pageID).hover(function () {
        $(this).css("color", "#f2bf0c");
    },
        function () {
        $(this).css("color", "#f2bf0c");
    });
    
    page_id = pageID;
    
    if(pageID == 'home')
    {
        document.getElementById('maincontent').style.marginLeft = '60px';
        document.getElementById('maincontent').style.marginBottom = '20px';
        
        $("#homelogo").unbind('click');
        $("#homelogo").css('cursor','auto');
    }
    else
    {
        document.getElementById('maincontent').style.marginLeft = '80px';
        document.getElementById('maincontent').style.marginBottom = '160px';
        
        $("#homelogo").unbind('click');
        $("#homelogo").click(function(event){
            loadPage("home");
        });
        $("#homelogo").css('cursor','pointer');
    }
    
    jQuery('#maincontent').load('pages/' + page_id + '.php',function(){document.getElementById('maincontent').style.visibility = 'visible';});
    
    
    
}

function hiliteSection(section)
{
    document.getElementById('footer_' + section).style.color = "#f2bf0c";
}

function deliteSection(section)
{
    if(page_id != section)
    {
        document.getElementById('footer_' + section).style.color = "#ffffff";
    }
}

function transMasthead_init(section)
{
    //alert(section);
    document.getElementById('masthead_' + section).style.display = "inline";
    jQuery('#masthead_' + section).fadeIn(1000)
    sectionID = section;
}

function transMasthead(section)
{
    //alert(sectionID + " " + section);
    if(section != sectionID)
    {
        jQuery('#masthead_' + sectionID).fadeOut(1000,"easeInOutQuad")
        jQuery('#masthead_' + section).fadeIn(1000,"easeInOutQuad")
        sectionID = section;
    }
}

//Initially sets up the Homepage Gallery
function getGallery()
{
    lineitemcount = 1;
    linecount = 1;

    pagecount = 0;
    
    galleryContent = '';
    
    galleryContent += "<div id='hp_gallery_mainimage'>";
    
    imgcount = parseInt(hpgImageCount[0]);
    if(imgcount > 1)
    {
        galleryContent += "<a href='javascript:nextGallery(0);'><img src='images/gallery/main_images/" + hpgImageName[0] + "_1.jpg' class='mainimage_show' id='mainimage_1' alt='' title='' border='0'></a>";
        
        for(ii=2;ii<=imgcount;ii++)
        {
            galleryContent += "<a href='javascript:nextGallery(0);'><img src='images/gallery/main_images/" + hpgImageName[0] + "_" + ii + ".jpg' class='mainimage_hide' id='mainimage_" + ii + " alt='' title='' border='0'></a>";
        }
        //alert(galleryContent);
    }
    else
    {
        galleryContent += "<img src='images/gallery/main_images/" + hpgImageName[0] + "_1.jpg' alt='' title='' border='0'>";
    }
    
    galleryContent += "</div>";
    galleryContent += "<div id='hp_gallery_info'><div id='hp_gallery_captiontitle'>Pew Health Group Website</div><div id='hp_gallery_captiontext'>";
    galleryContent += "Transparent title panels and images card sort to navigate featured news stories. Providing information for policy makers with data on current issues concerning health and financial well-being.";
    galleryContent += "</div><div class='clear'></div></div>";
    galleryContent += "<div id='hp_gallery_thumbselect'></div>";    
    galleryContent += "<div class='clear'></div><div class='hpg_greyline'></div>";
    galleryContent += "<div id='hpg_thumbs'>";
    
    for(i=0;i<hpgTitle.length;i++)
    {
        //alert("lic: " + lineitemcount + " lc: " + linecount + " pc: " + pagecount);
        if(linecount == 1 && lineitemcount == 1)
        {
            pagecount++;
        }
        
        if(lineitemcount == 1 && linecount == 1)
        {
            if(pagecount == 1)
            {
                galleryContent += "<div class='hpg_thumbs_page' id='thumbpage_" + pagecount + "'>";
            }
            else
            {
                galleryContent += "<div class='hpg_thumbs_page_hide' id='thumbpage_" + pagecount + "'>";
            }
        }
        
        galleryContent += "<div class='hpg_thumb_title'>";
        galleryContent += "<div class='hpg_thumb' id='thumb_bg_" + i + "' style='background-image:url(images/gallery/main_images/thumb_" + hpgImageName[i] + ".jpg);'>";
        galleryContent += "<img src='images/gallery/hpg_pict_overlay.png' name='thumb_" + i + "' id='thumb_" + i + "' alt='' title='' border='0' onclick='commandGallery(" + i + ");' onmouseover='swapIn(" + i + ");' onmouseout='swapOut(" + i + ");' />";
        galleryContent += "</div>";
        galleryContent += "<div class='hpg_title'><a href='javascript:commandGallery(" + i + ")' onmouseover='swapIn(" + i + ");' onmouseout='swapOut(" + i + ");'>" + hpgTitle[i] + "</a></div>";
        galleryContent += "</div>";
        
        //alert("lic: " + lineitemcount + " lc: " + linecount + " pc: " + pagecount);
        
           
        if(lineitemcount == 3)
        {
            galleryContent += "<div class='clear'></div>";
            lineitemcount = 0;
            linecount++;
        }
        
        if(linecount == 3)
        {
            linecount = 1;
            galleryContent += "</div>";
            galleryContent += "<div class='clear'></div>";
        }
        else
        {
            if(i == hpgTitle.length - 1)
            {
                galleryContent += "</div>";
            }
        }
        
        lineitemcount++;
        //alert("lic: " + lineitemcount + " lc: " + linecount + " pc: " + pagecount);
    }
    
    
    galleryContent += "<div class='clear'></div></div><div id='hpg_thumbs_indicator'>";
    
    if(pagecount > 1)
    {
        for(i=1;i<=pagecount;i++)
        {
            if(i == 1)
            {
                galleryContent += "<img src='images/gallery/thumbselected_lite.png' alt='' title='' border='0' name='pageInd_" + i + "' />";
            }
            else
            {
                galleryContent += "<img src='images/gallery/thumbselect_lite.png' alt='' title='' border='0' class='pointer' name='pageInd_" + i + "' onclick='changePage(" + i + ");' onmouseover='swapPageIndIn(" + i + ");' onmouseout='swapPageIndOut(" + i + ");' />";
            }
        }
    }
    galleryContent += "</div>";
    
    //alert(galleryContent);
    
    document.getElementById('hp_gallery').innerHTML = galleryContent;
    
    activeGalleryPage = 0;
    swapIn(0);
    
    //alert(pagecount);
}

function changePage(id)
{
    for(i=1;i<=pagecount;i++)
    {
        //alert("for i: " + i);
        document.getElementById('thumbpage_' + i).style.position = "absolute";
        document.getElementById('thumbpage_' + i).style.display = "none";
        document.getElementById('thumbpage_' + i).style.top = "-5000px";
        
    }
    document.getElementById('thumbpage_' + id).style.position = "relative";
    document.getElementById('thumbpage_' + id).style.display = "block";
    document.getElementById('thumbpage_' + id).style.top = "0px";
    
    projectId = (id - 1) * 6;
    commandGallery(projectId);
    setPageInd(id);
}

function setPageInd(id)
{
    thumbInd = "";
    for(j=1;j<=pagecount;j++)
    {
        if(j == id)
        {
            //alert(j + " yes " + id);
            thumbInd +=  "<img src='images/gallery/thumbselected_lite.png' alt='' title='' border='0' name='pageInder_" + j + "' />";
        }
        else
        {
            thumbInd += "<img src='images/gallery/thumbselect_lite.png' alt='' title='' border='0' class='pointer' name='pageInder_" + j + "' onclick='changePage(" + j + ");' onmouseover='swapPageIndIn(" + j + ");' onmouseout='swapPageIndOut(" + j + ");' />"; 
        }
    }
    //alert(thumbInd);
    document.getElementById('hpg_thumbs_indicator').innerHTML = "";
    
    document.getElementById('hpg_thumbs_indicator').innerHTML = thumbInd;
}

function swapPageIndIn(id)
{
    document.images['pageInd_' + id].src = "images/gallery/thumbselected_lite.png";
}

function swapPageIndOut(id)
{
    document.images['pageInd_' + id].src = "images/gallery/thumbselect_lite.png";
}

function commandGallery(id)
{   
    activeimageid = 1;
    mainimagehtml = "";
    window.scroll(0,0);
    
    imgcount = parseInt(hpgImageCount[id]);
    if(imgcount > 1)
    {
        mainimagehtml += "<a href='javascript:nextGallery(" + id + ");'><img src='images/gallery/main_images/" + hpgImageName[id] + "_1.jpg' class='mainimage_show' id='mainimage_1' alt='' title='' border='0'></a>";
        
        for(ii=2;ii<=imgcount;ii++)
        {
            mainimagehtml += "<a href='javascript:nextGallery(" + id + ");'><img src='images/gallery/main_images/" + hpgImageName[id] + "_" + ii + ".jpg' class='mainimage_hide'  id='mainimage_" + ii + "' alt='' title='' border='0'></a>";
        }
        //alert(mainimagehtml);
    }
    else
    {
        mainimagehtml += "<img src='images/gallery/main_images/" + hpgImageName[id] + "_1.jpg' alt='' title='' border='0'>";
    }
    
    document.getElementById('hp_gallery_mainimage').innerHTML = mainimagehtml;
    document.getElementById('hp_gallery_captiontitle').innerHTML = hpgTitle[id];
    document.getElementById('hp_gallery_captiontext').innerHTML = hpgDescription[id];
    
    activeGalleryPage = id;
    
    for(i=0;i<hpgTitle.length;i++)
    {
        swapOut(i);
        document.getElementById('thumb_' + i).style.cursor = "pointer";
    }
    document.getElementById('thumb_' + id).style.cursor = "default";
    swapInPerm(id);
    
    mainimagethumbselect = "";
    
    if(hpgImageCount[id] > 1)
    {
        mainimagethumbselect = "<img src='images/gallery/thumbselected.png' alt='' title='' border='0'>";
        for(i=2;i<=hpgImageCount[id];i++)
        {
            mainimagethumbselect += "<a href='javascript:gotoGallery(" + id + "," + i + ")'><img src='images/gallery/thumbselect.png' alt='' title='' border='0'></a>";
        }
    }
    document.getElementById('hp_gallery_thumbselect').innerHTML = mainimagethumbselect;
}

function nextGallery(id)
{
    //alert('nextGallery');
    var imagecount = parseInt(hpgImageCount[id]);
    var nextimageid = activeimageid;
    
    
    if(nextimageid < imagecount)
    {
        nextimageid++;
    }
    else
    {
        nextimageid = 1;
    }
    
    activeimageid = nextimageid;
    
    //alert(imagecount);
    
    for(i=1;i<=imagecount;i++)
    {
       jQuery('#mainimage_' + i).fadeOut(500,"swing");
    }
    
    jQuery('#mainimage_' + nextimageid).fadeIn(500,"swing");   
        
    resetThumbsMain(id,nextimageid);
}

function gotoGallery(id,imageid)
{
    var imagecount = parseInt(hpgImageCount[id]);
    activeimageid = imageid;
    
    for(i=1;i<=imagecount;i++)
    {
        jQuery('#mainimage_' + i).fadeOut(500,"swing");
    }
    
    jQuery('#mainimage_' + imageid).fadeIn(500,"swing");
    
    resetThumbsMain(id,imageid)
}

function resetThumbsMain(id,imageid)
{
    mainimagethumbselect = "";
    for(i=1;i<=hpgImageCount[id];i++)
    {
        if(imageid == i)
        {
            mainimagethumbselect += "<img src='images/gallery/thumbselected.png' alt='' title='' border='0'>";
        }
        else
        {
            mainimagethumbselect += "<a href='javascript:gotoGallery(" + id + "," + i + ")'><img src='images/gallery/thumbselect.png' alt='' title='' border='0'></a>";
        }
    }
    document.getElementById('hp_gallery_thumbselect').innerHTML = mainimagethumbselect;
}

function swapIn(id)
{
    //alert(id);
    document.images['thumb_' + id].src = "images/gallery/main_images/thumb_over.png";
}

function swapOut(id)
{
    if(id != activeGalleryPage)
    {
        document.images['thumb_' + id].src = "images/gallery/main_images/hpg_pict_overlay.png";
    }
}

function swapInPerm(id)
{
    //alert(id);
    document.images['thumb_' + id].src = "images/gallery/main_images/thumb_on.jpg";
}
