var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-3083121-16']);
_gaq.push(['_trackPageview']);

(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
function mailproof(name, domain) {
	document.write('<a href=\"mailto:' + name + '@' + domain + '\">'+ name + '@' + domain +'</a>');
}


function word_count(text) {

	var y = text;
	var r = 0;
	a=y.replace(/\s/g,' ');
	a=a.split(' ');
	for (z=0; z<a.length; z++) {if (a[z].length > 0) r++;}
	number = r;
	
	return number;
}
function find_n_pos(text, search, num) {
	place = -10;
	for(i = 0; i < num; i++){
		place = text.indexOf(search, place + 1);
	}
	return(place);
}
function insert_at(place, string, insertion) {
	part_1 = string.substr(0,place);
	part_2 = string.substr(place);
	parts = part_1+insertion+part_2;
	
	return(parts);
}
$(document).ready(function() {
    /* Apply fancybox to multiple items */
    
    $("ul.image-gallery li a").fancybox({
        'transitionIn'  :   'elastic',
        'transitionOut' :   'elastic',
        'speedIn'       :   200, 
        'speedOut'      :   300, 
        'overlayShow'   :   true
    });
  
	if ($(".columns").html())
	{
		do_columns();
	}
	
	containers = $(".lamp");
	
	if (containers.length)
	{
		var html = $("#test_header").html();
		
		html = html.replace(/<p>/g, "");
		html = html.replace(/<\/p>/g, "");
		$("#test_header").html(html);
		
		$.each(containers, function() {
			update_thumb(this, true);
		});
	}
});
function do_columns() {
	var str = $(".columns").html();
	
	reg = /<\/P>/g;
	str = str.replace(reg, "</p>");
	reg = /<P>/g;
	str = str.replace(reg, "<p>");
	
	reg = /<\/p>([^\<]+)<p>/mg;
	
	str = str.replace(reg, "<br/><br/>");
	str = str.replace("<p>", "");
	str = str.replace("</p>", "");
	str = str.replace(/\t/g, "");
	$(".columns").html(str);

	words = (word_count(str));
	
	one_words = Math.floor((words-30)/3);
	two_words = one_words;
	three_words = words-one_words*2;
	
	//excute_columns(one_words, two_words, str);
	
	num = 0;
	a = 1;
	last_height = 9999;
	str3 = str;
	col1_add = "";
	while(a==1 && num<20)
	{
		excute_columns(one_words+num, two_words, str3);
		height = $(".column_1").height();
		if (height>last_height)
		{
			last_word_pos1 = find_n_pos(str, " ", one_words+num);
			last_word_pos2 = find_n_pos(str, " ", one_words+num-1);
			diference = last_word_pos2-last_word_pos1;
			word = str3.substring(last_word_pos2+1, last_word_pos1);
			col1_add = word;
			excute_columns(one_words+num-1, two_words, str3);
			first_words = one_words+num-1;
			first_height = last_height;
			if (!word.indexOf("<br/>")) {
				a = 0;
			}
		}
		num++;
		last_height = height;
	}
	a = 1;
	first_word = word;
	
	del_num = 0;
	deletes = 1;
	num = 0;
	while (deletes && num<10)
	{
		height = $(".column_2").height();
		if (first_height>height)
		{
			deletes = 0;
		}
		else
		{
			excute_columns(first_words, two_words-del_num*2, str3);
		}
		del_num++;
	}
	two_words = two_words-del_num*3;
	
	num = 0;
	a = 1;
	while(a==1 && num<80)
	{
		excute_columns(first_words, two_words+num, str3);
		height = $(".column_2").height();
		if (first_height<=height-1)
		{
			last_word_pos1 = find_n_pos(str, " ", two_words+first_words+num);
			last_word_pos2 = find_n_pos(str, " ", two_words+first_words+num-1);
			diference = last_word_pos2-last_word_pos1;
			word = str3.substring(last_word_pos2+1, last_word_pos1);
			
			col1_add = word;
			excute_columns(first_words, two_words+num-1, str3);
			a = 0;
			second_words = two_words+num-1;
		}
		num++;
		last_height = height;
	}
	second_word = word;
	
	excute_columns(first_words, second_words, str3, first_word, second_word);
	//excute_columns(first_words, second_words, str3, first_word, second_word);
}
function excute_columns(one_words, two_words, str, word__1, word__2) {
	word__1 = word__1 || "";
	word__2 = word__2 || "";

	word_1 = find_n_pos(str, " ", one_words);
	word_2 = find_n_pos(str, " ", one_words+two_words);
	
	str2 = insert_at(word_2, str, " <span class='hidden_word'>"+word__2+"</span></div><div class='column column_3'>");
	str2 = insert_at(word_1, str2, " <span class='hidden_word'>"+word__1+"</span></div><div class='column column_2'>");
	str2 = "<div class='column column_1'>"+str2;
	str2 = str2+"</div>";

	$(".columns").html(str2);
}

var url_prefix = "/ledx_bilder/";

var full_part = "full";
var half_part = "half";
var thumb_part = "_thumb";
var current_mode = "full";

var full_text = "Helljus";
var half_text = "Halvljus";

function get_url(object, size) {
	var size = size || "big";
	var num = $(object).attr("image");
	var mode = $(object).attr("mode");
	var size_part = "";
	var mode_part = "";

	if (mode=="full")
	{
		mode_part = full_part;
	}
	if (mode=="half")
	{
		mode_part = half_part;
	}
	
	if (size=="thumb")
	{
		size_part = thumb_part;
	}
	
	url = url_prefix+mode_part+size_part+"_"+num+".jpg";
	return url;
}

function select_image(object) {
	var big_url = get_url(object, "big");
	var container = $("#test_big_image_container");
	
	var old_image = $(container).children("img");
	var new_image = new Image();
	
	$(new_image).attr("src", big_url);
	$(container).attr("image", $(object).attr("image"));
	
	old_image.remove();
	$(container).append(new_image);
	
	$("#image_overlay").children("h2").html($(object).children("h3").html());
}

function change_mode(object) {
	var image = $(object).attr("image");
	var last_mode = $(object).attr("mode");
	var container = $("#test_big_image_container");
	
	(last_mode=="full") ? new_mode="half" : new_mode="full";
	$(object).removeClass(last_mode);
	$(object).addClass(new_mode);
	$(object).attr("mode", new_mode);
	
	if ($(container).attr("image")==$(object).attr("image"))
	{
		select_image(object, "big");
	}
	
	update_thumb(object);
}

function update_thumb(object, init) {
	var old_image = $(object).children("img");
	
	if (!old_image[0])
	{
		old_image = $(object).children("canvas");
	}
	var new_image = new Image();
	new_image.onload = function() {
		Pixastic.process(new_image, "hsl", {hue:0,saturation:-50,lightness:0});
	}
	var src = get_url(object, "thumb");
	$(new_image).attr("src", src);
		
	$(new_image).attr("id", $(old_image).attr("id"));
	
	old_image.remove();
	$(object).append(new_image);
	
	var mode = $(object).attr("mode");
	var text = "";
	
	if (mode=="full")
	{
		text = full_text;
	}
	else
	{
		text = half_text;
	}
	//$(object).children("span").html(text);;
	
	if (init)
	{
		$(object).click(function(evt) {
			if (evt.srcElement.tagName!="SPAN")
			{
				containers = $(".lamp");
				$.each(containers, function() {
					$(this).removeClass("current");
				});
	
				$(".lamp_"+$(object).attr("image")).addClass("current");
				
				select_image(object, "big");
			}
			else
			{
				change_mode(object);
			}
		});
	}
}

$(document).ready(function() {
	containers = $(".lamp");
	
	$.each(containers, function() {
		update_thumb(this, true);
	});
});
