jQuery.noConflict();

var selectTags = [];
var selectTagsWidth = 0;

jQuery(document).ready(function() {
    if (jQuery('#ContentImage').size() === 0) {
        jQuery('#subsidebarright').css('margin-top', '20px');
    }
    if (jQuery('#deliverystream').size()) {
        jQuery('#deliverystream').height(jQuery('#deliverystream a:first img').height());
    }
    if (jQuery('#rightright').size() && jQuery('.managementright').size() != 0 && jQuery('#left_column').size() != 0) {
        var equalHeight = (jQuery('#subcontentmiddle').height() > jQuery('#subsidebarright').height()) ? jQuery('#subcontentmiddle').height() : jQuery('#subsidebarright').height();
        jQuery('#subsidebarright').height(equalHeight + parseInt(jQuery('#subcontentmiddle').css('margin-top')));
        jQuery('#rightright').height(equalHeight + parseInt(jQuery('#subcontentmiddle').css('margin-top')) - parseInt(jQuery('#rightright').css('padding-top')));
    }
    
    if (jQuery('.homecalloutnolinetext').size() != 0) {
      var height = 0;
      jQuery('.homecalloutnolinetext').each(function() {
          if (jQuery(this).height() > height) {
              height = jQuery(this).height();
          }
      });
    }

    jQuery('.sidebarcallout_italic div :last-child, .sidebarcallout_first_italic div :last-child').addClass("regular_left");

    jQuery('.homecalloutnolinetext').each(function() {
        jQuery(this).attr('style', 'float: left; height: ' + (height + 5) + 'px');
    });

    //if (!jQuery.browser.msie) {
    jQuery('.scfForm div').each(function() {
        if (this.id.indexOf('content_') != -1) {
            jQuery('#' + this.id + ' .scfRequired').each(function() {
                jQuery(this).parent().find('label').after('<span class="scfRequired">*</span>');
                jQuery(this).remove();
            });
            jQuery('#' + this.id + ' .scfValidatorRequired').each(function() {
                var span = document.createElement('span');
                span.setAttribute('id', jQuery(this).attr('id'));
                span.setAttribute('class', jQuery(this).attr('class'));
                span.setAttribute('style', jQuery(this).attr('style'));
                span.innerHTML = jQuery(this).html();
                jQuery(this).parent().find('label').after(span);
                jQuery(this).remove();
            });
        }
    });
    //}

    // Show 100% width select tag on IE so all option text is visible
    if (jQuery.browser.msie) {
        jQuery('.scfForm select').each(function() {
            if (!jQuery(this).hasClass('dropdown_no_resize')) {
                var selectTagsWidth = jQuery(this).innerWidth() + 2;
                var selectTagsAutoWidth = jQuery(this).css('width', 'auto').innerWidth();
                jQuery(this).css('width', selectTagsWidth);

                jQuery(this)
            .mousedown(function(event) {
                if (selectTagsAutoWidth > selectTagsWidth) {
                    jQuery(this).css('width', 'auto');
                } else {
                    jQuery(this).css('width', selectTagsWidth);
                }
            })
            .blur(function() {
                jQuery(this).css('width', selectTagsWidth);
            })
            .change(function() {
                jQuery(this).blur();
            });
            }
        });
    }

    if (jQuery('.scfSubmitButtonBorder')) {
        jQuery('.scfSubmitButtonBorder').before('<div class="graybutton"><input type="reset" value="Clear Form" name="resetB" onClick="customClearForm(); return false;"></div>');
    }

    jQuery('#main.products').innerfade({
        animationtype: 'fade',
        speed: 'normal',
        timeout: '6000',
        type: 'sequence',
        containerheight: '420',
        runningclass: 'innerfade'
    });

    jQuery('#featurednav.products').innerfade({
        animationtype: 'fade',
        speed: 'normal',
        timeout: '6000',
        type: 'sequence',
        containerheight: '420',
        runningclass: 'innerfade'
    });

    jQuery('body').supersleight();
    jQuery(".date_customTextBox").datepicker({
        maxDate: 0
    });

    //    jQuery('.scfForm textarea').each(function() {
    //        if (this.value != '') {
    //            jQuery(this).focus(function() { focusTextArea(this) });
    //            jQuery(this).blur(function() { blurTextArea(this) });
    //        }
    //    });

    jQuery('.scfForm input[type="text"], .scfForm select').each(function() {
        if (jQuery(this).parent().parent().find("label:first").html().toLowerCase() == "state") {
            jQuery(this).attr('style', 'width: 110px;');
            jQuery(this).parent().attr('style', 'width: 115px;');
            jQuery(this).parent().parent().attr('style', 'width: 145px;');
            //jQuery(this).parent().parent().find(":first").attr('style', 'width: 110px;');
        } else if (jQuery(this).parent().parent().find("label:first").html().toLowerCase() == "zip code") {
            jQuery(this).attr('style', 'width: 85px;');
            jQuery(this).parent().attr('style', 'width: 90px;');
            jQuery(this).parent().parent().attr('style', 'width: 110px;');
            //jQuery(this).parent().parent().find(":first").attr('style', 'width: 85px;');
        }
    });

    //    jQuery('.scfSubmitButtonBorder input').click(function () {
    //        jQuery('.scfForm select').each(function () {
    //            var next_elem = jQuery(this).parent().parent().next();
    //            var child = jQuery(next_elem).children();
    //            var label_value = jQuery(child[0]).html();
    //            var divOtherChildren = jQuery(child[1]).children();

    //            if (this.value.toLowerCase().indexOf('other') <= 0) {
    //                if (label_value.toLowerCase().indexOf('other') >= 0) {
    //                    //alert(jQuery(divOtherChildren[0]).val());
    //                    jQuery(divOtherChildren[0]).val('');
    //                }
    //            }
    //        });
    //    });

    //add custom text to validation area    
    jQuery('.scfValidationSummary').prepend('<strong>Something went wrong.</strong><div>Please check the following mandatory fields for missing or invalid input:</div>');

    //hide Email/Phone textboxes
    var div_email = jQuery(".wffm_custom_form_id_class").attr("div_email");
    var div_phone = jQuery(".wffm_custom_form_phone").attr("div_phone");

    var input_select = jQuery(".wffm_custom_form_id_class").attr("input_select");
    var label_droplist = jQuery(".wffm_custom_form_id_class").attr("label_droplist");

    if (typeof div_email != 'undefined') {
        var drop_down = document.getElementById(input_select);
        var value_selected = drop_down.options[drop_down.selectedIndex].value;

        if (value_selected == "Email") {
            document.getElementById(div_email).style.display = "block";
            document.getElementById(div_phone).style.display = "none";
        }
        else
            if (value_selected == "Phone") {
            document.getElementById(div_email).style.display = "none";
            document.getElementById(div_phone).style.display = "block";
        }
        else {
            document.getElementById(div_email).style.display = "none";
            document.getElementById(div_phone).style.display = "none";
            // jQuery(".scfValidationSummary ul").append("<li>" + jQuery("#" + label_droplist).html() + " must be filled in.</li>");
        }
    }


    // hide "Other" textboxes
    jQuery('.scfForm select').each(function() {
        var next_elem = jQuery(this).parent().parent().next();
        var child = jQuery(next_elem).children();
        var label_value = jQuery(child[0]).html();

        if (this.value.toLowerCase().indexOf('other') >= 0) {
            if (label_value.toLowerCase().indexOf('other') >= 0) {
                jQuery(next_elem).show();
            }
        }
        else {
            if (label_value != null && label_value.toLowerCase().indexOf('other') >= 0) {
                jQuery(next_elem).hide();
            }
        }
    });

    //hide hidden_customtextBox
    jQuery('.hidden_item_custom').parent().parent().hide();

    //    var div_element = jQuery(".wffm_custom_form_id_class2").attr("div_element");
    //    var input_selectElement = jQuery(".wffm_custom_form_id_class2").attr("input_select");
    //    var show_value = jQuery(".wffm_custom_form_id_class2").attr("show_value");

    //    if (typeof div_element != 'undefined') {
    //        var drop_down = document.getElementById(input_selectElement);
    //        var value_selected = drop_down.options[drop_down.selectedIndex].value;

    //        if (value_selected == show_value) {
    //            document.getElementById(div_element).style.display = "block";
    //        }
    //        else
    //            document.getElementById(div_element).style.display = "none";
    //    }

    var input_phone_id = jQuery(".wffm_custom_form_phone").attr("input_phone");

    // add phone validation
    if (input_phone_id != 'undefined') {
        jQuery("#" + input_phone_id).keydown(function(event) {
            return dFilter(event.keyCode, this, '###-###-####');
        });
    }

    //    if (typeof div_email != 'undefined' && typeof div_phone != 'undefined') {

    //        if (document.getElementById(div_email).style.display == "block") {
    //            if (jQuery("#" + input_email_id).val() != '') {
    //                var m = regex_email.exec(jQuery("#" + input_email_id).val());
    //                if (m == null) {
    //                    jQuery(".scfValidationSummary ul").append("<li>Invalid Email Address.</li>");
    //                }
    //            }
    //            else {
    //                jQuery(".scfValidationSummary ul").append("<li>Email must be filled in.</li>");
    //            }
    //        }
    //        if (document.getElementById(div_phone).style.display == "block") {
    //            if (jQuery("#" + input_phone_id).val() != '') {
    //                var m = regex_email.exec(jQuery("#" + input_email_id).val());
    //                if (m == null) {
    //                    jQuery(".scfValidationSummary ul").append("<li>Invalid Phone Number.</li>");
    //                }
    //            }
    //            else {
    //                jQuery(".scfValidationSummary ul").append("<li>Phone must be filled in.</li>");
    //            }
    //        }
    //    }
    jQuery('.otherprovider').parent().parent().addClass('divotherprovider');
    jQuery('.othermaterials').parent().parent().addClass('divothermaterials');
    jQuery('.otheraboutus').parent().parent().addClass('divotheraboutus');

    var otherprovider = jQuery('.otherprovider').parent().parent();
    jQuery('.otheraboutus').parent().parent().before(otherprovider);
});

function optimizeWidth() {
  maxWidth = jQuery('#slideNav').width();
  elementCount = jQuery('#slideNav a').size();
  elementMargin = 0;
  elementPadding = 0;

  element = jQuery('#slideNav a').first();
  elementMargin += parseInt(element.css('margin-left'));
  elementMargin += parseInt(element.css('margin-right'));
  elementPadding += parseInt(element.css('padding-left'));
  elementPadding += parseInt(element.css('padding-right'));
  elementWidth = element.width() + elementMargin + elementPadding;

  newMargin = Math.floor(maxWidth/(elementCount+1) + parseInt(element.css('margin-left')) - element.width());
  jQuery('#slideNav a').css('margin-left', newMargin);
}

jQuery(function ($) {	//slideshow cycle
    if ($('#slideshow')[0]) {
        $('#slideshow').before('<div id="slideButton">').before('<div id="slideNav">').cycle({
            fx: 'fade',
            pager: '#slideNav',
            timeout: jQuery('#carouselTimeoutInSec').val() * 1000
        });
        $('#slideButton').prepend('<a id="pauseButton" title="Stop" style="background-image: url(/images/Streamlite/buttons/stop.gif);" onclick="stopSlideshow()"></a>');
    }
    //optimizeWidth();
});

