// JavaScript Document
var sourceId;
var houseDropdownSrc = '';

$(function() {
	/* TYPO3 Extension - bra_formhandler_pi2 */
	$(".tx-braformhandler-pi2 input[name='tx_braformhandler_pi2[send_by]']").click(function() {
		/*if($(location).attr('href').match(this.value) == null) {
			var curUrl = $(location).attr('href').replace('#c34','').split('&');
			window.location = curUrl[0]+'&tx_braformhandler_pi2[send_by]='+this.value+'#c34';
		}*/			
		var labelStreeNr = $('.tx-braformhandler-pi2 form .labelStreetNr').text().replace(/\*/g, '');		
		var labelPostcodeCity = $('.tx-braformhandler-pi2 form .labelPostcodeCity').text().replace(/\*/g, '');		

		if($(this).val() == 'byPost') {
			labelStreeNr = labelStreeNr+'*';
			labelPostcodeCity = labelPostcodeCity+'*';
		}		
		$('.tx-braformhandler-pi2 form .labelStreetNr').text(labelStreeNr);
		$('.tx-braformhandler-pi2 form .labelPostcodeCity').text(labelPostcodeCity);
	});
	
	/* TYPO3 Extension - bra_jobmarket */
	if($(".tx-brajobmarket-pi1").length > 0){
		
		var defaultOption = 'Bitte wählen Sie aus...';
		
		var locations = new Array();
		var mode = new Array();
		var field = new Array();
		
		// get values from displayed table
		$(".tx-brajobmarket-pi1 table tr").each(function(key,value){
				field[key] = $(this).find("td:eq(0)").text();
				locations[key] = $(this).find("td:eq(1)").text();
				mode[key] = $(this).find("td:eq(2)").text();
		});
		// filter jobfield
		if($(field).size() > 1) {
			$(".tx-brajobmarket-pi1 select[name='tx_brajobmarket_pi1[jobfield]'] option").each(function(key,value){				
				if(field.indexOf($(value).text()) == -1 && $(value).text() != defaultOption) {
					$(value).remove();
				}
			});
		}
		// filter jobcity
		if($(locations).size() > 1) {
			$(".tx-brajobmarket-pi1 select[name='tx_brajobmarket_pi1[jobcity]'] option").each(function(key,value){	
				if(locations.indexOf($(value).text()) == -1 && $(value).text() != defaultOption) {
					$(value).remove();
				}
			});
		}
		// filter jobmode
		if($(mode).size() > 1) {
			$(".tx-brajobmarket-pi1 select[name='tx_brajobmarket_pi1[jobmode]'] option").each(function(key,value){	
				if(mode.indexOf($(value).text()) == -1 && $(value).text() != defaultOption) {
					$(value).remove();
				}
			});
		}
	}
	
	/* TYPO3 Extension - bra_recommend */
	$("a.modal, a.lightbox").fancybox({
		'scrolling' : 'no'
	});
	bindRecommendForm();
	/* end */
  
	// Fix localAnkers
	if($(".modal").length > 0) {
		$(".modal").each(function() {
			var href = $(this).attr('href');
			$(this).attr('href','#'+href.split('#')[1]); 
		});
	}
	
	// Fix localAnkers - FCE Video Content
	if($('.boxTeaser a[id^="fceSrc"]').length > 0) {
		$('.boxTeaser a[id^="fceSrc"]').each(function() {
			var href = $(this).attr('href');
			$(this).attr('href','#'+href.split('#')[1]);
		});
	}
	if($('.boxStartTeaser a[id^="fceSrc"]').length > 0) {
		$('.boxStartTeaser a[id^="fceSrc"]').each(function() {
			var href = $(this).attr('href');
			$(this).attr('href','#'+href.split('#')[1]);
		});
	}	
    
	// FCE Video Content in Fancybox
	$('.boxStartTeaser a[id^="fceSrc"], .boxTeaser a[id^="fceSrc"]').fancybox({
		'scrolling': false,
		'titleShow': false,
		'padding' : 0,
		'onStart' : function(srcElement) {			
			sourceId = $(srcElement).attr('href').replace('#fce','');
			var videFile = $('#fceSrc'+sourceId).attr('name');
			$("#fce"+sourceId).show();
			
			$f("fce"+sourceId, config.player, {
				clip: '/'+videFile, 
				plugins: {
					controls: config.controlerSkins.bigPlayer
				},
				key: '#@cf0cb49073d03f83918'
			});
		},
		'onClosed' : function() {
			sourceId = parseInt(sourceId)
			if(sourceId > 0) {
				//$f("fce"+sourceId).unload();
				$("#fce"+sourceId).parent().hide();
				$("#fce"+sourceId).hide();
			}
		}
	});
  
	/* TYPO3 Extension - bra_fieldreport, bra_jobmarket  */
	$("#selectFieldReports, #selectJobField").change(function() {
		parent.location = $(this).val();
		});
	/* end */
  
	/* TYPO3 Extension - bra_formHandler */
	$("#selectCardInfo").change(function() {
		var changeCardUid = $(this).val();		
	    $.ajax({
	    	type: 'POST',
	    	url: 'index.php?id=102&tx_braformhandler_pi1[change_to]=1&tx_braformhandler_pi1[send_to]='+changeCardUid,
	    	success: function(data) {
	    		$(".boxContacts.boxContact.clearfix").parent().html(data);
	    	}
	    });
	});
	/* end */  
  
	/* TYPO3 Extension - bra_houses */
    // location search
	$('#houseSearch').click(function(){
		var goToPage = $('select:[name="tx_brahouses_pi1[house]"]').val();
		
		if( goToPage != ''){
			document.location.href = goToPage;
		}
	});
	
	// filter options by selecting
	$('select:[name="tx_brahouses_pi1[area]"]').change(function() {
		$('select:[name="tx_brahouses_pi1[location]"]').attr('disabled','disabled');
		
		var areaUid = parseInt($(this).val());
		
		// save source dropdown
		if(houseDropdownSrc == '') {
			houseDropdownSrc = $('select:[name="tx_brahouses_pi1[location]"]').html();
		}
		if(isNaN(areaUid) || areaUid < 1) {
    		$('select:[name="tx_brahouses_pi1[location]"]').html(houseDropdownSrc);
    		$('select:[name="tx_brahouses_pi1[location]"]').attr('disabled','');
    		return true;
		} 
		else {
		    $.ajax({
		    	type: 'POST',
		    	url: 'index.php?id=30&tx_brahouses_pi1[areaUid]='+areaUid+'&tx_brahouses_pi1[ajax]=1&no_cache=1',
		    	success: function(data) {
		    		var houses = $.parseJSON(data)
		    		var options = '<option value="">Standort</option>';
		    		
		    		$.each(houses, function(key, value) {
		    			options+= '<option value="'+value.city+'">'+value.city+'</option>';
		    		});
		    		$('select:[name="tx_brahouses_pi1[location]"]').html(options);
		    		$('select:[name="tx_brahouses_pi1[location]"]').attr('disabled','');
		    	}
		    });
		}
	});	
	
    // how to find us
    $('.findus').change(function(){
        $('#howToFindUsForm').submit();
    });
    
    // gallery
    $(".tx-brahouses-pi1 .scrollable .items a").fancybox({
    	'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',	
		'overlayShow'	:	true
	});
	
	/* TYPO3 FCE - generate accordion index */
	if($('#accordion > span').length > 0) {
		$('#accordion > span').remove();
		$('#accordion').prepend('<ul></ul>');
		
		var href = String(window.location);
		href = href.replace(/#topic_[0-9]/,'');
		
		$('#accordion h3').each(function() {
			$('#accordion > ul').append('<li><a href="'+href+'#'+$(this).attr("id")+'">'+$(this).text()+'</a></li>');
		});
	}
  
	// initialize scrollable for start page      
		var stopScroll = 1; // autoscroll disabled
		var numItems  = $("#index .scrollable ol:not(.cloned)").length-1;

		var root = $("#index .scrollable").scrollable({
		                circular: true,
		                onSeek: function(event, index) {
			                if(index == numItems) {
			                    stopScroll = 1;
			                    //console.log(root);
			                }    
		                }
		});
		// autoscroll is disabled
		//}).autoscroll();


		var api = root.data("scrollable");

	
	
	// initialize scrollable for houses
	if ($(".houseTabs .scrollable").length > 0) {
		$(".houseTabs .scrollable").scrollable();
	}	
  
	// initialize tabs
	if ($("#tabs").length > 0) {
		$("ul.tabs").tabs("#tabs .pane");
	}
	
	// Accordion
	if ($("#accordion").length > 0) {
		// initial hide
		$('#accordion .pane').css({'display':'none'});
    
		$('#accordion h4').click(function () {
			$(this).siblings('h4').removeClass();
			$(this).siblings('div.pane').hide();
			
			if(!$(this).hasClass("current")) {
				$(this).addClass("current");
				$(this).next().show();
			} else {
				$(this).removeClass();
				$(this).next().hide();
			}
		});
	}
	
	// House - News / Events select
	if($(".houseTabs .tx-braevents-pi1 tr.even").length > 0) {
		generateSelectOption('tx-braevents-pi1', 'Termine');
	}
	if($(".houseTabs .tt_news tr.even").length > 0) {
		generateSelectOption('tt_news', 'Neuigkeiten');
	}	
	$(".houseTabs .wrapSingularFormGroup select").change(function() {
		var on,off;

		if($(this).val() == 'tt_news') {
			on = $(this).val();
			off = 'tx-braevents-pi1';
		} else {
			off = 'tt_news';
			on = $(this).val();
		}
		$('.houseTabs .'+on).show();
		$('.houseTabs .'+off).hide();
	});
/* END - ONLOAD*/
});

/* TYPO3 Extension - bra_events / tt_news on house page */
function generateSelectOption(cssClass, name) {

	var optionTag = '<option value="'+cssClass+'">'+name+'</option>';
	
	if($(".houseTabs .wrapSingularFormGroup form").length > 0) {
		// form exists
		$(".houseTabs .wrapSingularFormGroup form select").append(optionTag);
	} else {
		// form doesn't exists
		var selectHtml;
		var html;
		var headlineText = 'Sie wünschen eine Übersicht über';
		
		selectHtml = '<select class="w300">'+optionTag+'</select>'
		html = '<div class="wrapSingularFormGroup"><form><div class="clearfix"><label><strong>'+headlineText+'</strong></label>'+selectHtml+'</div></form></div>'
		
		$(".houseTabs .tx-braevents-pi1").parent().before(html);		
	}
	// default hide
	if(cssClass == 'tt_news' && $(".houseTabs .tx-braevents-pi1 tr.even").length > 0) {
		$("."+cssClass).hide();
	}
}


/* TYPO3 Extension - bra_recommend */
function bindRecommendForm() {
	$('#recommendationForm').submit(function() {
		$.ajax({
			type: 'POST',
			url: 'index.php?id=6&tx_brarecommend_pi1[ajax]=1&tx_brarecommend_pi1[submit]=1&no_cache=1',
			data: $('#recommendationForm').serialize(),
			success: function(data) {
				$('#recommendation').parent().html(data);
				bindRecommendForm();
			}
		});
		return false;
	});  
}
/* end */

// Zoom class
var Zoom = {
	currentStylesheet: 0,
	currentState: 0,

	zoom: function() {
		if(this.currentState == 0) {
			this.increase();
			this.currentState = 1;
		} else {
			this.decrease();
			this.currentState = 0;
		}
	},
	
	increase: function() {
		if (this.currentStylesheet < 3) {
			this.currentStylesheet++;

			$('.fontCss').attr('disabled', 'disabled');
			$('#fontCss' + this.currentStylesheet).attr('disabled', '');
		}
	},

	decrease: function() {
		if (this.currentStylesheet > 0) {
			this.currentStylesheet--;

			$('.fontCss').attr('disabled', 'disabled');

			if (this.currentStylesheet > 0) {
				$('#fontCss' + this.currentStylesheet).attr('disabled', '');
			}
		}
	}
};
