jQuery.fn.replaceAttr = function(aName, rxString, repString) {
	//console.log('repString: ' + repString);
    	return this.attr(
        	aName,
        	function() { return jQuery(this).attr(aName).replace(rxString,
		repString); }
    ); 
};

/*$(window).bind('load', function() {
    $(".thumbPics img").each(function() {
        $(this).replaceAttr('src','glawel_baut/','glawel_baut/big_').attr('src');
    });
});*/


$(document).ready(function(){
	$(".thumbPics img").each(function() {
        $(this).replaceAttr('src','glawel_baut/','glawel_baut/big_').attr('src');
    });
	var activeMonth = $('#picsMonths a.activeMonth').attr('rel');
	$('#' + activeMonth).show();
	$('#text' + activeMonth).show();
	//console.log($('#' + activeMonth + ' a img').eq(1));
	changePic($('#' + activeMonth + ' a img').eq(0));
	
	$('#picsMonths a').click(function(){
		$this = $(this);
		selectMonth($this);
	});
	
	$('.thumbPics a img').bind('mouseover',function(){
		var pic = $(this);
		//console.log("pic: " + pic);
		changePic(pic);
	});
	
	$(".fancyBox").fancybox({
		overlayOpacity:0.7,
		callbackOnShow:window.showArrows
	});
	
	$(".openFancybox").unbind('click').bind('click',function(){
		var fancyboxGroup = $(this).attr('rel');
		$('a.fancyBox[rel=' + fancyboxGroup + ']').eq(0).trigger('click');
		return false;
	});
	
}); 

function initPictures(layer,imgPath){
	$('#'+layer+' .thumbPics img').each(function() {
        $(this).replaceAttr('src',imgPath,imgPath+'big_').attr('src');
    });
	$('#'+layer+' .thumbPics').show();
	$('#' + layer + ' .thumbPics a img').bind('mouseover',function(){
		var pic = $(this);
		//console.log("pic: " + pic);
		changePicNew(pic,layer,imgPath);
	});
}

/* *************************************
* show arrows in lightbox permanently
************************************* */

function showArrows(){
	$('a#fancy_right span').css('right', '10px');
	$('a#fancy_right').css('visibility','visible');
	$('a#fancy_left span').css('left', '10px');
	$('a#fancy_left').css('visibility','visible');
	$('a#fancy_right').bind('mouseleave',function(){
		$(this).find('span').css('right','-9999px');
	}).bind('mouseenter',function(){
		$(this).find('span').css('right','10px');
	})
	$('a#fancy_left').bind('mouseleave',function(){
		$(this).find('span').css('left','-9999px');
	}).bind('mouseenter',function(){
		$(this).find('span').css('left','10px');
	})
}

function selectMonth(obj){
	var activeMonth = obj.attr('rel');
	changePic($('#' + activeMonth + ' a img').eq(0));
	$('.textMonth').hide();
	$('#text' + activeMonth).show();
	//console.log(obj.attr('rel'));
	$('#picsMonths a').attr('class','inactiveMonth');
	obj.attr('class','activeMonth');
	$('.thumbPics').hide();
	$('#' + activeMonth).show();
	obj
}
function changePic(obj){
	obj.replaceAttr('src','glawel_baut/big_','glawel_baut/')
	var activePicSrc = obj.replaceAttr('src','glawel_baut/','glawel_baut/big_').attr('src');
	$('#mainPicImg').attr('src',activePicSrc);
}
function changePicNew(obj,layer,imgPath){
	obj.replaceAttr('src',imgPath + 'big_',imgPath)
	var activePicSrc = obj.replaceAttr('src',imgPath,imgPath+'big_').attr('src');
	$('#' + layer + ' #mainPicImg').attr('src',activePicSrc);
}



