/* absolute path to the "loading" image */
var loadingImageAbsolutePath = "http://www.spindistribute.com/images/ajax-loader.gif";

/* get mouse position */
var mouseX = 0;
var mouseY = 0;
$(document).mousemove(function(e) { 
	mouseX = e.pageX;
	mouseY = e.pageY;
});
/* END :: get mouse position */

/* are we dealing with Google Chrome? */
var isChrome = /chrome/.test(navigator.userAgent.toLowerCase());
/* are we dealing with IE 6 or less? */
var isIElte6 = ($.browser.msie && parseFloat($.browser.version.substr(0, 3)) <= 6.0);

/* document ready function */
$(document).ready(function(){
	
	/* call an AJAX script every 2 minutes to prevent the session timeout */
	setInterval("preventTimeout();", 1000 * 120);
	
	/* initialize all listeners on elements that could be added via AJAX as well */
	initializeListeners();
	
	/* activate resizable textarea plugin (for all browsers except Google Chrome which already provides this functionality) */
	if (!isChrome) {
		if ($("textarea.resizable").size() > 0) $("textarea.resizable:not(.processed)").TextAreaResizer();	
	}
	
	/* activate lightBox plugin */
	$("a[@rel*=lightbox]").lightBox();
	
	/* attach the character counters to some of the textares */
	if ($("#article_summary").size() > 0) $("#article_summary").charCounter(400, { container: "#article_summary_counter" });
	
	/* enable "loading" functionality on a subset of HTML elements */
	$(".loadingOnClick").click(function(event) {
		var alreadyLoading = $(this).attr("alreadyLoading");
		if (!alreadyLoading) {
			$(this).attr("alreadyLoading", "true");
			var newElementId = $(this).attr("id") + $(this).attr("name") + $(this).attr("rel") + "LoadingElement";
			$(this).val("Please, wait ...").after("<span id='" + newElementId + "' style='display:none; margin-left:10px;'><img src='" + loadingImageAbsolutePath + "' align='absmiddle' /></span>");
			$("span#" + newElementId).fadeIn("fast");
		}
	});
	
	/* fix the flash elements for IE browsers */
	flashObjects = document.getElementsByTagName("object");
	for (var i = 0; i < flashObjects.length; i++) {
		flashObjects[i].outerHTML = flashObjects[i].outerHTML;
	}
	
	/* blink the error message if it is present */
	$("div.errorDiv").fadeIn().fadeOut().fadeIn();
	
	/* blink the success message if it is present */
	$("div.successDiv").fadeIn().fadeOut().fadeIn();
	
	/* show the SmileyTech logo in colors */
	$("img#logoSmileyTech").hover(function() {
		var currentSrc = new String($(this).attr("src"));
		$(this).attr("src", currentSrc.replace("grayscale", "color"));
	}, function() {
		var currentSrc = new String($(this).attr("src"));
		$(this).attr("src", currentSrc.replace("color", "grayscale"));
	});
	
	/* slide down the login form */
	if (location.href.indexOf("#/log-in") != -1) {
		$("div#topNavigationHolder a").removeClass("active");
		$("a#topNavLogIn").addClass("active");
		$("div#headerParent h3").animate({ opacity: 0.0 }, 250);
		$("div#headerWrapper").animate({ height: "470px" }, 250);
		if (isIElte6) {
			$("div#rightBox").slideUp("fast");	
		}
	}
	$("a#topNavLogIn").click(function(event) {
		event.preventDefault();
		window.location.hash = "#/log-in";
		$("div#topNavigationHolder a").removeClass("active");
		$("a#topNavLogIn").addClass("active");
		$("div#headerParent h3").animate({ opacity: 0.0 }, 250);
		$("div#headerWrapper").animate({ height: "470px" }, 250);
		if (isIElte6) {
			$("div#rightBox").slideUp("fast");	
		}
	});
	
	/* make active input fields seem lighter */
	$("div#logInHiddenLeft input").focus(function() {
		$(this).addClass("active");
	});
	
	$("div#logInHiddenLeft input").blur(function() {
		$(this).removeClass("active");
	});
	
	/* make active registration input fields seem lighter */
	$("input.quickRegInputText").focus(function() {
		$("input.quickreg_spam_protection").val(10);
		if ($(this).val() == "your email here ...") $(this).val("");
		$(this).addClass("quickRegInputTextActive");
	});
	
	$("input.quickRegInputText").blur(function() {
		if ($(this).val() == "") $(this).val("your email here ..."); 
		$(this).removeClass("quickRegInputTextActive");
	});
	
	/* hide the Special Offer box */
	$("a.closeSpecialOfferBox").click(function(event) {
		event.preventDefault();
		$("div#specialOffer").slideUp("slow");
		var exdate = new Date();
		exdate.setDate(exdate.getDate() + 7);
		document.cookie = "specialOfferHide=1;expires=" + exdate.toGMTString();
	});
	
	/* show the explanation div */
	$("a.explanationDivShow").click(function(event) {
		event.preventDefault();
		var explanation = $(this).attr("rel");		// get the appropriate explanation
		var explanationOutput = $("div#" + explanation + "Explanation").html();
		if (explanationOutput == "") explanationOutput = $("div#notAvailableExplanation").html();
		if ($("div.explanationDivWrapper").css("display") == "none") {
			$("div#explanationDivContent").html(explanationOutput);
			$("div.explanationDivWrapper").css("top", (mouseY - 150) + "px");
			$("div.explanationDivWrapper").fadeIn("slow");
		} else if ($("div#explanationDivContent").html() != explanationOutput && $("div.explanationDivWrapper").css("display") != "none") {
			$("div#explanationDivContent").slideUp();
			$("div#explanationDivContent").html(explanationOutput);
			$("div.explanationDivWrapper").animate({ top:(mouseY - 150) + "px" }, 500);
			$("div#explanationDivContent").slideDown();
		} else {
			$("div#explanationDivContent").html(explanationOutput);
			$("div.explanationDivWrapper").fadeOut("slow");
		}
	});
	
	/* validate and preview blocks of text */
	$("a.validatePreviewText").click(function(event) {
		event.preventDefault();
		var field = $(this).attr("rel");		// get the appropriate field (title, article, ...)
		var text = "";
		if (field == "title") {
			text = $("input#title").val();
		} else if (field == "article") {
			text = $("#article").val();
		} else if (field == "articleSummary") {
			text = $("#article_summary").val();
		} else if (field == "aboutAuthorHtml") {
			text = $("#bio_html").val();
		} else if (field == "aboutAuthorPlain") {
			text = $("#bio_plain").val();
		} else if (field == "articleBlog") {
			text = $("#article_blog").val();
		}
		$.post("action/ajax-cp-validate-preview", 
			{ text_field: field, validate_text: text }, 
			function(data) {		
				$("div#" + field + "Preview").html(data);
				if ($("div#" + field + "Preview").css("display") == "none") {
					$("a.closeValidatePreviewText[rel='" + field + "']").fadeIn("slow");
					$("a.validatePreviewText[rel='" + field + "']").html("Preview another version.");
					$("div#" + field + "Preview").slideDown("slow");
				} else {
					$("div#" + field + "Preview").fadeOut().fadeIn();	
				}
			}
		);
	});
	
	/* check the maximum length of 'About the Author' fields */
	if ($("textarea.bioText").size() > 0) {
		$("textarea.bioText").each(function() {
			checkMaxPossibleLength($(this).val(), $(this).attr("id"));
		});
	}
	
	$("textarea.bioText").keyup(function(e) {
		var field_name = new String($(this).attr("id"));
		var content = new String($("#" + field_name).val());
		if (content.length > 0 &&  content.length % 30 == 0 &&  substr_count(content, "[") == substr_count(content, "]")) {
			checkMaxPossibleLength(content, field_name);
		}
	});
	
	$("textarea.bioText").blur(function() {
		var field_name = new String($(this).attr("id"));
		var content = new String($("#" + field_name).val());
		checkMaxPossibleLength(content, field_name);
	});
	
	function checkMaxPossibleLength(content, field_name) {
		$.post("action/ajax-cp-check-max-length", { content: content }, 
			function(data) {		
				if (data) {
					var dataInt = parseInt(data);
					if (field_name == "bio_html" && dataInt <= 400) {
						$("span#" + field_name + "_longest").css("color", "#000000").html(data + " characters.");
					} else if (field_name == "bio_html" && dataInt > 400) {
						$("span#" + field_name + "_longest").css("color", "#FF0000").html(data + " (too long!)");
					} else if (field_name == "bio_plain" && dataInt <= 300) {
						$("span#" + field_name + "_longest").css("color", "#000000").html(data + " characters.");
					} else if (field_name == "bio_plain" && dataInt > 300) {
						$("span#" + field_name + "_longest").css("color", "#FF0000").html(data + " (too long!)");
					}
				} else {
					$("span#" + field_name + "_longest").css("color", "#FF0000").html("does not validate!");
				}
			}
		);
	}
	
	/* preview a version of an article */
	$("input#preview").click(function(event) {
		event.preventDefault();
		if ($("div#completeArticlePreview").css("display") == "none") {
			$("input#preview").val("Please, wait ...");
			$.post("action/ajax-cp-preview-article", 
				{ title: $("input#title").val(), article: $("#article").val(), article_summary: $("#article_summary").val(), bio_html: $("#bio_html").val(), 
					bio_plain: $("#bio_plain").val(), article_blog: $("#article_blog").val(), keywords: $("input#keywords").val() }, 
				function(data) {		
					$("div#completeArticlePreview").html(data);
					$("input#preview").val("Close this preview");
					$("div#completeArticlePreview").slideDown("slow");
				}
			);
		} else {
			$("input#preview").val("Preview this article");
			$("div#completeArticlePreview").slideUp("slow");
			$("div#completeArticlePreview").html("");
		}
	});
	
	/* automatically preview a version of an article if we are in the admin panel */
	var windowLocation = new String(window.location);
	if (windowLocation.indexOf("admin-edit-distributions") != -1 && windowLocation.indexOf("mode=prepare") == -1) {
		$("input#preview").click();	
	}
	
	/* close the preview DIV */
	$("a.closeValidatePreviewText").click(function(event) {
		event.preventDefault();
		var field = $(this).attr("rel");		// get the appropriate field (title, article, ...)
		var text = "";
		$("a.closeValidatePreviewText[rel='" + field + "']").fadeOut("slow");
		$("a.validatePreviewText[rel='" + field + "']").html("Validate and preview.");
		$("div#" + field + "Preview").slideUp("slow");
	});
	
	/* choose between automatically and manually creating an article version for blogs */
	$("a.toggleArticleBlogVersion").click(function(event) {
		event.preventDefault();
		if ($("div#articleBlogAutomatically").css("display") == "none") {
			$("div#articleBlogManually").slideUp("slow");
			$("div#articleBlogAutomatically").slideDown("slow");
			$("#article_blog").val("");
		} else {
			var article_blog = $("#article").val() + "\n\n" + $("#bio_html").val();
			if (article_blog != "\n\n") {
				$("#article_blog").val(article_blog);
			}
			$("div#articleBlogAutomatically").slideUp("slow");
			$("div#articleBlogManually").slideDown("slow");	
		}
	});
	
	/* choose between automatically and manually creating a short summary of the article */
	$("a.toggleArticleSummaryVersion").click(function(event) {
		event.preventDefault();
		if ($("div#articleSummaryAutomatically").css("display") == "none") {
			$("div#articleSummaryManually").slideUp("slow");
			$("div#articleSummaryAutomatically").slideDown("slow");
			$("#article_summary").val("");
		} else {
			var firstNewLine = $("#article").val().indexOf("\n");
			if (firstNewLine > 0 && firstNewLine < 395) {
				$("#article_summary").val($("#article").val().substring(0, firstNewLine));
			} else {
				$("#article_summary").val("");
			}
			$("div#articleSummaryAutomatically").slideUp("slow");
			$("div#articleSummaryManually").slideDown("slow");
		}
	});
	
	/* set the hover effect on the right hand buttons (unless we are dealing with IE 6 or less) */
	$("div.rightButtons").hover(function() {
		var name = new String($(this).attr("id"));
		if (!$(this).hasClass(name + "Active")) {
			$(this).removeClass(name);
			$(this).addClass(name + "Hover");
		}
	}, function() {
		var name = new String($(this).attr("id"));
		if (!$(this).hasClass(name + "Active") && $(this).hasClass(name + "Hover")) {
			$(this).removeClass(name + "Hover");
			$(this).addClass(name);
		}
	});
	
	/* set the hover effect on table rows in control panel archives */
	$("table.cpArchive tr").hover(function() {
		$(this).addClass("trHover");
	}, function() {
		$(this).removeClass("trHover");
	});
	
	/* set the hover effect on table rows in control panel payment options */
	$("table.paymentOptionsTable tr").hover(function() {
		$(this).addClass("trPaymentOptionsHover");
	}, function() {
		$(this).removeClass("trPaymentOptionsHover");
	});
	
	/* validate the registration form */
	$("#registerForm").submit(function(event) {	
		errorOutput = "";
		if ($("input#reg_email").val().indexOf("@") == -1 || $("input#reg_email").val().indexOf(".") == -1) {
			errorOutput += "- Your email address seems to be incorrect.<br />";
		}
		if ($("input#reg_email").val() == "" || ($("input#reg_email").val() != $("input#reg_email_repeat").val())) {
			errorOutput += "- Make sure to type in the same email address twice.<br />";
		}
		if ($("input#reg_password").val() == "" || ($("input#reg_password").val() != $("input#reg_password_repeat").val())) {
			errorOutput += "- Make sure to type in the same password twice.<br />";
		}
		if ($("input#captcha_number").val() == 1 && $("input#captcha").val().toUpperCase() != "K2G4X") errorOutput += "- Retype the text from the picture.<br />";
			else if ($("input#captcha_number").val() == 2 && $("input#captcha").val().toUpperCase() != "H26LS") errorOutput += "- Retype the text from the picture.<br />";
			else if ($("input#captcha_number").val() == 3 && $("input#captcha").val().toUpperCase() != "A8F4R") errorOutput += "- Retype the text from the picture.<br />";
			else if ($("input#captcha_number").val() == 4 && $("input#captcha").val().toUpperCase() != "J5HMT") errorOutput += "- Retype the text from the picture.<br />";
			else if ($("input#captcha_number").val() == 5 && $("input#captcha").val().toUpperCase() != "P3D7E") errorOutput += "- Retype the text from the picture.<br />";
		if (errorOutput == "") {
			return true;
		} else {
			showWarningDiv(errorOutput);
			return false;
		}
	});
	
	/* validate the forgotten password form */
	$("#forgottenPassForm").submit(function(event) {
		if ($("input#forgotten_email").val().indexOf("@") == -1 || $("input#forgotten_email").val().indexOf(".") == -1) {
			showWarningDiv("This email address seems to be invalid.");
			return false;
		} else {
			return true;
		}
	});
	
	/* validate the slide-down forgotten password form */
	$("#forgottenPassFormSlide").submit(function(event) {
		if ($("input#forgotten_email_slide").val().indexOf("@") == -1 || $("input#forgotten_email_slide").val().indexOf(".") == -1) {
			showWarningDiv("This email address seems to be invalid.");
			return false;
		} else {
			return true;
		}
	});
	
	/* display a list of some of the article directories in our network */
	$("a#showArticleDirectories").click(function(event) {
		event.preventDefault();
		$.post("action/ajax-network-list",
			function(data) {		
				showOptionsDiv("Distribution Network", data, 100);
			}
		);
	});
	
	/* validate the form for changing user's password */
	$("#changePassForm").submit(function(event) {	
		errorOutput = "";
		if ($("input#password_old").val() == "") errorOutput += "- You need to specify your current password.<br />";
		if ($("input#password").val() == "" || ($("input#password").val() != $("input#password_repeat").val())) {
			errorOutput += "- Make sure to type in the same password twice.<br />";
		}
		if (errorOutput == "") {
			return true;
		} else {
			showWarningDiv(errorOutput);
			return false;
		}
	});
	
	/* validate the form for suggesting article websites */
	$("#suggestWebsiteForm").submit(function(event) {	
		errorOutput = "";
		if ($("#suggestion_body").val() == "") errorOutput += "- Please, write your suggestion.<br />";
		if (errorOutput == "") {
			return true;
		} else {
			showWarningDiv(errorOutput);
			return false;
		}
	});
	
	/* validate the form for contacting the administrator */
	$("#contactAdministratorForm").submit(function(event) {	
		errorOutput = "";
		if ($("#message_body").val() == "") errorOutput += "- Please, write your message for the administrator.<br />";
		if (errorOutput == "") {
			return true;
		} else {
			showWarningDiv(errorOutput);
			return false;
		}
	});

	/* validate the form for ordering a distribution of your articles */
	$("#orderDistributionsForm").submit(function(event) {	
		var errorOutput = "";
		var buttonValue = $("#orderDistributionsForm #submit").val();
		$("#orderDistributionsForm #submit").val("Please, wait ...");
		
		/* simpler checks */
		if ($("input[name=type]:checked").val() == "") errorOuput += "- You need to choose a distribution type.<br />";
		if ($("#duration").val() == 0) errorOutput += "- Choose a desired distribution time.<br />";
		if ($("input#title").val() == "") errorOutput += "- Specify a title for this article.<br />";
		if ($("#category").val() == 0) errorOutput += "- Choose a category.<br />";
		if ($("#article").val() == "") errorOutput += "- Write the body of the article.<br />";
		if ($("#article_summary").val().length > 0 && $("#article_summary").val().length < 50) errorOutput += "- Your manually created article summary appears to be too short.<br />";
		if ($("#author_name").val() == "") errorOutput += "- Specify a valid Author Name.<br />";
		if ($("#bio_html").val() == "") errorOutput += "- Write the HTML version of About the Author box.<br />";
		if ($("#bio_plain").val() == "") errorOutput += "- Write the non-HTML version of About the Author box.<br />";
		if ($("#article_blog").val().length > 0 && $("#article_blog").val().length < 1200) errorOutput += "- The version we will be sending to blogs appears to be too short.<br />";
		
		/* more complicated checks */
		if (errorOutput == "") {
			$.ajax({
				url: "action/ajax-cp-validate-distribution",
				type: "post",
				data: { article: $("#article").val(), bio_html: $("#bio_html").val(), bio_plain: $("#bio_plain").val(), article_blog: $("#article_blog").val() },
				async: false,
				success: function(data) {
					if (substr_count(data, "article-length") != 0) errorOutput += "- The body of your article needs to have at least 400 words.<br />";
					if (substr_count(data, "bio_html-quotations") != 0) errorOutput += "- Your HTML version of About the Author box does not have the correct number of &quot; characters. Make sure you have closed all &lt;a href=&quot;URL HERE&quot;&gt; tags.<br />";
					if (substr_count(data, "bio_html-links") != 0) errorOutput += "- Your HTML version of About the Author box contains more than 3 links.<br />";
					if (substr_count(data, "bio_plain-links") != 0) errorOutput += "- Your non-HTML version of About the Author box contains more than 3 links.<br />";
					if (substr_count(data, "article_blog-links") != 0) errorOutput += "- Your Blog Version of the article contains more than 3 links.<br />";
					if (substr_count(data, "empty-article") != 0) errorOutput += "- Write the body of the article for all unique versions.<br />";
					if (substr_count(data, "empty-bio_html") != 0) errorOutput += "- Write the HTML version of About the Author box for all unique versions.<br />";
					if (substr_count(data, "empty-bio_plain") != 0) errorOutput += "- Write the non-HTML version of About the Author box for all unique versions.<br />"
				}
			}); 
			
			if (errorOutput == "") {
				return true;		
			} else {
				$("#orderDistributionsForm #submit").val(buttonValue);
				showWarningDiv(errorOutput);
				return false;
			}
		} else {
			$("#orderDistributionsForm #submit").val(buttonValue);
			showWarningDiv(errorOutput);
			return false;
		}
	});
	
	/* validate the form for editing Complete Service articles */
	$("#editCSArticlesForm").submit(function(event) {	
		errorOutput = "";
		if ($("input#title").val() == "") errorOutput += "- Specify a title for this article.<br />";
		if ($("#article").val() == "") errorOutput += "- Please, write the body of the article.<br />";
		if (substr_count($("#article").val(), " ") < 350) errorOutput += "- The body of your article needs to have at least 400 words.<br />";
		if (errorOutput == "") {
			return true;
		} else {
			showWarningDiv(errorOutput);
			return false;
		}
	});
	
	/* validate the form for ordering articles */
	$("#orderArticlesForm").submit(function(event) {	
		errorOutput = "";
		if ($("input[name=num_articles]:checked").val() == "") errorOuput += "- You need to choose how many articles you want.<br />";
		if ($("input#keywords").val() == "") errorOutput += "- Tell us at least a few keywords.<br />";
		if (errorOutput == "") {
			return true;
		} else {
			showWarningDiv(errorOutput);
			return false;
		}
	});
	
	/* validate the form for submitting information about Complete Service */
	$("#completeServiceForm").submit(function(event) {	
		errorOutput = "";
		if ($("input#website_url").val() == "") errorOutput += "- Please, tell us your website's URL.<br />";
		if ($("input#keywords").val() == "") errorOutput += "- Tell us at least a few keywords.<br />";
		if (errorOutput == "") {
			return true;
		} else {
			showWarningDiv(errorOutput);
			return false;
		}
	});
	
	/* filter orders by external order number in the admin panel */
	$("input#filter_order_number").focus(function() {
		$("input#filter_order_number").val("");
	});
	$("input#filter_order_number").blur(function() {
		var filter = $("input#filter_order_number").val();
		if (filter == "") {
			$("input#filter_order_number").val("external order number");
		} else {
			$("input#filter_order_number").val("... loading ...");
			$.post("action/ajax-admin-filter-external-order-number", 
				{ number: filter }, 
				function(data) {
					$("input#filter_order_number").val(filter);
					showOptionsDiv("Matching orders:", data, -34);
				}
			);
		}
	});

	/* display a message in the admin panel */
	$("a.readMessage").click(function(event) {
		event.preventDefault();
		var message_id = $(this).attr("rel");
		$.post("action/ajax-admin-display-message", 
			{ mid: message_id }, 
			function(data) {		
				showOptionsDiv("Message", data, 100);
			}
		);
	});
	
	/* send a message to any user from the list of users in the admin panel */
	$("a.sendMessage").click(function(event) {
		event.preventDefault();
		var user_id = $(this).attr("rel");
		$.post("action/ajax-admin-send-message", 
			{ uid: user_id }, 
			function(data) {		
				showOptionsDiv("Send a Message", data, 100);
			}
		);
	});
	
	/* choose a distribution way as our partner */
	$("a.distributionWay").click(function(event) {
		event.preventDefault();
		$("div.distributionWayOptions").slideUp("fast");
		var way = $(this).attr("rel");
		if (way == "RSSfeed") {
			$("div#distributionWayScript input#way").val("post");
			$("div#distributionWayRSSfeed").slideDown("slow");
		} else if (way == "ScriptPost") {
			$("td#tdMethodType").html("POST");
			$("div#distributionWayScript input#way").val("post");
			$("div#distributionWayScript").slideDown("slow");
		} else if (way == "ScriptGet") {
			$("td#tdMethodType").html("GET");
			$("div#distributionWayScript input#way").val("get");
			$("div#distributionWayScript").slideDown("slow");
		} else if (way == "Email") {
			$("div#distributionWayScript input#way").val("email");
			$("div#distributionWayEmail").slideDown("slow");	
		}
	});
	
});

function showWarningDiv(errorOutput) {
	$("div#warningDivContent").html(errorOutput);
	$("div.warningDivWrapper").css("top", (mouseY - 150) + "px");
	$("div.warningDivWrapper").fadeIn("slow");
}

function showOptionsDiv(divTop, divContent, distance) {
	$("div#optionsDivTop").html(divTop);
	$("div#optionsDivContent").html(divContent);
	$("div.optionsDivWrapper").css("top", (mouseY - distance) + "px");
	$("div.optionsDivWrapper").fadeIn("slow");
	initializeListeners();
}

/* initialize all listeners - this function can be called even after insertion of HTML code via AJAX */
function initializeListeners() {
	
	$("a.showTutorialVideo").unbind("click").click(function(event) {
		var dimensions = getWindowSize();
		if (dimensions[0] > 1100 && dimensions[1] > 650) {
			event.preventDefault();
			$("iframe#tutorialIframe").attr("src", "SpinDistributeVideoTutorial/SpinDistributeVideoTutorial.html");
			$("div#tutorialHolder").css("top", (mouseY - 400) + "px"); 
			$("div#tutorialHolder").css("left", ((dimensions[0] - 1010) / 2) + "px");
			$("div#tutorialHolder").slideDown("slow");
			$("div#rightBox").slideUp("fast");
		} else {
			return true;
		}
	});
	
	$("a.closeTutorialVideo").unbind("click").click(function(event) {
		event.preventDefault();
		$("iframe#tutorialIframe").attr("src", "SpinDistributeVideoTutorial/SpinDistributeVideoTutorial-Blank.html");
		$("div#tutorialHolder").slideUp("slow");
		$("div#rightBox").slideDown("fast");	
	});
	
	/* show and hide hidden divs */
	$("a.toggleHiddenDiv").unbind("click").click(function(event) {
		event.preventDefault();
		var field = $(this).attr("rel");		// get rel of the appropriate DIV
		if (field == "") {
			if ($("div.hiddenDiv").css("display") == "none") {
				$("div.hiddenDiv").slideDown("slow");
			} else {
				$("div.hiddenDiv").slideUp("slow");
			}
		} else {
			if ($("div.hiddenDiv[rel='" + field + "']").css("display") == "none") {
				$("div.hiddenDiv[rel='" + field + "']").slideDown("slow");
			} else {
				$("div.hiddenDiv[rel='" + field + "']").slideUp("slow");
			}
		}
	});
	
	/* hide the warning div */
	$("a.warningDivClose").unbind("click").click(function(event) {
		event.preventDefault();
		$("div.warningDivWrapper").fadeOut("slow");
	});
	
	/* hide the explanation div */
	$("a.explanationDivClose").unbind("click").click(function(event) {
		event.preventDefault();
		$("div.explanationDivWrapper").fadeOut("slow");
	});
	
	/* hide the additional options div */
	$("a.optionsDivClose").unbind("click").click(function(event) {
		event.preventDefault();
		$("div.optionsDivWrapper").fadeOut("slow");
	});
	
	/* show the contact & live support options in the Control Panel */
	$("a.supportContentLink").unbind("click").click(function(event) {
		event.preventDefault();
		$.post("action/ajax-cp-support", 
			{ action: "showAdditionalOptions" }, 
			function(data) {		
				showOptionsDiv("Contact and Live Support", data, 250);
			}
		);
	});
	
	/* connect to the Live Support Department */
	$("a.liveSupportLink").unbind("click").click(function(event) {
		event.preventDefault();
		$("div#liveSupportDiv").html("Connecting ...").slideDown("slow").css("border-right-width", "5px").animate({ borderRightWidth:"100px" }, 6000);
		$.post("action/ajax-cp-support", 
			{ action: "connect" }, 
			function(data) {		
				$("div#liveSupportDiv").css("border-right-width", "1px");
				$("div.optionsDivWrapper").animate({ top:"80px" }, 2000);
				$("div#liveSupportDiv").html(data);
			}
		);
	});

}

function substr_count(haystack, needle, offset, length) {
    var pos = 0;
	var cnt = 0;
    haystack += "";
    needle += "";
    if (isNaN(offset)) offset = 0;
    if (isNaN(length)) length = 0;
    offset--;
 
    while ((offset = haystack.indexOf(needle, offset + 1)) != -1) {
        if (length > 0 && (offset + needle.length) > length) {
            return false;
        } else {
            cnt++;
        }
    }
 
    return cnt;
}

function getWindowSize() {
	var dimensions = new Array(2);
	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;
	}
	dimensions[0] = myWidth;
	dimensions[1] = myHeight;
	return dimensions;
}

function preventTimeout() {
	$.post("action/ajax-prevent-session-timeout", 
		{ prevent: "prevent" }, 
		function(data) {		
			timeout_prevented = true;
		}
	);	
}