﻿$(document).ready(function () {

    //
    // CHECK PREVIEW MODE
    //

    var urlvals = getUrlVars();

    if (urlvals["PreviewMode"] != null) {
        $('a').click(function () {
            if ($(this).attr('id').indexOf('hplCMS') == -1) {
                alert('You are currently in preview mode. Links have been disabled. Click on Exit Preview Mode to return to the administration area.');
                return false;
            }
        });

        $('input').attr('disabled', 'true');
    }

    //
    //  HEADING REPLACEMENT
    //

    $('.title, .HeaderTagLine, .subtitle,.fresh_title,.hospitality_title,.logistics_title,.subtitle_short,.foodservice_title,.tagline, .subtitle, .subtitle_small, .subtitle_small_bold, .FreshTagline, .LogisticsTagline, .FoodServiceTagline, .HospitalityTagline, .subtitle_small_over, .pdfLink, .subtitle_over').each(function () {

        var rows = new Array();
        var title = $.trim($(this).html());
        var maxLength = 50;

        if ($(this).attr('maxlength') != undefined) {
            maxLength = $(this).attr('maxlength');
        }

        if ($(this).hasClass('pdfLink')) {
            maxLength = 15;
        }

        if ($(this).hasClass('subtitle_short')) {
            maxLength = 15;
        }

        //  is title too long to fit on one line?
        if (title.length > maxLength) {
            var next_space = maxLength + title.substr(maxLength).indexOf(' ');
            rows[0] = $.trim(title.substr(0, next_space));
            rows[1] = $.trim(title.substr(next_space));
            //alert("'" + rows[0] + "'");
            //alert("'" + rows[1] + "'");
        } else {
            rows[0] = title;
        }

        //  perform character replacement
        for (var i = 0; i < rows.length; i++) {
            var text = rows[i];
            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(/&amp;/gi, "_AMP_");
            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_");
            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').replace(' hover_heading', '').replace(' pdf_hover_heading', '') + '/' + escape(rows[i]) + '.aspx');
            img.attr('alt', rows[i]);
            $(this).append(img);
        }


        //
        //  EXTERNAL LOGIN
        //

        //  clears e-news text
        $('.LoginUserName').focus(function () {
            if ($(this).val() == 'Username') {
                $(this).val('');
                $(this).addClass('signup_box_active');
            }
            $(this).addClass('signup_box_focus');
        }).blur(function () {
            if ($(this).val() == '') {
                $(this).val('Username');
                $(this).removeClass('signup_box_active');
            }
            $(this).removeClass('signup_box_focus');
        });

        $('.searchTextBox').focus(function () {
            if ($(this).val() == 'Search') {
                $(this).val('');
                $(this).addClass('signup_box_active');
            }
            $(this).addClass('signup_box_focus');
        }).blur(function () {
            if ($(this).val() == '') {
                $(this).val('Search');
                $(this).removeClass('signup_box_active');
            }
            $(this).removeClass('signup_box_focus');
        });

        $('.LoginPassword').val('Password');

        $('.LoginPassword').focus(function () {
            if ($(this).val() == 'Password') {
                $(this).val('');
                $(this).addClass('signup_box_active');
            }
            $(this).addClass('signup_box_focus');
        }).blur(function () {
            if ($(this).val() == '') {
                $(this).val('Password');
                $(this).removeClass('signup_box_active');
            }
            $(this).removeClass('signup_box_focus');
        });


        // replace hover images

        var hoverMouseOver = function () {
            var t = $(this);
            var imgSrc = t.attr('src');

            // get the file extension and use that in the replace
            var fileExt = imgSrc.substring(imgSrc.length - 4);

            // check if the file ends with _over
            if (imgSrc.substring(imgSrc.length - 9) != "_over" + fileExt) {
                t.attr('src', imgSrc.replace("" + fileExt + "", "_over" + fileExt));
                $(this).css('cursor', 'pointer');
            }
        }

        var hoverMouseout = function () {
            var t = $(this);
            t.attr('src', t.attr('src').replace('_over', ''));
        }
        $('.hover').hover(hoverMouseOver, hoverMouseout);

        // preload images from the hover class automatically
        //var imgPreloadArray = new 
        $('.hover').each(function () {
            // get each of the hover class images on the pagse
            var t = $(this);
            // get the source of each of these images
            var imgSrc = t.attr('src');
            if (imgSrc != undefined) {

                // get the file extension and use that in the replace
                var fileExt = imgSrc.substring(imgSrc.length - 4);
                var newfilesource = imgSrc.replace("" + fileExt + "", "_over" + fileExt);

                jQuery("<img>").attr("src", newfilesource);
            }
        });

        // define the preloader function
        jQuery.preloadImages = function () {
            for (var i = 0; i < arguments.length; i++) {
                jQuery("<img>").attr("src", arguments[i]);
            }

        }

        // pdf replace


        // replace hover heading images

        var hoverMouseOverHeading = function () {
            var t = $('img', this);
            var imgSrc = t.attr('src');

            // get the file extension and use that in the replace
            var fileExt = imgSrc.substring(imgSrc.length - 5);

            var headingPath = imgSrc.substring(imgSrc.lastIndexOf("/"));


            // check if the file ends with _over
            if (imgSrc.substring(imgSrc.lastIndexOf("/") - 5) != "_over" + headingPath) {
                t.attr('src', imgSrc.replace("" + headingPath + "", "_over" + headingPath));
                $(this).css('cursor', 'pointer');
            }
        }

        var hoverMouseoutHeading = function () {
            var t = $('img', this);
            t.attr('src', t.attr('src').replace('_over', ''));
        }
        $('.hover_heading').hover(hoverMouseOverHeading, hoverMouseoutHeading);

        // preload images from the hover class automatically
        //var imgPreloadArray = new 
        $('.hover_heading').each(function () {
            // get each of the hover class images on the pagse
            var t = $('img', this);
            // get the source of each of these images
            var imgSrc = t.attr('src');
            if (imgSrc != undefined) {

                // get the file extension and use that in the replace
                var headingPath = imgSrc.substring(imgSrc.lastIndexOf("/"));
                var newfilesource = imgSrc.replace("" + headingPath + "", "_over" + headingPath);

                jQuery("<img>").attr("src", newfilesource);
            }
        });

        // replace pdf hover heading images that go over 2 lines

        var hoverMouseOverHeading = function () {

            $(this).find('img').each(function () {
                var t = $(this);
                var imgSrc = t.attr('src');

                var fileExt = imgSrc.substring(imgSrc.length - 5);

                var headingPath = imgSrc.substring(imgSrc.lastIndexOf("/"));

                //    check if the file ends with _over
                if (imgSrc.substring(imgSrc.lastIndexOf("/") - 5) != "_over" + headingPath) {
                    t.attr('src', imgSrc.replace("" + headingPath + "", "_over" + headingPath));
                    $(this).css('cursor', 'pointer');
                }
            });

            //            var t = $('img', this);
            //            var imgSrc = t.attr('src');

            //            // get the file extension and use that in the replace
            //            var fileExt = imgSrc.substring(imgSrc.length - 5);

            //            var headingPath = imgSrc.substring(imgSrc.lastIndexOf("/"));


            //            // check if the file ends with _over
            //            if (imgSrc.substring(imgSrc.lastIndexOf("/") - 5) != "_over" + headingPath) {
            //                t.attr('src', imgSrc.replace("" + headingPath + "", "_over" + headingPath));
            //                $(this).css('cursor', 'pointer');
            //            }
        }

        var hoverMouseoutHeading = function () {

            $(this).find('img').each(function () {
                var t = $(this);
                t.attr('src', t.attr('src').replace('_over', ''));
            });
        }

        $('.pdf_hover_heading').hover(hoverMouseOverHeading, hoverMouseoutHeading);

        // preload images from the hover class automatically
        //var imgPreloadArray = new 
        $('.pdf_hover_heading').each(function () {
            // get each of the hover class images on the pagse

            $(this).find('img').each(function () {
                var t = $(this);
                // get the source of each of these images
                var imgSrc = t.attr('src');
                if (imgSrc != undefined) {

                    // get the file extension and use that in the replace
                    var headingPath = imgSrc.substring(imgSrc.lastIndexOf("/"));
                    var newfilesource = imgSrc.replace("" + headingPath + "", "_over" + headingPath);

                    jQuery("<img>").attr("src", newfilesource);
                }
            });
        });

    });


    // THE BANNER SLIDER
    $(document).ready(function () {
        $('.header_slideshow ul').cycle({
            fx: 'fade',
            timeout: 6000,
            speed: 1500,
            pager: '#header_images_pager'
        });
    });

    function getUrlVars() {
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for (var i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    }
});
