$(document).ready(function(){
    //initSearch();
    //initMenu();
	if ($("#navigation").length > 0)
	{
		$("#navigation").treeview({
			persist: "location",
			collapsed: true,
			unique: false
		});
	}
    //  Tabs Initializations
	/*
    if(Ext.get('tabs_highlights')){
        var tabs_highlights = new Ext.TabPanel({
            renderTo: 'tabs_highlights',
            activeTab: 0,
            plain:true,
            frame:false,
            border:false,
            hideBorders: true,
            defaults:{autoHeight: false},
            tabPosition:'bottom',
            height:287,
            tabWidth:123,
            minTabWidth:120,
            resizeTabs: true,
            listeners:  {
                            beforetabchange:    emailAlertsClicked
                        },
            items:[
                {id: 'highlights_tab', contentEl:'highlights', title: 'Highlights'},
                {id: 'issues_tab', contentEl:'issues', title: 'Issues'},
                {id: 'archives_tab', contentEl:'archives', title: 'C&EN Archive'},
                {id: 'email_alerts',title: '<img src="images/email.png" alt="Email Alerts" title="Email Alerts" />Email Alerts', tabCls: 'email_alerts'}
            ]
        });
    }
	*/
    function emailAlertsClicked(tabPanel, newTab, currentTab){
        var newTabId = newTab.getId();
        //alert(newTabId);
        if(newTabId == 'email_alerts'){
            window.location = cen_root + 'static/about/email_alerts.html';
            return false;
        }
    }
    function emailAlertsHover(tabPanel, newTab, currentTab){
        var newTabId = newTab.getId();
        //alert(newTabId);
        if(newTabId == 'email_alerts'){
            window.status = cen_root + 'static/about/email_alerts.html';
            return false;
        }
    }
    
    //  About navigation event listeners setup
    if($('#about_nav').length > 0)
    {
        //  Initialize About current tab highlight display
        init_about();
    }
    //    Hide the right column and widen the main content area to full width
    if($('#login_form').length > 0)
    {
        $('#main').addClass("one_column");
        $('#right').addClass("hideDisplay");
    }
    if($('more_cover_stories').length > 0) {
        $("#more_cover_stories > .row").each(function(index, el) {
            //alert(index);
            matchFloatedDivHeights($(this).children());
        });
        /* Array rowItems = null;
        for(var i = 0; i < rows.length; i++){
            rowItems = $(rows[i].
            matchFloatedDivHeights(
        } */
        //matchFloatedDivHeights("#more_cover_stories .row .cover_page_container", "#more_cover_stories .row .cover_page_container");
    }
    if($('#departments_page_bar').length > 0) {
        $(".departments_page_content > .block").each(function(index, el) {
            //alert(index);
            matchFloatedDivHeights($(this).children());
        });
        /* Array rowItems = null;
        for(var i = 0; i < rows.length; i++){
            rowItems = $(rows[i].
            matchFloatedDivHeights(
        } */
        //matchFloatedDivHeights("#more_cover_stories .row .cover_page_container", "#more_cover_stories .row .cover_page_container");
    }
    if($('#multimedia_page_bar').length>0) {
        $(".multimedia_page_content > .block").each(function(index, el) {
            //alert(index);
            matchFloatedDivHeights($(this).children());
        });
        /* Array rowItems = null;
        for(var i = 0; i < rows.length; i++){
            rowItems = $(rows[i].
            matchFloatedDivHeights(
        } */
        //matchFloatedDivHeights("#more_cover_stories .row .cover_page_container", "#more_cover_stories .row .cover_page_container");
    }
});

function updateMonthSelect(year, elem)
{

	var sortOptions = [ new Option("Descending", "desc", true, true), new Option("Ascending", "asc", false, false) ];

	if (elem.month_select)
	{
		elem.month_select.length = 0;
	}
	
	if (Number(year) < 2010 && elem.sort_select && elem.sort_select.length > 0)
	{
		elem.sort_select.length = 0;
		elem.sort_select.options[0] = new Option();
	}
	else if (Number(year) >= 2010 && elem.sort_select && elem.sort_select.length === 1)
	{
		elem.sort_select.length = 0;
		for (var i = 0; i < sortOptions.length; i++)
		{
			elem.sort_select.options[i] = sortOptions[i];
		}
	}
	
    var months = yearMonthSelects[year];
    if (typeof months == 'undefined')
    {
        return;
    }
    for (var i = 0; i < months.length; i++)
    {
        elem.month_select.options[i] = new Option(months[i][0], months[i][1], months[i][2], months[i][3]);
    }
}

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return null;
  else
    return results[1];
}

//get a URL selector
//assumes there is no suffix and query string
//e.g: resource.sel1.sel2.html
//sel1 is index 0, sel2 is index 1
function gus( index )
{
    var href = window.location.href;
    var regex = /\/([^\/]*)*$/;
    var selectors = regex.exec(href);
    if ( selectors === null)
    {
        return null;
    }
    else
    {
        //take care of the resource and extension
        selectors = regex.exec(href)[1].split('.').slice(1,-1)
        if (index < selectors.length)
        {
            return selectors[index];
        }
        else
        {
            return null;
        }
    }
}

//highlights current tab based on current page url
function init_about(){    
    var children = document.getElementById('about_nav').getElementsByTagName("li");
    var href = "";
    var url =""+window.location;
    //alert(url);
    var nav;
    var atag;
    for(var i = 1; i <= children.length; i++){
        if($('#about_nav_' + i)){
            nav = document.getElementById('about_nav_' + i);
            atag= nav.getElementsByTagName("a");
			if (atag.length > 0)
			{
				href = atag[0].href;
				if(href != url){
					$('#about_nav_' + i).removeClass("current");
				}else{
					$('#about_nav_' + i).addClass("current");
				}
			}
        }
    }
}
/* function matchFloatedDivHeights(left, right){
    if($(left).css("height") > $(right).css("height")){
        //alert(left + ": " + $(left).css("height"));
        //alert(right + ": " + $(right).css("height"));
        $(right).css("height", $(left).css("height"));
        //alert(right + ": " + $(right).css("height"));
    }else if($(right).css("height") > $(left).css("height")){
        //alert(right + ": " + $(right).css("height"));
        $(left).css("height", $(right).css("height"));
    }
} */
function matchFloatedDivHeights(arr){
    //alert(jQuery.makeArray(array));
    //var array = jQuery.makeArray(arr);
    //alert($(arr[0]).attr('class'));
    var maxHeight = 0;
    var id = "";
    var alertStr = ""
    for(var i=0;i<arr.length;i++){
        alertStr += "<b>arr["+i+"] is </b>=>"+arr[i]+"<br>";
    }
    //alert(alertStr);
    for(var i = 0; i < arr.length; i++){
        //maxHeight = maxHeight > array[i].style.height ? maxHeight : array[i].style.height;
        maxHeight = maxHeight > $(arr[i]).css("height") ? maxHeight : $(arr[i]).css("height");
        //alert(maxHeight);
    }
    for(var i = 0; i < arr.length; i++){
		if(parseInt(maxHeight, 10) > 100) {
            $(arr[i]).css("height", maxHeight);
        } else {
            $(arr[i]).css("height", "100px");
        }
    }
} 
