var gameStarted = false;


$(document).ready
(
	
	function()
	{
		$('a[href^="http://"]').attr("target", "_blank");
		$('a[href$=".pdf"]').attr("target", "_blank");//open pdfs in a new window
		$('.start_rat').css('display','block');
		$('div.snippet p.set_print_option').show();
		$('p.poll input.no').replaceWith('<a href="#" class="no" id="no" title="Click here to vote no" rel="nofollow"><img src=" /z_images/poll/no.gif" alt="No" title="No" /></a>');
		$('p.poll input.yes').replaceWith('<a href="#" class="yes"  id="yes" title="Click here to vote yes" rel="nofollow"><img src=" /z_images/poll/yes.gif" alt="Yes" title="Yes" /></a>');
		if(isset(window.resetGame) && $.browser.msie )
		{
			gameStarted=true;
			showhide();
		
		}
		
		$("a.email_a_friend").click
		( 
			function() 
			{
				// Reveal hidden layer inside parent of A element of class reveal
				if(isset(window.statTracker) )
				{
					statTracker._trackPageview('/email/tellafriend');
				}
			}
		);
		
		$("a.email_counterfeit").click
		( 
			function() 
			{
				// Reveal hidden layer inside parent of A element of class reveal
				if(isset(window.statTracker) )
				{
					statTracker._trackPageview('/email/mhracounterfeit');
				}
			}
		);
		
		$("a.start_game").click
		( 
			function() 
			{
				if(!gameStarted)
				{
					if($.browser.msie){gameStarted=true;}
					showhide();
					return false;
				}
				else if($.browser.msie) {document.location = $(this).attr('href')+'?startgame=true';return false;}/* Game does not properly restart in ie if played once*/
			}
		);
		
		/*
		$("div.left_col ul.nav	li a").click
		( 
			function() 
			{
				// Reveal hidden layer inside parent of A element of class reveal
				if($(this).parent().find('ul').length > 0 && $(this).parent().hasClass('roll_down'))
				{
					$(this).parent().find('ul').slideToggle('normal');
					return false;
				}
			}
		);*/
		$("p.poll .yes").click
		( 
			function() 
			{
				$('input[name="alt_yes"]').val(1);
				$('input[name="alt_no"]').val('');
				var block = $(this).parents('div.vote_snippet:first');
				var formObj =  $(this).parents('div.vote_snippet:first').find('form');
				saveVote(block,formObj);
				return false;
			}
		);
		
		$("p.poll .no").click
		( 
			function() 
			{
				$('input[name="alt_yes"]').val('');
				$('input[name="alt_no"]').val(1);
				var block = $(this).parents('div.vote_snippet:first');
				var formObj =  $(this).parents('div.vote_snippet:first').find('form');
				saveVote(block,formObj);
				return false;
			}
		);
		
		
		
		$("a.printer").click/* Reveal hidden layer inside parent of A element of class reveal*/
		( 
			function() 
			{
				window.print();
				return false;
			}
		);
		//formatPoll();		
	}//doc ready function
	
);
var TIMEOUT = 500;
function saveVote(block,formObj)
{
	
	var innerBlock = block.find('div.vote_body');		
	var height = innerBlock.height();
	//alert(height);
	$(innerBlock).css({'background':'url(/z_images/structure/ajaxLoader.gif) no-repeat center center','height':height});
	innerBlock.html('');
	
	var str_data = formObj.serialize();	
	var action = formObj.attr('action')+'?jspoll=1';
	$.ajax(
	{
	   type: "POST",
	   async: false,
	   cache:false,
	   url: action,
	   data: str_data,
	   success: function(html)
	   {
			
			window.setTimeout(function ()
			{
				insertHTML(html,innerBlock);//reveals hiddn block
				
			},TIMEOUT,html);
		},//success
		error:function(XMLHttpRequest,status,error){
                      //alert(XMLHttpRequest.statusText+' uri '+uri);
                }

	  }); //ajax
	
}//function 

function insertHTML(html,block)
{
	/*
		html -string or xml
		block - class or id of html block to insert html into e.g '.block' or '#block'
	
	*/
	
	if(typeof html == 'string')
	{
		if(html != '')//can't put this test higher up as it causes ie errors if an xml object is returned
		{
			//alert(html);
			$(block).replaceWith(html);
		}
		
	}
	
}

function formatPoll()
{
	$('<span class="pds-clear"></span><br>').replaceWith('');
}


if(!Array.indexOf){//index of breaks ie
	Array.prototype.indexOf = function(obj){
		for(var i=0; i<this.length; i++){
			if(this[i]==obj){
				return i;
			}
		}
		return -1;
	}
}

try {//ie6 image swap cache problem
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

function isset( variable )
{

	return( typeof( variable ) != 'undefined' );

}

function showhide() 
{
 
 var item = document.getElementById('toggle_div');
 $(item).toggle(200);

}