﻿$(document).ready(function (){

    $('input:text').hint();
    bindSearchButtons();
	
    $('.tabBox').tabs();
    
	if ($('#mycarousel').length > 0){
        $('#mycarousel').jcarousel({
            scroll: 1,
            auto: 5,
            wrap: 'circular',
            initCallback: carouselFunctions
        });
    }
    
    shareButtons();
    highlightFirstnav(); 
    indicateChildNav();

    var imageGalleryElement = $('.imgGallery');
    if ($(imageGalleryElement).length > 0){
        initGallery(imageGalleryElement);
        //$('.imgGallery .galleryControl .caption').text($('.imgGallery img:eq(0)').attr('alt'));
    }
        
    //$("a.cboxElement").live("click", function() { alert('test'); return false; });


    // News category slider on the home page.
    $('#newsCats .prev').click(function (){
        $newsCatMenu = $('#news-cat-menu');
        p = $newsCatMenu.position();
        if ( p.top >= 0 ) {
            return;
        }
        $newsCatMenu.animate({ top: '+=36' }, {
            complete: function () {
                $newsCatMenu.clearQueue();
            }
        })
    })

    $('#newsCats .next').click(function (){
        $newsCatMenu = $('#news-cat-menu');
        p = $newsCatMenu.position();
        if ( ( $newsCatMenu.find('li').length-4.5)*-18 > p.top ) {
            return;
        }
        
        $newsCatMenu.animate({ top: '-=36'}, { 
            complete: function () {
                $newsCatMenu.clearQueue();
            }
        })
    })

});
   
function initGallery(el){
    // find the number of images in the gallery
    var picCount = el.find(".galleryItems img").length;
    el.find('.totalNum').text(picCount);
    
    // make gallery scrollable
    el.find('.galleryItems').cycle({
        fx:     'fade',
        speed:  'fast',
        timeout: 0,
        next:   '.galleryControl .next', 
        prev:   '.galleryControl .prev',
        before:  function (curr,next,opts) {
            el.find('.caption').text(this.alt);
            el.find('.indexNum').text(opts.currSlide + 1);
        }
    });
}
    


function carouselFunctions(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

function indicateChildNav(){
    $('#wrapper #mainContent #navBar #main_navigation .navOn2 ul').each(function() {
        var currentClass = $(this).parent().attr("class");
        $(this).parent().attr("class", currentClass + " hasChildren");
    });
}

function shareButtons(){

    $('#printPage').click(function(){
        window.print();
    });

    $('#emailPage').click(function() {
        $('#shareOptions:visible').slideUp();
        $('#emailPageOptions:visible').slideUp();
        $('#emailPageOptions:hidden').slideDown();
    });

    $('#share').click(function() {
        $('#emailPageOptions:visible').slideUp();
        $('#shareOptions:visible').slideUp();
        $('#shareOptions:hidden').slideDown();
    });
    
    $("a.bookmark").click(function(e) {
        e.preventDefault();
        // Grab url and title from the anchor
        var url = this.href;  
        var title = this.title;
        
        if ($.browser.mozilla == true) {
                // Its firefox call the built in firefox javascript function
                window.sidebar.addPanel(title, url, '');
                return false;
        } else if($.browser.msie == true) {  
                // Its IE so use its own built in function
                window.external.AddFavorite( url, title);
                return false;
        } else {
                alert('Please use CTRL + D to bookmark this website.');
        }
    });
    
}

var stoaf = ".sendToAFriendForm";

// display, change and hide stoaf form
function stoafMsgSent() {
    // after two seconds change to message sent, swap image, swap text, change color
    $(stoaf).delay(2000,function(){
        $(stoaf + ' .stoafSent img').attr("src","/images/shd/loaded.gif");
        $(stoaf + ' .stoafSent p').css("color","#A5C718");
        $(stoaf + ' .stoafSent p').text("Message sent");
        
        // after 3 seconds hide the panel    
        $(stoaf).delay(3000,function(){
            $(stoaf).slideToggle(600);   
            
            // once the panel is hidden change the message back to loading
            $(stoaf).delay(700,function(){
                $(stoaf + ' .stoafSent img').attr("src","/images/shd/loading.gif");
                $(stoaf + ' .stoafSent p').css("color","#666666");
                $(stoaf + ' .stoafSent p').text("Sending message...");
                $('#pnlMain').attr("style","display:block;");
                $('#pnlThanks').attr("style","display:none;");
            });
        });
    });
}


function highlightFirstnav(){
    if($('#home').length > 0){
        $('#home #main_navigation li.off:first a').removeClass('off').addClass('on');
        $('#home #main_navigation li.off:first').removeClass('off').addClass('on');        
    }
}

function labelSummaryRows(){
    var brilliantVar = $('.summaryList');
    $.each(brilliantVar, function(index, value) {
      $(brilliantVar[index]).addClass('list' + (index + 1));
    });
}


//Hide panel function
 function hideExtras(){
 
    $('.up_coach').click(function(){ //when hide button is clicked
        $('.coach').slideUp('slow'); // hide extras
        $('.up_coach').slideUp('slow'); //hide Hide Button
        $('.down_coach').slideDown('slow'); //show expand button
            
    });
    
    $('.up_ski_pass').click(function(){
       
        $('.ski_pass').slideUp('slow');
        $('.up_ski_pass').slideUp('slow'); //hide Hide Button
        $('.down_ski_pass').slideDown('slow');
            
    });    
    
    $('.up_equipment').click(function(){
       
        $('.equipment').slideUp('slow');
        $('.up_equipment').slideUp('slow'); //hide Hide Button
        $('.down_equipment').slideDown('slow');
            
    }); 
   
    $('.up_ski_school').click(function(){
       
        $('.ski_school').slideUp('slow');
        $('.up_ski_school').slideUp('slow'); //hide Hide Button
        $('.down_ski_school').slideDown('slow');
            
    });       
   
 }
 
 function showExtras(){
    
    $('.coach_btn').click(function(){ //when expand button is clicked...
        
        $('.coach').slideToggle('slow', function() {
            if($('.coach_btn').text() == "Hide") {
                $('.coach_btn').removeClass("active");
                $('.coach_btn').text("Expand");
            } else {
                $('.coach_btn').addClass("active");
                $('.coach_btn').text("Hide");
            }
            
        });
        return false;
    });
    
//    $('.down_ski_pass').click(function(){
//        $('.down_ski_pass').toggleClass("active");
//        $('.ski_pass').slideToggle('slow');
//        return false;
//    }); 
    
    
    $('.ski_pass_btn').click(function(){ //when expand button is clicked...
        
        $('.ski_pass').slideToggle('slow', function() {
            if($('.ski_pass_btn').text() == "Hide") {
                $('.ski_pass_btn').removeClass("active");
                $('.ski_pass_btn').text("Expand");
            } else {
            $('.ski_pass_btn').addClass("active");
            $('.ski_pass_btn').text("Hide");
            }
            
        });
        return false;
    });

    $('.equipment_btn').click(function(){ //when expand button is clicked...
        
        $('.equipment').slideToggle('slow', function() {
            if($('.equipment_btn').text() == "Hide") {
                $('.equipment_btn').removeClass("active");
                $('.equipment_btn').text("Expand");
            } else {
            $('.equipment_btn').addClass("active");
            $('.equipment_btn').text("Hide");
            }
            
        });
        return false;
    });   
    
    $('.ski_school_btn').click(function(){ //when expand button is clicked...
        
        $('.ski_school').slideToggle('slow', function() {
            if($('.ski_school_btn').text() == "Hide") {
                $('.ski_school_btn').removeClass("active");
                $('.ski_school_btn').text("Expand");
            } else {
            $('.ski_school_btn').addClass("active");
            $('.ski_school_btn').text("Hide");
            }
            
        });
        return false;
    }); 
   

}
 
function hidePanes() {
  
    $('.coach').slideUp('slow');
    $('.ski_pass').slideUp('slow');
    $('.equipment').slideUp('slow');
    $('.ski_school').slideUp('slow');
  
}  

//$("*", document.body).click(function (e) {
//  var offset = $(this).offset();
//  e.stopPropagation();
//  $("#result").text(this.tagName + " coords ( " + offset.left + ", " +
//                                  offset.top + " )");
//});

$(".liveChatToggle").toggle(
    function () {
	    $("#live-chat").animate({width: "38px"}, {queue:false, duration: 500}).toggleClass('hid');
            },
            function () {
	    $("#live-chat").animate({width: "134px"}, {queue:false, duration: 500}).toggleClass('hid');
    }
); 

