﻿function limitChars(textarea, limit, infodiv) {
	var returnVal = true;
	var text = document.getElementById(textarea).value;
	var info = document.getElementById(infodiv);
	var infoText = info.innerHTML;
	var textlength = text.length;
	if (textlength > limit) {
		document.getElementById(textarea).value = text.substr(0, limit);
		textlength = document.getElementById(textarea).value.length;
		returnVal = false;
	}
	infoText = infoText.replace(/\d+/, (textlength));
	info.innerHTML = infoText;
	return returnVal;
}

function InitReadMore(readmoretext) {
	$('.text').each(function () {
		var maxtextlength = 1000;
		var t = $(this);
		if (t.hasClass('readmoreinit')) {
			return;
		}
		t.addClass('readmoreinit');
		var l = t.text().length;
		if (l > maxtextlength) {
			var preview = t.html().substr(0, maxtextlength);
			var therest = t.html().substr(maxtextlength);
			//check for incomplete html tags...
			var checkstring = preview.replace(/[^<>]/g, '').replace(/<>/g, '');
			if (checkstring != '') {
				maxtextlength = preview.lastIndexOf('<');
				preview = t.html().substr(0, maxtextlength);
				therest = t.html().substr(maxtextlength);
			}
			t.html(preview + ' ... <a href="#" class="moretextbutton">' + readmoretext + '</a>');
		}
		$('.moretextbutton', t).click(function (e) {
			e.preventDefault();
			e.stopPropagation();
			$(this).hide();
			t.html(preview + therest);
		});
	});
}

$(function () {
	//BEGIN: menu
	if ($('#header').length == 1) {
		$("#header ul > li").hover(function () {
			var box = $('#wrapper');
			var t = $(this);
			t.addClass('hover');
			var parent = t.children("ul");
			parent.stop(true, true).fadeIn("fast");
			if (parent.offset() != null) {
				if ((parent.offset().left + parent.width()) > (box.offset().left + box.width())) {
					parent.css({ right: 0 });
				}
			}
		}, function () {
			var t = $(this);
			t.removeClass('hover');
			t.children("ul").stop(true, true).fadeOut("fast");
		});
	}
	//END: menu

	//BEGIN: homepage hovers
	if ($('.outer').length == 1) {
		var innerHoverOn = false;
		function DetectCollision(parent, dialog, my, at) {
			var box;
			box = $(".outer");
			var boxboundaries = { left: box.offset().left, top: box.offset().top, right: box.offset().left + box.width(), bottom: box.offset().top + box.height() };
			var dialogboundaries = { left: parent.offset().left, top: parent.offset().top, right: parent.offset().left + parent.width() + dialog.width(), bottom: parent.offset().top + parent.height() + dialog.height() };
			if (dialogboundaries.left < boxboundaries.left) {
				//alert("dialog is too left");
				my = my.replace(/right/, "left");
				at = at.replace(/left/, "right");
			}
			if (dialogboundaries.top < boxboundaries.top) {
				//alert("dialog is too high");
				my = my.replace(/bottom/, "top");
				at = at.replace(/bottom/, "top");
			}
			if (dialogboundaries.right > boxboundaries.right) {
				//alert("dialog is too right");
				my = my.replace(/left/, "right");
				at = at.replace(/right/, "left");
			}
			if (dialogboundaries.bottom > boxboundaries.bottom) {
				//alert("dialog is too low");
				my = my.replace(/top/, "bottom");
				at = at.replace(/top/, "bottom");
			}
			dialog.position({ my: my, at: at, of: parent });
		}
		$(".opaque .inner").hover(function () {
			innerHoverOn = true;
			var t = $(this);
			var p = t.parent('.opaque');
			p.css('zIndex', 5000);
			var o = t.children(".dialog");
			var my, at;
			my = "left top";
			at = "right top";
			o.stop(true, true).show().position({ my: my, at: at, of: t.parent(), collision: "fit" });
			DetectCollision(p, o, my, at);
		},
		function () {
			innerHoverOn = false;
			var t = $(this);
			t.parent('.opaque').css('zIndex', 1);
			t.children(".dialog").stop(true, true).hide();
		});
		var offsetsensitivity = 100;
		$(document).bind('mousemove', function (e) {
			$(".opaque .inner").each(function () {
				var t = $(this);
				if ((Math.abs(e.pageX - t.offset().left - Math.ceil(t.width() / 2)) < offsetsensitivity) && (Math.abs(e.pageY - t.offset().top - Math.ceil(t.height() / 2)) < offsetsensitivity)) {
					t.addClass("inner_hover");
				} else if (!innerHoverOn) {
					t.removeClass("inner_hover");
				}
			});
		});
	}
	//END: homepage hovers

	//BEGIN: featured stories
	$("#featured .dialog .close").live({
		click: function (event) {
			event.stopPropagation();
			event.preventDefault();
			$(this).parent().hide(0);
			$(this).parent().parent().removeClass("selected", 0);
		}
	});
	$("#featured .featuredstory").live({
		click: function () {
			var t = $(this);
			var dialog = t.children(".dialog");
			dialog.width(t.width() + 22);
			dialog.height(t.height() + 12);
			var step = dialog.height() / 2;
			$("#featured .dialog").hide();
			$("#featured .dialog").parent().removeClass("selected");
			t.addClass("selected");
			dialog.show();
			dialog.css('z-index', 1).position({
				my: "left top",
				to: "right top",
				of: dialog.parent(),
				offset: step + "px -" + step + "px",
				collision: "flip"
			});
			var parent = dialog.closest('#featured');
			var edge = parent.offset().left + parent.width();
			var right = dialog.offset().left + dialog.width();
			if (right > edge) {
				//reposition
				dialog.css('z-index', 1).position({
					my: "right top",
					to: "left top",
					of: dialog.parent(),
					offset: "-" + step + "px -" + step + "px",
					collision: "flip"
				});
			}
		}
	});
	//END: featured stories


	//BEGIN: search page
	$('#morebutton,#lessbutton').live('click', function (e) {
		e.preventDefault();
		if ($('#morecriteria').is(':visible')) {
			$(this).removeClass('selected');
			$('#morecriteria').hide();
			$('#morebutton').show();
			$('#lessbutton').hide();
		}
		else {
			$(this).addClass('selected');
			$('#morecriteria').show();
			$('#morebutton').hide();
			$('#lessbutton').show();
		}
	});
	$('#morebutton').show();
	$('#lessbutton').hide();
	//END: search page

	//BEGIN: staff page
	if ($('#staffpages').length == 1) {
		$('#staffpages').show();
		if ($('.Staff .slideshow').length > 0) {
			$('.Staff .slideshow').cycle({ fx: 'fade', random: 1 });
		}
	}
	//END: staff page

	//BEGIN: affiliates
	$('.page[id="Movies"], .page[id="PrivateLabels"], .page[id="MobileAffiliate"], .page[id="DesktopPublishing"], .page[id="CustomerService"], .page[id="Cloud"], .page[id="BoundBooks"]').live('pageshow', function (e) {
		SetupAffiliatePaging();
	});
	if ($('#affiliatetopiccontent').length == 1) {
		$('#affiliatetopiccontent li:not(:first)').hide();
		$('#affiliatetopics li a').click(function (e) {
			e.preventDefault();
			e.stopPropagation();
			$('#affiliatetopics li a').removeClass("selected");
			$('#affiliatetopiccontent li').hide();
			var i = $(this);
			$('#affiliatetopiccontent li.' + i.closest('li').attr('class')).show();
			i.addClass("selected");
		});
		SetupAffiliatePaging();
	}
	function SetupAffiliatePaging() {
		var sections = $('.affiliateDetail');
		sections.each(function () {
			var section = $(this);
			var showPageCount = section.hasClass('pagecount');
			var pages = section.children('.affiliatecontentpage');
			var totalpages = pages.length;
			if (pages.length > 1) {
				$('.affiliatecontentpage:not(:first-child)', section).hide();
				if ($('.paging', section).length === 0) {
					var pagingHTML = '<span class="paging">';
					if (showPageCount) {
						pagingHTML += '<span class="pagesCount">Page <span class="currentpage">[X]</span> of ' + totalpages + '</span>';
					}
					pagingHTML += '<span class="prev"><a href="#">previous</a></span><span class="next"><a href="#">next</a></span></span>';
					$('.affiliatecontentpage h3', section).each(function (i) { $(this).after(pagingHTML.replace('[X]', i + 1)); });
				}
			}
		});
		$('.affiliatecontentpage .paging .prev').live('click', function (e) {
			e.preventDefault();
			e.stopPropagation();
			var i = $(this);
			var page = i.closest('.affiliatecontentpage');
			if (page.prev('.affiliatecontentpage').length > 0) {
				page.prev('.affiliatecontentpage').show().siblings('.affiliatecontentpage').hide();
			} else {
				i.closest('.affiliateDetail').children('.affiliatecontentpage').hide().last().show();
			}
		});
		$('.affiliatecontentpage .paging .next').live('click', function (e) {
			e.preventDefault();
			e.stopPropagation();
			var i = $(this);
			var pages = i.closest('.affiliateDetail').children('.affiliatecontentpage').hide();
			var page = i.closest('.affiliatecontentpage');
			if (page.next('.affiliatecontentpage').length > 0) {
				page.next('.affiliatecontentpage').show();
			} else {
				pages.first().show();
			}
		});
	}
	//END: affiliates*/
});

function setCookie(c_name, value, exdays) { var exdate = new Date(); exdate.setDate(exdate.getDate() + exdays); var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString()); document.cookie = c_name + "=" + c_value; }
function getCookie(c_name) { var i, x, y, ARRcookies = document.cookie.split(";"); for (i = 0; i < ARRcookies.length; i++) { x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("=")); y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1); x = x.replace(/^\s+|\s+$/g, ""); if (x == c_name) { return unescape(y); } } }

