try{Typekit.load();}catch(e){}

/** 
 * little plugin to speed up jquery selector
 * @see http://james.padolsey.com/javascript/76-bytes-for-faster-jquery/
 */
jQuery.single=function(a){return function(b){a[0]=b;return a}}(jQuery([1]));
$_ = jQuery.single;

var processDonateForm = function()
{
    try {
        $('#donateForm select').each(function(){
            $_(this).fancySelect();
        });
    }catch(e) { }
    $('#donateForm label.required').append('<span class="required">*</span>');
    
    $('#purposeOfDonation-element ul a[class="0"]').parent().remove();
    $('#purposeOfDonation option[value="0"]').remove();
    $('#whyDonate-element ul a[class="0"]').parent().remove();
    $('#whyDonate option[value="0"]').remove();
    $('#purposeOfDonation-element a.option-selected').each(function(){
        var text = $_(this).text();
        
        if(text.match(/appeal/)) {
            $('#appeal-label, #appeal-element').show();
        } else if(text.match(/celebration/)) {
            $('#celebration-label, #celebration-element').show();
        } else if(text.match(/challenge/)) {
            $('#challenge-label, #challenge-element').show();
        } else if(text.match(/Other/)) {
            $('#purposeOther-label, #purposeOther-element').show();
        }
    });
    
    $('#purposeOfDonation-element ul a').click(function(){
        var text = $_(this).text();
        $('#appeal-label, #appeal-element, '
        + '#celebration-label, #celebration-element, '
        + '#challenge-label, #challenge-element, '
        + '#purposeOther-label, #purposeOther-element').hide();
        
        if(text.match(/appeal/)) {
            $('#appeal-label, #appeal-element').show();
            $('#appeal-label').addClass('red');
        } else if(text.match(/celebration/)) {
            $('#celebration-label, #celebration-element').show();
            $('#celebration-label').addClass('red');
        } else if(text.match(/challenge/)) {
            $('#challenge-label, #challenge-element').show();
            $('#challenge-label').addClass('red');
        } else if(text.match(/Other/)) {
            $('#purposeOther-label, #purposeOther-element').show();
            $('#purposeOther-label').addClass('red');
        }
    });
    if($('#whyDonate-element a.option-selected').text() == 'Other') {
        $('#whyDonateOther-label, #whyDonateOther-element').show();
    }
    $('#whyDonate-element ul a').click(function(){
        var text = $_(this).text();
        $('#whyDonateOther-label, #whyDonateOther-element').hide();
        if(text == 'Other') {
            $('#whyDonateOther-label, #whyDonateOther-element').show();
            $('#whyDonateOther-label').addClass('red');
        }
    });
    $('#giftAid-element').appendTo($('#giftAid-label'));
}
$(document).ready(function() {
    // main navigation tweaks
    var firstLevelLi = $('#header .menu ul.level1 > li');
    firstLevelLi.filter(':first').addClass(
        'firstItem').prepend('<span class="border"></span>');
    firstLevelLi.filter(':last').addClass(
        'lastItem').prepend('<span class="border"></span>');
    var cornered = false;
    firstLevelLi.hover(
        function(){
            $_(this).addClass('active');
            if(!cornered) {
                cornered = true;
                try {
                    $('#header .menu ul.level2').corner('5px');
                }catch(e) { }
            }
        }, function() {
            $_(this).removeClass('active');
        }
    );
    $('#header .menu a').each(function(){
        var _this = $_(this);
        _this.attr('title', _this.text());
    });
    // sub nav
    $('#sub-nav .menu li, #header .menu ul.level2 li').hover(
        function(){
            $_(this).addClass('hover');
        }, function() {
            $_(this).removeClass('hover');
        }
    );
    
    // sub nav
    $('#sub-nav .menu .level2 li').hover(
        function(){
            $_(this).next('li').addClass('neighbour');
        }, function() {
            $_(this).next('li').removeClass('neighbour');
        }
    );
    
    // links and buttons
    $('#eventTicketSelection input#selectTickets,'
    + '#submitCheckout,'
    + '#submitGuestDetails,'
    + '.onlineForm #submit,'
    + '#contact-form #submit,'
    + '#donateForm #submit').hover(function() {
        $_(this).toggleClass('hover');
    });
    
    // homepage
    $('.homepage-right-column .links li:even').addClass('even');
    $('.homepage-right-column .links li:odd').addClass('odd');
    
    $('.section-intro .section li:even').addClass('even');
    $('.section-intro .section li:odd').addClass('odd');
    
    // section intro
    $('div.section-intro').find('li').each(function() {
        $_(this).find('img, div').wrapAll('<div class="image-blurb" />')
    });
    
    // footer
    $('.sitemap-link li:not(:last)', '#footer').append('|');
    $('.snippet-totally-links li:not(:last)', '#footer').append('|');
    
    //x-marketing
    $('#x-marketing .links-container a').hover(function(){
        $_(this).toggleClass('hover');
    });
    
    //preload image
    var preloadImg = new Image();
    preloadImg.src = '/images/layout/bg-panel-left-small-hover.png';
    preloadImg.src = '/images/icons/orange-arrow.png';
    preloadImg.src = '/images/layout/bg-panel-left-hover.png';
    preloadImg.src = '/images/layout/bg-panel-right-hover.png';
    
    // listings
    $('.listings .time').prepend('<span class="at">at </span>');
    $('span.info').insertAfter('.pagination');
    $('.pagination').each(function(){
        var _this = $_(this);
        var next = _this.find('.next'),
            previous = _this.find('.previous');
        next.text(next.text().replace(' >', ''));
        previous.text(previous.text().replace('< ', ''));
    });
    $('.std-listings li').each(function(){
        var _this = $_(this);
        if(!_this.find('.image').length) {
            _this.addClass('no-image');
        }
        if(!_this.find('h3.title a').length) {
            _this.find('h3.title').wrapInner('<a></a>');
        }
    });
    if(!$('.std-listings image').length) {
        
    }
    
    // update features
    $('.update-feature img.800x600').hide();
    $('.update-feature .snippet-images li').each(function(){
        var _this = $_(this);
        var alt = _this.children('.image').attr('alt');
        var img = _this.find('img.150x200');
        var imgSrc = img.next().attr('src');
        img.wrap('<a rel="fancy" href="'+imgSrc+'" title="'+alt+'"></a>');
    });
    
    // event
    try {
        $('.event .date').append(' <span class="at">at </span>');
        var eventImg = $('.event .image').find('img');
        var eventImgSrc = eventImg.attr('src');
        var newEventImgSrc = eventImgSrc.replace(/large/, 'medium');
        eventImg.attr('src', newEventImgSrc).wrap('<a href="' + eventImgSrc + '" class="fancybox"></a>');
        
    }catch(e) { }
    
    try {
        $('#customerInfoForm select').each(function(){ $_(this).fancySelect(); })
    } catch(e) { }
    try {
        $('#guestDetailsForm select').each(function(){ $_(this).fancySelect(); })
    } catch(e) { }
    
    
    // Event Guest Details
    $('#eventGuestDetails input.notRequiredGuest').change(function() {
        if ($(this).attr('checked')) {
            $(this).parents('dl').nextAll('dl').toggle();
        } else {
            $(this).parents('dl').nextAll('dl').toggle();
        }
    });
    
    // photogallery
    $('#photoAlbumList').appendTo($('#body'));
    
    processDonateForm();
    
    // flash object
    $('#top-placeholder #homepage-top').live('mouseup', function(){
        var url = $(this).parent().attr('href');
        window.location = url;
    });
    
    // corners
    try {
        $('div.homepage-right-column').find('.top a.link').corner('5px');
        $('div.homepage-right-column').find('.bottom li.odd a.link').corner('right 5px');
        $('div.homepage-right-column').find('.bottom li.even a.link').corner('left 5px');
        $('div.section-intro .section .link').find('a').corner('5px');
        $('div.std-listings .link-text').find('a').corner('5px');
        $('#homepage-events').find('a.view-all').corner('5px');
        $('div.listings .more').find('a').corner('5px');
        $('div.cmsListings .more').find('a').corner('5px');
        $('div.listings .listingItem').find('.image').corner('5px');
        $('div.cmsListings .listingItem').find('.image').corner('5px');
        $('div.searchResults .result').find('.image').corner('5px');
        $('div.searchResults .result .more').find('a').corner('5px');
        $('div.update-feature').find('.additional-images a').corner('5px');
        $('li.lastBreadcrumb').find('a').corner('5px');
        $('div.event div.image').find('img').corner('5px');
        $('#eventTicketSelection input#selectTickets').corner('5px');
        $('#submitCheckout').corner('5px');
        $('#submitGuestDetails').corner('5px');
        $('.team-listings').find('img').corner('5px');
        $('.onlineForm #submit').corner('5px');
        $('#x-marketing .photo-gallery a.link').corner('5px');
        $('div.appartment-feature').find('div.snippet-image').corner('5px');
        $('div.snippet-orange-box').corner('bottom 8px');
        $('div.snippet-orange-box').find('.link').corner('5px');
        $('#donateForm #submit').corner('5px');
        $('#header .menu ul.level2').corner('5px');
        $('.default .snippet-image img').corner('5px');
        $('.event .image ').corner('5px');
        $('.std-listings img, .grid-listing img, .event-listings .listingItem .image img, .cmsListings .listingItem .image img, .searchResults .result .image img').corner('5px');
    } catch(e) { }
    
    // lightboxes
    try {
        $('.snippet-movie-link a', '#header').click(function() {
            $.fancybox({
                'padding':0,'autoScale':false,'transitionIn':'none',
                'transitionOut':'none','title':this.title,
                'width': 680,'height':495,
                'href':this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
                'type': 'swf',
                'swf':{
                    'wmode':'transparent',
                    'allowfullscreen':'true'
                }
            });
            return false;
        });
        $('a[rel="fancy"], a.fancybox').fancybox();
        $('a[rel|="right-box"], a.fancybox').fancybox();
        $('a[rel="photo-gallery"]').fancybox();
    }catch(e){ }
    
    // cycle
    try {
        $('#x-marketing .photo-gallery ul').cycle({ 
            fx:     'fade', 
            speed:  1000, 
            timeout: 3000
        });
    }catch(e){ }

});

