function embedVideo(video) {
	$('[id^=video:'+video.video_id+']:not(input,select,textarea)').get(0).innerHTML = unescape(video.html);
}


function removeLines(obj,maxLines,text) {
	var lines = 0;
	var lineHeight = -1;
	var $img = $('img',obj);
	
	obj.innerHTML = "";
	var oldHeight = 0;
	var i = 0;
	var j = -1;
	var ii = 0;
	var iii = 0;
	var oldHtml = "";
	if (text.indexOf('&nbsp;',j+1) == -1) j = text.indexOf(' ',j+1);
	else if (text.indexOf(' ',j+1) == -1) j = text.indexOf('&nbsp;',j+1)+5;
	else j = Math.min(text.indexOf('&nbsp;',j+1)+5,text.indexOf(' ',j+1));
	
	while (i > -1) {
		var last = false;
		if (j==-1) {
			last = true;
			var word = text;
		}
		else {
			var word = text.substr(0,j);
		}
		
		oldHtml = obj.innerHTML;
		
		obj.innerHTML = word;
		if (!last) obj.innerHTML += '...';
		else obj.innerHTML += ' ';
		
		$(obj).append($img);
		
		if (lineHeight == -1)
			lineHeight = obj.offsetHeight;
		
		if (obj.offsetHeight - 14 > oldHeight) {
			lines++;
			oldHeight = lineHeight*lines;
		}		
		
		if (lines > maxLines) {
			obj.innerHTML = oldHtml;
			break;
		}
		
		iii = ii; ii = i; i = j;
		
		if (text.indexOf('&nbsp;',j+1) == -1) j = text.indexOf(' ',j+1);
		else if (text.indexOf(' ',j+1) == -1) j = text.indexOf('&nbsp;',j+1)+5;
		else j = Math.min(text.indexOf('&nbsp;',j+1)+5,text.indexOf(' ',j+1));
	}
}

function removeOrphans(obj,text) {
	var i = 0; j = text.indexOf(' ');
	var orphans = ['/','-','a','i','lub','ani','ni','ale','zaś','bo','czy','że','w','pod'];
	for (i in orphans) {
		var reg = new RegExp(" "+orphans[i]+" ","ig");
		text = text.replace(reg," "+orphans[i]+"&nbsp;");
	}
	return text;
}

function insertBreaks(text) {
	var b = '&shy;';
	var after = {'e':true,'u':true,'i':true,'o':true,'a':true,'y':true};
	var newWords = [];
	var newText = text;
	
	var i = text.lastIndexOf(' '), j = text.length;
	while (j > 0) {
		var word = text.substr(i+1,j-i-1);
		var newWord = word;
		if (word.length > 6) {
			var shy = false;
			newWord = '';
			for (k=0; k<word.length; k++) {
				newWord += word.charAt(k);
				if (k>=3 && k<word.length-3 && k<word.length*(1/2)) {
					if (word.charAt(k) == '-') {
						shy = false;
					}
					else if (after[word.charAt(k)]) {
						newWord += b;
						shy = true;
					} else if (shy) {
						newWord += b;
						shy = false;
					}
				} else if (after[word.charAt(k)])
					shy = true;
				else
					shy = false;
			}
		}
		newWords.push(newWord);
		j = i;
		i = text.lastIndexOf(' ',j-1);
	}
	
	newWords.reverse();
	var newText = newWords.join(' ');
	return newText;
}

function prepareVideos(context) {
	$('[id^=video]:not(input,textarea,select)',context).each(function(){
		var videoId = this.id.split(':')[1];
		var videoUrl = 'http://www.vimeo.com/'+videoId;
		var endpoint = 'http://www.vimeo.com/api/oembed.json';
		
		var width = this.offsetWidth;
		
		var callback = 'embedVideo';
		
		var url = endpoint + '?url=' + encodeURIComponent(videoUrl) + '&callback=' + callback + '&width='+width;
		
		var js = document.createElement('script');
		js.setAttribute('type', 'text/javascript');
		js.setAttribute('src', url);
		document.getElementsByTagName('head').item(0).appendChild(js);
	});
}
prepareVideos('body');

function computeHeight($obj) {
	var maxHeight = 0;
	$obj.children().each(function(){
		if (this.offsetHeight + this.offsetTop > maxHeight) {
			maxHeight = this.offsetHeight + this.offsetTop;
		}
	});
	return maxHeight;
}

$(document).ready(function(){
	$('ul.menu ul').each(function(){
		$(this).css({opacity:0,display:"block"});
		if (this.offsetWidth < this.parentNode.offsetWidth) {
			var paddings = this.offsetWidth - $(this).width();
			$(this).width(this.parentNode.offsetWidth - paddings);
		}
		$(this).css({opacity:1,display:""});
	});
	
	/*$('ul.menu li.newsletter').one('mouseenter',function(){
		$('.newsletter-message',this).remove();
	});*/
	
	$('div.top div.tools form').bind('mouseenter',function(){
		$('input.text',this).focus();
	}).add('li.m.newsletter').bind('mouseleave',function(){
		if (!$(this).hasClass('hover'))
			$('input.text',this).blur();
	});
	
	$('.blog-box h2 a.name').each(function(){
		removeLines(this,3,$(this).text());
	}).each(function(){
		var text = $(this).html();
		var href = this.href;
		var pN = this.parentNode;
		var className = this.className;
		$(this).remove();
		$(pN).prepend('<a class="'+className+'" href="'+href+'">'+text+'</a>');
	});
	
	$('a.name2lines').each(function(){
		removeLines(this,2,$(this).text());
	});
	
	$("input[type=text],input[type=password],textarea")
	.filter(".auto-clear")
	.bind("focus", function(e) {
		if (this.value==this.defaultValue) { 
			this.value = "";
			$(this).addClass("focused");
		}
	})
	.bind("blur", function(e) {
		if (this.value=="") {
			this.value = this.defaultValue;
			$(this).removeClass("focused");
		}
	})
	.attr("value", function() {
		return this.defaultValue;
	});
		
	$('ul.attachments a[rel=opener]').bind('click',function(e){
		e.preventDefault();
		
		$ex = $(this).siblings('.expandable');
		
		if ($ex.hasClass('showed')) {
			$ex.stop().animate({height:0},600,"easeOutQuint").removeClass('showed');
		} else {
			newHeight = computeHeight($ex);
			$ex.stop().animate({height:newHeight},600,"easeOutQuint").addClass('showed');
		}
	});
	
	$('a[rel^=lang_swap]').bind('click',function(e){
		e.preventDefault();
		
		var lang = this.rel.split(':')[1];
		
		$(this).siblings().removeClass('active').end().addClass('active');
		$holder = $(this).closest('.lang-swap-holder');
		if ($holder.length > 0)
			$holder.removeClass('pl_ver').removeClass('en_ver').addClass(lang+'_ver');
		else
			$('.lang-swap-holder').removeClass('pl_ver').removeClass('en_ver').addClass(lang+'_ver');
		
		prepareTinyMCE();
	});
	
	$('select.swapper').bind('click',function(){
		var form = $(this).closest('.swapholder');
		$fields = $('.swapfield',form);
		$fields.css('display','none');
		$('input,select,textarea',$fields).attr('disabled','disabled');
		
		$good = $fields.filter('.swapper_'+$(this).val()).css('display','');
		$('input,select,textarea',$good).attr('disabled','');
		
		
		prepareTinyMCE();
	}).trigger('click');
	
	$("select.auto-redirect").bind("change",function(){
		window.location.href = this.options[this.selectedIndex].value;
	}).each(function() {
		$('input[type=submit]',$(this.form)).remove();
	});
	
	prepareVideos('body');
	
	var galleryAction = function(e) {
		var desc = this.rel.substr(this.rel.indexOf(':')+1);
		
		e.preventDefault();
		
		$(this).addClass('display-none');
		$(this).after($galleryThumb);
		$galleryThumb.removeClass('display-none').bind('click',galleryAction);
		
		$galleryThumb = $(this).remove();
		
		if ($(this).hasClass('video')) {
			id = this.href.substr(this.href.indexOf('#')+1);
			$('#gallery_main').html('<div class="video" id="video:'+id+'"></div>');
			prepareVideos('#gallery_main');
		} else if ($(this).hasClass('swf')) {
			var url = this.href.substr(0,this.href.indexOf('&'));
			var sizes = this.href.substr(this.href.indexOf('&')+1);
			sizes = sizes.split('=');
			$('#gallery_main').html('').flash({
				swf: url,
				width: sizes[0],
				height: sizes[1]
			});
		} else if ($(this).hasClass('gif')) {
			$('#gallery_main').html('<img src="'+this.href+'" />');
		}
		else
			$('#gallery_main').html('<img src="'+this.href+'" height="365" />');
			
		$('#gallery_desc').html(desc);
	}
	
	$('a[rel^=gallery]').bind('click',galleryAction);
	$galleryThumb = $('a.gallery-first').remove().removeClass('gallery-first');
	
	$('.top .tools form').bind('mouseleave',function(){
		if ($('input.text',this).val() != "")
			$(this).addClass('hover');
		else $(this).removeClass('hover');
	});
	
	$('ul.menu li').bind('mouseenter',function(){$(this).addClass('hover');}).bind('mouseleave',function(){$(this).removeClass('hover');});
	
	$('.left-side').each(function(){
		var trimContents = $.trim(this.innerHTML);
		if (trimContents == "") $(this).remove();
	});
});
