////////////////////////////////////////////////////////////////////
//	ASSASSIN'S JS FILE
//	author: Tristan Lapalme 
//					patatepoilue@hotmail.com
//  updated by: PAVL-MEDIA
////////////////////////////////////////////////////////////////////

var currentPage = '';
var lastPage = '';
var ubi_tag_js = '';
var lastPath = '';

/* Ajax function */
function ajaxManager(url) {
	var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	if (x) {
		x.onreadystatechange = function() {
			if (x.readyState == 4 && x.status == 200) {
				handleResponse(x.responseText);
			} else if (x.readyState == 4) {
				updateCenterContent("AJAX ERROR: " + x.status + ", called url : "+url);
			}
		}
		x.open("GET", url, true);
		x.send(null);
	}
}

/* Ajax handler */
function handleResponse(response) {
	var centerContent = document.getElementById("content");
	if(centerContent)
	{
		centerContent.innerHTML = response;
	}
}


////////////////////////////////////////////////////////////////////
//	SECTIONS SPECIFIC 
////////////////////////////////////////////////////////////////////
function submitNewsForm() {
	outStr = "newsarchives.php?";
	outStr += "month="+document.getElementById("fMonth").value+"&";
	outStr += "year="+document.getElementById("fYear").value+"&";
	outStr += "endmonth="+document.getElementById("fEndmonth").value+"&";
	outStr += "endyear="+document.getElementById("fEndyear").value+"&";
	outStr += "pArchivesProductID="+document.getElementById("fPArchivesProductID").value+"&";
	outStr += "pCategory="+document.getElementById("fPCategory").value;
	var currentTime = new Date();
	ajaxManager("./php/"+outStr+'&nocache='+ currentTime.getTime());
}

//image Roll-Over functions
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function popupVid(url){
        popAtt = "location=0,directories=0,status=no,menubar=no,scrollbars=no,resizable=no,titlebar=no,toolbar=no,width=650,height=490";
		window.open(url, 'Videos', popAtt);
}

/*
 * FLASH SPECIFIC FUNCTIONS
 */
function setPage(page){
	if(currentPage != page)
	{
		lastPage = currentPage;
		currentPage = page;

		var centerContent = document.getElementById("content");
		if(centerContent)
		{
			centerContent.innerHTML = '<p><img src="images/ld.gif" width="32" height="32" alt="" /></p>';
		}
		
		var currentTime = new Date();
		
		if (currentPage.indexOf('?') > 0)
		{
			ajaxManager("./php/"+currentPage +'&nocache='+ currentTime.getTime());
		}
		else
		{
			ajaxManager("./php/"+currentPage +'?nocache='+ currentTime.getTime());
		}
		
	}
}

function backUp() {
	if(lastPage != '')
	{
		setPage(lastPage);
	}
}

/* 
 * POLLS FUNCTIONS  
 */
function loadPolls()
{
	var currentTime = new Date();
	$("#polls").load("./php/poll.php?nocache="+ escape(currentTime.getTime()));
}


function postPoll(form)
{
	/* Parse all form inputs. */
	var param = null;
	var values = new Array();
	var name = '';
	
	for(var i = 0; i < form.elements.length; i++)
	{
		if(form.elements[i].checked)
		{
			values.push(form.elements[i].value);
			
			if(name == '')
			{
				name = form.elements[i].name;
			}
		}
	}
	
	if(values.length > 0)
	{
		param = new Object();
		param[name] = values;
		
		var currentTime = new Date();
		param['nocache'] = escape(currentTime.getTime());

		var div = document.getElementById("polls");
		if(div)
		{
			div.innerHTML = '<p><img src="images/ld.gif" width="32" height="32" alt="" /></p>';
		}
		
		$.post("./php/poll.php", param, function(data, textStatus)
			{ 
				$("#polls").html(data);
		  	}
		);
	} 
}

/* 
 * Used by the index.swf Flash to strech its height. 
 */
function setHeight(start,end) { 
		// Create/start the sequence.	
		var t1 = new Sequence();
		t1.addChild(new Tween(document.getElementById("flashcontent").style, "height", Tween.strongEaseOut, start, end, 0.5, "px"));
		t1.start();
}

/* 
 * Listen for onChange event on the SWFAddress object. 
 */
function initListeners(event)
{
	SWFAddress.removeEventListener(SWFAddressEvent.INIT, initListeners);
	SWFAddress.addEventListener(SWFAddressEvent.CHANGE, checkPath);	
	
	/* Check if we have the poll element. */
	pollDiv = document.getElementById('polls');
}

function checkPath(event)
{
	var nextPage = '';
	if(event)
	{
		if(event.path)
		{
			if(event.path == '/news/')
			{
				nextPage = 'news.php';
			}
			else
			{
				if(event.path == '/videoblog/')
				{
					nextPage = 'videoblogs.php';
				}
				else
				{
					nextPage = 'index.php';
				}
			}
		}
	}
	
	if(nextPage != '')
	{
		setPage(nextPage);	
	}
}

/* 
 * SWFAddress listener - Ubi UM
 */
function track_user(event)
{
	if(typeof(ubi_tag_js) != "undefined")
	{		
		var path = '';
		
		if(event.path != lastPath)
		{
			/* Load new polls... */
			loadPolls();
			
			switch(event.path)
			{
				case '/home/':
					path = "HOMEPAGE";
					break;
				case '/scenario/':
				case '/key-features/':
					path = "GAMEINFO";
					break;
				case '/screenshots-nintendods/':
				case '/screenshots-xboxpspc/':
				case '/concept-arts/':
					path = "DOWNLOAD_SCREENSHOTS";
					break;
				case '/video/':
				case '/devdiary/':
				case '/videoblog/':
					path = "DOWNLOAD_VIDEOS";
					break;
				case '/wallpapers/':
					path = "DOWNLOAD_WALLPAPERS";
					break;
				case '/screensavers/':
					path = "DOWNLOAD_SCREENSAVERS";
					break;
				case '/icons/':
				case '/signatures/':
				case '/msnkit/':
				case '/webkit/':
					path = "DOWNLOAD_GOODIES";
					break;
				case '/news/':
					path = "COMMUNITY_NEWS";
					break;
				default:
					break;
			}	
			
			if(path.length > 0)
			{
				path = ubi_tag_js + path;
				
				if(typeof(ubi_do_WT_stat_silent) != "undefined")
				{
					ubi_do_WT_stat_silent(path);		
				}
			}
		}
		
		lastPath = event.path;
	}
}

// Listener for current page.
SWFAddress.addEventListener(SWFAddressEvent.INIT, initListeners);
