$(document).ready(function() {

    // login form remember me warning highlight - show warning when checked
    $('#ctl00_cpContent_access1_loginBox1_chkRemember').click(function() {
        if(document.getElementById('ctl00_cpContent_access1_loginBox1_chkRemember').checked == true){
            document.getElementById('rememberMeHighlight').style.display = "block";
        }
        else{
            document.getElementById('rememberMeHighlight').style.display = "none";
        }
    });

    //png fix -  I will uncomment it when I amend the masterpage-mani
    //$('div.chart').pngFix();

    //remove icon hover
    $('a.remove').hover(
	  function() {
        $(this).children('img').attr('src', '/row/assets/images/clear-hover.png');
        $(this).append('<span class="hoverAbove"><span class="inner"><span>' + $(this).attr('title') + '</span></span></span>');
    },
      function() {
        $(this).children('img').attr('src', '/row/assets/images/clear.png');
        $(this).children('span').remove();
    }
	);

    //search help hover
    $("div.form div.hover").css('display', 'none');
    $("div.form div.hover").css('position', 'absolute');
    $("div.form a.triggerIcon").hover(
	  function() { $(this).siblings('div.hover').show('fast'); },
      function() { $(this).siblings('div.hover').hide('fast'); }
	);
    $("div.form a.triggerIcon").toggle(
	  function() { $(this).siblings('div.hover').show('fast'); },
      function() { $(this).siblings('div.hover').hide('fast'); }
	);

    // select all hover
    $('label.all').hover(
	  function() {
        $(this).append('<span class="hoverAbove"><span class="inner"><span>' + $(this).attr('title') + '</span></span></span>');
    },
    function() {
        $(this).children('span').remove();
    }
	);

    // toggle for rh boxes 
    $('.box img.toggle').toggle(
    function() {
        $(this).attr({
            src: '/row/assets/images/arrow-right.png',
            alt: 'Expand'
        });
        $(this).parents("div.box").children('div.content').addClass('closed');

    },
      function() {
        $(this).attr({
            src: '/row/assets/images/arrow-down.png',
            alt: 'Collapse'
        });
        $(this).parents("div.box").children('div.content').removeClass('closed');
    }
    );

    // toggle for filter boxes 
    $('div.editFilters').children('div.box').children('h3.boxHeader').children('img.toggleFilter').toggle(
    function() {
        $(this).attr({
            src: '/row/assets/images/arrow-down.png',
            alt: 'Collapse'
        });
        $(this).parents("div.box").children('div.content').css('display', 'block');
    },
    function() {
        $(this).attr({
            src: '/row/assets/images/arrow-right.png',
            alt: 'Expand'
        });
        $(this).parents("div.box").children('div.content').css('display', 'none');
    }
  );

    // popup - used for overlays with form elements
    $('a.popup').click(function() {
        $('div.overlay').parent().css('position', 'relative');
        $('div.overlay').css('top', '110px');
        $('div.overlay').css('left', '10px');
        $('div.overlay').show('fast');
        $('div.overlay').children('.close').click(function() {
            $(this).parents('div.overlay').hide('fast');
        });
        return false;
    });

    var counter = 500;
    //overlay 
    $('a.overlay').click(function() {
        // remove any existing flyouts
        $('div.overlay').remove();
        $(this).parent().css('position', 'relative');
        $(this).parent().css('z-index', counter);
        counter++;
        $(this).parent().append('<div class="overlay"><img src="/row/assets/images/icon-close.gif" alt="close" class="close" /><div class="inner clearfix"></div></div>');
        $('div.overlay').bgiframe();

        // if endeca filter, show loading message
        if ($(this).parents('li').length > 0) {
            $(this).parents('li').children('div.overlay').children('div.inner').append('<p class="loading">Loading, please wait...</p>');
        }
        $('div.overlay div.inner').load($(this).attr('href').substring(0, $(this).attr('href').indexOf('#')) + ' ' + $(this).attr('href').substring($(this).attr('href').indexOf('#')), function() {
            //if endeca filter
            if ($(this).parents('li').length > 0) {
                // if above 219 pixels high, show scroll buttons
                if ($(this).height() > 219) {
                    $(this).addClass('setHeight');
                    $(this).parent('div.overlay').append('<img src="/row/assets/images/scrolldown.gif" alt="scrolldown" class="scrolldown" />');
                    $('img.scrolldown').click(function() {
                        $(this).siblings('div.inner').scrollTo('+=210');
                        $(this).parent('div.overlay').append('<img src="/row/assets/images/scrollup.gif" alt="scrollup" class="scrollup" />');
                        $('img.scrollup').click(function() { $(this).siblings('div.inner').scrollTo('-=210'); });
                    });
                }
            }
            // attach event handlers for smart search
            $('input.new').click(function() {
                if (this.checked) {
                    $(this).parents('div.radio').siblings('div.newsearch').css('display', 'block');
                    $(this).parents('div.radio').siblings('div.replacesearch').css('display', 'none');
                }
            });
            $('input.replace').click(function() {
                if (this.checked) {
                    $(this).parents('div.radio').siblings('div.newsearch').css('display', 'none');
                    $(this).parents('div.radio').siblings('div.replacesearch').css('display', 'block');
                }
            });
        });
        $(this).siblings('div.overlay').show('fast');
        $('.close').click(function() {
            $(this).parents('div.overlay').hide('fast');
            //temp	
            $('div.overlay').remove();
        });
        return false;
    });

    $('input.child').click(function() {
        var cur = $(this).get(0);
        if ($(this).is(':checked')) {
            $(this).parents('li.parent').children('label').children('input').each(function() {
                if (this.checked && this.id != cur.id) {
                    this.checked = false;
                }
            });
            // uncheck all child elements
            uncheck($(this).parent('label').parent('li.parent').children('ul').children('li'), cur);
        }
        else {
            // unchecked, so activate child elements
            toggleState($(this).parent('label').parent('li.parent').children('ul').children('li'), cur, false);
        }
    });

    $('input.child').each(function() {
        var cur = $(this).get(0);
        if ($(this).is(':checked')) {
            // checked, so disable all child elements
            toggleState($(this).parent('label').parent('li.parent').children('ul').children('li'), cur, true);
        }
        else {
            // unchecked, so activate child elements
            toggleState($(this).parent('label').parent('li.parent').children('ul').children('li'), cur, false);
        }
    });

    // contact us map
    if (document.getElementById("regions") != null) {
        regions();
    }

    $('a.link').click(function() {
        window.open($(this).attr('href'), 'Email', 'width=420,height=706,top=50,left=50,resizable=yes');
        return false;
    });

    $('a.print').click(function() {
        window.print();
    });

    // notifications
    $('div.notifications').children('label').children('input').click(function() {
        if ((this.id === "ctl00_cpContent_myaccountresearchprofile1_editresearchprofile1_rblstResearchTypes_3" ||
        this.id === "ctl00_cpContent_signup1_signupform1_rblstResearchTypes_3") && this.checked) {
            $(this).parents('div.notifications').siblings('div.emailformat').css('display', 'none');
        }
        else {
            $(this).parents('div.notifications').siblings('div.emailformat').css('display', 'block');
        }
    });

    // sign-up form topics
    $('div.topics').children('label').find('input').click(function() {
        if (this.value == 'radioAllTopicsTrue') {
            $('div.topicoptions').children('div.checkbox').find('input').attr('disabled', 'disabled');
            $('div.topicoptions').children('div.checkbox').find('input').attr('checked', '');
        } else {
            $('div.topicoptions').children('div.checkbox').find('input').attr('disabled', '');
        }
    });

    if ($('input#ctl00_cpContent_signup1_signupform1_radioAllTopicsTrue').attr('checked') == true) {
        $('div.topicoptions').children('div.checkbox').find('input').attr('disabled', 'disabled');
    }

    // sign-up form regions
    $('div.regions').children('label').find('input').click(function() {
        if (this.value == 'radioAllRegionsTrue') {
            $('div.regionoptions').children('div.checkbox').find('input').attr('disabled', 'disabled');
            $('div.regionoptions').children('div.checkbox').find('input').attr('checked', '');
        } else {
        $('div.regionoptions').children('div.checkbox').find('input').attr('disabled', '');
        }
    });

    if ($('input#ctl00_cpContent_signup1_signupform1_radioAllRegionsTrue').attr('checked') == true) {
        $('div.regionoptions').children('div.checkbox').find('input').attr('disabled', 'disabled');
    }

    //Research by Category 
    $('ul.categories li ul').hide();
    $('ul.categories li ul').addClass('popup');
    $('ul.categories img.expand').css('visibility', 'visible');
    $('ul.categories img.close').show();

    $('ul.categories img.expand').click(function() {
        $(this).siblings('ul').show();
    });

    $('ul.categories img.close').click(function() {
        $(this).parent('ul').hide();
    });
});

function isSearchSelected() {
  if(!$('ul.popSmartSearches').find("input[@type=radio]:checked").size()) {
    return false;
  }
  else {
    return true;
  }
}

function uncheck(listItems,input) {
  $(listItems).each(function() {  
    $(this).children('label').children('input').each(function() {
      if (this.checked && this.id != input.id) {
        this.checked = false;
      }
      this.disabled = true;
      if ($(this).parent('label').parent('li.parent').length > 0) {
        uncheck($(this).parent('label').parent('li.parent').children('ul').children('li'),input);
      }
    });
  });
}

function toggleState(listItems,input,disabled) {
  $(listItems).each(function() {  
    $(this).children('label').children('input').each(function() {
      this.disabled = disabled;
      if ($(this).parent('label').parent('li.parent').length > 0) {
        toggleState($(this).parent('label').parent('li.parent').children('ul').children('li'),input,disabled);
      }
    });
  });
}



