﻿$(document).ready(function () {

    //
    //  HEADING REPLACEMENT
    //

    $('h1.title,h2.subtitle,h2.sidebar_title').each(function () {

        var rows = new Array();
        var title = $.trim($(this).text());

        var max_length = 90;
        if ($(this).attr('max_length')) {
            max_length = Number($(this).attr('max_length'));
        }

        //  is title too long to fit on one line?

        if (title.length > max_length) {
            var next_space = max_length + title.substr(max_length).indexOf(' ');
            rows[0] = $.trim(title.substr(0, next_space));
            rows[1] = $.trim(title.substr(next_space));
        } else {
            rows[0] = title;
        }

        //  perform character replacement
        for (var i = 0; i < rows.length; i++) {
            var text = rows[i];
            text = text.replace(/&amp;/gi, "_AMP_");
            text = text.replace(/&lt;/gi, "_LT_");
            text = text.replace(/&gt;/gi, "_GT_");
            text = text.replace(/\$/gi, "_DOL_");
            text = text.replace(/&copy;/gi, "_COPY_");
            text = text.replace(/&nbsp;/gi, " ");
            text = text.replace(/\./gi, "_DOT_");
            text = text.replace(/\+/gi, "_PL_");
            text = text.replace(/\,/gi, "_COM_");
            text = text.replace(/\//gi, "_SL_");
            text = text.replace(/\:/gi, "_COL_");
            text = text.replace(/\;/gi, "_SEM_");
            text = text.replace(/\=/gi, "_EQ_");
            text = text.replace(/\?/gi, "_QU_");
            text = text.replace(/%/gi, "_PER_");
            text = text.replace(/"/gi, "_QUO_");
            text = text.replace(/\@/gi, "_AT_");
            text = text.replace(/\\/gi, "_BS_");
            text = text.replace(/\’/gi, "'");
            text = text.replace(/&/gi, "_AMP_");
            text = text.replace(/™/gi, "_TM_");
            
            rows[i] = text;
        }

        //  add search engine friendly text
        $(this).html('<span>' + title + '</span>');

        //  add individual title images
        for (var i = 0; i < rows.length; i++) {
            var img = $('<img>');
            img.attr('src', 'http://' + document.domain + '/images/' + $(this).attr('class') + '/' + escape(rows[i]) + '.aspx');
            img.attr('alt', rows[i]);
            $(this).append(img);
            $(this).append('<br />');
        }

    });

    //
    //  SIGNUP BOX
    //

    //  clears e-news text
    $('.signup .text').focus(function () {
        if ($(this).val() == '') {
            $(this).addClass('text_active');
        }
    }).blur(function () {
        if ($(this).val() == '') {
            $(this).removeClass('text_active');
        }

    });

    //
    //  VARIETY NAVIGATION
    //

    $('.nav_aussie_grown_varieties').mouseenter(function () { $('.nav_varieties').show() })
                                    .mouseleave(function () { $('.nav_varieties').hide() });

    $('.nav_varieties').mouseenter(function () { $('.nav_varieties').show() })
                       .mouseleave(function () { $('.nav_varieties').hide() });

    //
    //  VARIETY BOXOUTS
    //

    $('.nav_varieties li').hover(function () { $('.boxout[variety_id=' + $(this).attr('variety_id') + ']').show(); },
                                 function () { $('.boxout[variety_id=' + $(this).attr('variety_id') + ']').hide(); });

    //
    //  HEADER IMAGES
    //

    if ($('#aspnetForm').hasClass('template_homepage')) {

        //  HOMEPAGE
        //

        $('.header_slideshow ul').cycle({
            fx: 'fade',
            speed: 2500,
            timeout: 5000,
            random: 0,
            fastOnEvent: 1000,
            pager: '.pager',
            pagerAnchorBuilder: function (idx, slide) {
                return '<b  class="apple_' + idx + '"><a href="#">' + idx + '</a></b>';
            },
            updateActivePagerLink: function (pager, startingSlide, pagerClass) {
                $(pager).each(function () {
                    $(this).children().find('a').removeClass(pagerClass);
                    $(this).children().eq(startingSlide).find('a').addClass(pagerClass);
                });
            }
        });

    }

});

//
//  RANDOM ELEMENT FUNCTION
//

jQuery.jQueryRandom = 0;
jQuery.extend(jQuery.expr[":"],
{
    random: function (a, i, m, r) {
        if (i == 0) {
            jQuery.jQueryRandom = Math.floor(Math.random() * r.length);
        };
        return i == jQuery.jQueryRandom;
    }
});

//
//  VALIDATOR CHECK
//

function checkValidators() {
    var val = Page_ClientValidate();
    if (!val) {
        for (var i = 0; i < Page_Validators.length; i++) {
            if (!Page_Validators[i].isvalid) {
                $("#" + Page_Validators[i].controltovalidate).addClass('error');
            } else {
               // $("#" + Page_Validators[i].controltovalidate).removeClass('error');
            }
        }
    }
    return val;
}

