var category_options = null;
var category = null;
var show_dislike = null;


function setupInitialTypes(){
    remove_phantom_nodes(gEBI('day_container'));
    category = gEBI('category');
    category_options = category.options;
    show_dislike = gEBI('show_dislike');
}

function fixDays(){
    var catValue = category.value;
    var is_all = category.value == 'all';
    var showDislike = show_dislike.checked;
    
    for(var i=1;true;i++){
        console.log(i);
        //this is as far as we can go, end loop
        if(!gEBI('day'+i+'_events')){
            break;
        }
        
        var all_day_count = 0;
        var all_day;
        if((all_day = gEBI('day'+i+'_all_day_events'))){
            for(var index=1; index < all_day.childNodes[0].childNodes.length; index++){
                var row = all_day.childNodes[0].childNodes[index];
                var rowProps = getClassProps(row.className);
                var display = ((!showDislike && (rowProps[2] == 'idi' || rowProps[2] == 'isdi')) || (!is_all && catValue != rowProps[1])) ? 'none' : '';
                if(display != row.style.display){
                    row.style.display = display;
                }
                if(display != 'none'){
                    all_day_count++;
                    row.className = (all_day_count%2 ? '' : 'odd ') + rowProps[2] + ' ' + rowProps[1];
                }
            }
            display = all_day_count > 0 ? '' : 'none';
            if(display != all_day.style.display){
                all_day.style.display = display;
            }
        }
        
        var ongoing_count = 0;
        var ongoing;
        if((ongoing = gEBI('day'+i+'_ongoing_events'))){
            for(var index=1; index < ongoing.childNodes[0].childNodes.length; index++){
                var row = ongoing.childNodes[0].childNodes[index];
                var rowProps = getClassProps(row.className);
                var display = ((!showDislike && (rowProps[2] == 'idi' || rowProps[2] == 'isdi')) || (!is_all && catValue != rowProps[1])) ? 'none' : '';
                if(display != row.style.display){
                    row.style.display = display;
                }
                if(display != 'none'){
                    ongoing_count++;
                    row.className = (ongoing_count%2 ? '' : 'odd ') + rowProps[2] + ' ' + rowProps[1];
                }
            }
            display = ongoing_count > 0 ? '' : 'none';
            if(display != ongoing.style.display){
                ongoing.style.display = display;
            }
        }
        
        var normal_count = 0;
        var normal;
        if((normal = gEBI('day'+i+'_normal_events'))){
            for(var index=1; index < normal.childNodes[0].childNodes.length; index++){
                var row = normal.childNodes[0].childNodes[index];
                var rowProps = getClassProps(row.className);
                var display = ((!showDislike && (rowProps[2] == 'idi' || rowProps[2] == 'isdi')) || (!is_all && catValue != rowProps[1])) ? 'none' : '';
                if(display != row.style.display){
                    row.style.display = display;
                }
                if(display != 'none'){
                    normal_count++;
                    row.className = (normal_count%2 ? '' : 'odd ') + rowProps[2] + ' ' + rowProps[1];
                }
            }
            display = normal_count > 0 ? '' : 'none';
            if(display != normal.style.display){
                normal.style.display = display;
            }
        }
        var day = gEBI('day'+i+'_events');
        display = (normal_count > 0 || ongoing_count > 0 || all_day_count > 0) ? '' : 'none';
        if(display != day.style.display){
            day.style.display = display;
        }
    }
    gEBI('ajax_loader').style.visibility = 'hidden';
}

function getClassProps(className){
    var is_odd = className.substring(0,3) == 'odd';
    var rest = is_odd ? className.substring(4) : className;
    var event_type;
    var favor_type;
    switch(rest.substring(0,4)){
        case 'ian ':
            favor_type = 'ian';
            event_type = rest.substring(4);
            break;
        
        case 'isdi':
            favor_type = 'isdi';
            event_type = rest.substring(5);
            break;
        
        case 'idi ':
            favor_type = 'idi';
            event_type = rest.substring(4);
            break;
        
        case 'ili ':
            favor_type = 'ili';
            event_type = rest.substring(4);
            break;
        
        default:
            favor_type = 'ilvi';
            event_type = rest.substring(5);
            break;
    }
    
    return new Array(is_odd, event_type, favor_type);
}

function changeBarBlue(){
    //alert('blue');
    if(gEBI('weekend_b1').style.background != "#29396A"){
        gEBI('weekend_b1').style.background = "#29396A";
        gEBI('weekend_b2').style.background = "#29396A";
        gEBI('weekend_b3').style.background = "#29396A";
    }
}

function changeBarWhite(){
    //alert('white');
    if(gEBI('weekend_b1').style.background != "#FFFFFF"){
        gEBI('weekend_b1').style.background = "#FFFFFF";
        gEBI('weekend_b2').style.background = "#FFFFFF";
        gEBI('weekend_b3').style.background = "#FFFFFF";
    }
}

function remove_phantom_nodes(xml){
	if(xml.nodeType != 8){
		for(var i = xml.childNodes.length-1; i >= 0; i--){
			var obj = xml.childNodes[i];
			//not valid xml if contains both text and non-text nodes, remove text nodes (Gecko-based browsers will have this)
			if(obj.nodeType == 3 && containsOnlyWhitespace(escape(obj.nodeValue)))
				xml.removeChild(obj);
			else
				remove_phantom_nodes(obj);
		}
	}
}

function containsOnlyWhitespace(string){
	var re1 = new RegExp(/%0A/);
	var re2 = new RegExp(/%09/);
	var re3 = new RegExp(/%20/);
	var m;
	while(m = re1.exec(string))
		string = string.replace(string.substring(m.index,m.index+3),'');
	while(m = re2.exec(string))
		string = string.replace(string.substring(m.index,m.index+3),'');
	while(m = re3.exec(string))
		string = string.replace(string.substring(m.index,m.index+3),'');
	return string.length == 0;
}
