// JavaScript Document
/// <reference path="jquery-1.4.2.min.js"/>
jQuery(document).ready(function ($) {

});

function Initializers() {
    WireMenu();
    if ($("#Carousel").size() == 1) {
        LoadImageCarousel();
    }
    WireImageGallery();

 /*   $('.specialList').css('width', $('.specialList').width()).css('margin', "0px auto"); */

    $("ul.photoSearchResults li:nth-child(3n)").css("margin-right", "0px");

   //WireCommercialForm();
}

function WireCommercialForm() {

//    $("#txtAdditionalUse").click(function {
//        $("#SelectProjectNature")
//    });
}

//function InitModalImages() {
//    $('a.modal').fancybox({
//        'centerOnScroll': true,
//        'hideOnContentClick': false,
//        'zoomOpacity': true,
//        'zoomSpeedIn': 600,
//        'zoomSpeedOut': 500,
//        'easingIn': 'easeOutBack',
//        'easingOut': 'easeInBack'
//    });
//}
function WireImageGallery() { //not HomePageCarousel
      if ($.fn.fancybox != undefined) {
        $('a.modal').fancybox({
            'centerOnScroll': true,
            'hideOnContentClick': false,
            'zoomOpacity': false
        });
    }
}

function WireMenu() {
    $("#MainNavigation #MainMenu ul.subNavigation").each(function () {

        if ($(this).children().size() <= 2)
            $(this).css("background-image", "none");
    });

    $('#MainMenu li.main').removeClass('nojs');
    $('#MainMenu li.main').hover(
        function () {
            $('ul li a', this).equalWidths(); // fixes width    .css('display','block')
            $('div.dropDown', this).slideDown({ speed: 'fast', easing: 'easeOutBack' });
            $(this).siblings('li').removeClass('current');
            $(this).addClass('current');
        },
        function () {
            $('div.dropDown', 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');
}

$.fn.equalWidths = function (px) { //alert('candy');
    $(this).each(function () {
        var currentWidest = 0;
        $(this).children().each(function (i) {
            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 */

jQuery.preloadImages = function () {
    for (var i = 0; i < arguments.length; i++) {
        jQuery("<img>").attr("src", arguments[i]);
    }
}
function LoadImageCarousel() {
    var imageList = "";
    var i = 0;
    for (i = 0; i < car_itemList.length; i++) {
        if (i > 0) imageList += ","
        imageList += "\"/Images/Carousel/" + car_itemList[i].img + "\"";
    }
    //alert("Candy " + imageList);
    $.preloadImages(imageList);

}
function carouselStart() {
    $('#Carousel').jcarousel({
        scroll: 1,
        auto: 10,
        animation: 'normal',
        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("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
//    $("#CarouselNav a[rel='" + idx + "']").addClass('selected');
//    $("#CarouselNav a:not([rel='" + idx + "'])").removeClass('selected');
    $("#CarouselNav #carouselPrev").attr('rel', idx - 1);
    $("#CarouselNav #carouselNext").attr('rel', idx + 1);
};

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 = '';
    var name = item.img.replace(/-/g, ' ').replace(".jpg", "");
    html += '<img class="banner" src="/Images/Carousel/' + item.img + '" alt="' + name + '\" title="' + name + '" />';
    html += '';
    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();
//    });

//    $('#CarouselNav a').bind('click', function () {
//        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).attr('rel')));
//        carousel.startAuto();
//        return false;
//      });

    // For Previous and Next links on Carousel
    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();
};
