
$(function() {
	updateIconLink();
	updateBCTFontSize();
	initProfiles();
	if(typeof(isMobile) == 'function' && !isMobile()){
		setTimeout(showSubscribeBox, 3000);
	}
});

// Update vip, ec, mag icons to link to /Other-Teen-Ink-Goodies/Badge-Legend
function updateIconLink(){
	function popup(){
		window.open('/Other-Teen-Ink-Goodies/Badge-Legend');
	}
	$("img.vip-icon").css('cursor', 'pointer');
	$("img.mag-icon").css('cursor', 'pointer');
	$("img.editors-choice").css('cursor', 'pointer');
	$("img.vip-icon").click(popup);
	$("img.mag-icon").click(popup);
	$("img.editors-choice").click(popup);
}

function updateBCTFontSize(){
	$(".book_cover_preview_title").each(function(index) {
		var jthis = $(this);
		var len = jthis.text().length;
		if(len==0)
			return;
		var oclass = "long_title";
		if(len <= 10) {
			oclass = "short_title";
		} else if(len <= 20) {
			oclass = "medium_title";
		}
		jthis.addClass(oclass);     
	});
}

function initProfiles(){
	var boxy = null;
	function showBox(){
		if(boxy)boxy.show();
	};
	function hideBox(){
		if(boxy)boxy.unload();
	};
	function checkMouse(event){
		if(boxy){
			var cos = boxy.getPosition();
			var size = boxy.getSize();
			var sx = cos[0];
			var sy = cos[1];
			var ex = sx + size[0];
			var ey = sy + size[1];
			var mX = event.x ? event.x : event.pageX; 
			var mY = event.y ? event.y : event.pageY; 
			if(mX < sx || mX > ex || mY < sy || mY > ey){
				setTimeout(hideBox, 300);
				return false;
			}
			return true;
		}
	};
	$('.user-profile a').mouseover(function(){
		hideBox();
		var pos = $(this).offset();
		var userid = $(this).attr('userid');
		var headertext = $(this).text();
		var nextNode = $(this).next();
		if(nextNode.hasClass('vip-icon')){
			headertext += '<img class="profile-box-vip" src="/images/icon_VIP_big.gif" alt="VIP" title="' + nextNode.attr('title') + '"/>';
		}
		boxy = new Boxy($('<img>').attr('src', '/images/loading.gif'), {
			fixed: false,
			cache: false,
			borderWidth: 0,
			title: headertext,
			unloadOnHide: true,
			show: false,
			draggable: false,
			closeText: '<img src="/images/boxy_close.png" alt="X" />'
		});
		
		$(boxy.boxy).mouseout(function(event){
			checkMouse(event);
		});
		$(this).mouseout(function(event){
			checkMouse(event);
		});
		boxy.moveTo(pos.left, pos.top + $(this).height());
		setTimeout(showBox, 300);
		$.ajax({
			url: "/userprofile.php?id=" + userid,
			dataType: "html",
			success: function (b) {
				boxy.setContent(b);
			},
			error: function () {
				alert("Oops... please check your internet connection.")
			}
		})
	});
	
}

function showSubscribeBox(){
	var visit_count = getCookie('visit_count');
	var visited = getCookie('visited');
	if(!visit_count)setCookie('visit_count', '1', 60);
	else if(visit_count > 1 || visited)return;
	else setCookie('visit_count', '2', 60);
	setCookie('visited', 'visited');
	$('#subscribe_box').animate({height:215}, 500, function(){
		$('#subscribe_box a.close').click(function(event){
			$('#subscribe_box').animate({height:0}, 500);
		});
	});
}

function setCookie(name, value, expired){ // expired in days    
	if(expired){
		var exp  = new Date();
		exp.setTime(exp.getTime() + expired * 24 * 60 * 60 * 1000);
		document.cookie = name + "="+ escape(value) +";expires="+ exp.toGMTString()+';path=/';
	}else{
		document.cookie = name + "="+ escape(value) +";path=/";
	}
}

function getCookie(name){
	var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
	if(arr != null) return unescape(arr[2]); 
	return null;
}

function deleteCookie(name){
	var exp = new Date();
	exp.setTime(exp.getTime() - 1);
	var cval = getCookie(name);
	if(cval!=null) document.cookie=name +"="+cval+";expires="+exp.toGMTString();
}

function showAds(area, url, ids, target)
{
	var areaAds = {
		'right_top' : ['dirB_300x250.gif','300x250_5677928_D4G2012_bnr_lopez_01.gif','300x250_5677928_D4G2012_bnr_lopez_02.gif','300x250_5677928_D4G2012_bnr_lopez_03.gif']
};
	var ads = areaAds[area];
	var ad = ads[0];
	switch(typeof(ids))
	{
		case 'number':
			if(ads[ids]) ad = ads[ids];
			break;
		case 'object':
			var tmp = [];
			if(ids.length)
			{
				for(var i in ids)
					if(ads[ids[i]])	tmp.push(ads[ids[i]])
			}
			else
				tmp = ads;
			if(tmp.length)
			{
				var n = Math.round(Math.random()*100) % (tmp.length);
				ad = tmp[n];
			}
			break;
	}
	document.write('<a href="'+url+'"'
		+ (target ? 'target="_blank"' :'')
		+ '><img src="/media/'+ad+'">'
		+ '<img src="http://ad.doubleclick.net/ad/N5295.795834.TEENINK.COM/B6249707;sz=1x1;ord=' + Math.random() + '?" style="height:1px; width:1px; border:none;" alt="Advertisement" />'
		+ '</a>');
}
