/**
* Core JS Utilities 
* * * * * * * * * * * * **/

/* Set the homepage carousel timer here (and only here) - the default will be 5000 miliseconds */
var CAROUSEL_TIMER = 8000; // 1000 = 1 second 

// DO NOT MAKE CHANGES TO THE FOLLOWING CODE
var timerID;
var global_timer = (CAROUSEL_TIMER != null) ? CAROUSEL_TIMER : 5000;

$.noConflict();

jQuery(document).ready(function() {

    jQuery.preloadCssImages();

    // General Carousel
    jQuery(".carousel_wrap").initCarousel();

    // Slider Functions
    jQuery(".slider_wrap").initSliderEv();

    // Zoom Modal 
    if (jQuery(".content_module.zoom").length) {
        buildImageModal(); // If zoom module - append overlay to body (only need to do this once)
        jQuery(".content_module.zoom").initImageModal();
    }

    // Photo Gallery
    jQuery(".photo_gallery").initGallery();

    // Winners Gallery
    jQuery(".winners_gallery  ul a").lightBox({
        imageLoading: 'projects/PNGWebsite/design/images/lightbox-ico-loading.gif',
	    imageBtnClose: 'projects/PNGWebsite/design/images/lightbox-btn-close.gif',
	    imageBtnPrev: 'projects/PNGWebsite/design/images/lightbox-btn-prev.gif',
	    imageBtnNext: 'projects/PNGWebsite/design/images/lightbox-btn-next.gif',
	    txtImage: ''
    });

    // Three Image
    jQuery(".three_image  li a").lightBox();

    // Calendar background fix (for cross browser support)
    jQuery(".navCalendar tbody tr:first td").css("background-color", "#000");

    // Nav Hover fix (for cross browser support)
    jQuery(".main_nav li").hover(
		function() {
		    jQuery(this).prev().addClass("hov");
		},
		function() {
		    jQuery(this).prev().removeClass("hov");
		}
	);

    jQuery(".main_nav li.current").prev().addClass("hov");

    // Sitecore module hacks to fix 
    jQuery(".selectorButton").html("<span>" + jQuery(".selectorButton").html() + "</span>");

	// adding spans to <ol> to allow for the bullets to be different colors 
	jQuery(".side_module ol li").wrapInner("<span></span>");
	jQuery(".content_module ol li").wrapInner("<span></span>");
	jQuery(".content_area ol li").wrapInner("<span></span>");


});    // end document.ready

// General carousel function
jQuery.fn.initCarousel =

	function() {

	    // Initiate interval
	    // var carousel_timer is set at the top of this file - if not it defaults to 5000

	    timerID = setInterval("carouselSlideTimer()", global_timer);

	    var carousel = jQuery(".carousel_wrap .carousel");
	    var num_items = carousel.find("li").length;
	    var pagin = jQuery(".carousel_wrap .pagination");
	    var actions = jQuery(".carousel_wrap .actions");

	    // Creating pagination based on number of carousel items
	    for (var i = 0; i < num_items; i++) {
	        pagin.append("<li><a href=\"#\">" + i + "</a></li>");
	    }

	    // Setting first li's a to active class
	    pagin.find("li:first").addClass("act");

	    // Setting onclick event for pagination nav items
	    pagin.find("a").click(function(e) {
	        e.preventDefault(); // prevent default action

	        if (num_items > 0) {
	            // Pass pagination obj ref, carousel obj ref, and index of clicked a's parent (li)
	            var idx = jQuery(this).parent().index();
	            swapCarouselSlide(pagin, carousel, idx);

	            timerID = resetTimer();
	        }

	    });


	    // Setting onclick for next/prev controls
	    actions.find("a").click(function() {

	        var cur_class = jQuery(this).attr("class");
	        var cur_idx = carousel.find("li.act").index();
	        var new_idx = 0;

	        if (cur_class == "next" && num_items > 0) {
	            new_idx = ((cur_idx + 1) > (num_items - 1)) ? 0 : cur_idx + 1;
	            swapCarouselSlide(pagin, carousel, new_idx);

	        } else if (cur_class == "prev" && num_items > 0) {
	            new_idx = ((cur_idx) == 0) ? num_items - 1 : cur_idx - 1;
	            swapCarouselSlide(pagin, carousel, new_idx);

	        } else if (cur_class == "pause") {
	            stopTimer();
	            jQuery(this).attr("class", "play");

	        } else if (cur_class == "play") {
	            resetTimer();
	            jQuery(this).attr("class", "pause");
	        }

	        return false; // prevent default action	

	    });


	}






// Swaps carousel slide to specfic index
function swapCarouselSlide(pagin, carousel, idx) {

    // Toggle active class on pagination
    pagin.find("li.act").removeClass("act");
    pagin.find("li:eq(" + idx + ")").addClass("act");

    // Remove active class from active slide, fade slide out
    carousel.find("li.act").fadeOut("slow", function() {
        // Callback
        jQuery(this).removeClass("act");
    });


    carousel.find("li:eq(" + idx + ")").fadeIn("slow", function() {
        jQuery(this).addClass("act");
    });

}

// Wrapper function for carousel timer
function carouselSlideTimer() {

    var pagin = jQuery(".carousel_wrap .pagination");
    var carousel = jQuery(".carousel_wrap .carousel");
    var idx = pagin.find("li.act").index();
    var num_items = pagin.find("li").length - 1;
    idx = (idx + 1 > num_items) ? 0 : idx + 1;

    swapCarouselSlide(pagin, carousel, idx);

}

function resetTimer() {
    if (timerID) { clearInterval(timerID); } // if timerID exists (ie: if the timer is running) clear it and and start a new one
    timerID = setInterval("carouselSlideTimer()", global_timer);

    return timerID;
}

function stopTimer() {
    if (timerID) { clearInterval(timerID); } // if timerID exists (ie: if the timer is running) clear it and and start a new one
}
// Custom Slider Functions 
jQuery.fn.initSliderEv =
	function() {

	    jQuery(".slider_wrap .btn .next").click(function(e) {
	        e.preventDefault();

	        var slider = jQuery(".slider_wrap .slider");
	        var left = parseFloat(slider.css("left"), 10) * -1;  // Removes 'px' and creates a positive # from 'left' css value
	        var i_width = slider.find("li").width();
	        var size = (slider.find("li").length) * i_width;   // Calculates the max 'left' css position using the number of items and their width

	        if (size > (left + 600)) { 							 // If not at the maximum 'left' value
	            slider.animate({ left: "-=" + i_width + "px" }); 	 // Advance the slider to the next item
	        } else {
	            slider.animate({ left: "0px" }); 				 // Return the slider position to the first item
	        }


	    });


	    jQuery(".slider_wrap .btn .prev").click(function(e) {

	        e.preventDefault();

	        var slider = jQuery(".slider");
	        var i_width = slider.find("li").width();
	        var left = parseFloat(slider.css("left"), 10); // Removes 'px'

	        if (left < 0) {										 // If slider is not sitting at the first item - advance the slider to the previous item
	            slider.animate({ left: "+=" + i_width + "px" });
	        } else {
	            slider.animate({ left: "0px" }); 				 // Return the slider position to the first item
	        }
	    });

	} // Initiate Slide Events



// Custom Image Modal Functions
jQuery.fn.initImageModal =
	function() {

	    // Click event for zoom link
	    jQuery(this).find(".zoom_link").click(function() {
	        showOverlay();

	        // Insert image
	        var wrap = jQuery(".overlay .content_wrap");
	        var title = jQuery(this).parents(".zoom").find("h3").clone();
	        var img_src = jQuery(this).attr("href");
	        var img = jQuery("<img src=\"" + img_src + "\" alt=\"alt text goes here\" />")

	        // Removing any previous images or titles
	        wrap.find("img").remove();
	        wrap.find("h3").remove();

	        // Inserting image and title
	        wrap.append(img);
	        wrap.append(title);

	        return false; // Prevent default action		
	    });
	}


function showOverlay() {
    jQuery(".overlay").fadeIn();
}

function hideOverlay() {
    jQuery(".overlay").fadeOut();
    return false; // Prevent default
}

function buildImageModal() {
    jQuery("body").append(jQuery('<div class="overlay"><div class="content_wrap"><a href="#" class="close">Close</a></div></div>'));
    jQuery(".overlay .close").click(hideOverlay);
}


jQuery.fn.initGallery =
	function() {

	    // Set featured image in gallery
	    var photo_cont = jQuery(".photos");
	    var parent = jQuery(".photo_gallery").find(".slider li:eq(0)");

	    setFeaturedImage(photo_cont, parent);

	    // Set on click event for image slider
	    jQuery(".photo_gallery .slider li a").click(
			function() {

			    // Set active state
			    jQuery(".slider li a.act").removeClass("act");
			    jQuery(this).addClass("act");
			    setFeaturedImage(photo_cont, jQuery(this).parent());

			    return false; // prevent default
			}
		);


}
	
// Utility function for gallery
function setFeaturedImage(photo_cont, parent) {

    var title = parent.find(".title").html();
    var caption = parent.find(".caption").html();
    var new_img = parent.find("a").attr("href");

    photo_cont.find("img").attr("src", new_img);
    photo_cont.find("h3").html(title);
    photo_cont.find("p").html(caption);


}

