﻿
var pageTracker;


jQuery(document).ready(function ($) {
    //http://twitter.com/frostbitefest
});

function Initializers() {
    if ($('a.SendPage').length > 0)
         SendPage('a.SendPage');
    WireMenu();
    pdfLinksNewWindow();
    if ($("#HomePage").size() == 1 )
        WireImageCarousel();
    else WireImageGallery();
}

function SetFooterMenuWidths() {
    var footerWidth = $("#FooterWrapper").width();//"#foo > div").size() 
    var numMenuItems = $("#FooterMenu > li.main").size();
    var newWidth = (footerWidth/numMenuItems);

   $("#FooterMenu li.main").css("width", newWidth);
}

function WireCall2ActionButtons() {
    $(".c2Action").click(function() { 
          window.location = $("a",this).attr("href");  
    });
    //alert($("#ButtonList").children(".c2Action").size());
    $(".c2Action").each(function(index) {
        //alert(index + ": index" + " button" + index % 3);
        $(this).addClass("button" + index % 3);
    });
}


function FormatAnchorImage() {
    if ($('img.anchorImage').length != 0)
        $('img.anchorImage').jcaption();
}



function WireImageGallery() { //not HomePageCarousel
    $('.sf_listAndPage').each(function() { 
        $(this).addClass("Gallery");
        $(this).children('li').css({ 'float':'left', 'padding':'0px','width':'90px', 'height':'90px', 'margin':'0px 5px 5px 0px', 'vertical-align':'top' });
        $(this).children('dl').css({ 'margin':'0px', 'padding':'0px','width':'90px', 'height':'90px'});
    });

    $('.sf_image').each(function() {
        var href = $(this).children('a').children('img').attr('src');
        href = href.substring(0, href.indexOf('?')).replace('.tmb', '.sflb');

        $(this).children('a').attr('href', href + '?show.jpg').attr('class', 'modalImage').attr('rel', 'group');
        $(this).css({ 'display':'block','margin':'0px', 'padding':'0px','width':'90px', 'height':'90px'});
    });

    if ($.fn.fancybox != undefined) {
        $('a.modalImage').fancybox({
            'centerOnScroll': true,
            'hideOnContentClick': false,
            'zoomOpacity': false
        });
    }
}

function WireMenu() {
    //$('#MainMenu li.main').equalWidths(); 
    
    $('#MainMenu li.main').removeClass('nojs');
    $('#MainMenu li.main').hover(
        function() { 
            $('ul li a', this).equalWidths();// fixes width
            $('ul', this).slideDown({ speed: 'fast', easing: 'easeOutBack' });
            $(this).siblings('li').removeClass('Current');
            $(this).addClass('Current');
        },
        function() {
        $('ul', this).slideUp({ speed: 'fast', easing: 'easeOutExpo' }, ClearCurrent(this));
        }
    );
        /*
        * easeOutBack
        * easeOutQuad
        * easeOutCubic
        * easeOutQuart
        * easeOutQuint
        * easeOutSine
        * easeInExpo - like
        * easeInCirc - like has authority ford tough
        */
}

function ClearCurrent(target) {
    $(target).removeClass('Current');
}




//jQuery.preloadImages = function() {
//    for (var i = 0; i < arguments.length; i++) {
//        jQuery("<img>").attr("src", arguments[i]);
//    }
//}





function pdfLinksNewWindow ()
{
	var fileTypes = ['pdf', 'PDF'];
	
	$('a').each(function() {
		 var $a = $(this);
		 var href = $a.attr('href');

		 if (href != null) 
		 {
		    var hrefArray = href.split('.');
            var extension = hrefArray[hrefArray.length - 1];
           
            if ($.inArray(extension, fileTypes) != -1) {
                 $a.attr('target', '_blank');
            }
		}
	});
}

/*
 * JQuery Plugin: "EqualHeights"
 * by:	Scott Jehl, Todd Parker, Maggie Costello Wachs (http://www.filamentgroup.com)
 *
 * Copyright (c) 2008 Filament Group
 * Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
 */
//$.fn.equalHeights = function(px) {
//    $(this).each(function() {
//        var currentTallest = 0;
//        $(this).children(".adjust").each(function(i) {
//            if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
//        });
//        //if (!px || !Number.prototype.pxToEm) currentTallest = currentTallest.pxToEm(); //use ems unless px is specified
//        
//        // for ie6, set height since min-height isn't supported
//        if ($.browser.msie && $.browser.version == 6.0) { $(this).children(".adjust").css({ 'height': currentTallest }); }
//        $(this).children(".adjust").css({ 'min-height': currentTallest });
//    });
//    return this;
//};

$.fn.equalWidths = function(px) { //alert('candy');
    $(this).each(function() {
        var currentWidest = 0;
        $(this).children().each(function(i) {
            //alert($(this).width());
            if ($(this).width() > currentWidest) { currentWidest = $(this).width(); }
        });
        //if (!px || !Number.prototype.pxToEm) currentWidest = currentWidest.pxToEm(); //use ems unless px is specified
        // for ie6, set height since min-height isn't supported
        if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({ 'width': currentWidest }); }
        $(this).children().css({ 'min-width': currentWidest });
    });
    return this;
};

/**** Carousel ****/
function WireImageCarousel()
{ //.sf_listAndPage
 //$("#Carousel").children('li').css("display","none");
 //$("#Carousel").css("display","block").children("img").css({display: 'block', width: '560px',height: '313px'});

//start carList array
 var thisString = "";
 var thisScript = "<script type=\"text/javascript\">\nvar car_itemList = [";

 //create carouselNav
 var carouselNav = "<div id=\"CarouselNav\">";
 var relCount = 1; 

 // for previous
 var start = 1;
 carouselNav = carouselNav + "<a href=\"#\" id=\"carouselPrev\" title=\"Previous\" rel=\"" + $('#Carousel ul li').size() + "\"> &nbsp;</a>"; 

$('#Carousel ul li').each(function() { 
    thisString = "";
    thisString = "<div class=\"box carContent\"><h2>" + $("img", this).attr('src').replace('Libraries/Carousel/', "").replace(".sflb.ashx", "").replace(/_/g,' ') + "</h2><p>" + $("img",this).attr("alt").replace(/'/g, "&quot;") + "</p></div>";
    //$(this).children("div.box").replaceWith(thisString);
    
    if (relCount ==1) thisScript = thisScript + "\n{ img: '" + $("img", this).attr('src') + "', box: '" + thisString + "'}";
    else thisScript = thisScript + ", \n{ img: '" + $("img", this).attr('src') + "', box: '" + thisString + "'}";
    // for numbered navigation
    //carouselNav = carouselNav + "<a href=\"#\" rel=\"" + relCount + "\">" + relCount + "</a>"; 
    relCount++; 
    
    // animating the box of text that displays
//    $('#MainMenu li.main').hover(
//        function() { 
//           
//            $('ul', this).slideDown({ speed: 'fast', easing: 'easeOutBack' });
//          
//        },
//        function() {
        $('.box', this).slideUp({ speed: 'fast', easing: 'easeOutExpo' });
//        }
//    var contentHeight = $('#Carousel .carContent').height();
//    $('#Carousel .carContent').css({ 'display': 'block','margin-top': '0px', 'width': '525px' }).animate({ marginTop: '-' + contentHeight + 'px' }, 500, 'easeOutExpo');
    //.animate({ marginTop: ($(this).position().top + 70) + 'px' }, 300, 'easeOutBack').animate({ width: '525px' }, 200);//,
//     $('#Carousel .carContent').css('display','block');//.animate({ marginTop: '-' + contentHeight + 'px' }, 500, 'easeOutExpo');
//     $('#Carousel .carContent').show(500);
    });
    
    //alert("start is " + (start + 1));
    // for next
    carouselNav = carouselNav + "<a href=\"#\" id=\"carouselNext\" title=\"Next\" rel=\"" + ($('#Carousel ul li').size() > 1 ? (start+1) : start) + "\"> &nbsp;</a>"; 
 
    //end script
    thisScript = thisScript + "\n]</script>";
    //alert(thisScript);
    $("#CarouselItems").replaceWith(thisScript);
     
    carouselNav = carouselNav + "</div>"; 
    $("#CarouselNav").replaceWith(carouselNav);
    $("#CarList li").replaceWith("");
    
}


/* Carousel */
function carouselStart() {
    $('#Carousel').jcarousel({
        scroll: 1,
        auto: 10,
        animation: 'slow',
        wrap: 'circular',
        initCallback: carousel_initCallback,
//        itemVisibleInCallback: { onBeforeAnimation: car_itemVisibleInCallback },
//        itemVisibleOutCallback: { onAfterAnimation: car_itemVisibleOutCallback },
//changes selected nav
        itemVisibleInCallback: {
            onBeforeAnimation: car_itemVisibleInCallback,
            onAfterAnimation:  mycarousel_itemVisibleInCallbackAfterAnimation 
        }, 
        itemVisibleOutCallback: { onAfterAnimation: car_itemVisibleOutCallback }    
    });
 
}

function car_itemVisibleOutCallback(carousel, item, i, state, evt) {
    carousel.remove(i);
};

/**
 * This is the callback function which receives notification
 * when an item becomes the first one in the visible range.
 * Triggered before animation.
 */
//function mycarousel_itemVisibleInCallbackBeforeAnimation(carousel, item, idx, state) {
// No animation on first load of the carousel
//    if (state == 'init')
//        return;
//    jQuery('img', item).fadeIn('slow');
//};
 
/**
 * This is the callback function which receives notification
 * when an item becomes the first one in the visible range.
 * Triggered after animation.
 */
function mycarousel_itemVisibleInCallbackAfterAnimation(carousel, item, idx, state) {
   // display('Item #' + idx + ' is now visible');
  
  if (idx > car_itemList.length) 
  { 
    //alert(idx + " is idex and length " + (idx % car_itemList.length));
    idx = (idx % car_itemList.length)
    // handle case when modulus is zero
   if (idx == 0) idx = car_itemList.length;
  }
   // shows which anchor is selected in Carousel // required for NUMBERED NAV
//    $("#CarouselNav a[rel='"+ idx +"']").addClass('selected');
//    $("#CarouselNav a:not([rel='"+ idx +"'])").removeClass('selected');   
    // for next and prev change navigation
    $("#CarouselNav #carouselPrev").attr('rel', idx-1);
    $("#CarouselNav #carouselNext").attr('rel', idx+1);
    
   // $('.box').slideDown({ speed: 'slow', easing: 'easeOutExpo' });
   $('.box').delay(5000).slideDown({ speed: 1000, easing: 'easeOutQuad' });
};

function car_itemVisibleInCallback(carousel, item, i, state, evt) {
// The index() method calculates the index from a
// given index that is out of the actual item range.

  var idx = carousel.index(i, car_itemList.length);
  carousel.add(i, car_getItemHTML(car_itemList[idx - 1]));
  
  
};

/**
* Item html creation helper.
*/
function car_getItemHTML(item) {
    var html = '';//'<li>'//don't require
    html += '<img class="banner" src="' + item.img + '" alt="' + item.img.replace("-", " ").replace(".jpg", "") + '\" />';
    html += item.box; 
    //html += '</li>';//not required
    return html; 
};

//From fritz site re: callback. Use in carousel start: initCallback: carousel_initCallback
function carousel_initCallback(carousel) {
// Required for Numbered Navigation
//    $('#CarouselNav a').hover(function() {
//        $(this).siblings().removeClass('selected');
//        $(this).addClass('selected'); 
//        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).attr('rel')), true, true);
//       // carousel.scroll(jQuery.jcarousel.intval(jQuery(this).children('img').attr('alt')), true, true);
//    }, function() {
//        carousel.startAuto();
//    });
     var index;
     $('#CarouselNav a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).attr('rel')));
        index = jQuery(this).attr('rel');
        carousel.startAuto();
        // For next and previous change rel values
      $("#CarouselNav #carouselPrev").attr('rel', index-1);
      $("#CarouselNav #carouselNext").attr('rel', index+1);
        return false;
    });
    
    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });

    // initialize start
    carousel.startAuto();
};

function theRotator() {
	//Set the opacity of all images to 0
	$('div#rotator ul li').css({opacity: 0.0});
	
	//Get the first image and display it (gets set to full opacity)
	$('div#rotator ul li:first').css({opacity: 1.0});
		
	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('rotate()',6000);
	
}

function rotate() {	
	//Get the first image
	var current = ($('div#rotator ul li.show')?  $('div#rotator ul li.show') : $('div#rotator ul li:first'));

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div#rotator ul li:first') :current.next()) : $('div#rotator ul li:first'));	
	
	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
};

function SendPage(target) {
    $(target).fancybox({
        'frameWidth': 540,
        'frameHeight': 420, 
        'centerOnScroll': true,
		'hideOnContentClick': false,
		'zoomOpacity': true,
		'zoomSpeedIn': 600,
		'zoomSpeedOut': 500
	});
	}

