
var labels = new Array('film','welt','extra','bundesliga','sport','hdfilm','hdwelt','hdfilm','hdsport','hdbundesliga');
var mlabels = new Array('welt');

function mark_channels_for_package(s) {
	$('input[rel|='+s+']').attr('checked','checked');
}

function unmark_channels_for_package(s) {
	$('input[rel|='+s+']').removeAttr('checked');
}

function highlight_channels_for_package(s) {
	for (a=0;a<labels.length;a++) {
		if (labels[a] == s) {
			$('label[rel|='+s+']').css('background-color','#00ff00');
		} else {
			$('label[rel|='+labels[a]+']').css('background-color','#ffffff');
		}
	}
}

function channel_marked(s) {
	// Pruefe, ob HD Selektion
	if (is_hd(s)) {
	
		if (s == 'hdsport' && !has_package('sport')) {
			mlabels[mlabels.length] = 'sport';
		}
		if (s == 'hdbundesliga' && !has_package('bundesliga')) {
			mlabels[mlabels.length] = 'bundesliga';
		}
		if (s == 'hdfilm' && !has_package('film')) {
			mlabels[mlabels.length] = 'film';
		}
		mlabels[mlabels.length] = 'hd';
		
	} else {
	
		// Pruefe, ob abopaket bereits selektiert ist
		if (has_package(s) == false) {
			mlabels[mlabels.length] = s;
		}
	}
	calc_abos();
}

function channel_unmarked(s) {
	
	if (is_hd(s)) {
		if (
			$('input[rel|=hdsport]:checked').length == 0 &&
			$('input[rel|=hdbundesliga]:checked').length == 0 &&
			$('input[rel|=hdfilm]:checked').length == 0 &&
			$('input[rel|=hdwelt]:checked').length == 0
		) {
			remove_package('hd');
		}
	}
	
	// pruefe, ob es noch andere markierte gibt
	if ($('input[rel|=film]:checked').length == 0 && $('input[rel|=hdfilm]:checked').length == 0) {
		remove_package('film');
	}
	if ($('input[rel|=sport]:checked').length == 0 && $('input[rel|=hdsport]:checked').length == 0) {
		remove_package('sport');
	}
	if ($('input[rel|=bundesliga]:checked').length == 0 && $('input[rel|=hdbundesliga]:checked').length == 0) {
		remove_package('bundesliga');
	}
	/*
	if ($('input[rel|=bundesliga]:checked').length == 0) {
		if ($('input[rel|=hdsport]:checked').length > 0) {
			if ($('input[rel|=sport]:checked').length > 0) {
				remove_package('bundesliga');
			} else {
				// kein sport, keine bundesliga, sport hd
				// der Fall sollte nicht eintreten
				
			}
		} else {
			remove_package('bundesliga');
		}
	}
	*/
	if ($('input[rel|=extra]:checked').length == 0) {
		remove_package('extra');
	}
	calc_abos();
}

function calc_abos() {
	for (b=0;b<labels.length;b++) {
		$('#sky'+labels[b]).css('display','none');
	}
	$('#skyhd').css('display','none');
	var num_packages = 0;
	for (a=0;a<mlabels.length;a++) {
		$('#sky'+mlabels[a]).css('display','block');
		if (mlabels[a] == 'welt' || mlabels[a] == 'film' || mlabels[a] == 'sport' || mlabels[a] == 'bundesliga') {
			num_packages++;
		}
	}
	
	var link = 'welt';
	var price = '16,90';
	if (num_packages == 1) {
		price = '16,90';
		$('#special_price').html('<u>16,90€</u>');
		if (has_package('hd')) {
			price = '16,90';
			$('#special_price').html('<u>16,90€</u>');
		}
	}
	if (num_packages == 2) {
		price = '33,90';
		if (has_package('hd')) {
			price = '43,90';
		}
		$('#special_price').html('<u>24,90€</u>');
	}
	if (num_packages == 3) {
		price = '45,90';
		if (has_package('hd')) {
			price = '55,90';
		}
		$('#special_price').html('<u>29,90€</u>');
	}
	if (num_packages == 4) {
		price = '55,90';
		if (has_package('hd')) {
			price = '59,90';
		}
		$('#special_price').html('<u>34,90€</u>');
	}
	
	if (has_package('hd')) {
		link += ',hd';
	}
	if (has_package('film')) {
		link += ',film';
	}
	if (has_package('extra')) {
		link += ',extra';
	}
	if (has_package('sport')) {
		link += ',sport';
	}
	if (has_package('bundesliga')) {
		link += ',bundesliga';
	}
	
	$('#orderlink').attr('href','/'+'sky_abo_bestellen.html?p='+link+'&nump='+num_packages);
	$('#sky_abo_preis2').html(price);
	
}


function checkform() {
	
	if (	$('#b1').attr('checked') ||
			$('#b2').attr('checked') ||
			$('#b3').attr('checked') ||
			$('#b4').attr('checked') ||
			$('#b5').attr('checked') ||
			$('#b6').attr('checked')
	) {
		$('#bestellenform').submit();
	} else {
		alert('Bitte wählen Sie einen Bonus');
	}
	
}




/*
HELPER FUNCTIONS 
*/

function is_hd(s) {
	if (s.match(/^hd/)) {
		return true;
	} else {
		return false;
	}
}

function has_package(s) {
	var found = false;
	for (a=0;a<mlabels.length;a++) {
		if (s == mlabels[a]) {
			found = true;
		}
	}
	return found;
}

function remove_package(s) {
	var mlabels2 = new Array();
	for (a=0;a<mlabels.length;a++) {
		if (mlabels[a] != s || mlabels[a] == 'welt') {
			mlabels2[mlabels2.length] = mlabels[a];
		}
	}
	mlabels = mlabels2;
}
