// JavaScript Document

function PageLoaded()
{
    document.forms[0].action = window.location.href;
    
	// Set up sections overlay/animation
	OverlaySections();
	
	// Change body class/if not index page
	ManipulateBodyAttr();
	
	// Highlight current page in menu
	HighlightMenuActiveItem();
	
	// Hide javascript sub content items if none exists
	HideSubContent();
}

function OverlaySections()
{
	jQuery("a", jQuery('#sections')).each(function(){
		jQuery(this)
			.append('<span class="overlay"></span>')
			.hover(function(){
				jQuery('span', this).css("opacity","0").dequeue().animate({"opacity":0.4, "height":"100%"}, 200);
			}, function(){
				jQuery('span', this).dequeue().animate({"opacity":0, "height":"0px"}, 200);
			});
	});
}

function ManipulateBodyAttr()
{
    if (window.location.href.split("/")[3] != "")
    {
        jQuery("body").addClass("body-content");
    }
}

function HighlightMenuActiveItem()
{
    currentPage = window.location.href.split("/")[3];
    if (currentPage != "")
    {
        jQuery("a[href^='/" + currentPage + "']", jQuery("#nav")).toggleClass("active");
    }
    else
    {
        //jQuery("a[href='/']", jQuery("#nav")).toggleClass("active");
    }
}

function LoadPage(PageID)
{
    jQuery.ajax({
        type: "POST",
        cache   : false,
        beforeSend: function(){},
        complete: function(){},
        url: "/resources/PageContent.asmx/GetPageContent",
        data: "{ 'PageID':'" + PageID + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(ajaxResponse) {
            // Parse JSON response
            ajaxResponse = JSON.parse(ajaxResponse.d);
            
            // If successful
            if (ajaxResponse.success)
            {
                //window.console.log("Title: %s", ajaxResponse.title);
                //window.console.log("Text: %s", ajaxResponse.text);
                
                // Show popup
                jQuery("#modalPopup").remove();
                modalHtml = '<div id="modalPopup"><div class="modalPopup-content">' +
                            '<a class="modalPopup-close" href="javascript:void(0)" title="Close">&nbsp;</a>' +
                            '<h1>' + ajaxResponse.title + '</h2>' +
                            '<div><div id="image-holder" style="float: left; margin: 0px 10px 10px 0px"><img src="/resources/images/' + ajaxResponse.title.replace(/ /g, "_") + '.JPG" alt="" /></div>' + ajaxResponse.text + '</div>' +
                            '</div><div class="modalPopup-bottom"></div></div>';
                jQuery('body').append(modalHtml);
                jQuery("a.modalPopup-close", jQuery('body #modalPopup').fadeIn(200).end()).click(function(){
                    jQuery(this).parent().parent().fadeOut(200);
                });
                
				/*
                jQuery.ajax({
                    type: "GET",
                    url: '/resources/images/' + ajaxResponse.title.replace(/ /g, "_") + '.JPG',
                    success: function(data){
                        jQuery("#image-holder").append('<img src="/resources/images/' + ajaxResponse.title.replace(/ /g, "_") + '.JPG" alt="" />');
                    },
                    error: function(){
                        //window.console.log("fail");
                         jQuery("#image-holder").remove()
                    }
                });    
				*/
            }
            else
            {
                window.console.log("Error:\n\t" + ajaxResponse.message);
            }
        },
        error   : function(ajaxResponse)
        {
            window.console.log(ajaxResponse.responseText);
        }
    })
}

function HideSubContent()
{
    // Get container and links
    var container = jQuery(".sub-content-pages");
    var links = jQuery("a", container);
    
    // Get the number of links
    activeCount = links.length;
    
    // Loop through the links and see if the link is in-active - has class "popout"
    for (i = 0; i < links.length; i++)
    {
        if (jQuery(links[i]).hasClass("popout"))
        {
            // if so decrement activecount
            activeCount--;
        }
    }
    
    GalleryImages = 0;
    
    if (activeCount == 0)
    {
        // No links are active, hide the container parent
        container.hide();
        
        if (GalleryImages == 0)
        {        
            // Change the main content area class to be larger (column-large)
            container.parent().parent().hide().prev().toggleClass("column-med").toggleClass("column-large");
        }
    }    
}

var CurrPage = 0;
var ViewSize = 0;
function SetupGallery(ViewAmount)
{
    GalleryImageContainer = jQuery("div.image-gallery");
    
    GalleryImageContainer.prepend("<div class='view'></div>");
    
    pages = 1;
    count = 0;
    
    jQuery("a", GalleryImageContainer).each(function(){
        if (count++ >= ViewAmount)
        {
            jQuery(GalleryImageContainer).append("<div class='view'></div>");
            pages++;
            count = 0;
        }
        jQuery(this)
            .find("img")
                .css("opacity", "0.6")
                .mouseover(function(){ 
                    jQuery(this).dequeue().stop().fadeTo(200, "1");
                })
                .mouseout(function(){ 
                    jQuery(this).dequeue().stop().fadeTo(200, "0.6");
                }).end()
            .appendTo( jQuery(".view:eq(" + (pages-1) + ")", GalleryImageContainer));
    });
    
    jQuery(GalleryImageContainer).wrapInner("<div class='wrapper'></div>")
    
    if (pages > 1)
    {
        jQuery(GalleryImageContainer)
            .append("<a href='javascript:void(0)' class='prev disabled'>« prev</a>")
            .append("<a href='javascript:void(0)' class='next'>next »</a>");
    }
    
    viewObj = jQuery(".view:eq(0)", GalleryImageContainer);
    ViewSize = { "w" : parseFloat(viewObj.css("width")), "h" : parseFloat(viewObj.css("height")) };
        
    jQuery("div.wrapper", GalleryImageContainer).css("width", (ViewSize.w * pages) + "px");
    //jQuery(GalleryImageContainer).css("height", ViewSize.h + 12);
    
    jQuery("a.prev", GalleryImageContainer).click(function(){
        if (CurrPage > 0)
        {
            jQuery("div.wrapper", GalleryImageContainer).animate({"left": "+=" + ViewSize.w}, 400)
        
            CurrPage--;
            
            if (CurrPage <= 0)
            {
                jQuery(this)
                    .addClass("disabled")
                    .next().removeClass("disabled")
            }
        }
    });
    jQuery("a.next", GalleryImageContainer).click(function(){
        views = jQuery("div.view", GalleryImageContainer).length-1;
        if (CurrPage < views)
        {
            jQuery("div.wrapper", GalleryImageContainer).animate({"left": "-=" + ViewSize.w}, 400)
            
            CurrPage++;
                            
            if (CurrPage >= views)
            {
                jQuery(this)
                    .addClass("disabled")
                    .prev().removeClass("disabled")
            }
        }
    });
}