$(document).ready(function() {

    // homepage search field default text, and removeal on focus:
    $('.home input#search, #login-box input.text, .donate-search input.text, #contact-overlay #divide .field input').each(function() {
        if(this.value == '') {
            this.value = $(this).attr('title');
        }
        $(this).focus(function() {
            if(this.value == $(this).attr('title')) {
                this.value = '';
            }
        });

        $(this).blur(function() {
            if(this.value == '') {
                this.value = $(this).attr('title');
            }
        });
    });
    
    // Equalise homepage column heights
    var homeColHeight = 0;
    $.each($('.home-panel .border-inner'), function(){
        homeColHeight = (homeColHeight < $(this).height()) ? $(this).height() : homeColHeight;
    })
    $('.home-panel .border-inner').css('height', homeColHeight);

    // address book :focus - stays on in ajax version without page reload
    $('#abook #abook-nav li a:focus').css({
        outline:'none'
    });

    // overlay functions
    $('.overlay-bg').height($(document).height());

    if ($.browser.msie && $.browser.version.substr(0,1)<7) {
        $('.open-overlay, #flickr li').click(function() {
            $('html, body').scrollTop(0);
            return false;
        });
    }

    // create step 4 - choose main image overlay:
    $('#main-image .open-overlay').click(function() {
        $('.overlay-bg').fadeIn('normal', function() {
            $('#main-image-overlay').fadeIn('normal');
        });
        return false;
    });

    // create step 4 - page theme image overlay:
    $('#page-theme .open-overlay').click(function() {
        $('.overlay-bg').fadeIn('normal', function() {
            $('#page-theme-overlay').fadeIn('normal');
        });
        return false;
    });

    // create step 4 - your photos overlay:
    $('#your-photos .open-overlay').click(function() {
        $('.overlay-bg').fadeIn('normal', function() {
            $('#your-photos-overlay').fadeIn('normal');
        });
        return false;
    });

    /* create step 4 - flickr overlay:
	$('#flickr .open-overlay').click(function() {
		$('.overlay-bg').fadeIn('normal', function() {
			$('#flickr-overlay').fadeIn('normal');
		});
		return false;
	});*/

    // create step 4 - your videos overlay:
    $('#your-videos .open-overlay').click(function() {
        $('.overlay-bg').fadeIn('normal', function() {
            $('#your-videos-overlay').fadeIn('normal');
        });
        return false;
    });

    // create step 4 - cruk videos overlay:
    $('#cruk-videos .open-overlay').click(function() {
        $('.overlay-bg').fadeIn('normal', function() {
            $('#cruk-overlay').fadeIn('normal');
        });
        return false;
    });

    // management edit page - hide fields in IE6 because they show through overlay

    if ($.browser.msie && $.browser.version.substr(0,1)<7) {
        $('.open-overlay').click(function() {
            $('.edit .dates select').css({
                display:'none'
            });
            return false;
        });
        $('.close-overlay').click(function() {
            $('.edit .dates select').css({
                display:'inline'
            });
            return false;
        });
    }

    // step 5 & example - dead link preview overlay:

    $('.preview-only').click(function() {
        $('.overlay-bg').fadeIn('normal', function() {
            $('#preview-overlay').fadeIn('normal');
        });
        return false;
    });

    // Equalises the heights of the two panels on the donate page
    if($('.donate-search')) {
        var maxHeight = null;
        if ($('.donate-search .search-box .border-inner').height() > $('.donate-search #copy').height()) {
            maxHeight = $('.donate-search .search-box .border-inner').height();
        } else {
            maxHeight = $('.donate-search #copy').height();
        }
        $('.donate-search .search-box .border-inner').css('height', maxHeight - 10);
        $('.donate-search .search-box .bottom-left, .donate-search .search-box .bottom-right').css('bottom', 0);
        $('.donate-search #copy').css('height', maxHeight);
    }

    // generic close overlay
    $('.close-overlay').click(function() {
        //the fadeout doesn't look at all nice in ie6
        if(navigator.userAgent.toLowerCase().match('msie 6')) {
            $('.overlay-container').hide();
            $('.overlay-bg').hide();
        } else {
            $('.overlay-container').customFadeOut('normal', function() {
                $('.overlay-bg').customFadeOut('normal');
            });
        }
        //remove it if Youtube - because it doesn't stop playing..........
        if(document.getElementById('youtube-overlay')) {
            $('#youtube-overlay').remove();
        }
        return false;
    });

    // Button clicking
    $('.btn-sm, .btn-m, .btn-m2, .btn-m3, .btn-lg').click(function() {
        var currentLocation = window.location.href;
        $.each($(this).find('a'), function(){
            if (typeof $(this).data('events') == 'object') $(this).click();
            else if ($(this).attr('onclick')) {
                // Run onclick function
                result = $(this).attr('onclick')();
                // If result is not false then user the href
                if (result != false) window.location = $(this).attr('href');
            }
            else window.location = $(this).attr('href');
        });
    });



    // promote - add contact overlay:
    /*
    $('#promote-2 .open-overlay').click(function() {
        $('.overlay-bg').fadeIn('normal', function() {
            $('#promote-1 select').css({visibility:'hidden'});
            $('#contact-overlay').fadeIn('normal');
        });
        return false;
    });


	// page set-up delete function
        $('.delete').click(function () {
		$(this).parent().fadeOut('normal');
		return false;
	});
*/

    hide_submits();
    hide_labels();

    // login show/hide forgot password area:
    $('#forgotten-box:not(.shown)').hide();
    $('#sky-bg-footer, .shadow-box .border-left.png, .shadow-box .border-right.png').css('border', 'none');

    $('a#forgot').click(function() {
        if ($.browser.msie && $.browser.version.substr(0,1)<7) {
            $('#sky-bg-footer').hide();
        }
        $('#forgotten-box').fadeIn('normal', function(){
            if ($.browser.msie && $.browser.version.substr(0,1)<7) {
                $('#sky-bg-footer').show();
            } else {
                $('#sky-bg-footer').css('display', 'none').css('display', 'block');
            }
        });
        return false;
    });

    $('#forgotten-box .close').click(function() {
        if ($.browser.msie && $.browser.version.substr(0,1)<7) {
            $('#sky-bg-footer').hide();
        }
        $('#forgotten-box').fadeOut('normal', function(){
            if ($.browser.msie && $.browser.version.substr(0,1)<7) {
                $('#sky-bg-footer').show();
            } else {
                $('#sky-bg-footer').css('display', 'none').css('display', 'block');
            }
        });
        return false;
    });

    // Sets up the page search link on home page and search page
    $('.home #search-submit-anchor').click(function() {
        $('#page-search-form').submit();
        return false;
    });

    $('.donate-search #search-submit-anchor').click(function(){
        $('#page-search-form').submit();
        return false;
    });

     $('.home #page-search-form').submit(function(){
        if ($('#search').val().length == 0) {
            $('#search-error').show();
            // Paul - In IE the bottom left/right corner images do not re-render
            // when the error is displayed this fixes this issue, not very nice...
            $('.bottom-left, .bottom-right').css('display', 'none').css('display', 'block');
            return false;
        }
    });

    // hides address fields for 'find address' function on create step 2
    hide_address();

    // fixes strange anomlies with shadow-box corners in IE7
    $('.donator .bottom-right, .donator .bottom-left').addClass("anomaly");


    // Counter functionality for textareas
    $('textarea.counter').each(function(){
        var limit = 500;
        var length = $(this).val().length;
        $(this).parent().find('.counterText span').html(limit - length);
        $(this).keyup(function(){
            var new_length = $(this).val().length;
            if (new_length > limit) $(this).val($(this).val().substr(0,limit));
            else $(this).parent().find('.counterText span').html(limit - new_length);
        });
    });

    // Make sure the second donate button only shows if the
    // content is longer than a certain length
    if (0 < $('.theme-content .col2 #message').length) {
        var height = $('.theme-content .col2 #message').height();
        if (height < 315) $('.theme-content .col2 .donate').hide();
        else $('.theme-content .col2 .donate').show();
    }

});

// Common DOM operations for forms //

hide_submits = function() {
    // hides submit button which is there for non-js purposes
    $(window).load(function() {
        $('.submit-box, submit-box input').css({
            'position': 'absolute',
            'top': '-9999px',
            'left': '-9999px'
        });
    });
}
hide_labels = function() {
    // hides labels for 'date born / pass away' form on create step 1 page (about your loved one)
    $('.select label').css({
        'display': 'none'
    });
}
hide_address = function() {
    $('.create2 .hidden, .donator .hidden').css({
        'display': 'none'
    });
}
toggle_address = function() {
    if ('' != $('#address_line_1').val() || '' != $('#city').val() || $('#noUK').is(':checked')) {
        $('#address_line_1, #address_line_2, #address_line_3, #city, #county, #country, #postcode').parent().show();
        $('#paf_address, #house_number').parent().hide();
        $('#enter-house-no-msg, #no-uk-address-msg, #postcode-lookup-a').hide();
    } else {
        $('#address_line_1, #address_line_2, #address_line_3, #city, #county, #country, #country_other').val('').parent().hide();
        $('#house_number, #postcode, #noUK').val('').parent().show();
        $('#enter-house-no-msg, #no-uk-address-msg, #postcode-lookup-a').show();
    }
    if ($('#paf_address').is(':visible')) {
        $('#postcode, #house_number').parent().hide();
        $('#enter-house-no-msg, #no-uk-address-msg').hide();
    }
}
toggle_title_other = function() {
    if ('Other' == $('#title').val()) $('#title-other').show();
    else $('#title-other').hide();
}
toggle_country_other = function() {
    if ('Other' == $('#country').val()) $('#country-other').show();
    else $('#country-other').hide();
}
toggle_how_heard_other = function() {
    if ('Other' == $('#how_heard').val() || 'HHOTH' == $('#how_heard').val()) $('#how-heard-other').show();
    else $('#how-heard-other').hide();
}
toggle_anonymous_fields = function(){
    if ($('#donateAnon:checked').length > 0) {
        $('div.field.name, div.field.message').hide();
    } else {
        $('div.field.name, div.field.message').show();
    }
}

// Analytics //

trackView = function(Url) {
    //remove domain
    Url = Url.replace($('base').attr('href'), '');
    if(pageTracker) {
        // if(window.console) console.info('tracking ' +Url);
        pageTracker._trackPageview(Url);
    }
};

trackEvent = function(Category, Action, Label) {
    if (pageTracker) {
        // if(window.console) console.info('tracking event' +Category+ ' '+Action+' '+ Label);
        pageTracker._trackEvent(Category, Action, Label);
    }
};


(function($) {
    $.fn.customFadeIn = function(speed, callback) {
        $(this).fadeIn(speed, function() {
            if(!$.support.opacity)
                $(this).get(0).style.removeAttribute('filter');
            if(callback != undefined)
                callback();
        });
    };
    $.fn.customFadeOut = function(speed, callback) {
        $(this).fadeOut(speed, function() {
            if(!$.support.opacity)
                $(this).get(0).style.removeAttribute('filter');
            if(callback != undefined)
                callback();
        });
    };
    $.fn.customFadeTo = function(speed,to,callback) {
        return this.animate({
            opacity: to
        }, speed, function() {
            if (to == 1 && jQuery.browser.msie)
                this.style.removeAttribute('filter');
            if (jQuery.isFunction(callback))
                callback();
        });
    };
})(jQuery);

