$.noConflict();

jQuery(document).ready(function($){

	$('.languageselectform select').hide().uniform().show();

	var reflectOpts = {
		height:0.15,
		opacity:0.2
	};

	$('.csc-textpic .image_container img').reflect(reflectOpts);

	if(typeof hs != 'undefined'){

		// set hs.lang values - hs_languages.js
		setHSLanguages();

		hs.dimmingOpacity = 0.5;
		hs.useBox = true;
		hs.allowSizeReduction = false;
		hs.width = 840;
		hs.height = 555;
		hs.maxWidth = 820;
		hs.maxHeight = 478;
		hs.outlineType = null;
		hs.wrapperClassName = ''; 
		hs.allowMultipleInstances = false;
		hs.align = 'left';
		hs.captionEval = 'this.thumb.alt';
		hs.showCredits = false;
		
		hs.graphicsDir = "/typo3conf/ext/pw_highslide_gallery/highslide/graphics/";

		hs.registerOverlay({
			thumbnailId: null,
			html: '<a class="close" href="#" onclick="return hs.close(this)" title="'+hs.lang.closeText+'"><span>'+hs.lang.closeText+'</span></a>',
			position: 'top right',
			useOnHtml: true
		});

		$('body').append('<div id="hs_html" class="highslide-html-content"><div class="highslide-body"></div></div>');

		$('.video_teaser').each(function(){
			$(this).find('a').each(function(){
				var a = $(this);
				var hstarg = a.attr('href') + '#video';
				a.attr('href',hstarg).click(function(){
					return hs.htmlExpand(this, { objectType: 'ajax', wrapperClassName: 'highslide_video', contentId: 'hs_html', captionEval: null});
				}).closest('.video_teaser:not(.img_ready)').addClass('img_ready').find('.video_thumb img').each(function(){
					var i = $(this);
					if(i.parent('a').length!=1){
						i.wrap('<a href="'+hstarg+'"></a>').parent('a').click(function(){
							return hs.htmlExpand(this, { objectType: 'ajax', wrapperClassName: 'highslide_video', contentId: 'hs_html'});
						});
					}
					i.parent('a').append('<span />');
				});
			}).end().find('.csc-textpic-text').css('width','155px'); // haslayout
		});

		$('.highslide_gallery_teaser a:not(.zoom)').each(function(){
			var a = $(this);
			var hstarg = a.attr('href') + '#hs_gallery';
			a.attr('href',hstarg).addClass('gallery_link').click(function(){
				return hs.htmlExpand(this, { objectType: 'ajax', wrapperClassName: 'highslide_gallery', contentId: 'hs_html', captionEval: null});
			});
		});

		hs.Expander.prototype.onBeforeClose  = function (sender) {
			$('.highslide-wrapper .highslide-image').css('opacity',0);
		}
		hs.Expander.prototype.onBeforeExpand  = function (sender) {
			$('.highslide-wrapper .highslide-image').css('opacity',0);
			$('.highslide_gallery .highslide-html li').css('opacity','0').eq(0).css('opacity','100');
		}
		hs.Expander.prototype.onAfterExpand  = function (sender) {

			$('.highslide-wrapper img.highslide-image').each(function(){
				var img = $(this), imgsize;
				img.unreflect();
				var origHt = img.height();
				var origWd = img.width();
				if(origHt > 478){
					imgsize = {
						'height':478,
						'width':'auto'
					};
				}else if(origWd > 820){
					imgsize = {
						'height':'auto',
						'width':820
					};	
				}else{
					imgsize = {
						'height':origHt+'px',
						'width':origWd+'px'
					};				
				}

				img.css(imgsize);
				var marg = (840 - img.width()) / 2;
				var margtop = 555 - img.height() - 67;
				img.css({'left':marg+'px','top':margtop+'px'}).css('opacity','100').reflect(reflectOpts).css(imgsize);

			});

			// ##################################

			function setupGallery(s) {
				var expanderA = hs.getExpander();
				var HSid = '';
				if(expanderA){HSid='#'+expanderA.content.parentNode.id;}

				// init some variables
				var animation_out = false, animation_in = false, // there's no animation
					imageList = $(HSid+' .images_list'), // init image wrapper
					itemCount = $('li', imageList).length, // count items
					itemWidth = $('li:first', imageList).outerWidth(true), // item width with margin
					currentItem = 0; // set current item to first one

				$(HSid+' .scrollright,'+HSid+' .scrollleft').unbind().die();

				if(s.data('gallerySet')==undefined){
					$(HSid+' .images_list li img').reflect(reflectOpts).closest('li').each(function(){
						var li = $(this);
						var imght = li.find('img:first').height();
						li.css('height',(imght+67)+'px')
					});
					s.data('gallerySet',true);
				}

				if(itemCount>1){
					var init = false;

					// attach navigation arrows and image wrapper
					var innerWrap = $(HSid + ' .inner-wrapper');
					if(innerWrap.length == 0) {
						init = true;	
						imageList.wrap('<div class="inner-wrapper"></div>');
					}
						
					var parentDiv = imageList.closest('.gallery_wrapper');
					parentDiv.find('.scrollleft').hide();
					
					var counter = parentDiv.find('.counter');

					$('li', imageList).each(function(i,val){
						animation_out = true;
						animation_in = true;
						$(val).show().css('opacity','100');
						var pos = itemWidth;
						if(i==0) {
							currentItem = 0;
							pos = 0;
						}
						$(val).animate({
							left: pos
						}, 0, function() {animation_out = false; animation_in = false;})

					});


					// show/hide navigation arrows
					navToggle(currentItem, itemCount, HSid);

					// hide items except first
					$('li:not(:first)', imageList).hide();




					// click on right navigation arrow
					$(HSid+' .scrollright').click(function(event) {
						// lock
						if (animation_out == false && animation_in == false) {
							animation_out = true;
							animation_in = true;

							// iterate current item to next
							currentItem++;

							// slide out current item
							$('li', imageList)
								.eq(currentItem - 1)
								.animate({
									left: -itemWidth
								}, 500, function() {
									// hide me
									$(this).hide();

									// unlock
									animation_out = false;
								});

							// slide in next item
							$('li', imageList)
								.eq(currentItem)
								.css('left', itemWidth)
								.show()
								.animate({
									left: 0
								}, 500, function(){
									// unlock
									animation_in = false;
								});

							// show/hide navigation arrows
							navToggle(currentItem, itemCount, HSid);
							updateCounter(currentItem, itemCount);
						}
					});

					// click on left navigation arrow
					$(HSid+' .scrollleft').live( 'click', function(event) {
						// lock
						if (animation_out == false && animation_in == false) {
							animation_out = true;
							animation_in = true;

							// iterate current item to previous
							currentItem--;

							// slide out current item
							$('li', imageList)
								.eq(currentItem + 1)
								.animate({
									left: itemWidth
								}, 500, function() {
									// hide me
									$(this).hide();

									// unlock
									animation_out = false;
								});

							// slide in previous item
							$('li', imageList)
								.eq(currentItem)
								.css('left', -itemWidth)
								.show()
								.animate({
									left: 0
								}, 500, function(){
									// unlock
									animation_in = false;
								});

							// show/hide navigation arrows
							navToggle(currentItem, itemCount, HSid);
							updateCounter(currentItem, itemCount);
						}
					});
					
					var updateCounter = function(){
						if(counter.length>0){
							counter.html(hs.lang.imageWord+' '+(currentItem+1)+' '+hs.lang.ofWord+' '+itemCount);
						}
					};
					updateCounter(currentItem, itemCount);

				}else{
					$(HSid+' .scrollright').add(HSid+' .scrollleft').hide();
				}
			}
			// function: show/hide navigation arrows
			function navToggle(current, items, HSid) {
				// if currentItem is lower than 1, hide scrolleft
				if (current < 1) {
					$(HSid+' .scrollleft').fadeOut();
				} else {
					$(HSid+' .scrollleft').fadeIn();
				}

				// if currentItem is lower than itemCount, show scrollright
				if (current < (items - 1)) {
					$(HSid+' .scrollright').fadeIn();
				} else {
					$(HSid+' .scrollright').fadeOut();
				}
			}

			////

			if($(sender.a).hasClass('gallery_link')) {
				setupGallery($(sender.a));
			}
		}

	}

	$('.slider').addClass('active_slider').each(function(){
		var el = $(this);
		var slBody = el.next('div');
		if(slBody.length > 0){ 
			slBody.hide().wrapInner('<div />');
			el.click(function(){
			  slBody.slideToggle(250,function(){
			  	el.toggleClass('open_slider');
			  })
			});
		}

	});
	
	$('table.zebra_table tr:not(thead tr)').filter(':nth-child(even)').addClass('even');

	
	$('#search').each(function(){
		var inputmess = getDefaultText();
		var inp = $(this);
		inp.val(inputmess).click(function(){
			if(inp.val()==inputmess){inp.val('');}
		}).closest('form').submit(function(){
			inp.click();
		});
	});

	$('#languageselect').each(function(){
		var sel = $(this);
		var orig = sel.find(':selected');

		if(orig.hasClass('select_cat')){
			sel.addClass('selected_language');
		}
		sel.change(function(){
			var curr = sel.find(':selected');
			if(curr.hasClass('select_cat')){
				curr.attr('selected','false');
				orig.attr('selected','true');
				var sval = orig.text();
				orig.closest('.selector').find('span').text(sval);	
			}
			else if(curr!=orig){
				curr.addClass('selected_language');
				if (curr.val() != "") {
					location = curr.val()+"?langSwitch=1";
				}
			}
		});	
	});


});

function zoomImg(el,gallery){
	if(gallery){
		jQuery(el).blur().next('a.gallery_link').click();
	}else{
		jQuery(el).blur().next('a.highslide').click();
	}
	return false;
}
 
 

 /* T3 functions */
 
 function decryptCharcode(n,start,end,offset){n=n+offset;if(offset>0&&n>end){n=start+(n-end-1);}else if(offset<0&&n<start){n=end-(start-n-1);}
 return String.fromCharCode(n);}
 function decryptString(enc,offset){var dec="";var len=enc.length;for(var i=0;i<len;i++){var n=enc.charCodeAt(i);if(n>=0x2B&&n<=0x3A){dec+=decryptCharcode(n,0x2B,0x3A,offset);}else if(n>=0x40&&n<=0x5A){dec+=decryptCharcode(n,0x40,0x5A,offset);}else if(n>=0x61&&n<=0x7A){dec+=decryptCharcode(n,0x61,0x7A,offset);}else{dec+=enc.charAt(i);}}
 return dec;}
function linkTo_UnCryptMailto(s){location.href=decryptString(s,-2);}



