var slider_timer;
var slidercount = 0;
var sliderpos = 0;
var okaytoautomove = true;
var okaytoautoplay = true;
var ipaused = false;

function highlight_link(link_id) {
	jQuery(".navheigh").removeClass("navheigh");
	
	jQuery("#" + link_id).each(function () {
		jQuery(this).addClass("navheigh");
		var toffset = jQuery(this).position();
		var newtop = toffset.top * (-1);
		var newleft = toffset.left * (-1);
		jQuery(this).css({
			'backgroundPosition':newleft +'px '+ newtop +'px'
		});
	});
}

function resize_other() {
	
}

function resize_sidebar() {
	jQuery("#tcontent").removeAttr("style");
	jQuery("#metaspace-content").removeAttr("style");
	
	var restheight = parseInt(jQuery("#metaspace-head").innerHeight()) + parseInt(jQuery("#metaspace-foot").innerHeight());
	var innerheight = jQuery("#metaspace-content").innerHeight();
	var rightside = jQuery("#indexcontainer").innerHeight();
	
	if (restheight + innerheight >= rightside) {
		var newinner = rightside + restheight + 40;
		jQuery("#tcontent").css('height', newinner +'px');
	} else {
		var newinner = rightside - restheight - 14;
		jQuery("#metaspace-content").css('height', newinner +'px');
	}
	
}

function changeslide(gotopos) {
	if (jQuery("#projector:animated").length >= 1 || jQuery("#forslider tr:nth-child("+ gotopos +") td").length <= 0) {
		return;
	}
	
	var outhtml = "";
	if (jQuery("#forslider tr:nth-child("+ gotopos +") td img:not(.alignnone)").length > 0) {
		var imgtag = "";
		var resthtml = jQuery("#forslider tr:nth-child("+ gotopos +") td").html();
		outhtml = '<div id="halfprojectorimg"></div><div id="halfprojector">' + resthtml + '</div>';
	} else {
		var resthtml = jQuery("#forslider tr:nth-child("+ gotopos +") td").html();
		outhtml = '<div id="fullprojector">' + resthtml + '</div>';
	}
	
	jQuery("#projector").animate( {
	    opacity: 0.0,
	    left: 60
	   },500,function () {
		   sliderpos = gotopos;
		   
		   if (sliderpos == slidercount) {
			   jQuery("#moveforw").hide();
		   } else {
			   jQuery("#moveforw").show();
		   }
		   
		   if (sliderpos == 1) {
			   jQuery("#moveback").hide();
		   } else {
			   jQuery("#moveback").show();
		   }
		   
		   jQuery(".sliderbull").removeClass("fullbull");
		   jQuery(".sliderbull:nth-child("+ gotopos +")").addClass("fullbull");
		   jQuery(this).html(outhtml);
		   jQuery("#halfprojectorimg").append(jQuery("#halfprojector img").first());
		   jQuery("#halfprojector img").remove();
		   jQuery(this).animate({
			   opacity: 1.0,
			   left: 80
		   });
	   }
	);
}

function autoslide() {
	if (!okaytoautomove) return;
	var newpos = sliderpos + 1;
	if (newpos > slidercount) {
		newpos = 1;
	}
	changeslide(newpos);
}

function moveslide(direction) {
	changeslide((sliderpos + direction));
}

var autoplay_element = '';

function registerforscroll(elementid) {
	autoplay_element = elementid;
	jQuery(window).scroll(function () {
		onflashscroll();
	});
}

var autoplaytimer = '';
var countdownnum = 11;

function playercountdown(elementid) {
	var player = document.getElementById(""+ (elementid) + "");
	var state_play = player.getPlayerState();
	if (state_play == 1 || state_play == 2 || state_play == 3 || state_play == 0) {
		count_timer_stop();
	}
	countdownnum--;
	var outputhtml = 'Auto-playback<br />video starts in:<div class="counternum">' + countdownnum + '</div><form action="#" method="get"><input type="button" value="cancel" onclick="hardcancel()" /></form>';
	var movie_pos = jQuery("#"+ elementid).offset();
	var movie_width = parseInt(jQuery("#"+ elementid).attr("width"));
	var counter_pos_x = movie_pos.left + movie_width;
	var counter_pos_y = movie_pos.top + 20;
	
	jQuery("#fcountdown").each(function () {
		jQuery(this).css({
			'left': counter_pos_x +'px',
			'top': counter_pos_y + 'px'
		});
		jQuery(this).html(outputhtml);
		jQuery(this).show();
	});
	if (countdownnum <= 0) {
		count_timer_stop();
		player.playVideo();
	}
}

function hardcancel() {
	if (document.cookie) {
		var expiresin = new Date();
		var newxtday = expiresin.getTime() + (24 * 60 * 60 * 1000);
		expiresin.setTime(newxtday);
		document.cookie = "autoplay=noautoplay; expires=" + expiresin.toGMTString();
	}
	count_timer_stop();
}

function count_timer_stop() {
	okaytoautoplay = false;
	countdownnum = 11;
	window.clearInterval(autoplaytimer);
	autoplaytimer = '';
	jQuery("#fcountdown").hide();
	ipaused = false;
}

function onflashscroll() {
	if ('undefined' == typeof jQuery("#"+ autoplay_element).attr("height")) return;
	var player = document.getElementById(""+ (autoplay_element) + "");
	
	var targetpos = jQuery("#"+ autoplay_element).offset();
	var targetheight = jQuery("#"+ autoplay_element).attr("height");
	var windowheight = parseInt(jQuery(window).height());
	var scrollpos = parseInt(jQuery(window).scrollTop());
	var visarea = scrollpos + windowheight;
	var targetbot = parseInt(targetpos.top) + parseInt(targetheight);
	
	if (scrollpos < targetpos.top && visarea > targetbot) {
		// Player visible
		
		if (ipaused) {
			player.playVideo();
			ipaused = false;
		}
		
		if ('string' == typeof autoplaytimer && okaytoautoplay) {
			countdownnum = 11;
			autoplaytimer = window.setInterval("playercountdown('"+ autoplay_element +"')", 1000);
		}
	} else {
		var state_play = player.getPlayerState();
		if (state_play == 1) {
			player.pauseVideo();
			ipaused = true;
		} else {
			if ('string' != typeof autoplaytimer) {
				count_timer_stop();
			}
		}
	}
}

function onYouTubePlayerReady(callingid) {
	if ("undefined" == typeof document.getElementById(""+ (callingid) +"") ) return;
	var player = document.getElementById(""+ (callingid) +"");
	player.addEventListener("onStateChange", "onytplayerStateChange");
	
}

function onytplayerStateChange(newState) {
	if (newState == 3) {
		count_timer_stop();
	}
}


function addyoutubeobts(youtubeurl,playerobjectid) {
	return youtubeurl + '&feature=player_embedded&version=3?color1=0xf0e0f2&color2=0xf0e0f2&fs=1&hd=1&feature=player_embedded&enablejsapi=1&playerapiid='+ playerobjectid +'';
}

jQuery(document).ready(function () {
	if (document.cookie) {
		var wcookie = document.cookie
		okaytoautoplay = wcookie.indexOf("noautoplay") == -1;
	} else {
		var expiresin = new Date();
		var newxtday = expiresin.getTime() + (24 * 60 * 60 * 1000);
		expiresin.setTime(newxtday);
		document.cookie = "autoplay=yesautoplay; expires=" + expiresin.toGMTString();
	}

	
	jQuery(".addthis_button_facebook_like").attr("fb:like:layout","button_count");
	jQuery("#topfacebook .addthis_button_facebook_like").attr("fb:like:href","http://www.facebook.com/vstarsss");
	
	
	if (jQuery("#navigation li.current_page_item").length >= 1) {
		var hightlightid = jQuery("#navigation li.current_page_item").attr("id");
		highlight_link(hightlightid);
	} else {
		if (jQuery("#navigation li.current_page_parent").length >= 1) {
			var hightlightid = jQuery("#navigation li.current_page_parent").attr("id");
			highlight_link(hightlightid);
		}
	}
	
	jQuery("#tcontent a[href^=\"http://\"]:not([href*=\"vanae.com\"]), .comment-body a[href^=\"http://\"]:not([href*=\"vanae.com\"])").each(function () {
		var marklink = true;
		if (jQuery(this).parent("h1, h2, h3").length >= 1) marklink = false;
		if (jQuery(this).children("img").length >= 1) marklink = false;
		if (marklink) {
			jQuery(this).addClass("lexternal");
		}
		jQuery(this).attr("target","_blank");
	});
	
	var flashcount = 0;
	jQuery("#tcontent object, #footcolums object").each(function () {
		if (jQuery(this).children("embed[src^=\"http://www.youtube\"]").length == 0) {
			return;
		}
		
		flashcount++;
		if ('undefined' != typeof jQuery(this).parent().attr("id")) {
			flashcount++;
			var flashparent = "flashparent"+ flashcount;
			jQuery(this).parent().attr("id",flashparent);
		} else {
			flashparent = jQuery(this).parent().attr("id");
		}
		var tid = "vflash"+ flashcount;
		var flashsrc = jQuery(this).children("embed").first().attr("src");
		var flash_height = jQuery(this).children("embed").first().attr("height");
		var flash_width = jQuery(this).children("embed").first().attr("width");
		
		// Lets Flash from another domain call JavaScript
		var params = {
				allowScriptAccess: "always",
				allowfullscreen: "true"
				};
		// The element id of the Flash embed
		var atts = { id: tid };
		// All of the magic handled by SWFObject (http://code.google.com/p/swfobject/)
		swfobject.embedSWF(addyoutubeobts(flashsrc,tid),
	                     flashparent, flash_width, flash_height, "8", null, null, params, atts);
		
		if (flashcount == 2 && okaytoautoplay) {
			registerforscroll(tid);
		}
	
	});
	
//	if (jQuery("#tcontent object").length >= 1) {
//		var flashamount = 0;
//		jQuery("#tcontent object").each(function () {
//			flashamount++;
//			var flashid = "vflash"+ flashamount;
//			jQuery(this).attr("id",flashid);
//			swfobject.registerObject(flashid, "8.0.0");
//		});
//		jQuery("#tcontent param[name=\"movie\"]").each(function () {
//			var getaddedtubeobts = jQuery(this).attr("value");
//			var parent_id = jQuery(this).parent().attr("id");
//			jQuery(this).attr("value",addyoutubeobts(getaddedtubeobts,parent_id));
//		});
//	
//		jQuery("#tcontent embed").each(function () {
//			var getaddedtubeobts = jQuery(this).attr("src");
//			var parent_id = jQuery(this).parent().attr("id");
//			jQuery(this).attr("src",addyoutubeobts(getaddedtubeobts,parent_id));
//		});
//	}
	
	jQuery(".indexedpost").each(function () {
		var outurl = purl + "/free-dating-advice/";
		
		if (jQuery(this).hasClass("vlife")) {
			outurl = purl + "/category/vs-life/";
		}
		
		if (jQuery(this).hasClass("dating")) {
			outurl = purl + "/category/tips-dating/";
		}
		
		if (jQuery(this).hasClass("self")) {
			outurl = purl + "/category/selfempowerment/";
		}
		
		if (jQuery(this).hasClass("sex")) {
			outurl = purl + "/category/sex/";
		}
		
		if (jQuery(this).hasClass("wednesdae")) {
			outurl = purl + "/category/game-day/";
		}
		
		if (jQuery(this).hasClass("askvan")) {
			outurl = purl + "/category/ask-vanae-fridae/";
		}
		
		if (jQuery(this).hasClass("thursdae")) {
			outurl = purl + "/category/vchat-thursdae/";
		}
		
		if (jQuery(this).hasClass("comshow")) {
			outurl = purl + "/category/vanaecom-show/";
		}
		
		var linkhtml = '<a class="categoryicon" href="'+ outurl +'"></a>';
		jQuery(this).append(linkhtml);
	});
	
	jQuery("a img").parent().each(function () {
		jQuery(this).css("border","none");
	});
	
	
	
	jQuery("#moveforw").click(function () {
		moveslide(1);
	});
	
	 jQuery("#moveback").click(function () {
		 moveslide(-1);
	 });
	
	jQuery(".standardval").focus(function () {
		jQuery(this).removeClass("standardval");
		jQuery(this).unbind("focus");
		jQuery(this).val("");
	});
	
	jQuery("#indexcontainer .alignnone").parent().parent("p").addClass("alinonep");
	jQuery("#indexcontainer .alignnone").parent("p").addClass("alinonep");
	
	if (jQuery("#firstcommentlist").length == 1) {
		var lastelement_pos = jQuery("#firstcommentlist li:last").position();
		jQuery("#firstcommentlist").scrollTop(lastelement_pos.top);
	}
	
	jQuery(".restcomment").each(function () {
		var newwidth = jQuery(this).parent().width() - 100;
		jQuery(this).css('width',newwidth +'px');
	});
	
	
	jQuery("#navigation li").mouseenter(function () {
		var tid = jQuery(this).attr('id');
		highlight_link(tid);
	});
	
	if ('undefined' != typeof hightlightid) {
		jQuery("#navigation").mouseleave(function () {
			highlight_link(hightlightid);
		});
	}
	
	if (jQuery("#metaspace-content").length == 1) {
		resize_sidebar();
		var doit = window.setTimeout("resize_sidebar()", 2000);
	}
	
	if (jQuery("#forslider").length == 1) {
		var forcount = "";
		var sliderwidth = 0;
		jQuery("#forslider tr td").each(function () {
			slidercount++;
			sliderwidth += 21;
			forcount += '<a id="changeslide'+ slidercount +'" class="sliderbull" href="javascript:changeslide('+ slidercount +')"></a>';
		});
		jQuery("#slidercount").html(forcount);
		var newmargin = Math.floor(sliderwidth / 2);
		jQuery("#slidercount").css({
			'width':sliderwidth +'px',
			'marginLeft':'-'+ newmargin +'px'
		});
		changeslide(1);
		slider_timer = window.setInterval("autoslide()", 20000);
		
		jQuery("#innerslider").mouseenter(function () {
			okaytoautomove = false;
		});
		jQuery("#innerslider").mouseleave(function () {
			okaytoautomove = true;
		});
	}
	
});
