/* vim: set expandtab tabstop=4 shiftwidth=4: */

var browserVersion = parseInt(navigator.appVersion);
var isNetscape = navigator.appName.indexOf("Netscape") != -1;
var isIE = navigator.appName.indexOf("Microsoft") != -1;
var agent = navigator.userAgent.toLowerCase();
var isWindows = agent.indexOf("win") != -1;
var isMac = agent.indexOf("mac") != -1;
var isNix = agent.indexOf("X11") != -1;

function doConfirm(text)
{
    var val = confirm(text);
    return (!val) ? false : true;
}

function manageCountryGallery()
{
    if($('#country-feature').text()) {
        if (!$('#country-feature .snippet-gallery-image ul.gallery-image').text()) {
            //$('.snippet-gallery-image').text('No images in this gallery');
        }
        else {
            $('#country-feature .snippet-gallery-image').prepend($('<h3>Image Gallery</h3>'));
            $('<a class="view-gallery">View Image Gallery</a>').appendTo($('#country-feature .snippet-title'));
            $('<a class="hide-gallery">Hide Image Gallery</a>').appendTo($('#country-feature .snippet-title'));
            $('a.view-gallery').click(function(){
                $(this).hide();
                $('a.hide-gallery').show();
                $('#country-feature .snippet-main-image' +
                ', #country-feature .snippet-images' +
                ', #country-feature .snippet-map' +
                ', #country-feature .snippet-intro' +
                ', #country-feature .snippet-body').hide();
                $('#country-feature .snippet-gallery-image').fadeIn();
            });
            $('a.hide-gallery').click(function(){
                $(this).hide();
                $('a.view-gallery').show();
                $('#country-feature .snippet-gallery-image').hide();
                $('#country-feature .snippet-main-image' +
                ', #country-feature .snippet-images' +
                ', #country-feature .snippet-map' +
                ', #country-feature .snippet-intro' +
                ', #country-feature .snippet-body').fadeIn();
            });
        }
        $('#country-feature ul.gallery-image li').each(function(){
            var src = $(this).children('img.image').attr('src');
            var href = src.replace('web', '600x399');
            $(this).prepend('<a href="'+href+'" class="lightbox"></a>');
            $(this).children('img.image').prependTo($(this).children('.lightbox'));
            $(this).children('.lightbox').lightBox();
        });
    }
}
// JQUERY
$(document).ready( function() {
    // Main Menu
    $("#main-menu ul.level1 li:last").addClass('lastItem');
    $('#main-menu ul.level1 li.selected > a').css('color', '#C3A941');
    $.each($("#main-menu ul.level2"), function() {
        $(this).children("li:first").addClass('firstItem');
        $(this).children("li:last").addClass('lastItem');
    })
    $('#main-menu ul.level1 li').hover( 
        function() { 
            $(this).children('ul.level2').css("display","block");
        },
        function() { 
            $(this).children('ul.level2').css("display","none");
        }
    );
    
    // Homepage
    $('#homepage-slideshow ul').innerfade({
        animationtype: 'fade',
        speed: 2300,
        timeout: 4000,
        type: 'random',
        containerheight: '430px',
        runningclass: 'innerfade'
    });
    $('#homepage-content > div').addClass('leftbox-content');
    if($('div.home').text()) {
        $('.snippet-logo').appendTo($('#homepage-content'));
    }
    
    // Footer
    $('.footer-links').append('<a class="expand-footer"><img class="down" src="/images/layout/footer-expand-arrow.gif" alt="Expand"/></a>');
    $('.expand-footer').click(function(){
        $('#sitemap, .footer-bottom').slideToggle();
        if($('.expand-footer img').hasClass('down')) {
            $('.expand-footer img').addClass('up');
            $('.expand-footer img').removeClass('down');
            $('.expand-footer img').attr('src', '/images/layout/footer-collapse-arrow.gif');
            var target = $('.footer-bottom ul li .link');
            $('html, body').animate({scrollTop:$("body").attr("scrollHeight")}, 'slow');
        } else if($('.expand-footer img').hasClass('up')) {
            $('.expand-footer img').addClass('down');
            $('.expand-footer img').removeClass('up');
            $('.expand-footer img').attr('src', '/images/layout/footer-expand-arrow.gif');
        }
    });
    
    $('#enquiryForm #submitButton').attr('value','');
    $('#contactForm #submitButton').attr('value','');
    $('input[type="text"]').addClass('text');
    $('#receiveBrochure-1').parent().parent().parent().addClass('receiveBrochure');
    
    // CMS Listing
    $('.cmsListings .listingItem').append('<div class="clear"></div>');
    $('.cmsListings .listingItem:last').addClass('lastItem');
    $('.cmsListings .listingItem').hover(
        function () {
            $(this).addClass("hover");
        },
        function () {
            $(this).removeClass("hover");
        }
    );
    
    // listing intro
    $('#listing-intro').append('<div class="content"></div>')
    $('#listing-intro .content').append($('#listing-intro img'));
    
    $('#listing-intro .content').append($('#listing-intro .intro'));
    $('#listing-intro .content').append($('.image-caption'));
    $('#listing-intro .content').append('<div class="clear"></div>');
    $('.snippet-place:firstItem').addClass('lastItem');
    
    // About Us > Testimonials
    $('#testimonials > div').addClass('testimonial-category');
    $('#testimonials .testimonial-category:last').addClass('lastItem');
    // News menu
    $('#news-menu ul li:last').addClass('lastItem');
    
    //Safari pages
    if ($("div.safari-menu").length == 0) {
        $("div.safari-intro-box").css('border-top', '1px solid #F5F2E3');
        $("div.safari-enquire").css('border-top', '1px solid #F5F2E3');
    }
    
    // safari dates tooltips
    $('.info-button img').hover(
        function () {
            $(this).next().show(200);
        },
        function() {
            $(this).next().hide(200);
        }       
    );

    // sidebar sub menu expanding
    $('#sub-menu ul.level1 li ul.level2').css('display','none');
    $('#sub-menu ul.level1 li.selected ul.level2').css('display','block');
    
    $('#sub-menu ul.level1 li ul.level2 ul.level3').css('display','none');
    $('#sub-menu ul.level1 li.selected ul.level2 li.selected ul.level3').css('display','block');
    
    // Contact > Office Contact
    $('ul.contact-details li:last').addClass('lastItem');
    $('#sidebar-contact-info').prependTo($('#office-contacts'));
    // Contact > FAQs
    $('#faq-content ul.items li:last').addClass('lastItem');
    
    // Contact > Maps
    $('#maps > div').addClass('map-item');
    $('#maps .map-item').append('<div class="clear"></div>');
    $('#maps > div:last').addClass('lastItem');
    $('ul.reading-list li:last').addClass('lastItem');
    $('ul.maps li').each(function(){
        var src = $(this).children('img.map').attr('src');
        var href = src.replace('web', '750x901');
        $(this).prepend('<a href="'+href+'" class="image-link"></a>');
        $(this).children('img.map').prependTo($(this).children('.image-link'));
        $(this).children('a.image-link').lightBox();
    });
    
    
    // Safari lodge
    if($('a.lightbox').html()){
        $('a.lightbox').lightBox();
    }
    if($('#image-gallery').text()){
        $('#image-gallery a').lightBox();
    }
    $('a.view-gallery').click(function(){
        $(this).hide();
        $('a.hide-gallery').show();
        $('#safari-lodge #content').hide();
        $('#safari-lodge #image-gallery').fadeIn();
    });
    $('a.hide-gallery').click(function(){
        $(this).hide();
        $('a.view-gallery').show();
        $('#safari-lodge #image-gallery').hide();
        $('#safari-lodge #content').fadeIn();
    });
    
    // Great ape guide
    $('#great-ape-guide-feature .snippet-photo-gallery-link').appendTo($('.snippet-title'));
    if($('#great-apes-index').text()) {
        $('#great-ape-guide-feature').remove();
    }
    
    // Countries
    manageCountryGallery();
    
    // Accommodations
    $('#accommodations ul.accommodations').each(function(){
        $(this).children('li:last').addClass('lastAccommodationItem');
    }); 
    $('#accommodations ul.accommodations li:last').addClass('lastItem');
    if($('#accommodations').text()) {
        var src = $('#listing-intro .image').attr('src');
        var href = src.replace('web', '648x414');
        $('#listing-intro .content').prepend('<a href="'+href+'" class="image-link"></a>');
        $('#listing-intro .image').prependTo($('.image-link'));
        $('a.image-link').lightBox();
    }
    
    $('div.home div#right_col h2').css('cursor', 'pointer');
    $('div.home div#right_col h2').click(function() {
        window.location.replace("/news/");
    });
    
    // News
    if($('.news .hide').length > 0) {
        $('.news .snippet-primary-image').remove();
    }

    $('.news-listings .result').each(function() {
        var comments = $(this).find('.listing-comments a');

        if (!comments.attr('href').match('#comments')) {
            comments.attr('href', comments.attr('href') + '#comments');
            if (comments.text().length == 0) {
                comments.text('0');
            }   

            if (comments.text() == 1) {
                comments.append(' comment');
            } else {
                comments.append(' comments');
            }          
        }
    });

    // Comments
    $('#commentsDiv span.title, #commentsDiv span.submitCommentTitle').addClass('cufon');
    $('#commentsDiv .comment:first').addClass('first-item');

    $('#commentsDiv .comment').each(function() {
        
        $(this).children('.author').after(' <span class="post-text">wrote:</span>');

        // Format Date & Remove Time
        var dateTimeArray = $(this).children('.datePosted').text().split(' ');
        var dateArray = dateTimeArray[0].split('/');

        var monthNames = [ "January", "February", "March", "April", "May", "June", "July",
                           "August", "September", "October", "November", "December" ];

        $(this).children('.author').before(
            $(this).children('.datePosted').text(dateArray[0] + ' ' + monthNames[dateArray[1]-1] + ' ' + dateArray[2] + ', ')
        );

        $(this).children('.datePosted').before('<span class="pre-text">On</span> ');

        // Wrap Everything In A Container
        $(this).wrapInner('<div class="date-header" />');
        $(this).append($(this).find('div.content'));        
    });
    
});

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-17600775-1']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' :
'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();

