/* From discover.php */

// this script uses functions in the script encoder.js - make sure you include that one before this script

function hideGetToolbarLink() {
	$('get-toolbar').hide();
}
function share_page(url,title,itemID) {
	var url = 'index.php?u=sharepage&url=' + url + '&title=' + title + '&itemID=' + itemID ;
	window.open(url,'sharepopup', 'toolbar=no,width=510,height=560,screenX=20,screenY=20,scrollbars=no,status=no,titlebar=no,toolbar=no,menubar=no,directories=no,resizable=yes'); return false;
}
function xpopup_twitter(title) {
	//var url = 'http://twitter.com/home?status=' + encodeURIComponent(title);
	//window.open(url,'twitterpopup', 'scrollbars=yes,width=800,height=640,screenX=20,screenY=20,resizable=yes');return false;
    var url = 'https://twitter.com/intent/tweet?text=' + encodeURIComponent(title);
	window.open(url,'twitterpopup', 'scrollbars=yes,width=550,height=345,screenX=20,screenY=20,resizable=yes');return false;
}
function figure_out_nontwitter(title,url) {
	var output = title + ' ' + url + " via http://YourVersion.com";
	if(output.length > 140){
	   output = title + ' ' + url + " via @YourVersion";
	   if(output.length > 140){
		   output =  title +  ' ' + url;
		   if(output.length > 140){
			   output = url + ' ' + title;
		   }
	   }
	}

	Encoder.EncodeType = "numerical";
	var decoded = Encoder.htmlDecode(output);
	return encodeURIComponent(decoded);

        //return encodeURIComponent(output);
        //return output;
		//return encodeURIComponent(output.gsub('&#039;',"'").gsub("&quot;",'"'));
}
function figure_out_twitter(description,username) {
	var title = description.unescapeHTML();
	var retweet = "RT @" + username + " ";
	output = retweet + title + " via http://YourVersion.com";
	if(output.length > 140) {
		   output = retweet + title + " via @YourVersion";
		   if(output.length > 140){
			   output = retweet + title;
		   }
	}
	return encodeURIComponent(output.gsub('&#039;',"'"));
}
// For sliver
function popup_twitter(title,url,urlId) {
	getTwitterShareID(urlId);
	var status = figure_out_nontwitter(title,url);        
	//var url = 'http://twitter.com/home?status=' + status;
	//window.open(url,'twitterpopup', 'scrollbars=yes,width=800,height=640,screenX=20,screenY=20,resizable=yes');
    var url = 'https://twitter.com/intent/tweet?text=' + status;
    window.open(url,'twitterpopup', 'scrollbars=yes,width=550,height=345,screenX=20,screenY=20,resizable=yes');
}
function setCookie(cookieName,cookieValue,nDays) { 
	document.cookie = cookieName+"="+cookieValue + "; max-age=" +(5*365*24*60*60);
} 
function toggleView(e) {
	if(suggestedSites.condensed == 0) {
		e.style.backgroundPosition='top';
		e.title='Switch to Expanded View';		
	}
	else {
		e.style.backgroundPosition='bottom';
		e.title='Switch to Condensed View';
	}
	if ($('pp_top')!=null) suggestedSites.numrows=$('pp_top').value;
	suggestedSites.condensed=1-suggestedSites.condensed;
	switchPage(suggestedSites.page,suggestedSites.numrows);
}

function share_bookmark(url,title,urlID) {
        var url = 'index.php?u=sharepage&url=' + encodeURIComponent(url) + '&title=' + encodeURIComponent(title) + '&urlID=' + urlID ;
        window.open(url,'sharepopup', 'toolbar=no,width=510,height=560,screenX=20,screenY=20,scrollbars=no,status=no,titlebar=no,toolbar=no,menubar=no,directories=no,resizable=yes');return false;
    }
function getTwitterShareID(url_id) {
	var request_url = 'index.php?l=fbshare&url_id='+url_id+'&twitter=1';
	var out = $_.get(request_url);
}
function getShareID(url_id) {
	var request_url = 'index.php?l=fbshare&url_id='+url_id;
	var shareID = $_.ajax( { url: request_url, async : false } ).responseText;
	return shareID;
}
function popup_facebook(title,url,urlID) {
    var shareID = getShareID(urlID);
	var qsUrl=thisHost+"index.php?l=facebook&share_id=" + shareID + "&kz=7faef681ef4a1ad89b08464c0503016202475086&cacheBust=" + Math.random();
	var win = window.open('#', 'facebook', 'scrollbars=yes,width=600,height=500,screenX=20,screenY=20,resizable=yes');
	if(shareID) {
		var qsUrl=thisHost+"index.php?l=facebook&share_id=" + shareID + "&kz=7faef681ef4a1ad89b08464c0503016202475086&cacheBust=" + Math.random();
		var fbUrl="http://www.facebook.com/sharer.php?src=bm&v=4&i=1209770855&u="+encodeURIComponent(qsUrl)+"&t="+encodeURIComponent(title);
        win.location = fbUrl;
	}
}
function breakFrame(url) {
	if (top.location != location) {
			top.location.href = url ;
	}	
	else {
			window.location.href = url ;
	}
}
function popup_facebook_logged_out(title, id){ // when using id, do it like so: 'item_id=X' where X is the item id.  It is set up to use share id and item id already.
	var win = window.open('#', 'facebook', 'scrollbars=yes,width=600,height=500,screenX=20,screenY=20,resizable=yes');
	var itemUrl = thisHost + 'index.php?l=facebook&' + id + "&kz=7faef681ef4a1ad89b08464c0503016202475086&cacheBust=" + Math.random(); 
	var fbUrl="http://www.facebook.com/sharer.php?src=bm&v=4&i=1209770855&u="+
				encodeURIComponent(itemUrl)+encodeURIComponent("&t=" + title);
	win.location = fbUrl;
}

