(function($) {
$(function(){


	if ($('#mast').length > 0) {
		var bgYPos = $('#mast').position().top-116;
		if ($.browser.msie && $.browser.version<7)
			bgYPos+=2;
		$(document.body).css({backgroundImage:'url(magazine/graphics/body-bg.jpg)', backgroundPosition:'0 '+bgYPos+'px'});
	}

});
})(jQuery);



function compareFirms(oForm) {    
    var oCompare = oForm.compare;
    
    var iCompare = 0
    for (var i=0; i< oCompare.length; i++) {
      if (oCompare[i].checked) {
        iCompare += 1;
      }
    }    
    
    if (iCompare == 0) {
        alert('Please select at least one firm to compare');
    }
    else if (iCompare > 3) {
        alert('Please select up to three firms to compare');
    }
    else {
        oForm.submit();
    }
}


function addColumn(oField) {    
    
    __doPostBack('headlineResults', oField.options[oField.selectedIndex].value);
    
}

function showFirmTab(iHolder) {
    var holderContainer = document.getElementById('tabContainer');
    var holders = holderContainer.childNodes;
    
    var iText = 0;
    for (var i=0; i<holders.length; i++) {           
        if (holders[i].nodeType != 3) {
            holders[i].style.display = 'none';
            if (i == iHolder + iText) {
                holders[i].style.display = 'block';
            }
        }
        else {
            iText += 1;
        }

    }


    var tabDiv = document.getElementById('changeView');
    tabs = tabDiv.getElementsByTagName('A');
   
    for (var i=0; i<tabs.length; i++) {    
        if ((i % 2) == 0) {
            tabs[i].className = 'odd';
        }
        else
        {
            tabs[i].className = 'even';
        }
        if (i == iHolder) {
            tabs[i].className = tabs[i].className + ' selectedTab';            
        }
    }

}



function showChart(oArray, sTitle) {

     var $ = jQuery;
     
  $(document).ready( function() {     
     
     var oChart = $("#chart");
     var s = "";
          
     var iMax = 0;
     for (var i=0; i<oArray.length; i++) {
       if (i % 2 == 0 ) {
         var sClass = 'odd';
       }
       else {
         var sClass = 'even';
       }
       s += "<div class=\"row " + sClass + "\"><div class=\"title\"><span>" + oArray[i][0] + "</span></div><div class=\"bar\"><span>" + oArray[i][1] + "</span></div></div>";
       var val = oArray[i][1];
           val = val.replace(/[^\d\.]*/igm, "");
           val = parseFloat(val);           
       if (val > iMax) {
         iMax = val;
       }
     }
             
     var oChart = $("#chart");
     $("h3", oChart).html(sTitle);
     $("div", oChart).html(s);
                          
                          
     var iMaxTitleWidth = 0;
     $("div div .title", oChart).each(function() {
       if ($("span", this).width() > iMaxTitleWidth) {
         iMaxTitleWidth = $("span", this).width();
       }
     });   
     
     $("div div .title", oChart).width(iMaxTitleWidth + 10);
     
     var iChartHeight = oChart.height() - $("h3", oChart).outerHeight();
     var iChartWidth = oChart.width();
     var iSpaceWidth = iChartWidth - iMaxTitleWidth - 30;                                                              
                                                                    
                          
     $("div div.row", oChart).each(function() {
         var iHeight = iChartHeight / oArray.length;
         $("div.bar span", $(this)).css('lineHeight', iHeight + 'px');
         $("div.title span", $(this)).css('lineHeight', iHeight + 'px');
                                                          
         var val = $("div.bar span", $(this)).html();                         
             val = val.replace(/[^\d\.]*/g, "");
             val = parseFloat(val);
             val = (iSpaceWidth / iMax) * val;                            
         $("div.bar", $(this)).animate({"width": val}, 1500);                        
     });                     
        
  });
                        
  return false;
}



function showSubMenu(oLi) {
}



/* COLLAPSIBLE MENU */

function initMenus() {
  var $ = jQuery;
  $('ul.collapsibleMenu ul').hide();
  $.each($('ul.collapsibleMenu'), function(){
    $('#' + this.id + '.expandfirst ul:first').show();
  }
);
 
$('ul.collapsibleMenu li a').click(

function() {

  var checkElement = $(this).next();
  var parent = this.parentNode.parentNode.id;


  if(parent && $('#' + parent).hasClass('noaccordion')) { 
    $(this).next().slideToggle('normal');
  return false;
  }
 
  if((checkElement.is('ul')) && (checkElement.is(':visible'))) { 
    if($('#' + parent).hasClass('collapsible')) {
      $('#' + parent + ' ul:visible').slideUp('normal');
    }
   return false;
  }
 
  if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
    $('#' + parent + ' ul:visible').slideUp('normal');
    checkElement.slideDown('normal'); 
    return false;
  }
}
);
}
 
jQuery(document).ready(function() {initMenus();});

/* END COLLAPSIBLE MENU */



function submitSearch(oBtn) {
  var oForm = oBtn.form;
  
  
//  for (var i=0; i<oForm.length; i++) {
//    if (oForm[i].type == 'hidden') {
//      oForm.remove(oForm[i]);
//      alert(oForm[i]);
//    }
//  }
//  oBtn.form.action = 'Search.aspx';
//  oBtn.form.method = 'get';
    
}

function firmAjax(oField) {
  var s = oField.value;
  setTimeout(function() {showFirms(oField, s)} , 500);
}

function showFirms(oField, s) {
  var $ = jQuery;
  
  
  var oDiv = $("<div>");
  oDiv.attr("id", "ajaxPredict");  
  oDiv.insertAfter($(oField));
  
}
