function get_pos(ar,str) {
	for (var i=0; i < ar.length; i++) {
	// use === to check for Matches. ie., identical (===), ;
		if (ar[i] == str) {
			return i;
		}
	}
}

current_pic = '';

$(document).ready(function() {
	
	$('a.gal_pic').click(function() {
		if ($('#panosalado').html() != '') {
			$("#pic").remove();
			$("#contentText").before("<div id='pic'></div>");
			$('#pic').html('<img src="'+$(this).attr('bigsrc')+'" />');
		} else {
			$('#pic img').attr('src',$(this).attr('bigsrc'));
		}
		current_pic = $(this).find('img').attr('title');
		$('html,body').animate({scrollTop: 0}, 700);
		return false;
		/*window.scrollTo(0,52);*/
	});
	
	
	if ($('#text').html() == '<!--TYPO3SEARCH_begin--><!--TYPO3SEARCH_end-->') {
		$('#contentText').css('display','none');
	}


	if ($('.tx-wtgallery-pi1').html() != null) {
		$('#enter-gallery').css('display','block');
//		$('p.bodytext:last').append('<br /><br /><a href="javascript:;" class="eg_link">Enter Gallery</a>');
		$('.eg_link').click(function() {
			$('p.bodytext').hide('slow',function() {
				$('.tx-wtgallery-pi1').show('slow');
			});
			$('#nav').fadeOut();
			$('#enter-gallery .eg_link').css('display','none');
			$('#enter-gallery #g-nav').css('display','block');
		});
		$('.cg_link').click(function() {
			$('.tx-wtgallery-pi1').hide('slow',function() {
				$('p.bodytext').show('slow');
			});
			$('#nav').fadeIn();
			$('#enter-gallery #g-nav').css('display','none');
			$('#enter-gallery .eg_link').css('display','block');
		});
		$('#subnav').hover(
			function() {
				$('#nav').fadeIn();
			}
		);
		$('#pic').mouseout(
			function() {
				$('#nav').fadeOut();
			}
		);
		
		

		allpics = new Array();
		ap_count = 0;
		
		$('.tx-wtgallery-pi1').find('img.wtgallery_list').each(function() {
			allpics[ap_count] = $(this).attr('title');
			ap_count++;
		});

		$('#g-next').click(function() {
			if (current_pic == '') {
				current_pic = $('a.gal_pic:first img').attr('title');
			}
			next_pic = get_pos(allpics,current_pic)+1;
			if (next_pic > allpics.length-1) { next_pic = 0; }
//				alert($("img[title='"+allpics[next_pic]+"']").parent().attr('bigsrc'));
			$('#pic img').attr('src',$("img[title='"+allpics[next_pic]+"']").parent().attr('bigsrc'));
			current_pic = allpics[next_pic];
		});

		$('#g-prev').click(function() {
			if (current_pic == '') {
				current_pic = $('a.gal_pic:first img').attr('title');
			}
			next_pic = get_pos(allpics,current_pic)-1;
			if (next_pic < 0) { next_pic = allpics.length-1 }
			$('#pic img').attr('src',$("img[title='"+allpics[next_pic]+"']").parent().attr('bigsrc'));
			current_pic = allpics[next_pic];
		});

	}
	
	if ($('div.tx-bddbflvvideogallery-pi1').html() != null) {
		$('#playfilm').css('display','block');
		$('#playfilm').click(function() {
			$('#pic').html($('div.tx-bddbflvvideogallery-pi1').html());
			$('#nav, #subnav_bg, #subnav').fadeOut();
		});
	}
	
});


function makeCookie(Name,Value,Expiry,Path,Domain,Secure){ 
	if (Expiry!= null) { 
		var datenow = new Date(); 
		datenow.setTime(datenow.getTime() + Math.round(86400000*Expiry)); 
		Expiry = datenow.toGMTString(); 
	}
	Expiry = (Expiry!= null)? '; expires='+Expiry : ''; 
	Path = (Path!= null)?'; path='+Path:''; 
	Domain = (Domain!= null)? '; domain='+Domain : ''; 
	Secure = (Secure!= null)? '; secure' : '';
	document.cookie = Name + '=' + escape(Value) + Expiry + Path + Domain + Secure; 
}
function readCookie(Name) { 
	var cookies = document.cookie; 
	if (cookies.indexOf(Name + '=') == -1) return null; 
	var start = cookies.indexOf(Name + '=') + (Name.length + 1); 
	var finish = cookies.substring(start,cookies.length); 
	finish = (finish.indexOf(';') == -1)? cookies.length : start + finish.indexOf(';'); 
	return unescape(cookies.substring(start,finish)); 
}

function setActiveStyleSheet(pTitle) { 
	var vLoop, vLink; 
	for(vLoop=0; (vLink = document.getElementsByTagName("link")[vLoop]); vLoop++) { 
		if(vLink.getAttribute("rel").indexOf("style")!= -1 && vLink.getAttribute("title")) { 
			vLink.disabled = true; 
			if(vLink.getAttribute("title") == pTitle) vLink.disabled = false; 
		}
	}
}
function selectStyle (vCookieName, vSelection) { 
	//WRITE COOKIE 
	makeCookie(vCookieName, vSelection, 90, '/'); 
	//ACTIVE SELECTED ALTERNAT STYLE SHEET 
	setActiveStyleSheet(vSelection) 
}
if (document.cookie.indexOf('layout=')!=-1) { 
	css = readCookie('layout'); 
	//ACTIVATE SELECTED STYLE SHEET 
	setActiveStyleSheet(css) 
}