var default_main = '';
var rows = 0;
var this_id = '';

$(document).ready(function(){
	$('div.yellow-container').append('<div class="yellow-tl"></div><div class="yellow-bl"></div><div class="yellow-tr"></div><div class="yellow-br"></div>');
	$('div.red-container').append('<div class="red-tl"></div><div class="red-bl"></div><div class="red-tr"></div><div class="red-br"></div>');
	$('div.green-container').append('<div class="green-tl"></div><div class="green-bl"></div><div class="green-tr"></div><div class="green-br"></div>');
	$('div.grey-container').append('<div class="grey-tl"></div><div class="grey-bl"></div><div class="grey-tr"></div><div class="grey-br"></div>');
	$('#main-nav ul li').each(function() { if($(this).attr('class') == 'selection') { default_main = this; }; });
});

function select_all(thecheckbox) {
	var theform = thecheckbox.form;
	for(z = 0; z < theform.length; z++){
		if(theform[z].type == 'checkbox' && theform[z].name != 'checkall'){
			theform[z].checked = thecheckbox.checked;
		}
	}
}

function change_date_range() {
	$('#range_from').val('');
	$('#range_to').val('');
}

function filter(field_id, filter_amount) {
	if(rows == 0) {
		rows = $('#report-table').find('tbody > tr').get();
	}
	if(navigator.appName == 'Microsoft Internet Explorer') { 
		display = 'block';
	} else {
		display = 'table-row';
	}
	$.each(rows, function(index, row) {
		if(filter_amount == 0) {
			if($(row).attr('id') == undefined) {
				this_id = 'a';
			} else {
				this_id = $(row).attr('id');
			}
			if(this_id.substr(0,9) == 'campaigns') {
				$(row).css('display', 'none');
			} else {
				$(row).css('display', display);
			}
			$('#report-table').children('tbody').append(row);
		} else if(index != 0 && index != (rows.length-1)) {
			if($(row).children('td').eq(field_id-1).html().match(/\d\.\d+$|\d+$/) >= filter_amount) {
				$(row).css('display', display);
				$('#report-table').children('tbody').append(row);
			} else {
				$(row).css('display', 'none');
				$('#report-table').children('tbody').append(row);
			}
		} else {
			$('#report-table').children('tbody').append(row);
		}
	});
	current_rows = $('#report-table').find('tbody > tr').get();
	var x = 0;
	$.each(current_rows, function(index, row) {
		$(row).removeClass();
		if($(row).css('display') != 'none') {
			if(parseInt(x%2) != 0) {
				$(row).addClass('cell-highlight');
			}
			x++;
		} else if(parseInt(x%2) == 0) {
			$(row).addClass('cell-highlight');
		}
		$('#report-table').children('tbody').append(row);
	});
	show_options(field_id);
}

function show_campaigns(id) {
	var display = '';
	var i = 1;
	if($('tr#campaigns-' + id + '-' + i).css('display') == 'none') {
		if(navigator.appName == 'Microsoft Internet Explorer') { 
			display = 'block';
		} else {
			display = 'table-row';
		}
		$('#campaigns-' + id + '-link').html('-');
	} else {
		$('#campaigns-' + id + '-link').html('+');
		display = 'none';
	}
	while($('tr#campaigns-' + id + '-' + i).length > 0) {
		$('tr#campaigns-' + id + '-' + i).css('display', display);
		i++;		
	}
}

var timeout_id = 0;

function show_options(id) {
	clear_timeout();
	for(var x = 0; x <= 10; x++) {
		if(id != x) {
			$('#options' + x).css('display', 'none');
		}
	}
	if(id > 0) {
		$('#options' + id).toggle();
	}
	timeout_id = window.setTimeout(close_options, 5000);
}

function clear_timeout() {
	if(timeout_id)
	{
		window.clearTimeout(timeout_id);
		timeout_id = null;
	}
}

function close_options() {
	for(var x = 0; x <= 10; x++) {
		if($('#options' + x).css('display') == 'block') {
			$('#options' + x).css('display', 'none');
		}
	}
}

// Copyright 2006-2007 javascript-array.com

var timeout	= 1500;
var closetimer	= 0;
var ddmenuitem	= 0;

// open hidden layer
function mopen(id)
{	
	// cancel close timer
	mcancelclosetime();
	
	$('#main-nav ul li').each(function() { $(this).removeClass('selection'); });
	$('li#' + id.replace('sub_', 'main_')).addClass('selection');

	// close old layer
	if(ddmenuitem) ddmenuitem.style.display = 'none';
	document.getElementById('main_sub').style.display = 'none';

	// get new layer and show it
	ddmenuitem = document.getElementById(id);
	ddmenuitem.style.display = 'block';

}
// close showed layer
function mclose()
{
	if(ddmenuitem) {
		ddmenuitem.style.display = 'none';
		$('#main-nav ul li').each(function() { $(this).removeClass('selection'); });
		$(default_main).addClass('selection');
	}
	document.getElementById('main_sub').style.display = 'block';
}

// go close timer
function mclosetime()
{
	closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}
