
// for areas of study

function getOffsetTop(element) {
    var offset = element.offsetTop
    while (element = element.offsetParent) {
        offset += element.offsetTop
    }

    return offset;
}

function adjustSpacer(height) {
    var contanerId = 'areaofStudy';
    var spacerId = 'KU_spacer';
    var eSpacer = ''
    var finalHeight = null;

    // get element
    if (document.getElementById(spacerId) == null) {
        eSpacer = document.createElement('div');
        eSpacer.id = spacerId;
        eSpacer.style.height = 0 + 'px';
        document.getElementById('areaofStudy').appendChild(eSpacer);
    }
    else {
        eSpacer = document.getElementById(spacerId);
    }

    finalHeight = height + eSpacer.offsetHeight;
    if (finalHeight < 0) {
        finalHeight = 0;
    }
    eSpacer.style.height = finalHeight + 'px';
}

function resetSpacer() {
    var spacerId = 'KU_spacer';
    var eSpacer = ''

    // get element
    if (document.getElementById(spacerId) != null) {
        eSpacer = document.getElementById(spacerId);
        eSpacer.style.height = '0px';
    }
}

$(document).ready(function() {

    var offset = null;
    var counter = null;

    $('.studyareas ul li.discipline a').mouseover(function(event) 
    {
        if ($(this).parent().hasClass('discipline')) 
        {

            offset = $(this).parent().attr('value');
            $('#active_dd').css('display', 'none');
            $('#active_dd_left_open').css('display', 'none');
            $('dl a.active').removeClass('active');
            $('.studyareas ul li.active dl').hide();
            $('.studyareas ul li.active').removeClass('active');
            $(this).parent().find('dl').show();
            $(this).parent().addClass('active');
            $(this).parent().blur();
            resetSpacer();
        }
    });

    function showProgram(clickObj)
      {  
          
          // determine where the active_dd div will sit
                  var top = $(clickObj).offset().top;
                  var left = $(clickObj).offset().left;
                  var top1 = $('.studyareas ul li > a').get(0);
                  top1 = $(top1).offset().top + offset;
                  //alert('top1 :' + top1);                 
                  //top = top - top1;
                  var topForLink = 0;
                  var topForBox = 0;
                  var topForImg = 0;
                  var ad = $('#active_dd');
                  if($(clickObj).attr('id') == 'wide_box')
                  {
                      $('#active_dd').css("background","transparent url(/kaplanimages/degree_data_bg.gif) no-repeat scroll 1px top");
                      $('#active_dd').css("height","400px");
                      $('#active_dd').css("width","370px");
                      //alert($(this).attr('id'));
                  }
                  else
                  {                    
                      $('#active_dd').css("background","transparent url(/kaplanimages/degree_data_bg.gif) no-repeat scroll left top");
                      $('#active_dd').css("height","265px");
                      $('#active_dd').css("width","300px");
                  }
                  var adOpenImg = $('#active_dd_left_open');
                  var duh = new String($(clickObj).parent().attr('id'));
                  var minTop = $('.program_group').offset().top - $('.studyareas').offset().top;
                  var spacerHeight = 0;
                  var isAlreadyActive = 0;
                  if( top < minTop ){
                        top = minTop;
                  }
                  
                  duh = 'd' + duh.substring(1, duh.length);
                  duh.replace('t','d');
                  if( $('#'+duh).hasClass('active_dd_contect') ){
                        //isAlreadyActive = 1;
                        $('.active_dd_contect').removeClass('active_dd_contect');
                  }                 
                  $('#'+duh).addClass('active_dd_contect');
                  //alert(duh);
                  //ad.html($('#'+duh+' .active_dd_contect').html());
                  if(ad)
                  {
                       ad.html($('#'+duh).html());
                  }
                  var popupLeft = parseFloat(left) + 300;
                  // if not repeated click, move box
                  if( isAlreadyActive == 0 && ad !=null){
                        ad.css('top', (top - offset)+ 'px');
                        ad.css('left', popupLeft + 'px');
                        ad.css('display','block');
                  }
                  
                  //setting popup contents height
                  var popupDiv = $("#active_dd");
                  if(popupDiv)
                  {
                      var containerHeight = parseInt(popupDiv.innerHeight());
                      var headerP = popupDiv.children("p.offerred");
                      var headerHeight =  parseInt(headerP.height()) + parseInt(headerP.css("marginTop")) + parseInt(headerP.css("marginBottom"));
                      var ulElement = popupDiv.children("ul.offered");
                      var ulHeight = containerHeight - headerHeight - parseInt(ulElement.css("marginTop")) - parseInt(ulElement.css("marginBottom")) - 8 ;
                      
		              ulElement.css("height", ulHeight + "px" );
                  }
                  
                  topForLink =  getOffsetTop(clickObj);
                  topForBox = getOffsetTop(document.getElementById('active_dd'));               
                  // generic location for box image opening
                  topForImg = top - 2;//+ topForLink - topForBox - 3;
                  // fixes per browser
                  if( navigator.userAgent.indexOf('MSIE 7.') >= 0 )
                  {
                        topForImg += (-2);
                  }
                  else if( navigator.userAgent.indexOf('MSIE 8.') >= 0 )
                  {
                        topForImg += (-1);
                  }                 
                  else if( navigator.userAgent.indexOf('Firefox') >= 0 )
                  {                       
                        if( $('.studyareas ul li dl:even:visible').length == 0 )
                        {                 
                        //topForImg += 1;
                        }
                        else
                        {topForImg += 1;}             
                  }
                  else if( navigator.userAgent.indexOf('Safari') >= 0 )
                  {
                        topForImg -= 1;
                  }
                  
                  // if not repeated click, move box opening image
                  if( isAlreadyActive == 0 ){               
                        adOpenImg.css('top', topForImg + 'px');
                        adOpenImg.css('left',popupLeft + 'px');
                        adOpenImg.css('display','block');
                  }
                  
                  $('dl a.active').removeClass('active');
                  $(clickObj).addClass('active');
                  activeObj = clickObj;
                  $(clickObj).blur();                       
          }

    $('.studyareas ul li dl dt > a').each(function() {
        $(this).click(function(event) {
            showProgram(this);
            event.stopPropagation();
            // adjust spacer for then box is lower than container
            var ad = $('#active_dd');
            if (document.getElementById('areaofStudy') != null)
                spacerHeight = ad.height() + top - document.getElementById('areaofStudy').offsetHeight;
            adjustSpacer(0);
        });

        $(this).mouseover(function(event) {
            showProgram(this);
            event.stopPropagation();
            // adjust spacer for then box is lower than container
            var ad = $('#active_dd');
            if (document.getElementById('areaofStudy') != null)
                spacerHeight = ad.height() + top - document.getElementById('areaofStudy').offsetHeight;
            adjustSpacer(0);
        });

    });

});

function reqOver(elemNo) {
    if (document.getElementById("emphasis") != null) {
        var elme = elemNo + 1;
        $("li[@id^='req_li']").removeClass("active");
        $("#req_li_" + elme).addClass("active");
        rollOverFunc('#desc > div', elemNo);
    }
}
function rollOverFunc(rolloverDiv, elemNo) {
    var noOfDivs = $(rolloverDiv).length;
    for (var i = 0; i < noOfDivs; i++) {
        $(rolloverDiv)[i].style.display = 'none';
    }
    $(rolloverDiv)[elemNo].style.display = 'block';

}

function toggle(id) {
    el = document.getElementById(id);
    var display = el.style.display ? '' : 'none';
    el.style.display = display;
}




//added from microportalhome.aspx pagetemplate

$ektron(document).ready(function() {
    $ektron('.Brand_MP_TabContentRow:gt(0)').hide();
    $ektron('.Brand_MP_TabContentRowBottom').hide();

    $ektron('#Brand_MP_TabContentRowUL ul> li > a').click(function() { return showHideTab(this); });

    //$ektron('.KHE_MP_RequestInfo').click(function(){showHideTab('req_info_desc');return true;});
    //$ektron('.KHE_MP_RequestInfo a').click(function(){return showHideTab('req_info_desc');return true;});
    var sTab = window.location.hash;
    sTab = sTab.replace('#', '');
    if (sTab != null && sTab != "") {
        showHideTab(sTab);
        return true;
    }

});

$(document).ready(function() {
    $('.Brand_Home_Promo_1').each(function(index) {
        if (index == 0) {
            $(this).addClass('Brand_Home_PromoFirst');
        }
        else {
            $(this).addClass('Brand_Home_PromoOther');
        }
    });
});

function showHideTab(tabId) {
    var tabElement = (typeof (tabId) != "object" && tabId != null && tabId != "") ? $ektron('#' + tabId) : $ektron(tabId);

    $ektron('.Brand_MP_TabContentRow').hide();
    var container = '#' + $ektron(tabElement).attr('name');
    //alert(container);
    $ektron('#Brand_MP_TabContentRowUL ul> li > a').attr("class", "");
    $ektron(tabElement).attr("class", "active");
    $ektron(container).fadeIn(350);
    if (container == "#tabSchool") {
        $ektron('.Brand_MP_TabContentRowBottom').hide();
    }
    else {
        $ektron('.Brand_MP_TabContentRowBottom').show();
    }
    return false;
}
function searchClick(buttonName, e) {

    var key;
    if (window.event)
        key = window.event.keyCode;     //IE
    else
        key = e.which;     //firefox

    if (key == 13) {

        var btn = document.getElementById(buttonName);
        if (btn != null) { //If we find the button click it
            btn.click();
            event.keyCode = 0
        }
    }
}
function fnCheckSearchText(elementId) {
    var textElement = document.getElementById(elementId);
    if (null != textElement && fnTrim(textElement.value) != "") {
        return true;
    }
    else {
        alert('Please enter one or more search words.');
        return false;
    }
}

function fnTrim(value) {
    return value.replace(/^\s+|\s+$/g, "");
}



