/************************************************************************************************************************************
 *	DOM-Ready
 ************************************************************************************************************************************/
 
	$(function(){
		if($('#filter_year').length) initSearchForm();
		if($('.js-box-expand').length) initExpandBox();
		$('.dz-lightbox').dzLightbox();
		initCountdown();
		initFlashHeader();
	});


/************************************************************************************************************************************
 *	Flash Header + Countdown
 ************************************************************************************************************************************/
 
	function initFlashHeader(){
		var src = $('#flash-header').attr('rel');
			if(src=='') {
				//Default SWF	
			};
		$('#flash-header').flash({ 
			  src: src,
			  width: 920,
			  wmode:'transparent',
			  height: 354,
			  flashvars: {  }
			},
			{ version: 9 }
		);
	}
	
	function initCountdown(){
		$('#countdown').flash({ 
			  src: '/wp-content/themes/karrierepreis/media/countdown/countdown.swf',
			  width: 150,
			  height: 80,
			  wmode:'transparent'
			},
			{ version: 9 }
		);
	}


/************************************************************************************************************************************
 *	Loge der Besten
 ************************************************************************************************************************************/
 	
	function initExpandBox(){
		var compHeight = []
		$('.js-box-expand').each(function(index,el){
			
			
			var img = $(el).find('img');
			$(el).children('.box-expand-text').append('<span class="full-text"></span>');
			if(img.length) $(el).children('.box-expand-text').css({marginLeft:90});

			compHeight[index] = $('.rest',el).height();
			$('.rest',el).hide();
			
			
			if(img.length) img.width(83).height(55.44);
			$(el).find('.preview').append('<span class="dots"> ...</span>');
			$(el).find('.link').click(function(){


				if($(el).hasClass('open')){
					$(el).removeClass('open');
					if(img.length) $('img',el).animate({width:83,height:55.44},300);
					if(img.length) $('.box-expand-text',el).animate({marginLeft:90},300);
					$('.dots',el).show();
					//$('.rest',el).slideUp(500);
					hideCaption($('.full-text',el));
					
				} else {
					var last = $(el).parent().find('.open');
					if(last.length>0){
						last.removeClass('open');
						if(img.length) $('img',last).animate({width:83,height:55.44},300);
						if(img.length) $('.box-expand-text',last).animate({marginLeft:90},300);
						$('.dots',last).show();
						//$('.rest',last).slideUp(500);	
						hideCaption($('.full-text',last));
					}
					
					
					$(el).addClass('open');
					if(img.length) $('img',el).animate({width:160,height:107},300);
					if(img.length) $('.box-expand-text',el).animate({marginLeft:170},300);
					
					$(el).find('.dots').hide();
					//$('.rest',el).slideDown(500);
					//showCaption($('.rest',el).html(),);
					showCaption($('.full-text',el),$('.rest',el).html());
				}
			});
		});
	}
	
	var char = 0;	
	var dchar = 0;
	var expandBox;
	var expandHtml;
	var expandTimer = 0;
	var minTimer = 0;
	var minBox;
	var expandSpeed=1;
	var minSpeed=1;

	function showCaption(obj,html) {
		expandBox = obj;
		expandHtml = html;
		expandSpeed = html.length/8;
		obj.html('');
		type();
	}

	function type() {
		
		char+=expandSpeed;
		expandBox.html(expandHtml.substr(0,char));
		
		if(char < expandHtml.length)
			setTimeout(type, 30);
		else {
			char = 0;
			hlp = expandBox.html();
			trace(expandHtml.length + ' / '+hlp.length);
			//clearTimeout(expandTimer);
		}
	}
	
	function hideCaption(obj) {
		minBox = obj;
		var str = obj.html();
		minSpeed = str.length/7;
		detype();
	}
	
	function detype() {
		var str = minBox.html();
		dchar = str.length-minSpeed;
		minBox.html(str.substring(0,dchar));
		
		if(dchar > 0)
			minTimer = setTimeout(detype, 33);
		else {
			dchar = 0;
			clearTimeout(minTimer);
		}
	}


	
/************************************************************************************************************************************
 *	Loge der Besten
 ************************************************************************************************************************************/
 
 	function initSearchForm(){
		
		$('select#filter_year').change(function(){
			manageOptions();							 
		});
		
		$('select#filter_category').change(function(){
			manageOptions();							 
		});
	}
	
	
	function showTheBest(){;
		trace('showthebest');
		var form = $('#search_form');
		trace(form);
		var string = $('input[name=search_string]',form).val();
		if(string == 'Suchbegriff') {	string = ''; }
		var year = $('select[name=filter_year]',form).val();
		var cat = $('select[name=filter_category]',form).val();
		var topic = $('select[name=filter_topic]',form).val();
		
		$.ajax({
			url: '/wp-content/themes/karrierepreis/search.php',
			data: {search_string:string,filter_year:year,filter_category:cat,filter_topic:topic},
			success: function(data, textStatus, XMLHttpRequest){
				var output = (XMLHttpRequest.responseText=='alle') ? "Bitte wählen Sie mindestens ein Suchkriterium aus." : XMLHttpRequest.responseText;
					output = (output!='') ? output : 'Zu dieser Anfrage gab es leider keine Treffer.';
				$('#search_default').hide();
				$('#search_alternative').show();
				$('#search-result').html(output);
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				$('#search-result').html('<div class="error">Es gab leider einen Server Fehler.</div>');
			}
		});
	}
	
	function showDetails(id){
		trace('@func showDetails ' + id);
		$.ajax({
			url: '/wp-content/themes/karrierepreis/person-details.php',
			data: {id:id},
			success: function(data, textStatus, XMLHttpRequest){
				var output = (XMLHttpRequest.responseText!='') ? XMLHttpRequest.responseText : 'Zu dieser Anfrage gab es leider keine Treffer';
				$('#search-result').html(XMLHttpRequest.responseText);
				document.location.href = document.location.href+'#'+id;
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				$('#search-result').html('<div class="error">Es gab leider einen Server Fehler.</div>');
			}
		});
	}
	
	function manageOptions(){
		var y = $('#filter_year');
		var c = $('#filter_category');
		var t = $('#filter_topic');
		
		$.ajax({
			url: '/wp-content/themes/karrierepreis/form_topic_snippet.php',
			data: {filter_year:y.val(),filter_category:c.val(),filter_topic:t.val()},
			success: function(data, textStatus, XMLHttpRequest){
				var output = XMLHttpRequest.responseText;
				t.html(XMLHttpRequest.responseText);
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				t.html('<option class="error">Es gab leider einen Server Fehler.</option>');
			}
		});
	}
	
	
	
/***************************************************************************************************
 * Trace function (Wrapper for Firebug console)
 ***************************************************************************************************/
			
	function trace(msg){
		try{
			console.log(msg);
		} catch(e){
			//alert(msg);	
		}
	}
	
