/*-----------------------------------------
 
 copyright 2010 by opendo
 
 
 Author: Georg Paul
 
 Author URI: http://www.opendo.at
 
 -------------------------------------------*/



$(document).ready(function(){
	
	if ($.browser.msie) {
		if ($('.search-mask .fieldwrap').length) {
			$('.fieldwrap input[type=text]').addClass('type-text');
		}
	}
	
	if ($('.tx-oewmevents-pi1').length > 0 || $('.tx-oewmevents-pi2').length > 0 || $('.tx-oewmimporteure-pi3').length > 0) {
	 
		/* = datepick								*/
		/*------------------------------------------*/
		
		var htmlLang = $('html').attr('lang');
		var datepickFields = $('.fieldwrap-datepicker input');
		
		if (htmlLang == 'de') {
			datepickFields.datepick($.extend({}, {
				minDate: -7,
				maxDate: '+3y',
				showTrigger: '<img src="fileadmin/template/wvdac/img/cal.gif" alt="" class="trigger" />'
			}, $.datepick.regional['de']));
		}
		if (htmlLang == 'en' || htmlLang == 'cs') {
			datepickFields.datepick($.extend({}, {
				minDate: -7,
				maxDate: '+3y',
				showTrigger: '<img src="fileadmin/template/wvdac/img/cal.gif" alt="" class="trigger" />'
			}, $.datepick.regional['']));
		}
			
	}


	/* = tablesorter							*/
	/*------------------------------------------*/
	
	var $resetBtnClone = $('.search-mask .search-reset').clone().addClass('search-reset-clone');
	var tablesorterSource = $('#results .tablesorter');

	if ($('tbody tr', tablesorterSource).length) {
		tablesorterSource
		.tablesorter({
			// sort on the first column and third column, order asc
			widthFixed: true, 
			sortList: [[0,0]],
			textExtraction: 'complex'
		})
		.tablesorterPager({
			container: $("#pager"),
			positionFixed: false
		})
		.after($resetBtnClone);
	} 

	$('.search-mask .search-reset').live('click', function(event){
		event.preventDefault();
		
		$(this).closest('.search-mask').find('input[type=text], select').val('');
	})
    
	$('#results .search-reset-clone').live('click', function(event){
		event.preventDefault();
		
		$('.search-mask').find('input[type=text], select').val('');
		window.location.hash = '#inhalt';
	})	
	
	
	if ($('#uid90-1301').length) { 
		$('#uid90-1301').find('option').each(function(){
			$(this).text($(this).html().replace('dac', 'DAC'));
		})
	}
	
	
	
    $('.form-force-reload').live('change', function() {

		var form = $(this).closest('form'),
			formAction = form.attr('data-typo3eid'),
			formId = form.attr('id');
		
		form.append('<div class="loading"><span class="animation"></span></div>');
		
		$.ajax({
			url: formAction,
			type: 'POST',
			context: form,
			data: form.serialize(),
			dataType: 'html',
			success: function(data) {
				form.replaceWith(data);
			}
		});
	});



    /* =2.2 gallery								*/
    /*------------------------------------------*/   
	var $galleryTree = $('#gallery-tree');
		
	
	if ($galleryTree.length) {
		$('#mainnavi > .active > div').after($galleryTree);
		$galleryTree.addClass('sub');
			
		
		$galleryTree.find('.sub').each(function () {
			var $this = $(this),
				extraClass = '';
			
			if ($this.hasClass('active-sub')) {
				extraClass = ' active';
			}

			$this.find('a:first').before('<a class="gallery-tree-toggle' + extraClass + '" href="#" title="aufklappen"><span class="hidden">aufklappen</span></a>');
		});
		
		$galleryTree.show();
		
		$('.gallery-tree-toggle').bind('click', function(event){
			event.preventDefault();
			var $this = $(this);

			if (!$(this).hasClass('active') ) {
				$this.addClass('active');
				$this.parent().toggleClass('active active-sub');
				
				$this.nextAll('ul').show();
			} else {
				$this.removeClass('active')
				$this.parent().toggleClass('active active-sub');	
				$(this).nextAll('ul').hide();
			}			
		})
	}

    
    /* =3.1 jcarousel							*/
    /*------------------------------------------*/
    
    // jcarousel - lightbox
    lightboxCarousel = $('.jcarousel-skin-lightbox');
    // init						
    lightboxCarousel.jcarousel({
        scroll: 4
    });
    
    lightboxCarousel.find('a').bind('click', function(event){
        event.preventDefault();
        
        lightboxCarousel.find('li').removeClass('active');
        $(this).parent().addClass('active');
    })


    var lightbox = $(".jcarousel-skin-lightbox").ariaLightbox({
        imageArray: "a",
        altText: function(){
            // $(this) is the triggered element (in this case the link element)
            return $(this).find("img").attr("alt");
        },
        descText: function(){
            return $(this).find("img").attr("title");
        },
        useDimmer: true,
        pos: "auto",
        titleText: "",
        em: false,
		pictureText: $.i18n._('pictureText'),
		ofText: $.i18n._('ofText')
    });

	/* gMap */

	// Betriebe
	var betriebMap = $('#betrieb-map');
	
	if (betriebMap.length) {
		var vcardDuplicate = $('.betrieb-vcard .vcard').clone();
		vcardDuplicate.find('.org .hidden').removeClass('hidden').after('<br />');
		var betrieb_address = $('.betrieb-vcard .street-address').text() + ', ' + $('.betrieb-vcard .postal-code').text() + ' ' + $('.betrieb-vcard .locality').text()
		betriebMap.gMap({ markers: [
				{address: betrieb_address, html: vcardDuplicate.html()}
			],
			address: betrieb_address,
			zoom: 12,
			controls: [
				'GSmallMapControl',
				'GMapTypeControl'
			],
			scrollwheel: false
			});
		
		var gMapHref = 'http://maps.google.at/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=' + betrieb_address + '&amp;ie=UTF8&amp;hq=&amp;hnear=' + betrieb_address + '&amp;z=12';
		gMapHref = encodeURI(gMapHref);
		
		betriebMap.after('<p class="gmap-link-wrap"><a target="_blank" href="' + gMapHref + '">' + $.i18n._('gMapLink') + '</a></p>');
	}
});



function getURLParam(name) {
	
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	
	if( results == null )
		return "";
	else
		return results[1];
		
}




