

/* SYSTEM */

$(document).ready(function(){

	
	// Spamm 
   var a = document.getElementsByTagName("a");
   for (var i = a.length-1; i >= 0; i--) { if (a[i].className.search(/\bemail\b/) != -1) { var email = a[i].firstChild.data + "@" + a[i].lastChild.data; a[i].innerHTML = email; a[i].href = "mailto:" + email;  }}  
   
   // Link
   $('a[href^=http]').click(function() { window.open($(this).attr("href")); return false; }); 
   $('a[href^=ml]').each(function() {
      t = $(this).attr("href"); t = t.replace("ml:","mailto:").replace("-ml32-","@"); $(this).attr("href", t);
   }); 
     
   // Galerie
   $(".slGal a[rel^='pPGal'], .cGal a[rel^='pPGal'], .nGal a[rel^='pPGal']").prettyPhoto({"theme": "facebook", "social_tools":""});
   
   
   // Portraits
   $('.navigation_portrait_opener').hover(function() {
		var ms = $(this); var pos = ms.position(); ms.addClass("hover");
	   $('.navigation_portrait').css({"top":pos.top+"px", "left":pos.left+"px"}).show(200).unbind().mouseleave(function(){ $(this).hide(200); ms.removeClass("hover"); });
	   //$('.navigation_portrait').find(".track").unbind().click(function() { document.location=($("a", ms).attr("href")!= undefined) ? $("a", ms).attr("href") : "/redaxo/galerie.html" ; });
	   
 	});
 	
 	/*
 	$(".navigation_portrait li").each(function() { 
 	    if($(this).find("a").hasClass("active")) { 	  
       $('.navigation_portrait_opener').html($(this).html()); 
      }
  });
  */
	
	// Dot 
	
	$('.main').html($('.main').html().replace(/•/g, "<span class=dot>•</span>"));


	// Projector 
	
	$('.cat_navigation').find('li').not("li.current").addClass('opc_60').mouseenter(function(){  $(this).addClass("opc_100"); }).mouseleave(function() { $(this).removeClass("opc_100"); });


	if($('.projektor').size() && $('.page').hasClass("galerie")) { 
	
		// Setup
		tk = document.URL.split("#")[1];
		var element_pager = {};
		createPagerArow();
		
		num_pix = $('.projektor').size();
		
		// Create Pic Pager
		if(num_pix>1) { 
		
			out = "<div class=image_pager>";
				for(a=0; a < num_pix; a++) {
					out+= "<div class=item></div>";	
				}
				out+= "<div class=clear></div>";	
			out+= "</div>";
			
			var image_pager = $(out).appendTo(".main");
			//image_pager.find(".item").click(function() { showPrjcElement($(this).index()); }); 
			
			function image_pg(index) { 
				image_pager.find(".item").removeClass("active").eq(index).addClass("active");			
			}
		}
		
		
		// Set first pic 
		if(tk == "next") { showPrjcElement(0); } 
		else if(tk == "back") { showPrjcElement($('.projektor').size()-1); } 
		else { showPrjcElement(0); }
		
		
	} 
	
	function showPrjcElement(index) { 
	
		el = $('.projektor').hide().removeClass("current").eq(index).addClass("current").fadeIn(300);
		if(el.find(".description").html()!="") { $('.projector_info .description').html(el.find(".description").html()).show(); } else {  $('.projector_info .description').hide(); } 
	
		updatePagerArow();

		// Pager Element

		if($('.cat_navigation_pager').show().size()) { 
			cpos = Math.ceil(($('.cat_navigation li.current').index()+1)/3);
			$('.cat_navigation_pager .item').removeClass("current").eq(cpos-1).addClass("current");
			$('.cat_navigation_pager .item').unbind("click").click(function() { return false; document.location = $('.cat_navigation li').eq(($(this).index()*3)).find("a").attr("href"); });
		}

	
		min_t_block = 1; 
		max_t_block = Math.ceil(($('.cat_navigation li').size())/3); 
		cur_t_block = Math.ceil(($('.cat_navigation li.current').index()+1)/3);

		$('.cat_navigation li').each(function() {
			t_block = Math.ceil(($(this).index()+1)/3);
			if((t_block >=cur_t_block-1 && t_block <= cur_t_block+1) || (cur_t_block==min_t_block && t_block == cur_t_block+2) || (cur_t_block==max_t_block && t_block == cur_t_block-2)) { $(this).show(); } else { $(this).hide(); }
		});
		
		
		// Update Image pager
		if(typeof image_pg == 'function') { image_pg(index); }

	}
	
	
	function createPagerArow() { 

		element_pager = $('<div class="pager_arow"><div class="back"></div><div class="next"></div></div>').appendTo('body');
		$(window).resize(function() { main_pos = $('.main').offset(); $('.pager_arow').css({"top":(main_pos.top + 2) + "px", "left":(main_pos.left - 45) + "px"});  }).trigger("resize");
		
		$(".back, .next", element_pager).addClass("opc_30").mouseenter(function() { 
			if(!$(this).hasClass("off")) { $(this).addClass("opc_100"); }
		}).mouseleave(function() { 
			if(!$(this).hasClass("off")) { $(this).removeClass("opc_100"); } 
		});
	}
	
	function updatePagerArow() { 
		$(".back, .next", element_pager).each(function(){
			el = $(this); 
			
			cur_idx = $('.projektor.current').index();
			cat_idx = $('.cat_navigation li.current').index();
			
			if(el.hasClass("back")) { 
				 if(!isPrjcElement(cur_idx-1)) { 
				 	if(isCatNavElement(cat_idx-1)) { el.unbind("click").click(function() { document.location=$('.cat_navigation li').eq(cat_idx-1).find("a").attr("href")+"#back"; }); } 
					else { el.unbind("click").addClass("off"); }
				 } 
				 else { el.removeClass("off").unbind("click").click(function() { showPrjcElement(cur_idx-1); });  }
			} else { 
				 if(!isPrjcElement(cur_idx+1)) {
					 	if(isCatNavElement(cat_idx+1)) { el.unbind("click").click(function() { document.location=$('.cat_navigation li').eq(cat_idx+1).find("a").attr("href")+"#next"; }); } 
						else { el.unbind("click").addClass("off"); }
				 } else { el.removeClass("off").unbind("click").click(function() { showPrjcElement(cur_idx+1); });  }
			}
		});
	}
	
	function isPrjcElement(index) {
		return (typeof $('.projektor')[index]=="object") ? true : false;
	}
	
	function isCatNavElement(index) {
		return (typeof $('.cat_navigation li')[index]=="object") ? true : false;
	}

});


