
// IE5.5+ PNG Alpha Fix v2.0 Alpha: Background Tiling Support
// (c) 2008 Angus Turnbull http://www.twinhelix.com

// This is licensed under the GNU LGPL, version 2.1 or later.
// For details, see: http://creativecommons.org/licenses/LGPL/2.1/

var IEPNGFix = window.IEPNGFix || {};

IEPNGFix.tileBG = function(elm, pngSrc, ready) {
	// Params: A reference to a DOM element, the PNG src file pathname, and a
	// hidden "ready-to-run" passed when called back after image preloading.

	var data = this.data[elm.uniqueID],
		elmW = Math.max(elm.clientWidth, elm.scrollWidth),
		elmH = Math.max(elm.clientHeight, elm.scrollHeight),
		bgX = elm.currentStyle.backgroundPositionX,
		bgY = elm.currentStyle.backgroundPositionY,
		bgR = elm.currentStyle.backgroundRepeat;

	// Cache of DIVs created per element, and image preloader/data.
	if (!data.tiles) {
		data.tiles = {
			elm: elm,
			src: '',
			cache: [],
			img: new Image(),
			old: {}
		};
	}
	var tiles = data.tiles,
		pngW = tiles.img.width,
		pngH = tiles.img.height;

	if (pngSrc) {
		if (!ready && pngSrc != tiles.src) {
			// New image? Preload it with a callback to detect dimensions.
			tiles.img.onload = function() {
				this.onload = null;
				IEPNGFix.tileBG(elm, pngSrc, 1);
			};
			return tiles.img.src = pngSrc;
		}
	} else {
		// No image?
		if (tiles.src) ready = 1;
		pngW = pngH = 0;
	}
	tiles.src = pngSrc;

	if (!ready && elmW == tiles.old.w && elmH == tiles.old.h &&
		bgX == tiles.old.x && bgY == tiles.old.y && bgR == tiles.old.r) {
		return;
	}

	// Convert English and percentage positions to pixels.
	var pos = {
			top: '0%',
			left: '0%',
			center: '50%',
			bottom: '100%',
			right: '100%'
		},
		x,
		y,
		pc;
	x = pos[bgX] || bgX;
	y = pos[bgY] || bgY;
	if (pc = x.match(/(\d+)%/)) {
		x = Math.round((elmW - pngW) * (parseInt(pc[1]) / 100));
	}
	if (pc = y.match(/(\d+)%/)) {
		y = Math.round((elmH - pngH) * (parseInt(pc[1]) / 100));
	}
	x = parseInt(x);
	y = parseInt(y);

	// Handle backgroundRepeat.
	var repeatX = { 'repeat': 1, 'repeat-x': 1 }[bgR],
		repeatY = { 'repeat': 1, 'repeat-y': 1 }[bgR];
	if (repeatX) {
		x %= pngW;
		if (x > 0) x -= pngW;
	}
	if (repeatY) {
		y %= pngH;
		if (y > 0) y -= pngH;
	}

	// Go!
	this.hook.enabled = 0;
	if (!({ relative: 1, absolute: 1 }[elm.currentStyle.position])) {
		elm.style.position = 'relative';
	}
	var count = 0,
		xPos,
		maxX = repeatX ? elmW : x + 0.1,
		yPos,
		maxY = repeatY ? elmH : y + 0.1,
		d,
		s,
		isNew;
	if (pngW && pngH) {
		for (xPos = x; xPos < maxX; xPos += pngW) {
			for (yPos = y; yPos < maxY; yPos += pngH) {
				isNew = 0;
				if (!tiles.cache[count]) {
					tiles.cache[count] = document.createElement('div');
					isNew = 1;
				}
				var clipR = (xPos + pngW > elmW ? elmW - xPos : pngW),
					clipB = (yPos + pngH > elmH ? elmH - yPos : pngH);
				d = tiles.cache[count];
				s = d.style;
				s.behavior = 'none';
				s.left = xPos + 'px';
				s.top = yPos + 'px';
				s.width = clipR + 'px';
				s.height = clipB + 'px';
				s.clip = 'rect(' +
					(yPos < 0 ? 0 - yPos : 0) + 'px,' +
					clipR + 'px,' +
					clipB + 'px,' +
					(xPos < 0 ? 0 - xPos : 0) + 'px)';
				s.display = 'block';
				if (isNew) {
					s.position = 'absolute';
					s.zIndex = -999;
					if (elm.firstChild) {
						elm.insertBefore(d, elm.firstChild);
					} else {
						elm.appendChild(d);
					}
				}
				this.fix(d, pngSrc, 0);
				count++;
			}
		}
	}
	while (count < tiles.cache.length) {
		this.fix(tiles.cache[count], '', 0);
		tiles.cache[count++].style.display = 'none';
	}

	this.hook.enabled = 1;

	// Cache so updates are infrequent.
	tiles.old = {
		w: elmW,
		h: elmH,
		x: bgX,
		y: bgY,
		r: bgR
	};
};


IEPNGFix.update = function() {
	// Update all PNG backgrounds.
	for (var i in IEPNGFix.data) {
		var t = IEPNGFix.data[i].tiles;
		if (t && t.elm && t.src) {
			IEPNGFix.tileBG(t.elm, t.src);
		}
	}
};
IEPNGFix.update.timer = 0;

if (window.attachEvent && !window.opera) {
	window.attachEvent('onresize', function() {
		clearTimeout(IEPNGFix.update.timer);
		IEPNGFix.update.timer = setTimeout(IEPNGFix.update, 100);
	});
}


/* SYSTEM */

$(document).ready(function(){

	// Link
   $('a[href^=http]').click( function() { window.open(this.href); return false;  }); 

	// 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;  }}


	// POSTI
	if($('#posti').size()>0) { 
	
	  $('#posti').css({"visibility":"visible"});
	  
		var num = $('#posti').find("a").size();
		var posti = $('#posti').show().find("a").css({'height':0}).eq(n_rand(0, num-1)).css({'height':'auto'});
		var rand_intervall = setInterval(function() {
			if(num>0) { $('#posti').find("a").css({'height':0}).eq(n_rand(0, num-1)).css({'height':'auto'}); }								  
		}, 10000);						 
	}
	
 	// NV
	if($('.start_nv').size()>0) { 
		$('.start_nv').css({'background':'none'});
		$('.start_nv div').each(function() { 
			$(this).css({"background-position": "-" + $(this).css("margin-left") + " -" + $(this).css("margin-top")}).addClass("pa");							 
		}).has('a').mouseenter(function() {
			$(this).addClass("ac");				
		}).mouseleave(function() { 
			$(this).removeClass("ac") 
		}); 
	}
	
	// Overlay
	if($('.page_overlay_bg').size()) {
		$(window).resize(function() { $('.page_overlay_bg').css({"height":$(window).height()+"px"});	}).trigger("resize");
	}

	function n_rand( min, max ) {
		if( min > max ) { return( -1 );	} if( min == max ) { return( min );	}
		return( min + parseInt( Math.random() * (max-min+1)));
	} 


	// Scroll Window
 	var sc = $(".content_window");
 	
	if(sc.size()) {

    var scs = {
      "view":sc.find(".inner"),
      "view_h": sc.find(".inner").height(),
      "content": sc.find(".inner_co"),
      "content_h":sc.find(".inner_co").height(),
      "move_lock":false,
      "move_step":25,
      "speed": 200,
      "action_bo":$(".move_bo, .move_top", sc),
      "update":function() { 

			  of = parseInt(this.content.css("top"));
			  vp = parseInt(this.view.height());
		
			if(of >= 0) { this.action_bo.filter('.move_top').css({"visibility":"hidden"}); }
			else { this.action_bo.filter('.move_top').css({"visibility":"visible"}); }  

			if(Math.abs(of-vp) > this.content_h) { this.action_bo.filter('.move_bo').css({"visibility":"hidden"}); } 
			else { this.action_bo.filter('.move_bo').css({"visibility":"visible"}); }

      },
      "move":function(opt) { 
      
         var scs = this;
         if(scs.move_lock==false) { scs.move_lock = true; } else { return false; }
         this.content.animate({"top": opt + "=" + scs.move_step_num}, 200, function() {  scs.update(); scs.move_lock = false;  });	
      
      },
      
      "init": function() {
      
         if(parseInt(sc.find(".inner_co").height()) > parseInt(sc.find(".inner").height())) { 

             var scs = this; 
             
             this.move_step_num =  parseInt(sc.find(".inner_co").height()) / this.move_step;
             this.action_bo.click(function() { scs.move(($(this).hasClass("move_bo") ? "-" : "+"));  });   
             
            this.update(); 
            return true; 
         } 
         
         this.action_bo.hide();
      }
    }
 
    $(window).load(function(){ scs.init(); });

    	

   }
   
     
   // Galerie
   $(".slGal a[rel^='pPGal'], .cGal a[rel^='pPGal'], .nGal a[rel^='pPGal']").prettyPhoto({"theme": "facebook", "social_tools":""});

 
   // PicHover STD Gal
   $('.slGal li').each(function() { 
		$(this).find("img").attr("old", $(this).find("img").attr("src"));  
	}).mouseenter(function() { 
		$(this).find("img").attr("src", "/ly/pic_frame_o.png");  
	}).mouseleave(function(){ 
		$(this).find("img").attr("src", $(this).find("img").attr("old")); 
	});


  // PicHover EXT Gal
   $('.cGal img').each(function() { 
		$(this).attr("old", $(this).attr("src"));  
	}).mouseenter(function() { 
		$(this).attr("src", "/ly/pic_frame_o.png");  
	}).mouseleave(function(){ 
		$(this).attr("src", $(this).attr("old")); 
	});


  // nGal Single Galerie
  
  $('.nGal').each(function() {
  
    var pc = false;
    var $active = null;
    var intervall = null;
  
    function next() {
    
      $active = $('.nGal li.active');
      if ( $active.length == 0 ) $active = $('.nGal li:last');
      var $next =  $active.next().length ? $active.next() : $('.nGal li:first');
      $active.addClass('last-active');
      
      pc = true;
      $next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 500, function() { $active.removeClass('active last-active'); $active = $('.nGal li.active');  pc = false; titel_update(); });      
    
   
    }
    
    function prev() {
    
      $active = $('.nGal li.active');
      if ( $active.length == 0 ) $active = $('.nGal li:first');
      var $prev =  $active.prev().length ? $active.prev() : $('.nGal li:last');
      $active.addClass('last-active');
      
      pc = true;
      $prev.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 500, function() { $active.removeClass('active last-active'); $active = $('.nGal li.active'); pc = false; titel_update();});
      
      
    }
    
    function titel_update() { 
    
      if($('.nGal .title:visible'))  { $('.nGal .title').hide(function() { $('.nGal .title_ov').hide(); }); }
      
      var  n = $('.nGal li.active').find("a");
      var o = "";
      
      if(n.is("[title]") && n.attr("title") != "") { o += decodeURIComponent(n.attr("title")); }
      if(n.is("[cr]") && n.attr("cr") != "") { o += "<br>" + decodeURIComponent(n.attr("cr")); }
      
      if(o!="") {
        $('.nGal .title_ov').show(function() {  $('.nGal .title').html(o).show(); });
      }
    }
    
    function start_intervall() {
      if(intervall!=null) { clearInterval(intervall); intervall=null; }
      intervall = setInterval(function(){ next(); }, 7000 );
    }

    titel_update();
    start_intervall(); 
     
    $('.nGal .pref').click(function() { if(pc==false) { start_intervall(); prev(); }}); 
    $('.nGal .next').click(function() { if(pc==false) { start_intervall(); next(); }});
    $('.nGal .ov').click(function(){ if($active!=null) { $active.find("a").trigger('click'); } }); 
  
   
    
  });
  

	// Formerror
	
	if($('.forminfo').size()>0) {
	
	e = $('<div class=warning></div>').html($('.forminfo').html()).click(function(){ $(this).remove() });	
	$('body').append(e);
	
	
	
	}
	


});


