/*////////////////////////////////////////////////////////////*/
/* MatthiasSchuetz.com JavaScript | (C) 2009 Matthias Schuetz */
/*////////////////////////////////////////////////////////////*/

var addingLink = false;
var errorActive = false;
var loadError = false;

function newLink(link) {
	if ($("#create").attr("class") == "de") {
		var title_m = "Link verschieben (Maustaste gedrückt halten)";
		var title_a = "Link hinzufügen";
		var title_r = "Link entfernen";
	} else {
		var title_m = "Move link (hold down mouse button)";
		var title_a = "Add link";
		var title_r = "Remove link";
	}
	return '<li><div class="newlink"><input type="text" class="link" value="' + link + '" />' +
			'<a href="#" class="move" onclick="return false;" title="' + title_m + '">#</a>' +
			'<a href="#" class="add" onclick="javascript:addLink(this);return false;" title="' + title_a + '">+</a>' +
			'<a href="#" class="remove" onclick="javascript:removeLink(this);return false;" title="' + title_r + '">-</a>' +
			'</div></li>';
}

function createBookmarklet(p, lang) {
	if (lang == "de") {
		var title_u = "Nach oben";
		var title_d = "Nach unten";
		var title_o = "Übersicht";
	} else {
		var title_u = "Up";
		var title_d = "Down";
		var title_o = "Overview";
	}
	var bookmarklet = 'javascript:(function(){document.write(\'<html><head><title>PageSlider</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><style type=\"text/css\">html,body{width:100%;height:100%;margin:0;padding:0;font-family:Arial,Tahoma,Helvetica,sans-serif;font-size:11px;background:#000000;}div.pf{position:relative;width:100%;height:80%;}div#ov{width:980px;height:auto;margin:20px auto;}a.re,a.up,a.do,a.ov{position:absolute;top:6px;right:6px;width:36px;height:36px;background:url(http://matthiasschuetz.com/pageslider/btns.gif) no-repeat 0 0;display:block;outline:none;}a.up{top:48px;background:url(http://matthiasschuetz.com/pageslider/btns.gif) no-repeat 0 -36px;}a.do{top:90px;background:url(http://matthiasschuetz.com/pageslider/btns.gif) no-repeat 0 -72px;}a.ov{top:132px;background:url(http://matthiasschuetz.com/pageslider/btns.gif) no-repeat 0 -108px;}a.pl{width:309px;height:18px;margin:10px 0 0 10px;padding:23px 0 0 0;font-weight:bold;text-align:center;text-decoration:none;color:#FFFFFF;background:#000000;border:2px #858585 solid;display:block;float:left;overflow:hidden;}</style><!script src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js\" type=\"text/javascript\"><!/script><!script type=\"text/javascript\">var p=new Array();'+p+'cp=0;lp=-1;o=0;m=0;function mu(pn){m=1;vm=0;cpm=0;if(pn==p.length-1){if($(\"body\").children().length==2){$(\"body\").prepend(np(pn-2))}else{window.scrollTo(0,($(document).height()/3))}pn+=4}else if(pn==0){window.scrollTo(0,0);vm=1}else{try{if(pn==$(\"body div:eq(0)\").attr(\"id\").substr(5)){ci=1;cpm=1}else if(pn==$(\"body div:eq(2)\").attr(\"id\").substr(5)){ci=3;cpm=-1}else{ci=2}}catch(e){}if($(\"body div:eq(0)\").attr(\"id\").substr(5)==0){if($(\"body\").children().length==3){if(pn==$(\"body div:eq(1)\").attr(\"id\").substr(5)){cp=pn+1}else{cp=pn}$(\"body div:eq(2)\").remove()}else if($(\"body\").children().length==2){cp=pn+1}else{cp=pn}window.scrollTo(0,0);le();vm=1}else{$(\"body div:eq(2)\").remove();$(\"body\").prepend(np(pn-ci))}}if(!vm){window.scrollTo(0,($(document).height()/3));cp=pn+1+cpm;le()}m=0}function md(pn){m=1;vm=0;fm=0;cpm=0;if(pn==p.length-1){window.scrollTo(0,$(document).height());vm=1}else if($(\"body\").children().length==3&&$(\"body div:eq(2)\").attr(\"id\").substr(5)==p.length-1){if(pn==p.length-3){window.scrollTo(0,($(document).height()/3))}else{window.scrollTo(0,$(document).height())}vm=1}else if(pn==0&&$(\"body\").children().length==2){$(\"body\").append(np(pn+2))}else{if(pn==$(\"body div:eq(0)\").attr(\"id\").substr(5)){ci=3;cpm=1}else if(pn==$(\"body div:eq(1)\").attr(\"id\").substr(5)&&$(\"body\").children().length==2){$(\"body\").prepend(np(pn-1));ci=1;fm=1}else if(pn==$(\"body div:eq(2)\").attr(\"id\").substr(5)){ci=1}else{ci=2}$(\"body div:eq(0)\").remove();$(\"body\").append(np(pn+ci))}if(!vm){window.scrollTo(0,($(document).height()/3));fm?cp=pn+2+cpm:cp=pn+3+cpm;le();$(\"#page\"+(pn+2)).attr(\"src\",p[pn+2]);}m=0}function so(){if(!o){o=1;$(\"body\").children().each(function(){$(this).stop().animate({opacity:0.1},400,function(){$(this).remove()})});$(\"body\").append(\\\'<div id=\"ov\"></div>\\\');$(\"#ov\").css(\"opacity\",0).stop().animate({opacity:1},1200,function(){o=0});for(i in p){i==(p.length-1)?ls=\\\'style=\"margin:10px 0 30px 10px !important;\"\\\':ls=\"\";$(\"#ov\").append(\\\'<a href=\"#\" id=\"link\\\'+i+\\\'\" class=\"pl\" \\\'+ls+\\\' onclick=\"javascript:jtp(\\\'+i+\\\');return false;\">\\\'+p[i]+\\\'</a>\\\');pi=p[i].replace(/http:\\\\/\\\\/(.*)/,\"$1\").replace(/(.*)\\\\/$/,\"$1\");$(\"#link\"+i).css(\"background\",\"url(\"+\"http://www.google.com/s2/favicons?domain=\"+pi+\") no-repeat center 6px\").append(\\\'<style type=\"text/css\">#link\\\'+i+\\\':hover{background:#FFFFFF url(\"\\\'+\\\'http://www.google.com/s2/favicons?domain=\\\'+pi+\\\'\") no-repeat center 6px !important;color:#000000;}</style>\\\');}}}function jtp(pn){o=0;$(\"body\").children().remove();for(i=(pn-1);i<(pn+2);i++){if(i!=-1&&i<p.length){$(\"body\").append(np(i))}}pn==0?window.scrollTo(0,0):window.scrollTo(0,($(document).height()/3));cp=pn+2;le()}function np(pn){return \\\'<div id=\"frame\\\'+(pn)+\\\'\" class=\"pf\">\\\'+\\\'<iframe id=\"page\\\'+(pn)+\\\'\" width=\"95%\" height=\"100%\" src=\"\\\'+p[pn]+\\\'\"></iframe>\\\'+\\\'<a href=\"#\" class=\"re\" onclick=\"javascript:$(\\\\\\\'#page\\\'+pn+\\\'\\\\\\\').attr(\\\\\\\'src\\\\\\\', \\\\\\\'\\\'+p[pn]+\\\'\\\\\\\');return false;\" title=\"\\\'+p[pn]+\\\'\"></a>\\\'+\\\'<a href=\"#\" class=\"up\" onclick=\"javascript:mu(\\\'+pn+\\\');return false;\" title=\"'+title_u+'\"></a>\\\'+\\\'<a href=\"#\" class=\"do\" onclick=\"javascript:md(\\\'+pn+\\\');return false;\" title=\"'+title_d+'\"></a>\\\'+\\\'<a href=\"#\" class=\"ov\" onclick=\"javascript:so();return false;\" title=\"'+title_o+'\"></a>\\\'+\\\'</div>\\\';}function le(){$(\".pf a\").mouseover(function(){$(this).stop().animate({opacity:0.6},200)}).mouseout(function(){$(this).stop().animate({opacity:1}),600});$(\".re\").click(function(){$(this).stop().animate({opacity:0.1},300).animate({opacity:0.6},300)})}$(document).ready(function(){for(i=0;i<2;i++){$(\"body\").append(np(cp));cp++}$(window).scroll(function(){if(!m&&!o){if($(window).scrollTop()==$(document).height()-$(window).height()){if(cp<p.length){$(\"body\").append(np(cp));if($(\"body\").children().length>3){$(\"body div:eq(0)\").remove()}window.scrollTo(0,$(\"#page\"+cp).height());lp++;cp++;le();$(\"#page\"+(cp-1)).attr(\"src\",p[cp-1]);}}if($(window).scrollTop()<=($(\"body div:eq(1)\").height()-$(\"body div:eq(1)\").height()+10)){pp=$(\"body div:eq(0)\").attr(\"id\").substr(5);if(pp>=1){$(\"body\").prepend(np(pp-1));$(\"body div:eq(3)\").remove();cp--}if(pp>=1){window.scrollTo(0,($(document).height()/3))}le()}}});le()});<!/script></head><body></body></html>\');document.close();void 0;})();';
	bookmarklet = bookmarklet.replace(/(\<!)/ig, "<");
	$("#createlinks").stop().animate({ marginBottom: 98 }, 400, function(){
		$(this).css("marginBottom", "0px")
		$("#bookmarklet")
			.attr("href", bookmarklet)
			.mouseover(function(){ $(this).stop().animate({ opacity: 0.7 }, 200); })
			.mouseout(function(){ $(this).stop().animate({ opacity: 1 }, 200); })
		$("#bookmarkletfooter")
			.css("display", "block")
			.stop().animate({ opacity: 1 }, 400, function(){ $("html").animate({ scrollTop: $(document).height() }, "slow"); });
	});
}

function addLink(link) {
	if (!addingLink) {
		hideBookmarklet();
		addingLink = true;
		if ($("#pagelinks li").index($(link).parent().parent()) == $("#pagelinks").children().length - 1) {
			$(link).parent().parent()
				.stop().animate({ marginBottom: 41 }, 200, function(){
					$(this)
						.css("marginBottom", "0px")
						.after(newLink(""));
					addingLink = false;
				});
			$("#pagelinks li:eq(" + ($("#pagelinks li").index($(link).parent().parent()) + 1) + ")")
				.css("opacity", 0)
				.stop().animate({ opacity: 1 }, 200);
		} else {
			$("#pagelinks li:eq(" + ($("#pagelinks li").index($(link).parent().parent()) + 1) + ")").stop().animate({ marginTop: 41 }, 300, function(){
				$(this).css("marginTop", "0px");
				$(link).parent().parent().after(newLink(""));
				$("#pagelinks li:eq(" + ($("#pagelinks li").index($(link).parent().parent()) + 1) + ")")
					.css("opacity", 0)
					.stop().animate({ opacity: 1 }, 200, function(){ addingLink = false; });
			});
		}
	}
}

function removeLink(link) {
	if ($("#pagelinks").children().length <= 3) {
		if ($("#create").attr("class") == "de") {
			$.browser.msie ? alert("Es müssen mindestens 3 Links ausgefüllt sein.") : errorBox();
		} else	{
			$.browser.msie ? alert("At least 3 links need to be defined.") : errorBox();
		}
	} else {
		hideBookmarklet();
		$(link).parent().parent().stop().animate({ opacity: 0 }, 300, function(){
			if ($("#pagelinks").children().length >= 3) {
				if ($("#pagelinks li").index(this) == $("#pagelinks").children().length - 1) {
					$(this).remove();
					$("#pagelinks").children("li:last-child")
						.css("marginBottom", "41px")
						.stop().animate({ marginBottom: 0 }, 200, function(){ $("#create").fadeOut(1).fadeIn(1); });
				} else {
					$("#pagelinks").children("li:eq(" + ($("#pagelinks li").index(this) + 1) + ")")
						.css("marginTop", "41px")
						.stop().animate({ marginTop: 0 }, 200, function(){ $("#create").fadeOut(1).fadeIn(1); });
					$(this).remove();
				}
			}		
		});
		addingLink = false;
	}
}

function errorBox() {
	$("#page").stop().animate({ opacity: 0.1 }, 200, function(){
		errorActive = true;		
		$("#modalbox")
			.css("display", "block")
			.stop().animate({ opacity: 1 }, 100);
	});
}

function hideBookmarklet() {
	if ($("#bookmarkletfooter").css("display") == "block") {
		$("#bookmarklet").attr("href", "#");
		$("#bookmarkletfooter").stop().animate({ opacity: 0 }, 400, function(){
			$(this).css("display", "none");
			$("#create").fadeOut(1).fadeIn(1);
			$("#createlinks")
				.css("marginBottom", 98)
				.stop().animate({ marginBottom: 0 }, 200);
		});
	}
}

$(document).ready(function(){
	$("#pagelinks li").remove();
	$("#pagelinks").append(newLink("www.domain.com"));
	for (i = 0; i < 2; i++) {
		$("#pagelinks").append(newLink(""));
	}
	$("#bookmarkletfooter, #tooltip2, #tooltip3, #modalbox").css({ opacity: 0, display: "none" });
	$("#tooltip1").fadeOut(6000);
 
	$("#optioncreate")
		.click(function(e){
			if (!$(this).hasClass("option_act")) {
				$(this).addClass("option_act");
				if ($("#optionimport").hasClass("option_act")) $("#optionimport").removeClass("option_act");
				if ($("#optionchange").hasClass("option_act")) $("#optionchange").removeClass("option_act");
				if ($("#importlinks").css("display") == "block") {
					$("#importlinks")
						.stop().animate({ opacity: 0 }, 400, function(){
							$("#optionchange")
								.css("marginBottom", 90)
								.stop().animate({ marginBottom: 0 }, 400);
							$(this).css("display", "none");
						});
				} else if ($("#changelinks").css("display") == "block") {
					$("#changelinks")
						.stop().animate({ opacity: 0 }, 400, function(){
							$("#optionchange")
								.css("marginBottom", 105)
								.stop().animate({ marginBottom: 0 }, 400);
							$(this).css("display", "none");
						});
				}
			}
			e.preventDefault();
		})
		.mouseover(function(e){
			$("#tooltip1")
				.css("display", "block")
				.stop().animate({ opacity: 1 }, 500);
		})
		.mouseout(function(e){
			$("#tooltip1").stop().animate({ opacity: 0 }, 300, function(){ $(this).css("display", "none"); });
		});

	$("#optionimport")
		.click(function(e){
			if (!$(this).hasClass("option_act")) {
				$(this).addClass("option_act");
				if ($("#optioncreate").hasClass("option_act")) $("#optioncreate").removeClass("option_act");
				if ($("#optionchange").hasClass("option_act")) $("#optionchange").removeClass("option_act");
				if ($("#changelinks").css("display") == "block") {
					$("#importlinks").css("marginBottom", 17);
					$("#changelinks")
						.stop().animate({ opacity: 0 }, 400, function(){
							$(this).css("display", "none");
							$("#importlinks")
								.css({ opacity: 0, display: "block" })
								.stop().animate({ opacity: 1, marginBottom: 4 }, 400);
						});
				} else {
					$("#optionchange").stop().animate({ marginBottom: 90 }, 400, function(){
						$(this).css("marginBottom", 0);
						$("#importlinks")
							.css({ opacity: 0, display: "block" })
							.stop().animate({ opacity: 1 }, 400);
					});
				}
			}
			e.preventDefault();
		})
		.mouseover(function(e){
			if ($("#tooltip1").css("opacity") != 0) $("#tooltip1").stop().animate({ opacity: 0 }, 300, function(){ $(this).css("display", "none"); });
			$("#tooltip2")
				.css("display", "block")
				.stop().animate({ opacity: 1 }, 500);
		})
		.mouseout(function(e){
			$("#tooltip2").stop().animate({ opacity: 0 }, 300, function(){ $(this).css("display", "none"); });
		});
	
	$("#optionchange")
		.click(function(e){
			if (!$(this).hasClass("option_act")) {
				$(this).addClass("option_act");
				if ($("#optioncreate").hasClass("option_act")) $("#optioncreate").removeClass("option_act");
				if ($("#optionimport").hasClass("option_act")) $("#optionimport").removeClass("option_act");
				if ($("#importlinks").css("display") == "block") {
					$("#changelinks").css("marginBottom", -9);
					$("#importlinks")
						.stop().animate({ opacity: 0 }, 400, function(){
							$(this).css("display", "none");
							$("#changelinks")
								.css({ opacity: 0, display: "block" })
								.stop().animate({ opacity: 1, marginBottom: 4 }, 400);
						});
				} else {
					$("#optionchange").stop().animate({ marginBottom: 105 }, 400, function(){
						$(this).css("marginBottom", 0);
						$("#changelinks")
							.css({ opacity: 0, display: "block" })
							.stop().animate({ opacity: 1 }, 400);
					});
				}
			}
			e.preventDefault();
		})
		.mouseover(function(e){
			if ($("#tooltip1").css("opacity") != 0) $("#tooltip1").stop().animate({ opacity: 0 }, 300, function(){ $(this).css("display", "none"); });
			$("#tooltip3")
				.css("display", "block")
				.stop().animate({ opacity: 1 }, 500);
		})
		.mouseout(function(e){
			$("#tooltip3").stop().animate({ opacity: 0 }, 300, function(){ $(this).css("display", "none"); });
		});

	$("#create").click(function(e){
		var pages = "";
		var pageCount = 0;
		$(".newlink").each(function(){
			if ($(this).children(".newlink .link").val() != "") pageCount++;
			if ($(this).children(".newlink .link").val().match(/http:\/\//)) {
				pagelink = $(this).children(".newlink .link").val();
			} else {
				$(this).children(".newlink .link").val().match(/https:\/\//) ? pagelink = $(this).children(".newlink .link").val() : pagelink = "http://" + $(this).children(".link").val();
			}
			pages += "p.push(\"" + pagelink + "\");";
		});
		if (pageCount > 2) {
			if ($("#bookmarkletfooter").css("display") == "none") {
				$(this).attr("class") == "de" ? createBookmarklet(pages, "de") : createBookmarklet(pages, "en");
			}
		} else {
			if ($("#create").attr("class") == "de") {
				$.browser.msie ? alert("Es müssen mindestens 3 Links ausgefüllt sein.") : errorBox();
			} else	{
				$.browser.msie ? alert("At least 3 links need to be defined.") : errorBox();
			}
		}
		e.preventDefault();
	});
	
	$("#import").click(function(e){
		try {
			var loadCount = 0;
			$("#importdata").html(document.getElementById("importfile").files[0].getAsText("utf8"));
			var importedFile = $("#importdata").html().split("\n");
			var importedLinks = importedFile[0];
			for (var i = 1; i < importedFile.length; i++) {
				if (importedFile[i].match(/href="[^"]*"/)) {
					loadCount++;
				}
			}
			if (loadCount <= 2) {
				loadError = true;
				$("#create").attr("class") == "de" ? $("#modalbox p").html("Es wurden keine Links gefunden.") : $("#modalbox p").html("No links could be found.");
				errorBox();
			} else {
				hideBookmarklet();
				$("#pagelinks").children().each(function(){ $(this).remove(); });
				$("#pagelinks").css({ opacity: 1, display: "block" });
				for (var i = 1; i < importedFile.length; i++) {
					if (importedFile[i].match(/href="[^"]*"/)) {
						importedLinks += importedFile[i].match(/href="[^"]*"/);
						importedLinks = importedLinks.replace(/href="([^"]*)"/, "p.push(\"$1\");");
						$("#pagelinks").append(newLink(importedFile[i].match(/href="([^"]*)"/)[0].toString().replace(/href="([^"]*)"/, "$1")));
					}
				}
			}
		} catch(e) {}
		e.preventDefault();
	});
	
	$("#load").click(function(e){
		var loadCount = 0;
		var loadedFile = $("#loadfile").val().split(";");
		for (var i = 1; i < loadedFile.length; i++) {
			if (loadedFile[i].match(/p.push/)) {
				loadCount++;
			}
		}
		if (loadCount <= 2) {
			loadError = true;
			$("#create").attr("class") == "de" ? $("#modalbox p").html("Es wurden keine Links gefunden.") : $("#modalbox p").html("No links could be found.");
			errorBox();
		} else {
			hideBookmarklet();
			$("#pagelinks").children().each(function(){ $(this).remove(); });
			$("#pagelinks").css({ opacity: 1, display: "block" });
			for (var i = 1; i < loadedFile.length; i++) {
				if (loadedFile[i].match(/p.push/)) {
					loadCount++;
					$("#pagelinks").append(newLink(loadedFile[i].toString().replace(/p.push\("(.*)"\)/, "$1")));
				}
			}
		}
		e.preventDefault();
	});

	$(".clean").click(function(e){
		$("#importfile").val("");
		$("#create").attr("class") == "de" ? $("#loadfile,").val("Bookmarklet hier hineinziehen") : $("#loadfile,").val("Drag bookmarklet here");
		$("#pagelinks").children().each(function(){ $(this).remove(); });
		for (i = 0; i < 3; i++) {
			$("#pagelinks").append(newLink(""));
		}
		hideBookmarklet();
		e.preventDefault();
	});
	
	$("#pagelinks").sortable({
		handle: ".move",
		update: function(){
			hideBookmarklet();
		}
	});

	$("#okbtn, #page").click(function(e){
		if (errorActive) {
			$("#modalbox").stop().animate({ opacity: 0 }, 100, function(){
				if (loadError) {
					loadError = false;
					$("#modalbox p").html(modalTemp);
				}					
				errorActive = false;
				$(this).css("display", "none");
				$("#page").stop().animate({ opacity: 1 }, 200);
			});
			e.preventDefault();
		}
	});
	
	$("a#lang_de, a#lang_en, .weblink")
		.mouseover(function(){
			$(this).stop().animate({ opacity: 0.6 }, 200 );
		})
		.mouseout(function(){
			$(this).stop().animate({ opacity: 1 }, 500 );
		})
		
	$('#pagelinks li div.newlink input')
		.focus(function(){
			if ($(this).attr("value") == "www.domain.com") $(this).attr({ value: "" });
		})
	var modalTemp = $("#modalbox p").html();
});
