// JavaScript Document

var shareThis;
var strLinks;
var contentID = null;
var contentURL;
var contentTitle;
var contentType;
var	recieverEmail = '';
var	senderName = '';
var	senderEmail = '';
var currentID = null;
var pluginLayerOpen = false;
var apsolutePath = '/auto/socialbookmark/';

function share(id, url, title, type)
{	
	if(url.length == 0) url = document.URL;
	if(title.length == 0) title = document.title;

	contentID = id;
	contentURL = escape(url);
	contentTitle = escape(title);
	contentType = type;

	shareThis = document.getElementById("share_this");	
	shareLink = document.getElementById("share_link_" + contentID);
		
	if(shareThis.style.display != "block" ||
	   (shareThis.style.display == "block" && contentID != currentID))
	{
		shareThis.className = "share_this";
		initTabs();
		changeTab('social');
		shareThis.style.display = "block";
		pluginLocation();
		pluginLayerOpen = true;
	}
	else 
	{
		shareThis.style.display = 'none';
		pluginLayerOpen = false;
	}	
	currentID = id;
}

function pluginLocation()
{
	shareLinkName = "share_link_" + contentID;
	shareThis = document.getElementById("share_this");	
	shareLink = document.getElementById(shareLinkName);

	var winWidth, winHeight; 
	if (typeof window.innerWidth!='undefined')
	{ 
		winWidth = window.innerWidth; 
		winHeight = window.innerHeight; 
	}
	else
	{
		if (document.documentElement && 
		typeof document.documentElement.clientWidth!='undefined' && 
		document.documentElement.clientWidth!=0) 
		{ 
			winWidth = document.documentElement.clientWidth 
			winHeight = document.documentElement.clientHeight 
		}
		else
		{ 
			if (document.body && 
			typeof document.body.clientWidth!='undefined')
			{ 
				winWidth = document.body.clientWidth 
				winHeight = document.body.clientHeight 
			} 
		} 
	} 
	
	topPositionOffset = 6;

	objPageScroll = getScrollXY();
	pageScrollX = objPageScroll[0];
	pageScrollY = objPageScroll[1];
	shareLinkPagePositionX = findPosX(shareLink);
	shareLinkPagePositionY = findPosY(shareLink);
	shareLinkWindowPositionX = shareLinkPagePositionX - pageScrollX;
	shareLinkWindowPositionY = shareLinkPagePositionY - pageScrollY;

	pluginBottom = shareLinkWindowPositionY + shareLink.offsetHeight + shareThis.offsetHeight;
	pluginRight = shareLinkWindowPositionX + shareThis.offsetWidth;
		
	if(pluginBottom <= winHeight)
	{
		shareThisStyleTop = shareLinkPagePositionY + shareLink.offsetHeight;
	}
	else
	{
		shareThisStyleTop = shareLinkPagePositionY - shareThis.offsetHeight - topPositionOffset;
	}
	
	if(pluginRight <= winWidth)
	{
		shareThisStyleLeft = shareLinkPagePositionX;
	}
	else
	{
		shareThisStyleLeft = shareLinkPagePositionX - shareThis.offsetWidth + shareLink.offsetWidth;
	}
	
	shareThis.style.top = shareThisStyleTop + "px";
	shareThis.style.left = shareThisStyleLeft + "px";
}

function winSize()
{
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' )
	{
    	//Non-IE
    	myWidth = window.innerWidth;
    	myHeight = window.innerHeight;
  	}
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
	{
    	//IE 6+ in 'standards compliant mode'
    	myWidth = document.documentElement.clientWidth;
    	myHeight = document.documentElement.clientHeight;
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ))
	{
    	//IE 4 compatible
    	myWidth = document.body.clientWidth;
    	myHeight = document.body.clientHeight;
	}
	return [ myWidth, myHeight ];
}

function getScrollXY()
{
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' )
	{
    	//Netscape compliant
    	scrOfY = window.pageYOffset;
    	scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) )
	{
    	//DOM compliant
    	scrOfY = document.body.scrollTop;
    	scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) )
	{
    	//IE6 standards compliant mode
    	scrOfY = document.documentElement.scrollTop;
    	scrOfX = document.documentElement.scrollLeft;
	}
	return [ scrOfX, scrOfY ];
}

function findPosX(obj)
{
	var curleft = 0;
	if(obj.offsetParent)
	{
		while(1)
		{
			curleft += obj.offsetLeft;
			if(!obj.offsetParent)
			{
				break;
			}
			obj = obj.offsetParent;
		}
	}
	else if(obj.x)
	{
		curleft += obj.x;
	}
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if(obj.offsetParent)
	{
		while(1)
		{
			curtop += obj.offsetTop;
			if(!obj.offsetParent)
			{
				break;
			}
			obj = obj.offsetParent;
		}
	}
	else if(obj.y)
	{
		curtop += obj.y;
	}
	return curtop;
}

function initTabs()
{
	var strContent = '';
	
	strContent += '<div class="close_bar"><div class="close_link"><div class="close_button" onClick="document.getElementById(\'share_this\').style.display = \'none\'; return false;">X</div></div></div>';
	strContent += '<div class="share_this_menu">';
	strContent += '<ul>';
	strContent += '<li id="tab_button_social" class="active_tab" onClick="changeTab(\'social\');">Social Web</li>';
	strContent += '<li id="tab_button_whatis" class="inactive_tab" onClick="changeTab(\'whatis\');">What is Social Bookmarking?</li>';
	strContent += '<li id="tab_button_email" class="inactive_tab" onClick="changeTab(\'email\');">E-mail</li>';
	strContent += '</ul>';
	strContent += '</div>';
	strContent += '<div id="tab_content" class="tab_content">';
	strContent += '<div id="tab_social" class="tab_social">';
	strContent += '<div id="tab_social_subwindow" class="tab_social_subwindow" onClick="changeTab(\'social\');">';
	strContent += '</div>';
	strContent += '</div>';
	strContent += '<div id="tab_whatis" class="tab_whatis">';
	strContent += '</div>';
	strContent += '<div id="tab_email" class="tab_email">';
	strContent += '</div>';
	strContent += '</div>';
	
	shareThis.innerHTML = strContent;
}

function changeTab(tab)
{
	tabSocial = document.getElementById("tab_social");
	tabEmail = document.getElementById("tab_email");
	tabWhatis = document.getElementById("tab_whatis");
	tabButtonSocial = document.getElementById("tab_button_social");
	tabButtonEmail = document.getElementById("tab_button_email");
	tabButtonWhatis = document.getElementById("tab_button_whatis");
	
	if(tab == "social")
	{
		updateSocialTab();
		// setInterval('updateSocialTab()', 10*1000); // enables real-time update of social tab
 		tabButtonSocial.className = "active_tab";
		tabButtonEmail.className = "inactive_tab";
		tabButtonWhatis.className = "inactive_tab";
		tabEmail.style.display = "none";
		tabWhatis.style.display = "none";
		tabSocial.style.display = "block";
	} 
	else if(tab == "email")
	{
		updateEmailTab();
 		tabButtonSocial.className = "inactive_tab";
		tabButtonEmail.className = "active_tab";
		tabButtonWhatis.className = "inactive_tab";
		tabSocial.style.display = "none";
		tabWhatis.style.display = "none";
		tabEmail.style.display = "block";
		
	} 
	else if(tab == "whatis")
	{
		updateWhatisTab();
 		tabButtonSocial.className = "inactive_tab";
		tabButtonEmail.className = "inactive_tab";
		tabButtonWhatis.className = "active_tab";
		tabSocial.style.display = "none";
		tabEmail.style.display = "none";
		tabWhatis.style.display = "block";
	}
}

function updateSocialTab()
{
	var type = "article";
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest)
	{
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject)
	{
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', apsolutePath + 'load_links.php', true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function()
	{
        if (self.xmlHttpReq.readyState == 4)
		{
			strLinks = self.xmlHttpReq.responseText;
			arrLinks = eval('(' + strLinks + ')');
			tabSocial = document.getElementById("tab_social_subwindow");
			tabSocial.innerHTML = drawLinks(arrLinks);
        }
    }
    self.xmlHttpReq.send('type=' + type);
}

function updateEmailTab()
{
	var strContent = '';
	strContent += '<form id="send_email_form" action="' + apsolutePath + 'send_email.php" method="post">';
	strContent += '<div class="email_form_row"><strong>E-mail this content:</strong></div>';
	strContent += '<br />';
	strContent += '<div class="email_form_row">Send to:</div>';
	strContent += '<div class="email_form_row"><input type="text" name="reciever_email" value="' + recieverEmail + '" class="email_form_textbox" /></div>';
	strContent += '<div class="email_form_row">Your name:</div>';
	strContent += '<div class="email_form_row"><input type="text" name="sender_name" value="' + senderName + '" class="email_form_textbox" /></div>';
	strContent += '<div class="email_form_row">Your e-mail:</div>';
	strContent += '<div class="email_form_row"><input type="text" name="sender_email" value="' + senderEmail + '" class="email_form_textbox" /></div>';
	strContent += '<div class="email_form_row"></div>';
	strContent += '<div class="email_form_row"><input type="submit" name="submit" value="Send" class="submit_button" onClick="submitEmailForm(); return false;" /></div>';
	strContent += '<input type="hidden" name="title" value="' + contentTitle + '" />';
	strContent += '<input type="hidden" name="url" value="' + contentURL + '" />';
	strContent += '</form>';
	
	tabEmail = document.getElementById("tab_email");
	tabEmail.innerHTML = strContent;
}

function updateWhatisTab()
{
	strContent  = '<div id="tab_whatis_subwindow" class="tab_whatis_subwindow" onClick="changeTab(\'social\');">';
	strContent += '<div class="tab_whatis_text">';

	strContent += '<p><big><strong>Social Bookmarks</strong></big></p>';
	strContent += '<p><strong>"Share" our articles with Social Bookmarking</strong></p>';
	strContent += '<p>Did you read an article recently on our website that you\'d like to share with the world? Well, now you can with our new Social Bookmarking feature.</p>';
	strContent += '<p>Our readers can click on one of the graphic buttons on the bottom of any article to link the story to a variety of bookmarking sites.</p>';
	strContent += '<p>Social Bookmarking is a relatively new technology that has changed the way people experience the Web by allowing users to store, classify, rate, comment on and search links to any website or blog.</p>';
	strContent += '<p>Our website offers bookmarking to del.icio.us, Technorati, Digg, MyYahoo!, Newsvine, reddit, Google Bookmarks, Furl and more. Once an article is linked back to one of these sites, other users can then read it as well as post comments and related links. Instead of a search engine, you can find interesting links through a site\'s community of users, thereby transforming navigation of the Internet into a "social" experience.</p>';
	strContent += '<p>We encourage everyone to visit the sites listed above to learn more about their Social Bookmarking features. Each offers a similar, but distinct interface to fulfill your net navigation needs. So start sharing!</p>';
	strContent += '<p>In the near future, we will be incorporating additional features into the site to allow more interaction with our users.</p>';
	
	strContent += '</div>';
	strContent += '</div>';
	
	tabWhatis = document.getElementById("tab_whatis");
	tabWhatis.innerHTML = strContent;
}

function drawLinks(arrLinks)
{
	var strLinks = '';
	var linkURL = '';
	for(i = 0; i < arrLinks['links'].length; i++)
	{
		linkID = arrLinks['links'][i].id;
		linkURL = arrLinks['links'][i].link_url;
		linkURL = linkURL.replace("{url}", contentURL);
		linkURL = linkURL.replace("{title}", contentTitle);
		strLinks += '<div class="tab_social_row">';
		strLinks += '<a href="' + linkURL + '" onClick="followURL(\'' + linkURL + '\', ' + linkID + ');" target="_blank">';
		strLinks += '<img src="' + arrLinks['links'][i].link_icon + '" border="0" class="tab_social_icon" />';
		strLinks += '<div class="tab_social_link">' + arrLinks['links'][i].link_name + '</div>';
		strLinks += '</a>';
		strLinks += '</div>';	
	}	
	return strLinks;
}

function submitEmailForm()
{
	objForm = document.getElementById('send_email_form');
	// objForm.submit.disabled = true;
	recieverEmail = objForm.reciever_email.value;
	senderName = objForm.sender_name.value;
	senderEmail = objForm.sender_email.value;
	submitButton = objForm.submit.value;

    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest)
	{
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject)
	{
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', apsolutePath + 'send_email.php', true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function()
	{
        if (self.xmlHttpReq.readyState == 4)
		{
			strMessage = self.xmlHttpReq.responseText;
			tabEmail = document.getElementById("tab_email");
			tabEmail.innerHTML  = '<div class="email_message_spacer"></div>';
			tabEmail.innerHTML += '<div class="email_message">' + strMessage + '</div>';
			tabEmail.innerHTML += '<div class="email_ok_div"><input name="button" type="button" value="OK" onClick="updateEmailTab(); return false;" class="submit_button" /></div';
			setTimeout('updateEmailTab()', 5*1000);
        }
    }
    self.xmlHttpReq.send('reciever_email=' + recieverEmail + '&sender_name=' + senderName + '&sender_email=' + senderEmail + '&submit=' + submitButton + '&content_id=' + contentID + '&url=' + contentURL + '&title=' + contentTitle);
}

function followURL(url, link_id)
{
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest)
	{
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject)
	{
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', apsolutePath + 'save_stats.php', true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function()
	{
        if (self.xmlHttpReq.readyState == 4)
		{
            return true;
        }
    }
    self.xmlHttpReq.send('link_id=' + link_id + '&content_id=' + contentID + '&type=social');
}

function emailStats()
{
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest)
	{
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject)
	{
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', apsolutePath + 'save_stats.php', true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function()
	{
        if (self.xmlHttpReq.readyState == 4)
		{
            return true;
        }
    }
    self.xmlHttpReq.send('content_id=' + contentID + '&type=email');
}