/**
 * @author Sebastian Baro
 */
//Om conflicten met andere javascript files te voorkomen gebruiken we hier een andere selector. ($ wordt $j)
 var $j = jQuery.noConflict();
 // Use jQuery via $j(...)
     
$j(document).ready(function(){
    //Embedden van flash (swf) bestanden:
 	//var params = {wmode: 'transparent'};
	//swfobject.embedSWF("flash/InDeKijker.swf", "promo_swf", "245", "355", "10.0.0", "expressInstall.swf",null,params);
	//swfobject.embedSWF("flash/ZitjesKiezer.swf", "zitjeskiezer", "500", "600", "10.0.0", "expressInstall.swf",null,params);

	//$j('#searchForm').keyup(interceptSearch).attr('autocomplete', 'off');
	//$j('body').click(function(){$j('#searchForm ul').remove();});
	
	//Likebuttons instellen.
	$j('p.fb_like_news').each(function(){
		var dataId = $j(this).attr('id');
		var link = "http://www.gulyckers.be/index.php?page=nieuws&amp;show="+dataId;
		$j(this).append('<fb:like href="'+link+'" send="false" width="280" show_faces="true" layout="standard" font="trebuchet ms"></fb:like>'); 
	});
	$j('p.fb_like_event').each(function(){
		var dataId = $j(this).attr('id');
		var link = "http://www.gulyckers.be/index.php?event="+dataId;
		$j(this).append('<fb:like href="'+link+'" send="false" width="280" show_faces="true" layout="standard" font="trebuchet ms"></fb:like>'); 
		//$j(this).append('<div class="fb-like" data-send="false" data-width="290" data-show-faces="true" data-font="trebuchet ms"></div>');
	});
	//NIEUWS
	$j('h4.news_archive').next().hide();
	$j('li#active').parent().show();
	$j('h4.news_archive').css( 'cursor', 'pointer' ).hover(function(){
		$j(this).css('text-decoration', 'underline');
	},function(){
		$j(this).css('text-decoration', 'none');
	});
	$j('h4.news_archive').click(function(e){
		//$j('h4.news_archive').next().hide();
		$j(this).next().slideToggle(400);
	});
	
	//FOTO - FILTERS
	$j('#filterForm').attr('id', 'filterFormJava');
	
	//REGISTRATIE
	if($j('#regForm').length == 1){
		$j('#regForm input').keyup(checkData);
		check("form");
	}
	//FOTO - UPLOAD
	//$j('#uploadFotoForm fieldset br:last').remove();
	$j('#uploadJava').replaceWith('<input type="submit" class="btn_big" id="uploadFotosBtn" name="up_submit" value="Start!" />');
	
	//INPUT - LOADER
	$j("input[name=up_submit]").mouseup(function(e){
		if ($j("#loadingThumbs").length == 0){
			//$j('.form_upload').css('float', 'left');
			$j(this).after('<img id="loadingThumbs" height="20" width="20" src="images/loading.gif" alt="Loading..." />');
			$j('#loadingThumbs').css('float', 'right').css('margin-top', '7px').css('width', '2Opx').css('height', '20px').css('display', 'inline-block');
		}
	});
	
	/*FOTOS*/
	$j('#albums').css("overflow", "hidden");
	$j('.album').css("position", "relative");
	//foto-thumbnails voorzien van verschillende effecten ook klaarmaken voor het gebruik van de lightbox.

	fotoActionsInstellen();
	
	//live updaten van de foto's.
	$j(".tab_thumbs_id").live('click', function() {
		var toLoad = $j(this).attr('href');
		var fHeight = Math.ceil($j("#fotoThumbsBack").height()/2)-21;
		var fWidth = Math.ceil($j("#fotoThumbsBack").width()/2)-16;
		$j("#fotoThumbs").animate({
		    opacity: "0.0"
		  }, 1000, function() {
			  	$j('.foto_link').remove();
			  	if ($j("#loadingThumbs").length == 0){
					$j("#fotoThumbsBack").append('<img id="loadingThumbs" src="images/loading.gif" alt="Even geduld..." />');
					$j("#loadingThumbs").css("position", "relative").css("top", fHeight).css("left", fWidth);
					//alert('loader!');
					}
				$j('#fotoThumbs').load(toLoad, function() {
					//fotoActionsInstellen("thumbs");
					fotoActionsInstellen();
				});
		  });
		var thisHref = $j(this).attr('href').replace("&tab="+parseInt($j(this).text()-1),"&tab="+parseInt($j('.actieve_tab').text()-1));
		var thisRel = $j(this).attr('rel').replace(" - "+$j(this).text()," - "+$j('.actieve_tab').text());
		$j('.actieve_tab').replaceWith( '<li><a class="tab_thumbs_id" href="'+thisHref+'" rel="'+thisRel+'" >' + $j(".actieve_tab").text() + '</a></li>');
		$j(this).parent().replaceWith('<li class="actieve_tab">'+$j(this).text()+'</li>');
		$j('#tabIds ol li:first').addClass('first');
		$j('#tabIds ol li:last').addClass('last');
		return false;
	});
	$j(".tab_albums_id").live('click', function() {
		var fWidth = $j(".album:first").width();
		var toLoad = $j(this).attr('href');
		if ($j("#loadingThumbs").length == 0){
			$j("h2").css('float', 'left').after('<img id="loadingThumbs" height="32" width="32" src="images/loading.gif" alt="Loading..." />');
			$j('#loadingThumbs').css('margin-top', '13px').css('margin-left', '10px').css("width", "32px").css("height", "32px");
		}
		$j.ajax({
			type: "POST",
			data: {
				'f_jaar': $j('#f_jaar').val(),
				'f_tak': $j('#f_tak').val(),
				'f_plaats': $j('#f_plaats').val()
			},
			url: toLoad+"&action=filter",
			timeout: '3000',
			error: function(XMLHttpRequest, textStatus, errorThrown){
				alert('Error loading HTML document: ' + textStatus + ' - ' + errorThrown);
			},
			success: function(data, textStatus){
				//var aantalAlbums = $j(".album").length;
				  	$j('.album').remove();
				  	$j('#filterFormJava').remove();
					$j('#geenAlbums').remove();
					$j('#tabIds').remove();
					$j('#albums .anti_float').remove();
					$j('#albums').append(data);
					fotoActionsInstellen();
			  }
		  });

		return false;
	});

	/*FANCYBOX*/
	$j("a.foto_link").live('click', function() {
		var currFoto = $j(this).attr('href');
		$j.ajax({
	 		type: "POST",
	 		data: {
	 			'album': $j(this).attr('rel'),
	 		},
				url: "index.php?page=fotos&action=getAll",
				timeout: '3000',
				error: function(XMLHttpRequest, textStatus, errorThrown){
					alert('Error loading HTML document: ' + textStatus + ' - ' + errorThrown);
				},
				success: function(data, textStatus){
				var fotoArr = $j.parseJSON(data);
				var index = 0;
				for ( var i = 0, max = fotoArr.length; i < max; i++ ) {
					if(fotoArr[i]['href']==currFoto){
						index = i;
					}
				}
				$j.fancybox(fotoArr, {
							'index' : index,
							'transitionIn'	:	'elastic',
							'transitionOut'	:	'elastic',
							'speedIn'		:	500, 
							'speedOut'		:	350,
							'overlayShow'	:	false,
							'titlePosition':'over',
							'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
							    return '<span id="fancybox-title-over">Foto ' +  (currentIndex + 1) + ' / ' + currentArray.length + ' ' + title + '</span>';
							},
							'type' : 'image'
						});
				}
			});
		return false;
	});
	$j('a[rel=FancyBox]').fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	false
	});
 });
function filterAlbums(e){
	if ($j("#loadingThumbs").length == 0){
		$j("h2").css('float', 'left').after('<img id="loadingThumbs" height="32" width="32" src="images/loading.gif" alt="Loading..." />');
		$j('#loadingThumbs').css('margin-top', '13px').css('margin-left', '10px').css("width", "32px").css("height", "32px");
	}
	$j.ajax({
		type: "POST",
		data: {
			'f_jaar': $j('#f_jaar').val(),
			'f_tak': $j('#f_tak').val(),
			'f_plaats': $j('#f_plaats').val()
		},
		url: "index.php?page=fotos&action=filter",
		timeout: '3000',
		error: function(XMLHttpRequest, textStatus, errorThrown){
			alert('Error loading HTML document: ' + textStatus + ' - ' + errorThrown);
		},
		success: function(data, textStatus){

			  	$j('.album').remove();
			  	$j('#filterFormJava').remove();
				$j('#geenAlbums').remove();
				$j('#tabIds').remove();
				$j('#tabIds').remove();
				$j('#albums .anti_float').remove();
				$j('#albums').append(data);
				//fotoActionsInstellen("filter");
				fotoActionsInstellen();
		  }

	  });
 	return false;
 }

function fotoActionsInstellen(){
	$j('.filters').change(filterAlbums).attr('autocomplete', 'off');
	//$j('#albums').css('height', (($j('.album').height()+10)*$j('.album').length)+$j('.anti_float').length+$j('#tabIds').height());
	var jTeller = 0;
	aWidth = $j('.album').width();
	if($j('#geenAlbums').length == 1){
		$j('#geenAlbums').css("opacity", "0").css("position", "relative").css('left', -aWidth).each(function(){
			//console.log('#'+this.id+'.album_thumbs li.foto_link img');
			$j('#geenAlbums').animate({ "left": "0", "opacity": "1" }, 1000);
		});
	}
	$j('#fotoThumbs').css("opacity", "0").css("filter", "alpha(opacity=0)").animate({opacity: 1}, 1000);
	$j(".foto_thumb").each(function(){
		var foto_link = $j(this).attr('src').replace('thumbs', 'fotos');
		$j(this).parent().attr('href', foto_link);
		$j(this).css("opacity", "0.85").css("filter", "alpha(opacity=0.85)");
		$j(this).hover(function() { $j(this).stop().animate({ opacity: 1.0 }, 500); }, function() { $j(this).stop().animate({ opacity: 0.85 }, 500); });
	});
	$j("#loadingThumbs").remove();
	
}
function interceptSearch(e){
 	$j.ajax({
 		type: "POST",
 		data: {
 			'zoektext': $j('#zoekTxt').val(),
 		},
			url: "index.php?page=home&action=zoeken",
			timeout: '3000',
			error: function(XMLHttpRequest, textStatus, errorThrown){
				alert('Error loading HTML document: ' + textStatus + ' - ' + errorThrown);
			},
			success: function(data, textStatus){
			$j('#zoek_form ul').remove();
			if ($j('#zoekTxt').val() != "") {
				$j('#zoek_form').append(data);
				}
			}
		});
	 	return false; 
 }
function checkData(e) {
	if (e.currentTarget.id != "reg_submit") {
		check(e.currentTarget.id);
	}
	return false;
}

function check(input){
		
		if(input == "postcodeTxt"||input=="gemeenteTxt"){
			post_of_gemeente = input;
			input = 'gemPostTxt';
		}
		var foutImg = '<div class="input_fout" id="';
			foutImg += input;
			foutImg += '_fout">&nbsp;</div>';
		
		var juistImg = '<div class="input_juist" id="';
			juistImg += input;
			juistImg += '_juist">&nbsp;</div>';
		
		$j('#' +input +'_fout').remove();
		$j('#' +input +'_juist').remove();
		$j('#'+input+'_error').remove();
		$j('.info').remove();
		var error = false;
		var postError = false;
		var gemError =false;
		
		var errormessage = "Gelieve je"+$j('label[for='+input+']').text()+"in te vullen";
		
		if ($j('#' + input).val() != "" || input == 'gemPostTxt') {
			switch (input) {
				case 'gebruikersnaamTxt':
					
					checkExistingData(input);
					
					break;
				case 'paswoordTxt':

					if ($j('#paswoordTxt').val() != "") {
					
						$j('#cPaswoordTxt_fout').remove();
						$j('#cPaswoordTxt_juist').remove();
						
						if($j('#paswoordTxt').val().length < 6){
							error = true;
							errormessage = "Een paswoord moet minstens 6 tekens bevatten";
						}else if ($j('#cPaswoordTxt').val() != $j('#paswoordTxt').val()) {
							error = true;
							errormessage = "Gelieve u paswoord goed te herhalen!"
							$j('#cPaswoordTxt').before('<div class="input_fout" id="cPaswoordTxt_fout">&nbsp;</div>');
							$j('#cPaswoordTxt_fout').attr('title', 'Paswoord niet goed herhaalt!');
						}
						else {
							error = false;
							$j('#cPaswoordTxt').before('<div class="input_juist" id="cPaswoordTxt_juist">&nbsp;</div>');
						}
					}
					break;
				case 'cPaswoordTxt':
					if ($j('#cPaswoordTxt').val() != "") {
						if($j('#paswoordTxt').val().length < 6){
							error = true;
							errormessage = "Een paswoord moet minstens 6 tekens bevatten";
						}else if ($j('#cPaswoordTxt').val() != $j('#paswoordTxt').val()) {
								error = true;
								errormessage = "Gelieve u paswoord goed te herhalen!"
						}
						else {
							error = false;
							$j('#paswoordTxt_fout').remove();
							$j('#paswoordTxt_juist').remove();
							$j('#paswoordTxt').before('<div class="input_juist" id="paswoordTxt_juist">&nbsp;</div>');
						}
					}
					else {
						error = true;
						errormessage = "Gelieve eerst een wachtwoord in te vullen!";
					}
					break;
				case 'emailTxt':
					if (!$j('#emailTxt').val().match(/(([a-zA-Z0-9\._-]+)@([a-zA-Z0-9-]+)\.([a-zA-Z]{2,4}))/)) {
						error = true;
						errormessage = "Gelieve een geldige E-mail in te vullen!"
					}
					else {
						checkExistingData(input);
					}
					break;
				case 'emailUpdateTxt':
					if (!$j('#emailUpdateTxt').val().match(/(([a-zA-Z0-9\._-]+)@([a-zA-Z0-9-]+)\.([a-zA-Z]{2,4}))/)) {
						error = true;
						errormessage = "Gelieve een geldige E-mail in te vullen!"
					}
					else {
						checkExistingData(input);
					}
					break;
				case 'gemPostTxt':
					if ($j('#postcodeTxt').val() == "") {
						errormessage = "Gelieve uw Postcode";
						postError = true;
						error = true;
					}
					if (!$j('#postcodeTxt').val().match(/^[.\d]{4}/)) {
						errormessage = 'Een postcode bestaat uit 4 cijfers';
						error = true;
						postError = true;
					}
					if ($j('#gemeenteTxt').val() == "") {
						error = true;
						gemError =true;
						if (postError) {
							errormessage += " en gelieve uw Gemeente";
						}
						else {
							errormessage = "Gelieve uw Gemeente";
						}
						if (postError||gemError) {
							errormessage += " in te vullen!";
						}
					}
				break;
				case 'adresTxt':
					if(!$j('#adresTxt').val().match(/^['`]?[a-zA-Z0-9\s]{1,}[-_\/]?[a-zA-Z0-9\s]*$/)){
						error = true;
						errormessage = "Gelieve een geldig adres in te vullen";
					}
				break;
				case 'gsmTxt':
					if(!$j('#gsmTxt').val().match(/04[789][0-9]\/[0-9]{2}\.[0-9]{2}\.[0-9]{2}/) || $j('#gsmTxt').val().length != 13){
						error = true;
						errormessage = "Gebruik de vorm 04xx/xx.xx.xx";
					}
				break;
				default:
					//"nix"
			}
		}
		if ($j('#' +input).val() == "" || error) {
			$j('#' +input).before(foutImg);
			$j('#' +input+"_fout").attr('title', errormessage);
		}else {
			$j('#' +input).before(juistImg);
		}
		if(!error && !postError && !gemError && input == "gemPostTxt"){
			$j('#'+input+'_fout').remove();
			$j('#'+input).before(juistImg);
		}
		if($j('.form_big input').val() != "" && $j('.input_fout').length == 0 && $j('.input_juist').length == 9 ){
			$j('#regBtn').show();
			$j('#regBtn').attr("disabled", false);	
		}else{
			
			if($j('#regBtn').attr('title')!="Bewaar"){
				$j('#regBtn').hide();
				$j('#regBtn').attr("disabled", true);
				$j('.info').remove();

			}	
		}
		if($j('.input_fout').length != 0){
			$j('.form_big').after('<p class="info">(Beweeg de muiscursor over de het error-teken voor meer info)</p>');
		}
		return false;
}

function checkExistingData(input){
	var foutImg = '<div class="input_fout" id="'
		foutImg += input;
		foutImg += '_fout">&nbsp;</div>';
	
	var foutMelding = '<p class="errormessage" id="'
		foutMelding += input;
		foutMelding +='_error">&quot;';
		foutMelding += $j('#'+input).val();
		foutMelding += '&quot; is blijkaar al in gebruik.</p>';
		
	$j.ajax({
		type: "POST",
		data: {
			input: $j('#'+input).val(),
		},
		url: 'index.php?page=registratie&action=check'+input,
		timeout: '3000',
		error: function(XMLHttpRequest, textStatus, errorThrown){
			alert('Error loading HTML document: ' + textStatus + ' - ' + errorThrown);
		},
		success: function(data, textStatus){
			$j('#'+input+'_error').remove();
			if (data) {
				$j('#'+input+'_fout').remove();
				$j('#'+input+'_juist').remove();
				$j('#'+input).before(foutImg);
				$j('#'+input).after(foutMelding);
				$j('#'+input+'_fout').attr('title', '"'+$j('#'+input).val()+'" is al in gebruik!');
				$j('#regBtn').hide();
				$j('#regBtn').attr("disabled", true);
			}
		}
	});
	return false;
}

