// 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 = 'http://www.actiontrip.com/social_bookmark_plugin/';

function share(id, url, title, type)
{	 
	if(url.length == 0) url = escape(document.URL);
	if(title.length == 0) title = escape(document.title);

	contentID = id;
	contentURL = url;
	contentTitle = 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";
	
	/****************** DEBUG CODE ***********************
	message  = 'winWidth: ' + winWidth + ' winHeight: ' + winHeight + '\n';
	message += 'shareLinkName: ' + shareLinkName + ' Top: ' + shareLinkWindowPositionY + ' Left: ' + shareLinkWindowPositionX + '\n';
	message += 'shareLinkPagePositionX: ' + shareLinkPagePositionX + ' shareLinkPagePositionY: ' + shareLinkPagePositionY + '\n';
	message += 'shareThis.style.top: ' + shareThis.style.top + ' shareThis.style.left: ' + shareThis.style.left + '\n';
	alert(message);
	******************************************************/
}

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="share_this"><div class="inner">';
	strContent += '  <div class="share_tabs">';
	strContent += '  <div class="share_tab_a" id="tab_button_social" style="margin-right:5px;"><a href="#" class="share_tab_link" onClick="changeTab(\'social\'); return false;">Share this</a></div>';
	strContent += '  <div class="share_tab_b" id="tab_button_email" ><a href="#" class="share_tab_link" onClick="changeTab(\'email\'); return false;">E-Mail</a></div>';
	strContent += '  <div class="share_close"><a href="#" class="share_close_link" onClick="document.getElementById(\'share_this\').style.display = \'none\'; return false;">CLOSE</a></div>';
	strContent += '  </div>';
	strContent += '<div id="tab_social" class="tab_social"><div class="share_items" id="tab_social_subwindow"></div><img src="../images/white.png" height="2" width="340" /></div>';
	strContent += '<div id="tab_email" class="tab_email"></div>';
	strContent += '</div></div>';
	
	shareThis.innerHTML = strContent;

}

function changeTab(tab)
{
	tabSocial = document.getElementById("tab_social");
	tabEmail = document.getElementById("tab_email");
	tabButtonSocial = document.getElementById("tab_button_social");
	tabButtonEmail = document.getElementById("tab_button_email");
	
	if(tab == "social") {
		updateSocialTab();
		// setInterval('updateSocialTab()', 10*1000); // enables real-time update of social tab
 		tabButtonSocial.className = "share_tab_a";
		tabButtonEmail.className = "share_tab_b";
		tabEmail.style.display = "none";
		tabSocial.style.display = "block";
	} else if(tab == "email") {
		updateEmailTab();
 		tabButtonSocial.className = "share_tab_b";
		tabButtonEmail.className = "share_tab_a";
		tabSocial.style.display = "none";
		tabEmail.style.display = "block";
	} 

}

function updateSocialTab()
{
    var type = "article";
    var xmlHttpReq = false;
    var self = this;

    if (window.XMLHttpRequest) {					    // Mozilla/Safari
        self.xmlHttpReq = new XMLHttpRequest();
    } else if (window.ActiveXObject) {					    // IE
        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  = '<div class="social_tab_spacer">&nbsp;</div>';
			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="share_forms">';
	strContent += '  <div class="share_form"><span class="emailthis">Email this content</span><br />';
	strContent += '       Send To:<br clear="all"  /><input name="reciever_email" type="text" value="' + recieverEmail + '" class="share_input"/><br clear="all"  />';
	strContent += '       Your Name:<br clear="all"  /><input name="sender_name" type="text" value="' + senderName + '" class="share_input" /><br clear="all"  />';
	strContent += '       Your Email:<br clear="all"  /><input name="sender_email" type="text" value="' + senderEmail + '" class="share_input" />';
	strContent += '  </div>';
	strContent += '  </div>';
	strContent += '  <div class="share_bottom_holder">';
	strContent += '  <div class="share_bottom_submits" onClick="submitEmailForm(); return false;">SEND</div>';
	strContent += '  <div class="share_bottom_submits spacer"></div>';
	strContent += '  <div class="share_bottom_submits" onclick="document.getElementById(\'send_email_form\').reset(); return false;">CANCEL</div>';
	strContent += '  </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 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="share_social_service">';
		strLinks += '<a href="' + linkURL + '" onClick="followURL(\'' + linkURL + '\', ' + linkID + ');" target="_blank">';
		strLinks += '<img src="' + arrLinks['links'][i].link_icon + '" border="0" class="share_icon" /></a>';
		strLinks += '<a href="' + linkURL + '" onClick="followURL(\'' + linkURL + '\', ' + linkID + ');" target="_blank" class="share_icon_link">' + arrLinks['links'][i].link_name;
		strLinks += '</a></div>';

		if (i/2!=parseInt(i/2) && i>0) {
			strLinks += '<br clear="all" />';
			}


	}	
	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;

    if (window.XMLHttpRequest) {					    // Mozilla/Safari
        self.xmlHttpReq = new XMLHttpRequest();
    } else if (window.ActiveXObject) {					    // IE
        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;
    if (window.XMLHttpRequest) {					    // Mozilla/Safari
        self.xmlHttpReq = new XMLHttpRequest();
    } else if (window.ActiveXObject) {					    // IE
        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;
    if (window.XMLHttpRequest) {					    // Mozilla/Safari
        self.xmlHttpReq = new XMLHttpRequest();
    } else if (window.ActiveXObject) {					    // IE
        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');
}