// Initialisation of public vars
var hideinformationrows = false;
var priceTo = 99;

// Bootstrap
$(document).ready(
	function() 
    {
		// Assign the table events
        assignTableEvents();
		
		// Fix PNG
		$('.trans').supersleight();
	
		// Assign controlpanel events
		$('.searchbutton').click(
			function(){
				fetchTable($('#tabletype').attr('value'));
			}
		);
		
		$('#priceto').keyup(
			function(e){
				if(e.keyCode == 13){
					$('.searchbutton').click();
				}
			}
		);
		
		$('.resetbutton').click(
			function(){
				eraseCookie('priceto');
				window.location = window.location;
			}
		);
		
		$('.tablecontrols > .column > .subscriptionTypes > div > span').click(
			function(){
				if($('input', $(this).parent()).attr('checked') && $('input', $(this).parent()).attr('disabled') == false){
					$('input', $(this).parent()).attr('checked', false);
				}else{
					$('input', $(this).parent()).attr('checked', true);
				}
				
				subscriptionTypesCheckboxClick();
			}
		);
		
		$('.hideInformationrows').click(
			function(){
				if ($('#sortable > tbody > tr:odd').css('display') == 'none') {
					$('#sortable > tbody > tr:odd').show();
					$('input', $('.hideInformationrows')).attr('checked', true);
					hideinformationrows = false;
				} else {
					$('#sortable > tbody > tr:odd').hide();
					$('input', $('.hideInformationrows')).attr('checked', false);
					hideinformationrows = true;
				}
			}
		);
		
		$('.subscriptionTypesCheckbox').click(subscriptionTypesCheckboxClick);
		
		$('.prices > input').change(controlpanelPriceChange);
    } 
);

// Event functions
function subscriptionTypesCheckboxClick()
{
	output = Array();
	$('.subscriptionTypesCheckbox').each(
		function(){
			if($(this).attr('checked')){
				output.push($(this).attr('value'));
			}
		}
	);
	
	// Create the correct type, always with internet
	type = 'internet';
	
	// Search for bellen
	for(x = 0; x < output.length; x++){
		if(output[x] == 'bellen'){
			type += '-bellen';
		}
	}
	
	// Search for tv
	for(x = 0; x < output.length; x++){
		if(output[x] == 'tv'){
			type += '-tv';
		}
	}
	
	// Set the page's type to the new type
	if($('#tabletype').attr('value').indexOf('|') > -1){
		oldvalue = $('#tabletype').attr('value').split('|');
		type = oldvalue[0]+'|'+type;
	}
	$('#tabletype').attr('value', type);
}

function assignTableEvents(){
	$('#sortable > thead:first > tr > th').hover(
		function(){
			$(this).addClass('hover');
		},
		function(){
			$(this).removeClass('hover');
		}
	);
	
	$('#sortable > thead:first > tr > th').click(
		function(){
			if($(this).html() != ''){
				order = $(this).attr('name');
				
				// Determine the direction
				cookieInfo = readCookie('sortlog');
				
				if (cookieInfo != null) {
					// Parse the cookie string to an array
					cookieInfo = cookieInfo.split('-');
					
					sortlog = Array();
					sortlog['order'] = cookieInfo[0];
					sortlog['direction'] = cookieInfo[1];
				} else {
					sortlog = Array();
					sortlog['order'] = '';
					sortlog['direction'] = '';
				}
				
				if(sortlog['order'] == order || sortlog['order'] == ''){
					if(sortlog['direction'] == 'ASC' || sortlog['direction'] == ''){
						direction = 'DESC';
					}else{
						direction = 'ASC';
					}				
				}else{
					direction = 'ASC';
				}
				createCookie('sortlog', order + '-' + direction);
				
				fetchTable($('#tabletype').attr('value'));
			}
		}
	);
	
	$('#sortable > tbody > tr:even > td').hover(
		function(){
			$(this).css('text-decoration', 'underline');
		},
		function(){
			$(this).css('text-decoration', 'none');
		}
	);
	
	$('#sortable > tbody > tr:even').hover(
		function(){
			$(this).children().each(
				function(){
					if(!$(this).hasClass('nomouseover')){
						$(this).css('background-color', '#99FF99');
					}
				}
			);
			$(this).next().children().each(
				function(){
					if(!$(this).hasClass('nomouseover')){
						$(this).css('background-color', '#99FF99');
					}
				}
			);
		},
		function(){
			$(this).children().each(
				function(){
					if(!$(this).hasClass('nomouseover')){
						$(this).css('background-color', '#F0F0F6');
					}
				}
			);
			$(this).next().children().each(
				function(){
					if(!$(this).hasClass('nomouseover')){
						$(this).css('background-color', '#FFFFFF');
					}
				}
			);
		}
	);
	
	$('#sortable > tbody > tr:odd').hover(
		function(){
			$(this).children().each(
				function(){
					if(!$(this).hasClass('nomouseover')){
						$(this).css('background-color', '#99FF99');
					}
				}
			);
			$(this).prev().children().each(
				function(){
					if(!$(this).hasClass('nomouseover')){
						$(this).css('background-color', '#99FF99');
					}
				}
			);
		},
		function(){
			$(this).children().each(
				function(){
					if(!$(this).hasClass('nomouseover')){
						$(this).css('background-color', '#FFFFFF');
					}
				}
			);
			$(this).prev().children().each(
				function(){
					if(!$(this).hasClass('nomouseover')){
						$(this).css('background-color', '#F0F0F6');
					}
				}
			);
		}
	);
}

$('.hideInformationrows').hover(
	function(){
		$(this).css('color', '#808080');
	},
	function(){
		$(this).css('color', '#B60D0A');
	}
);

// Other functions

// Toggle div's and remember how they are toggled
function toggle(id, caller, hiddenHtml, visibleHtml)
{
	if ($('#' + id).css('display') == 'block') {
		$('#' + id).slideUp();
		
		if (caller != 'undefined' && hiddenHtml != 'undefined') {
			$('#' + caller).html(hiddenHtml);
		}
		
		createCookie('toggle-' + id, 'none');
	} else {
		$('#' + id).slideDown();
		if (caller != 'undefined' && visibleHtml != 'undefined') {
			$('#' + caller).html(visibleHtml);
		}
		
		createCookie('toggle-' + id, 'block');
	}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function fetchTable(type)
{
	// Set the mousepointer to busy
	$('#loaderposition').html('<img src="' + rootUrl + 'images/loader.gif" style="display:none;" id="loaderimage" />');
	$('#loaderimage').fadeIn('fast');
	
	$.get(
		rootUrl + 'table.php?type=' + type + '&priceTo=' + priceTo + '&hideinformationrows=' + hideinformationrows + '&t=' + new Date().getTime(),
		function(r){
			// Parse the json respons
			r = eval('(' + r + ')');
			
			if($('#loaderimage').attr('id') != undefined){
				$('#loaderimage').fadeOut('fast',
					function(){ 
						$('#loaderposition').html('');
						$('#tablecontainer').html(r['html']);						
						assignTableEvents();
						
						if(r['resultcount'] == '0' || r['resultcount'] == r['resultcountNoquery']){
							$('.moreresultshint').hide();
						}else{
							$('.moreresultshint').show();							
						}
						
						$('#resultcount').html(r['resultcount']);
						$('#resultcountNoquery').html(r['resultcountNoquery']);
					}
				);
			}else{
				$('#tablecontainer').html(r['html']);
				assignTableEvents();
			}
		}
	);
}

function controlpanelPriceChange()
{
	priceTo = $(this).attr('value');
}