//////////////////////////////////////////////////
// GetSocial Live - JavaScript                  //
// (Version 1.01)                               //
//                                              //
// (c) 2010 Hillel Stoler, All Rights Reserved. //
// www.hillelstoler.com                         //
//////////////////////////////////////////////////

var fadeTime = 3000;
var imagesPrefix="http://getsocialserver.files.wordpress.com/2010/08/gs"
var radioOptions = 8;
var numberOfMirrors = 9;

function show(id) {
	document.getElementById(id).className = "show";
}

function hide(id) {
	document.getElementById(id).className = "hide";
}

function replaceImage(id, src) {
    document.getElementById(id).src = src;
}

function selectPreview(selection) {
	var i = 0;
	
	for(i = 1; i <= radioOptions; i++) { 
		hide("Preview" + i);
	}
	show("Preview" + selection);
}

function trim(stringToTrim) { 
	return stringToTrim.replace(/^\s+|\s+$/g, "");
}

function setOpacity(id, opacity) {
	var object = document.getElementById(id).style; 
  
	object.opacity = (opacity / 101); 
	object.MozOpacity = (opacity / 101); 
	object.KhtmlOpacity = (opacity / 101); 
	object.filter = "alpha(opacity=" + opacity + ")";   
	if (opacity < 1) hide(id);
} 

function fadeOut(id, duration) { 
	var speed = Math.round(duration / 100); 
	var timer = 0; 

	for(i = 100; i >= 0; i--) { 
		setTimeout("setOpacity('" + id + "'," + i + ")", (timer * speed)); 
		timer++; 
	}
} 

function sendToClipboard() {
	var text = document.selection.createRange();
  
	text.execCommand("Copy");
	show("cbok");
	fadeOut('cbok', fadeTime);
	document.getElementById('result').focus(); 
	document.getElementById('result').select(); 
} 

function makeCode(form) {
	var blogURL;
	var blogURLpre;
	var mirror = Math.floor(Math.random() * numberOfMirrors) + 1;
	var myStyle = 1;
	var output;
	var radioSelsction = document.dashboard.desireddesign;
	var title = document.dashboard.title.value;
	var url = document.dashboard.permalink.value;
	var validInput = true;
	
	title = trim(title);
	url = trim(url);
	
	if (title == "") {
		show("f1w");
		fadeOut('f1w', fadeTime);
		hide("resultsection");
		validInput = false;
	} else {
		hide("f1w");
	}
  	
  	if (url == "") {
		show("f2w");
		fadeOut('f2w', fadeTime);
		hide("resultsection");
		validInput = false;
	} else {
		hide("f2w");
	}
	
	if (validInput) {
		blogURL = url;
		blogURLpre = "http://";
		
		if((url.substring(0, 8)).toLowerCase() == "https://") {
			url = url.substring(8);
			blogURL = url;
			blogURLpre = "https://";
			url="https://" + url;
		} else {
			if((url.substring(0, 7)).toLowerCase() == "http://") { 
				url = url.substring(7);
				blogURL = url;
				blogURLpre = "http://";
				url="http://" + url;
			} else {
				url="http://" + url;
			}
		}
		 		
		form.permalink.value = url;
		form.title.value = title;
		
	    if((blogURL.substr(0, 4)).toLowerCase() == "www.") { 
			blogURL = blogURL.substr(4);
			blogURLpre = blogURLpre + "www.";
		}
		 
		if (blogURL.indexOf("/") != (-1)) {
			blogURL = blogURL.substring(0, blogURL.indexOf("/"));
		}
		
		for (i = 0; i < radioOptions; i++) {
			if (radioSelsction[i].checked == true) myStyle = i + 1;
		}

		if (myStyle == 5) {
			myStyle=0;
		}
		
		output = "<p style=\"text-align:left;";
		if (myStyle == 0) {
			output = output + "font-size:8pt;\">Add to: ";
		} else {
			output = output + "\" class=\"getsocial\">";
		}
		
		// Toolbar Header
		if ((myStyle > 0) && (myStyle < 3)) {
			output = output + "<img style=\"border:0;margin:0;padding:0;\" src=\"";
			output = output + imagesPrefix;
			output = output + (myStyle + '');
			output = output + "00";
			output = output + (mirror + '');
			output = output + ".png\" alt=\"\" />";
		}
    
		// Facebook
		output = output + "<a title=\"Add to Facebook\" href=\"http://www.facebook.com/sharer.php?u=";
		output = output + url;
		output = output + "\" rel=\"nofollow\" target=\"_blank\">";
		if (myStyle != 0) {
			output = output + "<img style=\"border:0;margin:0;padding:0;\" src=\"";
			output = output + imagesPrefix;
			output = output + (myStyle + '');
			output = output + "01";
			output = output + (mirror + '');
			output = output + ".png\" alt=\"Add to Facebook\" /></a>";
		} else {
			output=output+"Facebook</a> | "
		}
		
		// Digg
		output = output + "<a title=\"Add to Digg\" href=\"http://digg.com/submit?phase=2&amp;url=";
		output = output + encodeURIComponent(url);
		output = output + "&amp;title=";
		if (title.length > 60) {
			output = output + encodeURIComponent(trim(title.substr(0, 57)));
			output = output + ("...");
		} else {
			output=output+encodeURIComponent(title);    
		}
		output = output + "\" rel=\"nofollow\" target=\"_blank\">";
		if (myStyle != 0) {
			output = output + "<img style=\"border:0;margin:0;padding:0;\" src=\"";
			output = output + imagesPrefix;
			output = output + (myStyle + '');
			output = output + "02";
			output = output + (mirror + '');
			output = output + ".png\" alt=\"Add to Digg\" /></a>";
		} else {
			output = output + "Digg</a> | ";
		}
		
		// Del.icio.us
		output = output + "<a title=\"Add to Del.icio.us\" href=\"http://del.icio.us/post?url=";
		output = output + encodeURIComponent(url);
		output = output + "&amp;title=";
		output = output + encodeURIComponent(title);
		output = output + "\" rel=\"nofollow\" target=\"_blank\">";
		if (myStyle != 0) {
			output = output + "<img style=\"border:0;margin:0;padding:0;\" src=\"";
			output = output + imagesPrefix;
			output = output + (myStyle + '');
			output = output + "03";
			output = output + (mirror + '');
			output = output + ".png\" alt=\"Add to Del.icio.us\" /></a>";
		} else {
			output = output + "Del.icio.us</a> | ";
		}
		
		// Stumbleupon
		output = output + "<a title=\"Add to Stumbleupon\" href=\"http://www.stumbleupon.com/submit?url=";
		output = output + encodeURIComponent(url);
		output = output + "&amp;title=";
		output = output + encodeURIComponent(title);
		output = output + "\" rel=\"nofollow\" target=\"_blank\">";
		if (myStyle != 0) {
			output = output + "<img style=\"border:0;margin:0;padding:0;\" src=\"";
			output = output + imagesPrefix;
			output = output + (myStyle + '');
			output = output + "04";
			output = output + (mirror + '');
			output = output + ".png\" alt=\"Add to Stumbleupon\" /></a>";
		} else {
			output = output + "Stumbleupon</a> | ";
		}
		
		// Reddit
		output = output + "<a title=\"Add to Reddit\" href=\"http://reddit.com/submit?url=";
		output = output + encodeURIComponent(url);
		output = output + "&amp;title=";
		output = output + encodeURIComponent(title);
		output = output + "\" rel=\"nofollow\" target=\"_blank\">";
		if (myStyle != 0) {
			output = output + "<img style=\"border:0;margin:0;padding:0;\" src=\"";
			output = output + imagesPrefix;
			output = output + (myStyle + '');
			output = output + "05";
			output = output + (mirror + '');
			output = output + ".png\" alt=\"Add to Reddit\" /></a>";
		} else {
			output = output + "Reddit</a> | ";
		}
		
		// Blinklist
		output = output + "<a title=\"Add to Blinklist\" href=\"http://www.blinklist.com/index.php?Action=Blink/addblink.php&amp;Description=&amp;Url=";
		output = output + encodeURIComponent(url);
		output = output + "&amp;Title=";
		output = output + encodeURIComponent(title);
		output = output + "\" rel=\"nofollow\" target=\"_blank\">";
		if (myStyle != 0) {
			output = output + "<img style=\"border:0;margin:0;padding:0;\" src=\"";
			output = output + imagesPrefix;
			output = output + (myStyle + '');
			output = output + "06";
			output = output + (mirror + '');
			output = output + ".png\" alt=\"Add to Blinklist\" /></a>";
		} else {
			output = output + "Blinklist</a> | ";
		}
		
		// Twitter
		output = output + "<a title=\"Add to Twitter\" href=\"http://twitter.com/home/?status=";
		if (title.length + url.length < 138) {
			output = output + encodeURIComponent(title);
			output = output + "+%40+";
			output = output + encodeURIComponent(url);
		} else {
			if (url.length < 130) {
				output = output + encodeURIComponent(trim(title.substr(0, 134-url.length))); 
				output = output + "+%40+";
				output = output + encodeURIComponent(title);
			} else {
				output = output + encodeURIComponent(url);
			}
		}   
		output = output + "\" rel=\"nofollow\" target=\"_blank\">";
		if (myStyle != 0) {
			output = output + "<img style=\"border:0;margin:0;padding:0;\" src=\"";
			output = output + imagesPrefix;
			output = output + (myStyle + '');
			output = output + "07";
			output = output + (mirror + '');
			output = output + ".png\" alt=\"Add to Twitter\" /></a>";
		} else {
			output = output + "Twitter</a> | ";
		}
		
		// Technorati
		output = output + "<a title=\"Add to Technorati\" href=\"http://www.technorati.com/faves?add=";
		output = output + url;
		output = output + "\" rel=\"nofollow\" target=\"_blank\">"; 
		if (myStyle != 0) {
			output = output + "<img style=\"border:0;margin:0;padding:0;\" src=\"";
			output = output + imagesPrefix;
			output = output + (myStyle + '');
			output = output + "08";
			output = output + (mirror + '');
			output = output + ".png\" alt=\"Add to Technorati\" /></a>";
		} else {
			output = output + "Technorati</a> | ";
   		}
   		
		// Yahoo Buzz
		output = output + "<a title=\"Add to Yahoo Buzz\" href=\"http://buzz.yahoo.com/buzz?targetUrl=";
		output = output + encodeURIComponent(url);
		output = output + "&amp;headline=";
		output = output + encodeURIComponent(title);
		output = output + "\" rel=\"nofollow\" target=\"_blank\">";
		if (myStyle != 0) {
			output = output + "<img style=\"border:0;margin:0;padding:0;\" src=\"";
			output = output + imagesPrefix;
			output = output + (myStyle + '');
			output = output + "09";
			output = output + (mirror + '');
			output = output + ".png\" alt=\"Add to Yahoo Buzz\" /></a>";
		} else {
			output = output + "Yahoo Buzz</a> | ";
		}

		// Newsvine 
		output = output + "<a title=\"Add to Newsvine\" href=\"http://www.newsvine.com/_wine/save?u=";
		output = output + encodeURIComponent(url);
		output = output + "&amp;h=";
		output = output + encodeURIComponent(title);
		output = output + "\" rel=\"nofollow\" target=\"_blank\">";
		if (myStyle != 0) {
			output = output + "<img style=\"border:0;margin:0;padding:0;\" src=\"";
			output = output + imagesPrefix;
			output = output + (myStyle + '');
			output = output + "10";
			output = output + (mirror + '');
			output = output + ".png\" alt=\"Add to Newsvine\" /></a>";
		} else { 
			output = output + "Newsvine</a>";
		}
		
		// Toolbar Footer
		if ((myStyle > 0) && (myStyle < 3)) {
			output = output + "<img style=\"border:0;margin:0;padding:0;\" src=\"";
			output = output + imagesPrefix;
			output = output + (myStyle + '');
			output = output + "11";
			output = output + (mirror + '');
			output = output + ".png\" alt=\"\" />";
		}
   
		output = output + "</p>";
		
		// Google Buzz (via Google Reader)
		if (myStyle == 6) {
			output = "<p style=\"text-align:left;\" class=\"getsocial\">";
			output = output + "<a title=\"Add to Google Buzz\" href=\"http://www.google.com/reader/link?url=";
			output = output + encodeURIComponent(url);
			output = output + "&amp;title=";
			output = output + encodeURIComponent(title);
			output = output + "&amp;snippet=Add%20a%20description&amp;srcURL=";
			if (blogURL.toLowerCase() == "wp.me") {
				output = output + encodeURIComponent("http://wp.com");
				output = output + "&amp;srcTitle=";
				output = output + encodeURIComponent("WordPress.com");
			} else {
				output = output + encodeURIComponent(blogURLpre);
				output = output + encodeURIComponent(blogURL);
				output = output + "&amp;srcTitle=";
				output = output + encodeURIComponent(blogURL);
			}
			output = output + "\" rel=\"nofollow\" target=\"_blank\">";
			output = output + "<img style=\"border:0;margin:0;padding:0;\" src=\"http://getsocialserver.files.wordpress.com/2010/08/gs";
			output = output + "gb";
			output = output + (mirror);
			output = output + ".png\" width=\"50\" height=\"50\" alt=\"Add to Google Buzz\" /></a></p>";	
		}
		
		// Facebook Like
	    if (myStyle == 7) { 
			output = "<p style=\"text-align:left;\" class=\"getsocial\">";
			output = output + "<a title=\"Like This!\" href=\"http://getsociallive.com/gslike.php?likeurl=";
			output = output + encodeURIComponent(url);
			output = output + "&amp;liketitle=";
			output = output + encodeURIComponent(title);
			output = output + "\" rel=\"nofollow\" target=\"_blank\">";
			output = output + "<img style=\"border:0;margin:0;padding:0;\" src=\"http://getsocialserver.files.wordpress.com/2010/08/gs";
			output = output + "lk";
			output = output + (mirror);
			output = output + ".png\" width=\"49\" height=\"23\" alt=\"Like This!\" /></a></p>";		
		}
		
		// FaceTweet (via LinksAlpha)
		if (myStyle == 8) { 
			output = "<p style=\"text-align:left;\" class=\"getsocial\">";
			output = output + "<a title=\"FaceTweet it!\" href=\"http://getsociallive.com/gsfbt.php?fbturl=";
			output = output + encodeURIComponent(url);
			output = output + "&amp;fbttitle=";
			output = output + encodeURIComponent(title);
			output = output + "&amp;fbtmessage=";
			output = output + "\" rel=\"nofollow\" target=\"_blank\">";
			output = output + "<img style=\"border:0;margin:0;padding:0;\" src=\"http://getsocialserver.files.wordpress.com/2010/08/gs";
			output = output + "ft";
			output = output + (mirror);
			output = output + ".png";
			output = output + "\" width=\"50\" height=\"30\" alt=\"FaceTweet it!\" /></a></p>";	
		}

		show("resultsection");
		form.result.value = output;
		form.result.focus(); 
		form.result.select(); 
	}
	form.permalink.value = url;
}