﻿$(function() {
    // first add the code needed for the pop out
    var popOutContainer = $('<div class="popOutContainer"></div>');
    $('<p>Tell your friends about our work by sending an email help spread the word about the valuable work that we do</p>').appendTo(popOutContainer);
    var emailAction = $('.actionEmail'); // should only be one.
    popOutContainer.appendTo(emailAction);
    popOutContainer.hide();
    emailAction.find('a').removeAttr('title');
    emailAction.hover(function() {
        popOutContainer.fadeIn();
    },
    function() {
        popOutContainer.fadeOut();
    });

    // function to randomise the donate link text
    /* Removed in Rebrand 2011
    var donate = $('.donate');
    var donatePs = donate.find('p');
    var randomNumber = Math.floor(Math.random() * donatePs.length);
    donatePs.hide();
    donate.find('p:eq(' + randomNumber + ')').show();
    */

    // function to rotate kids section photos.
    index = 0;
    var photosWrap = $('.downloadPhotos');
    var index = 0;
    if (photosWrap.length > 0) {
        // first add the controls.
        var photos = photosWrap.find('.photoInnerWrap');
        photosWrap.find('.photoInnerWrap:not(:first)').hide();
        var photosCount = 0;
        var next = $('<a href="javascript:void(0)">Next</a>').appendTo(photosWrap);
        var back = $('<a href="javascript:void(0)">Back</a>').appendTo(photosWrap);
        next.click(function() {
            photosWrap.find('.shown').removeClass('shown');
            photosWrap.find('.photoInnerWrap').hide();
            var plength = photos.length - 1;
            index = ((index == plength) ? 0 : (index + 1));
            console.log(index);
            photosWrap.find('.photoInnerWrap:eq(' + index + ')').addClass('shown').show();
        });

        back.click(function() {
            photosWrap.find('.shown').removeClass('shown');
            photosWrap.find('.photoInnerWrap').hide();
            var plength = photos.length - 1;
            index = ((index == 0) ? plength : (index - 1));
            console.log(index);
            photosWrap.find('.photoInnerWrap:eq(' + index + ')').addClass('shown').show();
        });
    };

    // script to apply rounded images AND RESIZES elements.
    // 11/10/2011 - Removed with rebrand
    //18/10/11 - Put back in as breaking channel summary layout
    $(".rounded-img").each(function() {
        $(this).wrap(function() {
            return '<span class="' + $(this).attr('class') + '" style=\'display: block; background: #ccc url("' + $(this).attr('src') + '") no-repeat center center;\' ></span>';
        });
        if ($.browser.msie) {
            if ($.browser.version == '8.0');
            $(this).parent().parent().find('span').css('background', 'url(' + $(this).attr('src') + ') center center no-repeat');
        }
        $(this).remove();
    });

    $(".channelSummaryContainer").find('img').each(function() {
        $(this).wrap(function() {
            return '<span class="rounded-img" style=\'display: block; background: #ccc url("' + $(this).attr('src') + '") no-repeat center center;\' ></span>';
        });
        if ($.browser.msie) {
            if ($.browser.version == '8.0');
            $(this).parent().parent().find('span').css('background', 'url(' + $(this).attr('src') + ') center center no-repeat');
        }
        $(this).remove();
    });
  
    
});
