function getCursorXY(e)
{
	var ev = arguments[0] || window.event,
	origEl = ev.target || ev.srcElement;
	_x = (!IE) ? e.pageX : event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
	_y = (!IE) ? e.pageY : event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
	_x -= 290;
	_y -= 32;
}

//window.onload = init;
function init()
{
	if (window.Event)
	{
		if(!IE) document.addEventListener('click',getCursorXY,false);
	}
	document.onmousedown = getCursorXY;
}

function moveIt(obj, mvTop, mvLeft)
{
	obj.style.position = "absolute";
	obj.style.top = mvTop+'';
	obj.style.left = mvLeft+'';
}

function onBody()
{
	init();
	(function($) {
		//return;
		
		var flashvars = window.videoID ? {videoID:window.videoID} : {videoID:"-1"};
		$.embedSWF(
			window.templateURL+"/assets/swf/VTE_147_VIDEO.swf", "mast_header", "995", "470", "9.0.0",
			window.templateURL+"/assets/swf/expressInstall.swf", 
			/* flashvars */ flashvars,
			/* params */ {menu:"false", allowfullscreen:"true", wmode:"transparent"},
			/* attributes */ {id:"mast_header"}
		);
		
		$.embedSWF(
			window.templateURL+"/assets/swf/poll_module.swf", "weekly_poll_swf", "407", "355", "9.0.0",
			window.templateURL+"/assets/swf/expressInstall.swf",
			/* flashvars */ {url:""},
			/* params */ {menu:"false", wmode:"transparent"},
			/* attributes */ {id:"weekly_poll_swf"}
		);
		
	 })(swfobject);
	
	(function($) {
		
		$('.weekly_questions_tab_title')
		.click(function(e) {
			e.preventDefault();
			var $window = $(window);
			var scrollTop = $window.scrollTop();
			$('.weekly_questions_tab').removeClass('active_tab');
			$(this).parent().addClass('active_tab');
			$window.scrollTop(scrollTop);
		});
	
		$('.footer_tab_title')
		.click(function(e) {
			e.preventDefault();
			var $window = $(window);
			var scrollTop = $window.scrollTop();
			$('.footer_view_tab').removeClass('active_tab');
			$(this).parent().addClass('active_tab');
			$window.scrollTop(scrollTop);
		});
	
		$('#connect_share_overlay')
		.mouseleave(function(e) {
			$('#connect_share_overlay')
				.css({
					left: -9999,
					top: -9999
				});
		})
	
		$('.share_link.preview')
		.mouseenter(function(e) {
			$('#connect_share_overlay')
				.css({
					left: $(this).offset().left,
					top: $(this).offset().top
				});
		});
	
		$('#login_username')
		.bind({
			focus: function(e) {
				$this = $(this);
				var dattr_value = 'data-default-value';
				if (!$this.attr(dattr_value)) {
					$this.attr(dattr_value, $this.val());
				}
				if ($.trim($this.val()) == "" || $.trim($this.val()) == $this.attr(dattr_value)) {
					$this.val("");
				}
			},
			blur: function(e) {
				var dattr_value = 'data-default-value';
				var type = $this.attr('type');
				if ($.trim($this.val()) == "") {
					$this.val($this.attr(dattr_value));
				}
			}
		});
	
		$('#login_password')
		.bind({
			focus: function(e) {
				$this = $(this);
				var dattr_value = 'data-default-value';
				if (!$this.attr(dattr_value)) {
					$this.attr(dattr_value, $this.val());
					$real = $('#' + $this.attr('data-real-field'));
					$real.bind({
					   blur: jQuery.proxy($real_blur, $this)
					});
					$this.data('real_field', $real);
				}
				$this.hide();
				$this.data('real_field').show().focus();
				function $real_blur(e) {
				   if ($.trim($real.val()) == "") {
					   $real.hide();
					   $real.val("");
					   $(this).show();
				   }
				}
			}
		});
	
		// Display the default content when we leave the specific content/image area //
		$('#promo_section').bind
		({
			mouseout:function(e)
				{
					e.preventDefault();
					$('.active_context_content').removeClass('active_context_content');
					$('.default_context_content').addClass('active_context_content');
					$('#connect_share_secondary_promos').find('div').each(function()
						{
							var me = $(this);
							// console.log('[header.php][onBody][#promo_section][mouseout]', 'this.id:', this.id);
							if(me.hasClass('link_content_over'))
							{
								me.removeClass('link_content_over');
								me.addClass('link_content_up');
							}
						});
				}
		});
	
		// Make sure we can transfer from the image to the content //
		$('#promo_group').bind
		({
			mouseenter:function(e){ e.stopImmediatePropagation(); },
			mouseout:function(e){ e.stopImmediatePropagation(); }
		});
		
		// Trigger content on mouse over the target images //
		
		$('#promo_section').find('.context_content_trigger').bind(
			{
				mouseenter:function(e)
				{
					e.preventDefault();
					
					// console.log('[header.php][onBody][#connect_share_secondary_promos][mouseenter]', 'this.id:', this.id);
					
					// Check if this is not one of the roll-over images //
					if($(this).find('.promo_rollover').size() == 0)
					{
						$('.active_context_content').removeClass('active_context_content');
						$('.default_context_content').addClass('active_context_content');
						$('#connect_share_secondary_promos').find('div').each(function()
							{
								var me = $(this);
								// console.log('[header.php][onBody][#promo_section][mouseout]', 'this.id:', this.id);
								if(me.hasClass('link_content_over'))
								{
									me.removeClass('link_content_over');
									me.addClass('link_content_up');
								}
							});
					}
					else
					{
						// Activate roll-over images and context copy //
						var $content = $('#' + $(this).attr('rel'));
						var $parent = $content.parents('.has_context_content').eq(0);
						var $active_context_content = $parent.find('> .context_content').siblings('.active_context_content');
						$active_context_content.removeClass('active_context_content');
						$content.addClass('active_context_content');

						$('.promo_rollover', this).each(function()
						{
							var me = $(this);
							if(me.hasClass('link_content_up'))
							{
								me.removeClass('link_content_up');
								me.addClass('link_content_over');
							}
						});
						
						// Deactivate Siblings //
						$(this).parents('.connect_share_promo').find('.promo_rollover').not($('.promo_rollover', this)).each(function()
						{
							var me = $(this);
							console.log('[header.php][onBody][#connect_share_secondary_promos][mouseenter]', 'Siblings', 'this.id:', this.id);
							if(me.hasClass('link_content_over'))
							{
								me.removeClass('link_content_over');
								me.addClass('link_content_up');
							}
						});
					}
				}
			});
	})(jQuery);
}

function recordInboundLink(category, action) 
{
	  try {
	    var pageTracker=_gat._getTracker("UA-2550467-10");
	    pageTracker._trackEvent(category, action);
	  }catch(err){}
}

function recordOutboundLink(link, category, action) 
{
	  try {
	    var pageTracker=_gat._getTracker("UA-2550467-10");
	    pageTracker._trackEvent(category, action);
	    setTimeout('document.location = "' + link.href + '"', 100)
	  }catch(err){}
}

function registerShare()
{
	$.post('/sharing_add_entry.php', {content_id: _content_id, content_type: _content_type, uid: _uid, share_method: _share_method }, function(data) {
		 // alert("share registered..." + data);
		});		
}

function registerSurvey(content_id)
{
	var _result = $('input[name=survey_answer]:checked').val();
	
	$.post('/survey_add_entry.php', {uid: _uid, id: content_id, result: _result	}, function(data) {
		 // alert("survey registered..." + data);
		});			
}

function shareComplete(method)
{
	_share_method = method;
	//alert("shareComplete.." + " _share_method: " + _share_method + " content_type: "  + _content_type + " uid: " + _uid + " id: " + _content_id);
	registerShare();
}

function shareMethod(method)
{
	_share_method = method;
}

function sendQuestion(page)
{
	//alert("page: " + page);
	var _form = $('weekly_questions');
	//alert("_form: " + _form);
	var _question = $('textarea[name=audience_question]').val();
	//alert("_question: " + _question);
	$("#weekly_questions").attr("action", page);
	//$("weekly_questions").submit();
	//alert("_form: " + $("#weekly_questions").attr("action"));
	//$.post('/vtech/staygrounded/curl_test.php', {content: _question }, function(data) {
		 //	 alert("survey registered..." + data);
		 //	 $("#weekly_questions").submit();
		//});

	$("#weekly_questions").submit();
}

function toggleShare(id, type)
{
	if(shareVisible == false)
	{
		toggleShareOn(id, type);
	}
	else
	{
		toggleShareOff();
	}
}

function toggleShareOff()
{
	shareVisible = false;
	document.getElementById("shareContainer").style.display = "none";
}

function toggleShareOn(id, type)
{
	_content_id		= id;
	_content_type	= type;
	
	//registerShare();
	
	shareVisible = true;

	var flashvars	= {shareURL: window.wpurl+"/?id=" + id, loggedIn: _loddedIN};
	if(type == "video")
	{
		_x += 249 ;
		var flashvars	= {shareURL: window.wpurl+"/?videoID=" + id, loggedIn: _loddedIN};
	}
	
	document.getElementById("shareContainer").style.left	= _x + "px";
	document.getElementById("shareContainer").style.top		= _y + "px";
	document.getElementById("shareContainer").style.display = "block";
	
	///var flashvars	= {shareURL: "<?php bloginfo('wpurl'); ?>/videoID=" + id, loggedIn: false};
	var params		= {scale: "noscale", salign: "TC", allowScriptAccess: "always", allowFullScreen: "true", bgcolor: "#000000", quality: "HIGH", wmode: "transparent"};
	var attributes	= {id: "shareSWF"};

	//alert("toggling share...");
	
	swfobject.embedSWF("/wp-content/themes/staygrounded/assets/swf/VTE_SHARE.swf", attributes.id, "330", "204", "9.0.0", "_swf/expressInstall.swf", flashvars, params, attributes);

	//alert("toggling share...2 ");
}
