jQuery(document).ready(function(){
    var inactive = "inactive";
	var active = "active";
	var focused = "focused";
	/*     
	* This function places the label inside the textbox.     
	* When the textbox is clicked, the text is removed.     
	*/   
	jQuery("label.label2value").each(function(){
    	obj = document.getElementById(jQuery(this).attr("for"));
		if ((jQuery(obj).attr("type") == "text") || (obj.tagName.toLowerCase() == "textarea")){
			jQuery(obj).addClass(inactive);
			var text = $(this).text();
			jQuery(this).css("display","none");    
			jQuery(obj).val(text);
			jQuery(obj).focus(function(){
				jQuery(this).addClass(focused);
				jQuery(this).removeClass(inactive);
				jQuery(this).removeClass(active);
				if (jQuery(this).val() == text)
					jQuery(this).val("");
			});
			jQuery(obj).blur(function(){
				jQuery(this).removeClass(focused);
				if (jQuery(this).val() == "") {
					jQuery(this).val(text);
					jQuery(this).addClass(inactive);
				} else {
					jQuery(this).addClass(active);
				};
			});
		};
	});
});

var relatedLink = "";
var videoLink = "";

function updateDescription(sender, videoID) {
    if (document.getElementById("spnDescriptionLong_" + videoID).style.display == "none") {
        document.getElementById("hlUpdateDescription_" + videoID).innerHTML = "Less";
        document.getElementById("spnDescriptionLong_" + videoID).style.display = "inline";
        document.getElementById("spnDescriptionShort_" + videoID).style.display = "none";
    } else {
        document.getElementById("hlUpdateDescription_" + videoID).innerHTML = "More";
        document.getElementById("spnDescriptionLong_" + videoID).style.display = "none";
        document.getElementById("spnDescriptionShort_" + videoID).style.display = "inline";
    }
}
function writeHeader() {	
	if (relatedLink != null && relatedLink != "") {
		document.write("<a href=\"" + relatedLink + "\">" + name + "</a>");
	} else {
		document.write(name);
	}
}

function writeThumbnail (videoID) {
        document.write("<div class=\"Thumbnail\">");
        document.write("    <a class=\"Thumbnail\" href=\"javascript:EtherStudios.Video.Player.Play(" + videoID + ")\">");
        document.write("        <img src=\"http://media.video.etherstudios.com/Videos/" + videoID + "/" + videoID + ".thumbnail.jpg\" alt=\"\" />");
        document.write("    </a>");
        
        if (relatedLink != null && relatedLink != "") {
            document.write("    <img class=\"PlayVideoWithArticle\" src=\"/Images/Button_PlayVideo.png\" alt=\"Play Video\" usemap=\"#PlayVideoMap_" + videoID + "\" />");
            document.write("    <map id=\"PlayVideoMap_" + videoID + "\" name=\"PlayVideoMap_" + videoID + "\">");
            document.write("        <area shape=\"circle\" coords=\"20,20,20\" href=\"javascript:EtherStudios.Video.Player.Play(" + videoID + ")\" alt=\"Play Video\" title=\"Play Video\" />");
            document.write("    </map>");            
            document.write("    <a class=\"PlayVideoWithArticle\" href=\"javascript:EtherStudios.Video.Player.Play(" + videoID + ")\" title=\"Play Video\"><img src=\"/Images/Text_PlayVideo.png\" alt=\"Play Video\" /></a>");
            document.write("    <img class=\"ReadArticle\" src=\"/Images/Button_ReadArticle.png\" alt=\"Read Article\" usemap=\"#ReadArticleMap_" + videoID + "\" />");
            document.write("    <map id=\"ReadArticleMap_" + videoID + "\" name=\"ReadArticleMap_" + videoID + "\">");
            document.write("        <area shape=\"circle\" coords=\"20,20,20\" href=\"" + relatedLink + "\" alt=\"Read Article\" title=\"Read Article\" />");
            document.write("    </map>");
            document.write("    <a class=\"ReadArticle\" href=\"" + relatedLink + "\" title=\"Read Article\"><img src=\"/Images/Text_ReadArticle.png\" alt=\"Read Article\" /></a>");
        } else {
            document.write("    <img class=\"PlayVideo\" src=\"/Images/Button_PlayVideo.png\" alt=\"Play Video\" usemap=\"#PlayVideoMap_" + videoID + "\" />");
            document.write("    <map id=\"PlayVideoMap_" + videoID + "\" name=\"PlayVideoMap_" + videoID + "\">");
            document.write("        <area shape=\"circle\" coords=\"20,20,20\" href=\"javascript:EtherStudios.Video.Player.Play(" + videoID + ")\" alt=\"Play Video\" title=\"Play Video\" />");
            document.write("    </map>");
            document.write("    <a class=\"PlayVideo\" href=\"javascript:EtherStudios.Video.Player.Play(" + videoID + ")\" title=\"Play Video\"><img src=\"/Images/Text_PlayVideo.png\" alt=\"Play Video\" /></a>");
        }
        
        document.write("</div>");
}

function writeThumbnail2 (videoID) {
	document.write("<div class=\"Thumbnail\">");
	document.write("    <a class=\"Thumbnail\" href=\"javascript:EtherStudios.Video.Player.Play(" + videoID + ")\">");
	document.write("        <img src=\"http://media.video.etherstudios.com/Videos/" + videoID + "/" + videoID + ".thumbnail.jpg\" alt=\"\" />");
	document.write("    </a>");
	writeLinks(videoID);
	document.write("</div>");
}

function writeLinks2 (videoID) {
	if (relatedLink != null && relatedLink != "" && videoLink != null && videoLink != "") {
		document.write("    <img class=\"PlayVideoWithArticle\" src=\"/Images/Button_PlayVideo.png\" alt=\"Play Video\" usemap=\"#PlayVideoMap_" + videoID + "\" />");
		document.write("    <map id=\"PlayVideoMap_" + videoID + "\" name=\"PlayVideoMap_" + videoID + "\">");
		document.write("        <area shape=\"circle\" coords=\"20,20,20\" href=\"/Watch/" + videoLink + "\" alt=\"Play Video\" title=\"Play Video\" />");
		document.write("    </map>");            
		document.write("    <a class=\"PlayVideoWithArticle\" href=\"javascript:EtherStudios.Video.Player.Play(" + videoID + ")\" title=\"Play Video\"><img src=\"/Images/Text_PlayVideo.png\" alt=\"Play Video\" /></a>");
		document.write("    <img class=\"ReadArticle\" src=\"/Images/Button_ReadArticle.png\" alt=\"Read Article\" usemap=\"#ReadArticleMap_" + videoID + "\" />");
		document.write("    <map id=\"ReadArticleMap_" + videoID + "\" name=\"ReadArticleMap_" + videoID + "\">");
		document.write("        <area shape=\"circle\" coords=\"20,20,20\" href=\"" + relatedLink + "\" alt=\"Read Article\" title=\"Read Article\" />");
		document.write("    </map>");
		document.write("    <a class=\"ReadArticle\" href=\"" + relatedLink + "\" title=\"Read Article\"><img src=\"/Images/Text_ReadArticle.png\" alt=\"Read Article\" /></a>");
	} else if (videoLink != null && videoLink != "") {
		document.write("    <img class=\"PlayVideo\" src=\"/Images/Button_PlayVideo.png\" alt=\"Play Video\" usemap=\"#PlayVideoMap_" + videoID + "\" />");
		document.write("    <map id=\"PlayVideoMap_" + videoID + "\" name=\"PlayVideoMap_" + videoID + "\">");
		document.write("        <area shape=\"circle\" coords=\"20,20,20\" href=\"/Watch/" + videoLink + "\" alt=\"Play Video\" title=\"Play Video\" />");
		document.write("    </map>");
		document.write("    <a class=\"PlayVideo\" href=\"javascript:EtherStudios.Video.Player.Play(" + videoID + ")\" title=\"Play Video\"><img src=\"/Images/Text_PlayVideo.png\" alt=\"Play Video\" /></a>");
	}
}

function writeDescription (videoID) {
    var spnDescriptionLong = document.getElementById("spnDescriptionLong_" + videoID);
    var description = spnDescriptionLong.innerHTML

    if (description != null && description != "") {

        var spnDescriptionLong = document.getElementById("spnDescriptionLong_" + videoID);
        if (relatedLink != null && relatedLink != "")
            spnDescriptionLong.innerHTML = "<a href=\"" + relatedLink + "\">" + spnDescriptionLong.innerHTML + "</a>";

        var words = description.split(" ");

        var short = "";
        for (var i = 0; i <= 14; i++) {
            if (i == 14) {
                if (words[i] != undefined)
					short += words[i] + "... ";
            } else {
                if (words[i] != undefined)
                	short += words[i] + " ";
			}
        }

        document.write("<span id=\"spnDescriptionShort_" + videoID + "\" class=\"Description\">");
        if (relatedLink != null && relatedLink != "")
            document.write("<a href=\"" + relatedLink + "\">");
        document.write(short);
        if (relatedLink != null && relatedLink != "")
            document.write("</a>");
        document.write("</span>");

        if (words.length >= 15)
            document.write("<a id=\"hlUpdateDescription_" + videoID + "\" href=\"javascript:updateDescription(this, " + videoID + ")\">More</a>");
    }
} 

function queryString(key) {
    keyValuePairString = window.location.search.substring(1);
    if (keyValuePairString != undefined) {
        keyValuePairArray = keyValuePairString.split("&");
        for (i = 0; i < keyValuePairArray.length; i++) {
            keyValuePair = keyValuePairArray[i].split("=");
            if (keyValuePair[0] == key) {
                return keyValuePair[1];
            }
        }
    }
}

function validate() {
    var isValid = true;
    var isNewsletterSelected = true;
    var errorString = '';
    var x = document.OptinForm;
    if (x.UEmail.value == "") {
        errorString = errorString + "Email Address. \n ";
        isValid = false;
    } else {
        if (x.UEmail.value.indexOf("@") == -1 || x.UEmail.value.indexOf(".") == -1) {
            errorString = errorString + 'Invalid email address.';
            isValid = false;
        }
    }

    if (isValid == true && isNewsletterSelected == true) {
        return true;
    } else {
        if (isNewsletterSelected == false) {
            errorString = errorString + "----------------------\nPlease select at least one subscription";
        }
        alert("Following fields are required:\n\n" + errorString); return false;
    }
}
