function doAddDiploma(){
	var curnr;
	curnr = this.document.applicationForm.showdiplomacounter.value;
	if (curnr < 10){
		curnr++;
		this.document.applicationForm.showdiplomacounter.value = curnr;
		div = document.getElementById("diplomadiv" + curnr);
		if (div) {
			div.style.display= "block";
		}
		if (curnr == 10){
			div = document.getElementById("diploma_addmore");
			if (div) {
				div.style.display= "none";
			}
		}
	}
}

function doAddExp(){
	var curnr;
	curnr = this.document.applicationForm.showexpcounter.value;
	if (curnr < 10){
		curnr++;
		this.document.applicationForm.showexpcounter.value = curnr;
		div = document.getElementById("expdiv" + curnr);
		if (div) {
			div.style.display= "block";
		}
		if (curnr == 10){
			div = document.getElementById("exp_addmore");
			if (div) {
				div.style.display= "none";
			}
		}
	}
}

function doAddComp(){
	var curnr;
	curnr = this.document.applicationForm.showcompcounter.value;
	if (curnr < 10){
		curnr++;
		this.document.applicationForm.showcompcounter.value = curnr;
		div = document.getElementById("compdiv" + curnr);
		if (div) {
			div.style.display= "block";
		}
		if (curnr == 10){
			div = document.getElementById("comp_addmore");
			if (div) {
				div.style.display= "none";
			}
		}
	}
}

function validateParticipantform(){
	var msg;
	msg = "";

	if (this.document.applicationForm.a_firstname.value == ""){
		msg += "- " + this.document.applicationForm.jsmsg_firstname.value + "\n";
	}
	
	if (this.document.applicationForm.a_lastname.value == ""){
		msg += "- " + this.document.applicationForm.jsmsg_lastname.value + "\n";
	}
	
	if (this.document.applicationForm.a_birthday.selectedIndex == 0 ||
		this.document.applicationForm.a_birthmonth.selectedIndex == 0 ||
		this.document.applicationForm.a_birthyear.selectedIndex == 0){
		msg += "- " + this.document.applicationForm.jsmsg_birthday.value + "\n";
	}
	
	if (this.document.applicationForm.a_gender.selectedIndex == 0){
		msg += "- " + this.document.applicationForm.jsmsg_gender.value + "\n";
	}
		
	if (this.document.applicationForm.a_nationality.value == ""){
		msg += "- " + this.document.applicationForm.jsmsg_nationality.value + "\n";
	}
	
	if (this.document.applicationForm.a_paddress.value == ""){
		msg += "- " + this.document.applicationForm.jsmsg_paddress.value + "\n";
	}
	
	if (this.document.applicationForm.a_zip.value == ""){
		msg += "- " + this.document.applicationForm.jsmsg_zip.value + "\n";
	}
	
	if (this.document.applicationForm.a_city.value == ""){
		msg += "- " + this.document.applicationForm.jsmsg_city.value + "\n";
	}
	
	if (this.document.applicationForm.a_country.selectedIndex == 0){
		msg += "- " + this.document.applicationForm.jsmsg_country.value + "\n";
	}
	
	if (this.document.applicationForm.a_tel.value == ""){
		msg += "- " + this.document.applicationForm.jsmsg_tel.value + "\n";
	}
	
	if (this.document.applicationForm.a_mobile.value == ""){
		msg += "- " + this.document.applicationForm.jsmsg_mobile.value + "\n";
	}
	
	if (this.document.applicationForm.a_email.value == ""){
		msg += "- " + this.document.applicationForm.jsmsg_email.value + "\n";
	}
	
	var diplomactr = 0;
	var expctr = 0;
	var compctr = 0;
	
	for (i=0; i<this.document.applicationForm.elements.length; i++){
		if (this.document.applicationForm.elements[i].name){
			if (this.document.applicationForm.elements[i].name.substring(0,13) == "a_diplomaname"){
				if (this.document.applicationForm.elements[i].value != "" || this.document.applicationForm.elements[i+1].selectedIndex != 0 || this.document.applicationForm.elements[i+2].value != "" || this.document.applicationForm.elements[i+3].value != ""){
					if (this.document.applicationForm.elements[i].value == "" || this.document.applicationForm.elements[i+1].selectedIndex == 0 || this.document.applicationForm.elements[i+2].value == "" || this.document.applicationForm.elements[i+3].value == ""){
						alert(this.document.applicationForm.jsmsg_diploma_incomplete.value);
						return false;
					}
					diplomactr++;
				}
			}
			if (this.document.applicationForm.elements[i].name.substring(0,10) == "a_position"){
				if (this.document.applicationForm.elements[i].value != "" || this.document.applicationForm.elements[i+1].selectedIndex != 0 || this.document.applicationForm.elements[i+2].value != "" || this.document.applicationForm.elements[i+3].value != ""){
					if (this.document.applicationForm.elements[i].value == "" || this.document.applicationForm.elements[i+1].selectedIndex == 0 || this.document.applicationForm.elements[i+2].value == "" || this.document.applicationForm.elements[i+3].value == ""){
						alert(this.document.applicationForm.jsmsg_position_incomplete.value);
						return false;
					}
					expctr++;
				}
			}
			if (this.document.applicationForm.elements[i].name.substring(0,10) == "a_compname"){
				if (this.document.applicationForm.elements[i].value != "" || this.document.applicationForm.elements[i+1].selectedIndex != 0 || this.document.applicationForm.elements[i+2].value != "" || this.document.applicationForm.elements[i+3].value != ""){
					if (this.document.applicationForm.elements[i].value == "" || this.document.applicationForm.elements[i+1].selectedIndex == 0 || this.document.applicationForm.elements[i+2].value == "" || this.document.applicationForm.elements[i+3].value == ""){
						alert(this.document.applicationForm.jsmsg_comp_incomplete.value);
						return false;
					}
					compctr++;
				}
			}
		}
	}

	if (diplomactr == 0){
		msg += "- " + this.document.applicationForm.jsmsg_diploma.value + "\n";
	}

	if (expctr == 0){
		msg += "- " + this.document.applicationForm.jsmsg_position.value + "\n";
	}

	if (compctr == 0){
		msg += "- " + this.document.applicationForm.jsmsg_comp.value + "\n";
	}

	if (this.document.applicationForm.a_motivation.value == ""){
		msg += "- " + this.document.applicationForm.jsmsg_motivation.value + "\n";
	}

	if (this.document.applicationForm.a_entremets.value == ""){
		msg += "- " + this.document.applicationForm.jsmsg_entremets.value + "\n";
	}

	if (this.document.applicationForm.a_praline.value == ""){
		msg += "- " + this.document.applicationForm.jsmsg_praline.value + "\n";
	}

	if (this.document.applicationForm.a_presentation.value == ""){
		msg += "- " + this.document.applicationForm.jsmsg_presentation.value + "\n";
	}

	if (msg == ""){
		if(!checkemail2(this.document.applicationForm.a_email.value)){
			alert(this.document.applicationForm.jsmsg_validemail.value);
			return false;
		} else {
			return true;
		}
	} else {
		msg = this.document.applicationForm.jsmsg_fillin.value + ":\n" + msg;
		alert(msg);
		return false;
	}
}

function checkemail2(mail){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(mail)) return true; else return false;
}

function doValidateNewsletter(form) {
	var msg = "";
	var error = 0;
	if (form.lastname.value.length == 0){
		error++;
		msg += "\n- " + form.jsmsglastname.value;
	}
	if (form.gender.value.length == 0){
		error++;
		msg += "\n- " + form.jsmsggender.value;
	}
	
	if (form.activity.value.length == 0){
		error++;
		msg += "\n- " + form.jsmsgactivity.value;
	}
	if (form.activity.value.length > 0 && form.activity.value == "others"){
		if (form.activity2.value.length == 0) {
			error++;
			msg += "\n- " + form.jsmsgactivity.value;
		}	
	}
	if (form.country.value.length == 0){
		error++;
		msg += "\n- " + form.jsmsgcountry.value;
	}
	if (form.thislang.value.length == 0){
		error++;
		msg += "\n- " + form.jsmsglanguage.value;
	}
	if (form.email.value.length == 0){
		error++;
		msg += "\n- " + form.jsmsgemail.value;
	}
	if(error) {
		alert(form.jsmsgfillout.value + ":\n" + msg);
		return false;
	}
	return true ;
}

function do_showSpecify(str) {
	var spec = document.getElementById("specifyactivity");
	if (spec) {
		if (str == "others") {
			spec.style.display = "block";
		}
		else {
			spec.style.display = "none";
		}
	}	
}

function doJump(lang){
	this.document.pressFormSelect.action = "/" + lang + "/" + this.document.pressFormSelect.myselect[this.document.pressFormSelect.myselect.selectedIndex].value;
	this.document.pressFormSelect.submit();
}


function partnerslide(step, shownr, totalnr){
	var i;
	var min = step;
	var max = min + shownr - 1;
	var icon;
	
	for(i=1; i<=totalnr; i++){
		icon = document.getElementById("partnericon" + i);
		if (icon){
			if ( (i >= min && i<= max) || (i+totalnr >= min && i+totalnr <= max)){
				icon.style.display="inline";
			} else {
				icon.style.display="none";
			}
		}
	}
	
	step++;
	if (step > totalnr) step = 1;
	setTimeout("partnerslide(" + step + "," + shownr + "," + totalnr + ")",2000);
}

function recipeslide(step, totalnr){
	var i;


	for(i=1; i<=totalnr; i++){
		icon = document.getElementById("recpicture" + i);
		if (icon){
			if (i == step){
				icon.style.display="block";
			} else {
				icon.style.display="none";
			}
		}
	}
	
	step++;
	if (step > totalnr) step = 1;
	setTimeout("recipeslide(" + step + "," + totalnr + ")",5000);
	
}


function animatePartnersslide(nr){
	sub = document.getElementById("partnerssub1");
	sub2 = document.getElementById("partnerssub2");
	if (sub && sub2) {
		leftdownmini = leftdownmini - 2;
		sub.style.left = (leftdownmini-(leftdown*100))+"px";
		sub2.style.left = (leftdownmini-(leftdown*100))+"px";
	}
	if (leftdownmini > -100){
		setTimeout("animatePartnersslide("+nr+")",25);
	}else{
		leftdown = leftdown + 1;
		if (leftdown == nr){
			leftdown = 0;
		}
	}
}

function animatePartners(nr){
	leftdownmini = 0
	animatePartnersslide(nr);
	setTimeout("animatePartners("+nr+")",2000);
}

function doValidateContact(form) {
	var msg = "";
	var error = 0;
	if (form.lastname.value.length == 0){
		error++;
		msg += "\n- " + form.jsmsglastname.value;
	}
	if (form.gender.value.length == 0){
		error++;
		msg += "\n- " + form.jsmsggender.value;
	}
	
	if (form.activity.value.length == 0){
		error++;
		msg += "\n- " + form.jsmsgactivity.value;
	}
	if (form.activity.value.length > 0 && form.activity.value == "others"){
		if (form.activity2.value.length == 0) {
			error++;
			msg += "\n- " + form.jsmsgactivity.value;
		}	
	}
	if (form.country.value.length == 0){
		error++;
		msg += "\n- " + form.jsmsgcountry.value;
	}
	
	if (form.email.value.length == 0){
		error++;
		msg += "\n- " + form.jsmsgemail.value;
	}
	if(error) {
		alert(form.jsmsgfillout.value + ":\n" + msg);
		return false;
	}
	return true ;
}




$(document).ready(function() {

	// GALLERY
	var current = 1;
	var button = 1;
	var images = $('#slides').children().size(); 
	//width = 358;
	var width = 545;
	
	$('#p1').animate({'left': '0px'}, 400, 'swing');
	
	$('#next').click(function() {
		button = current;
		current++
		if (current == (images+1) ) {current = 1}
		animateLeft(current,button)
		return false;
	});
	
	$('#previous').click(function() {
		button = current;
		current--
		if (current == 0 ) {current = images}
		animateRight(current,button)
		return false;
	});
	
	function animateLeft(current,button) {
		$('#p'+current).css('left',width +'px');
		$('#p'+current).animate({'left': '0px'}, 400, 'swing');
		$('#p'+button).animate({'left': -width+'px'}, 400, 'swing');
	}
	
	function animateRight(current,button) {
		$('#p'+current).css('left',-width+'px');
		$('#p'+current).animate({'left': '0px'}, 400, 'swing');
		$('#p'+button).animate({'left': width+'px'}, 400, 'swing');
	} 	
	
	$('#fader').before('<div id="nav">');
	
	$('#fader').cycle({
		fx:     'fade',
		speed:  'slow',
		timeout: 5000,
		pager:  '#nav'
	});
	
	// form validation
	$('#addrecipe').submit(function() { 
		var valid = true;
		var scrollto = false;
		$('.required').each(function() {
			var instance = FCKeditorAPI.GetInstance('_' + $(this).attr('id'));
			if (instance) {
				instance.UpdateLinkedField();
			}
			if ($(this).val() == '') {
				valid = false;
				$(this).addClass('error');
				$('label[for="' + $(this).attr('id') + '"]').addClass('error');
				if (!scrollto) {
					scrollto = $('label[for="' + $(this).attr('id') + '"]');
				}
			} else {
				$(this).removeClass('error');
				$('label[for="' + $(this).attr('id') + '"]').removeClass('error');
			}
		});
		if (scrollto) {
			$.scrollTo(scrollto, 800, {offset: {top:-25} });
		}
		return valid;
	});

	$('#addrecipePart').click(function () {
		var newPartNr = parseInt($('#recipe_parts').val()) + 1;
		var dataString = 'number=' + newPartNr + '&new=Y&language=en';
		$.ajax({ 
			type: 'GET',
			url: '/inc/ajax_recipe_part.dhtml',
			data: dataString,
			success: function(html) {
				$('#recipe_parts_wrapper').append(html);
				$('#recipe_parts').val(newPartNr);
			}
		});
		return false;
	});
	if($('#recipe_assembling_finishing_details').length > 0) {
		var oFCKeditor0 = new FCKeditor('_recipe_assembling_finishing_details') ;
		oFCKeditor0.Config['CustomConfigurationsPath'] = '/js/FCKconfigFront.js';
		oFCKeditor0.ToolbarSet = 'Basic';
		oFCKeditor0.Width = 546;
		oFCKeditor0.Height = 250;
		oFCKeditor0.ReplaceTextarea(); 
	}
	$('#addrecipeAssemblingFinishingDetails').click(function () {
		$('#recipe_assembling_finishing_details_wrapper').show();
		$('#addrecipeAssemblingFinishingDetails').hide();
		$('#recipe_assembling_finishing_details').addClass('required');
		return false;
	});
	$('#deleterecipeAssemblingFinishingDetails').click(function () {
		var instance = FCKeditorAPI.GetInstance('_recipe_assembling_finishing_details');
		instance.SetHTML('');
		instance.UpdateLinkedField();
		$('#recipe_assembling_finishing_details_wrapper').hide();
		$('#addrecipeAssemblingFinishingDetails').show();
		$('#recipe_assembling_finishing_details').removeClass('required');
		return false;
	});
	$('.recipe_part_delete').live('click', function () {
		var wrapper_id = $(this).parent().parent().attr('id');
		$('#' + wrapper_id + ' input').val('');
		$('#' + wrapper_id + ' textarea').each(function() {
			var instance = FCKeditorAPI.GetInstance('_' + this.id);
			instance.SetHTML('');
			instance.UpdateLinkedField();
		});
		$('#' + wrapper_id + ' *').removeClass('required');
		$('#' + wrapper_id).hide();
		return false;
	});

	// showpiece vote
	$('.participantShowPieceVoteButton').click(function () {
		var participant = $(this).attr('id').replace('participantShowPieceVoteButton','');
		var dataString = 'participant=' + participant;
		$.ajax({ 
			type: 'GET',
			url: '/inc/ajax_showpiece_vote.dhtml',
			data: dataString,
			async: false,
			success: function(html) {
				//alert(html);
				if(html == 'success') {
					var newVoteCount = parseInt($('#participantShowPieceVoteCount' + participant).html()) + 1;
					//alert('participant: ' + participant + ' newVoteCount: ' + newVoteCount + '*');
					$('#participantShowPieceVoteCount' + participant).html(newVoteCount);
					$('#participantShowPieceVoteButton' + participant).hide();
					$('#participantShowPieceVoteButtonInactive' + participant).show();
				}
				if(html == 'duplicate') {
				}
				if(html == 'invalid') {
				}
			}
		});
		return false;
	});

	// general fancyboxes
	$("a.group").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300 });
	$("a.fancyMovie").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'type': 'ajax'/*, 'padding': -1*/ });
	
	// fancybox: participant details
	$('a[href].popup_link').attr('href', function(index, href) {
        return (href.indexOf('?') > 0) ? href + '&popup=Y' : href + '?popup=Y';
    });
	$('.popup_link').fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'type': 'iframe', 'width': 625, 'height': '95%', 'overlayOpacity': 0.8, 'overlayColor': '#000', 'padding': 0, 'ajax': {cache: false}  });

	$('#highlightsScrollableArea').jcarousel({
        vertical: true,
        itemFallbackDimension: 54,
        scroll: 1,
        start: parseInt($('#highlightsStartingPoint').html())
    });
	$('#contestantsScrollableArea').jcarousel({
        itemFallbackDimension: 113,
        scroll: 1,
        wrap: 'circular',
        start: parseInt($('#contestantsStartingPoint').html())
    });
	$('#showpiecesScrollableArea').jcarousel({
        itemFallbackDimension: 113,
        scroll: 1,
        wrap: 'circular',
        start: parseInt($('#showpiecesStartingPoint').html())
    });
    
	$('.sidebar_image_fade').cycle({
		fx: 'fade'
	});

	$('#tekstballon_telex').everyTime(30000,function(i){
		$.ajax({
			url: '/inc/ajax_telex.dhtml',
			cache: false,
			success: function(html){
				$('#tekstballon_telex').html(html);
			}
		})
	});
	
	$('#upcoming_live_stream_minutes_counter').everyTime(60000,function(i){
		var upcoming_minutes = parseInt($('#upcoming_live_stream_minutes_counter').html()) - 1;
		if (upcoming_minutes < 1) {
			// refresh
			window.location = '/en/';
		} else {
			$('#upcoming_live_stream_minutes_counter').html(upcoming_minutes);
		}
	});
	
	$('#upcoming_live_stream_local_time').jclock({
		utc: true,
		utcOffset: 2,
		format: '%H:%M'
	});

});
