﻿var shareTypes;
var shareValues;
var pageRating;
var advertiserFeedUrl;
var titleDigg;
var bodyDigg;

var releaseId;
var adId;
var adBlockId;
var cookieId;
var publisherId;

function load()
{
	shareTypes = "digg,delicious,reddit,newsvine,yahoo";
	shareTypes = shareTypes.split(",");

	shareValues = new Array();

	for (i = 0; i < shareTypes.length; i++)
	{
		shareValues[shareTypes[i]] = false;
	}
	
	setStar("avgstar", pageRating);
}

function testAjax()
{
	landingpage.Test(testAjax_callback);
}

function testAjax_callback()
{
	alert("callback");
}

function postEmail()
{
	show(document.getElementById("emailprogress"));

	var from = document.getElementById("emailfrom").value;
	var to = document.getElementById("emailto").value;
	var subject = document.getElementById("emailsubject").value;
	var message = "";
	if(document.all)
	    message = document.getElementById("emailmessage").innerText;
	else
	    message = document.getElementById("emailmessage").textContent;
	    	
	landingpage.PostEmail(from, to, subject, message, postEmail_callback);
}

function postEmail_callback(message)
{
	hide(document.getElementById("emailprogress"));
	hide(document.getElementById("emaildata"));
	var responseText = document.getElementById("emailresponsetext");
	var responseTitle = document.getElementById("emailresponsetitle");
	
	if (message.value.Success)
	{
	    responseTitle.innerHTML = "<br/>BRAVO!";
		responseText.innerHTML = "Email sent successfully!";
		window.setTimeout("closeEmail", 1000);
	} else {
	    responseTitle.innerHTML = "<br/>ERROR";
		responseText.innerHTML = "There was a problem sending your email. Please try again.<br />"; /*(error: '" + message.value.ResultMessage + "')";*/
	}	
	show(document.getElementById("emailresponse"));	
}

function closeEmail()
{
	hide(document.getElementById("emailresponse"));
	hide(document.getElementById("emaildialog"));
}

function postComment()
{
	var name = document.getElementById("commentname").value;
	
	var comment;
	if(document.getElementById("commenttext").innerText)
	    comment = document.getElementById("commenttext").innerText;
	else if (document.getElementById("commenttext").textContent)	
	    comment = document.getElementById("commenttext").textContent;
	else
	    comment = document.getElementById("commenttext").value;
	
	landingpage.PostComment(releaseId, adId, publisherId, name, comment, postComment_callback);
}

function postComment_callback(message)
{
	var commentText = "";
	
	if(message.value == null)
	{
	    alert('No comments allowed.');
	    return false;
	}
	
	if (message.value.Success == true && message.value.Moderated == false)
	{
		var commentEntries = document.getElementById("commententries");
		var commentHTML = "<div class='comment_entry'><b>Comment by: " + message.value.CommentName + "</b><br /><font class='mini_gry'>Posted: " + message.value.CommentDate + "</font><br />" + message.value.CommentText + "</div>" + commentEntries.innerHTML;
		
		commentEntries.innerHTML = commentHTML;
		alert ("Thank you for your post. Your comment is already available.");
	} else if(message.value.Success == true && message.value.Moderated == true)
	{
		alert ("Thank you for your post. Your comment is awaiting approval.");
	}
}

function postRating(rating)
{
	landingpage.PostRating(releaseId, adId, adBlockId, cookieId, rating, postRating_callback);
}

function postRating_callback(message)
{
	if (message.value.Success == true)
	{
		pageRating = message.value.Average;
		var votes = message.value.Votes;
		
		setStar("avgstar", pageRating);
		document.getElementById("votes").innerHTML = votes;
	} else {
		//alert ("unable to post ratings");
	}
}

function postAction(name, value)
{
	landingpage.PostAction(releaseId, adId, cookieId, adBlockId, name, value, postAction_callback);	
}

function postAction_callback(message)
{
}

function printPage()
{
	window.print();
}

function share()
{
	document.getElementById('sharemenu').style.visibility = 'hidden';
	
	for (i = 0; i < shareTypes.length; i++)
	{
		var type = shareTypes[i];
		var windowParams = ""; //"'toolbar=no,width=600,height=600,resizable=yes,scrollbars=yes'";

		if (shareValues[type])
		{
			switch (type)
			{
				case "digg":
					window.open("http://digg.com/submit?phase=2&url=" + escape(window.location.href) + "&title=" + titleDigg + "&bodytext=" + bodyDigg, "", windowParams);
					break;
					
				case "delicious":
					window.open("http://del.icio.us/post?url=" + escape(window.location.href) + "&title=" + titleDigg, "", windowParams);
					break;
					
				case "reddit":
					window.open("http://reddit.com/submit?url=" + escape(window.location.href) + "&title=" + titleDigg, "", windowParams);
					break;
					
				case "newsvine":
					window.open("http://www.newsvine.com/_wine/save?popoff=1&u=" + escape(window.location.href), "", windowParams);
					break;
					
				case "yahoo":
					window.open("http://myweb2.search.yahoo.com/myresults/bookmarklet?&u=" + escape(window.location.href) + "&t=" + titleDigg, "", windowParams);
					break;
					
			}
		}
	}
}

function checkCloseMenu(e)
{
	if (!e) var e = window.event;
	var tg = (window.event) ? e.srcElement : e.target;
	
	if (tg.nodeName == 'DIV' && tg.id.indexOf("_extender") == -1)
	{
		var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
		
		while (reltg != tg && reltg.nodeName != 'BODY')
			reltg = reltg.parentNode
			
		if (reltg != tg)
		{	
			hide(tg);
		}
	}
}

function openMenu (element)
{
	var menuName = element.id + 'menu';
	var menu = document.getElementById(menuName);
	
	if (menu)
	{
		if (!isOpen(menu))
		{
			show(menu);
			
			menu.keepAlive = false;
			menu.timeout = null;
		}
	}
}

function closeMenu(element, event)
{
	if (!e) var e = event;
	
	var tg = (e.srcElement) ? e.srcElement : e.target;
    
	if (tg.nodeName == 'LI')
	{
		var menuName = element.id + 'menu';
		var menu = document.getElementById(menuName);
		
		hide(menu);
	}
}

function toggleCheckmark(element)
{
	var id = element.id;
	
	shareValues[id] = !shareValues[id];

	if (shareValues[id]) {element.src = "images/checkbox-checked.gif";} else {element.src = "images/checkbox-unchecked.gif";}
}

function show(element,overflow)
{
	if (element) 
	{
	    element.style.visibility = 'visible';
	    var array = element.getElementsByTagName('textarea');
	    for(i=0;i<array.length;i++)
	    {
	        try
	        {
	            array[i].style.overflow=overflow;
	        }
	        catch(e)
	        {
	        }
	    }		
	}
}

function hide(element,overflow)
{
	if (element) 
	{
	    element.style.visibility = 'hidden';
	    var array = element.getElementsByTagName('textarea');
	    for(i=0;i<array.length;i++)		
	    {
	        try
	        {
	            array[i].style.overflow=overflow;
	        }
	        catch(e)
	        {
	        }
	    }
	}
}

function isOpen(menuName)
{
	var menu = document.getElementById(menuName);
	if (menu)
	{
		return (menu.style.visibility == 'visible');
	} else {
		return false;
	}
}

function setRating(element)
{
	var rating = parseFloat(element.id.replace("star", ""));
	setStar("star", rating);
	
	var ratingComments = "not ,somewhat ,,very ,highly ,extremely ";
	ratingComments = ratingComments.split(",");
	document.getElementById("ratingtext").innerHTML = ratingComments[rating - 1] + "important";	
	postRating(rating);				
	alert('Thanks for rating this article!');	
}

function HideMenu()
{
    var menuName = "ratemenu";                    
	var menu = document.getElementById(menuName);		
	if(menu)
	{
	    hide(menu);	    	   
	}
}
function showRating(element)
{
	var rating = parseFloat(element.id.replace("star", ""));
	setStar("star", rating);
	
	var ratingComments = "not ,somewhat ,,very ,highly ,extremely ";
	ratingComments = ratingComments.split(",");
	document.getElementById("ratingtext").innerHTML = ratingComments[rating - 1] + "important";		
}

function setStar(prefix, rating)
{
	for (i = 1; i <= 6; i++)
	{
		var imageId = prefix + i;
		
		if (i <= rating)
		{
			document.getElementById(imageId).src = "images/star-full.gif";
		} else {
			document.getElementById(imageId).src = "images/star-empty.gif";
		}
	}
}

function copy(id)
{
    try
	{
	    var textField = document.getElementById(id);
	    if (textField.createTextRange)
	    {	
            textField.select();            
            textRange = textField.createTextRange();
            textRange.execCommand('RemoveFormat');
            textRange.execCommand('Copy');
	    }
	    else 
	    {
	        var range = document.createRange();        
	        var newText = document.createTextNode(textField.value);	        
	        range.selectNodeContents(newText,0);
	        range.execCommand('Copy');
	    }
	 }	    	    
	catch(e)
	{		 
	    alert("Your browser security settings don't permit the editor to automatically execute copying operations. Please use the keyboard for that (Ctrl+C).");
	}
}

function showCommentDialog()
{
	show(document.getElementById('commentmenu')); 
	var textField = document.getElementById('commenttext'); 
	textField.select(); 
	textField.focus();
}