//////////////////////////////////////////////////////////////////////////////////////////////
/********************************************************************************************
POST SAVE CLASS
version: 2.0.0.18
********************************************************************************************/
//////////////////////////////////////////////////////////////////////////////////////////////
var PostSave = {};

///////////////////////////////////////
/**************************************
ENUMS
**************************************/
///////////////////////////////////////
PostSave.TABS = {FACEBOOK: 'Facebook', TWITTER: 'Twitter', DOWNLOAD: 'Download', MAIL: 'GSMail', DONE: 'Done', PLEASEWAIT: 'PleaseWait'};
PostSave.ACTIVITIES = {MYWEBFACE: 'myWebFace', MYWEBFACEGALLERY: 'myWebFaceGallery', MYPHOTOSFX: 'photoFX', MYPHOTOSFXGALLERY: 'myPhotoFxGallery', PHOTOMIX: 'photoMIX', PHOTOMIXGALLERY: 'photoMIXGallery'};
PostSave.BOOLEAN = {TRUE: true, FALSE: false};

///////////////////////////////////////
/**************************************
INIT
**************************************/
///////////////////////////////////////
PostSave.isContestSubmissionTime = null;
PostSave.pollStatus = 0;
PostSave.shareMode = false;
PostSave.shareActivityMode = false;
PostSave.isBodyLoaded = false;
PostSave.showEvent = null;
PostSave.hideEvent = null;
PostSave.itemDownloadHandler = false;
PostSave.isItemsDownloaded = false;
PostSave.wasActivated = false;

PostSave.init = function()
{
	////// [getting user info] //////
	PostSave.User.get();
	
	////// [registering to user change event] //////
	EVENTS.addEventListener("userChangeEvent", PostSave.User.userChange);
	
	////// downloading jss + css //////
	PostSave.loadJs();
	
	/////// [initiating tabs] //////
	PostSave.Tabs.init();
	
	////// downloading images //////
	PostSave.Images.setDownloadImages();
	setTimeout(PostSave.Images.processQueue, 300);
	
	// !! activity init is after JS load !! //
	
	// loading addToAny //
	if (registered === '1')
	{
		var html = new Array();
		html.push("<div style='display:none'>");
		html.push("<a id='a2aA' class='a2a_dd' href='http://www.addtoany.com/share_save'>");
		html.push("<img src='http://static.addtoany.com/buttons/share_save_256_24.png' width='256' height='24' border='0' alt='Share/Bookmark'/>");
		html.push("</a><script type='text/javascript'>a2a_linkname=document.title;a2a_linkurl=location.href;</script><script type='text/javascript' src='http://static.addtoany.com/menu/page.js'></script>);");
		html.push("</div>");
		document.write(html.join(""));
	}
	
}

PostSave.loadJs = function()
{
	/////////////
	/*
		POST SAVE JS FILES AND CSS FILE IS LOADED THROUGH PERL/PlantIncludes or by <script>/<link> include
	*/
	/////////////
	
	PostSave.jsArray = new Array();
	PostSave.jsArray.push('http://cdn.gigya.com/wildfire/js/wfapiv2.js');
	PostSave.jsArray.push('http://www.plaxo.com/css/m/js/util.js');
	PostSave.jsArray.push('http://www.plaxo.com/css/m/js/basic.js');
	PostSave.jsArray.push('http://www.plaxo.com/css/m/js/abc_launcher.js');
	if (typeof(General) === 'undefined')
		PostSave.jsArray.push('http://home.mywebface.com/javascripts/general.js');
	if (typeof(AJAX) === 'undefined')
		PostSave.jsArray.push('http://home.mywebface.com/javascripts/ajax.js');
	if (typeof(zazzle) === 'undefined')
		PostSave.jsArray.push('http://home.mywebface.com/zazzle.js');
	if (typeof(EVENTS) === 'undefined')
		PostSave.jsArray.push('http://home.mywebface.com/javascripts/events.js');
	if (typeof(Popup) === 'undefined')
		PostSave.jsArray.push('http://home.mywebface.com/javascripts/popup.js');
	if (typeof(GET_AC_FL_RunContent) === 'undefined')
		PostSave.jsArray.push('http://home.mywebface.com/AC_RunActiveContent.js');
	if (typeof(BlackFilterAjax) === 'undefined')
		PostSave.jsArray.push('http://home.mywebface.com/javascripts/blackFilter.js');
	PostSave.downloadJs();
}


PostSave.isJsLoaded = false;
PostSave.downloadJs = function()
{
	var _script1 = document.createElement('script');
	_script1.src = PostSave.jsArray[PostSave.loadedJs];
    _script1.type = 'text/javascript';
    if (navigator.appName.indexOf("Microsoft") !== -1)
    	_script1.onreadystatechange = function(){if (this.readyState == 'loaded' || this.readyState == 'complete'){ setTimeout(PostSave.jsLoader, 200);} };
    else
    	_script1.onload = setTimeout(PostSave.jsLoader, 200);
    document.getElementsByTagName('head')[0].appendChild(_script1);	
}
PostSave.jsArray = null;
PostSave.loadedJs = 0;
PostSave.jsLoader = function()
{
	PostSave.Tools._log.push("JS: downloaded " + (PostSave.loadedJs+1) + "/" + PostSave.jsArray.length); 
	if (++PostSave.loadedJs === PostSave.jsArray.length)
		PostSave.afterJSLoad();
	else
		PostSave.downloadJs();	
}

PostSave.afterJSLoad = function()
{
	//////// [initiating events] /////////
	PostSave.showEvent = new EVENTS.Event("PostSaveShow");
	PostSave.hideEvent = new EVENTS.Event("PostSaveHide");
	PostSave.Items.Download._event = new EVENTS.Event("itemsDownloadEvent");
	
	/////// [initiating activites] ///////
	PostSave.Activities.init();
	
	/////// [initiating gui ajax] ////////
	PostSave.Tabs.GUI.ajax = new AJAX.request('http://home.mywebface.com/javascripts/postSaveV2/postSave.pl', AJAX.Method.POST, AJAX.Type.ASYNC);
	PostSave.Tabs.GUI.ajax.onFailFunction = PostSave.Tabs.GUI.ajaxRequestFail;
	PostSave.Tabs.GUI.ajax.onTimeoutFunction = PostSave.Tabs.GUI.ajaxRequestFail;
	
	//////// [loading facebook] /////////
	PostSave.Tabs.GUI.Facebook.loadEvent = new EVENTS.Event("FacebookConnectLoad");
	EVENTS.addEventListener(PostSave.Tabs.GUI.Facebook.loadEvent, PostSave.loadHandler);
	PostSave.Tabs.GUI.Facebook.init();
	
	//////// [setting flag] /////////
	PostSave.isJsLoaded = true;
}

PostSave.numOfLoads = 3;
PostSave.alreadyLoaded = 0;
PostSave.loadHandler = function(whatIsLoaded)
{
	PostSave.Tools._log.push("loadHandler: " + whatIsLoaded); 
	if (++PostSave.alreadyLoaded >= PostSave.numOfLoads)
	{
		/// adding log ///
		PostSave.Tools._log.push("preloading finished");
		
		/// disposing events ///
		PostSave.Tabs.GUI.Facebook.loadEvent.dispose();
		
		/// dispatching event ///
		PostSave.Items.Download._event.dispatch();
	}
}

///////////////////////////////////////
/**************************************
GUI
**************************************/
///////////////////////////////////////
PostSave.GUI = {};
PostSave.GUI.isBuilt = false;
PostSave.GUI.isShown = false;
PostSave.GUI.openTab = '';
PostSave.GUI.firstTabToShow = '';
PostSave.GUI.builtTabsElements = new Array();

PostSave.GUI.Pointers = 
{
	postSaveDiv: null,
	menuDiv: null,
	menuTds: null,
	tabHolder: null
}

PostSave.GUI.Popup = null;
PostSave.GUI.build = function()
{
	var postSaveDiv = document.createElement('div');
	postSaveDiv.setAttribute('id','postSave_BigDiv');
	
	/********** 
	Big Div
	***********/
	var htmlA = new Array();
	htmlA.push("<table width='100%' cellspacing='0' cellpadding='0' border='0'>");

		/********** move control ***********/
		htmlA.push("<tr><td id='postSaveDragTd' height='30px'><font style='font-size:2px;'>&nbsp;</font></td></tr>");
		
		/********** Tabs Table ***********/
		htmlA.push("<tr><td height='40px' align='center' valign='top'><div id='postSave_TabsDivTable'></div></td></tr>");
		
		/********** space ***********/
		htmlA.push("<tr><td height='20px'>&nbsp;</td></tr>");
		
		/********** Tabs divs ***********/
		htmlA.push("<tr><td height='405px'>");
			htmlA.push("<table width='100%' cellspacing='0' cellpadding='0' border='0'>");
				htmlA.push("<tr>")	;
					htmlA.push("<td class='width40'></td>");
					htmlA.push("<td><div id='postSave_tabDiv_round'></div></td>");
					htmlA.push("<td class='width30'></td>");
				htmlA.push("</tr>");
			htmlA.push("</table>");	
		htmlA.push("</td></tr>");
		
		htmlA.push("<tr><td height='55px'>&nbsp;</td></tr>");
	htmlA.push("</table>");	
	
	/// close btn
	htmlA.push("<a href='javascript:PostSave.GUI.hide(\"x\")'><img border='0' id='postSave_closeImg' src='"+PostSave.Images.array['closeA'].src+"' onmouseover='this.src=\""+PostSave.Images.array['closeB'].src+"\"'  onmouseout='this.src=\""+PostSave.Images.array['closeA'].src+"\"' /></a>");
	/// see your face 
	htmlA.push("<h6 id='yourFaceMSG'>See your creation at <a href='' id='yourFaceLink' target='_blank'></a></h6>");
	/// facebook message 
	htmlA.push("<h6 id='facebookMessage'>Facebook&reg is a registered trademark of Facebook Inc.  myWebFace.com is not affiliated with or endorsed by Facebook Inc.</h6>");
	htmlA.push("</div>");
	
	postSaveDiv.innerHTML = htmlA.join("");
	
	/********** 
	Div's append
	***********/
	document.body.appendChild(postSaveDiv);
	
	//// creating pointer ////
	PostSave.GUI.Pointers.postSaveDiv = postSaveDiv;
	
	//// setting as popup ////
	//PostSave.GUI.Popup = new Popup.Popup(PostSave.GUI.Pointers.postSaveDiv, 'postSaveDragTd', {type: Popup.PositionType.SCREEN_CENTER_MIDDLE});
	if (document.getElementById('flashTd') !== null)
		PostSave.GUI.Popup = new Popup.Popup(PostSave.GUI.Pointers.postSaveDiv, 'postSaveDragTd', {type: Popup.PositionType.RELATIVE, positionToElement:'flashTd', top:-10, left:-10});
	else
		PostSave.GUI.Popup = new Popup.Popup(PostSave.GUI.Pointers.postSaveDiv, 'postSaveDragTd', {type: Popup.PositionType.SCREEN_CENTER_MIDDLE});
	
	PostSave.GUI.Popup.toShowAlone = false;
	PostSave.GUI.Popup.toAnchorPosition = false;
	
	//// is built flag ////
	PostSave.GUI.isBuilt = true;
}

PostSave.GUI.show = function(activity, itemString, tabsStr, nicknameCreator, firstTabToShow)
{
	PostSave.wasActivated = true;
	PostSave.changesWereMade = false;
	
	////// if div is built //////
	if (!PostSave.GUI.isBuilt)
		PostSave.GUI.build();
		
	////// showing div ///////
	PostSave.GUI.Popup.show();
	
	////// Checking if in shareMode + nickname creator //////
	PostSave.User.nicknameCreator = (typeof(nicknameCreator) !== 'undefined' && nicknameCreator !== '') ? nicknameCreator : PostSave.User.nicknameViewer;
	PostSave.shareMode = !(PostSave.User.nicknameCreator === PostSave.User.nicknameViewer);
	
	////// Checking activity //////
	PostSave.Activities.currentActivity = PostSave.Activities.arr[activity];
	if (typeof(PostSave.Activities.currentActivity) == 'undefined')
	{
		PostSave.GUI.Popup.hide();
		return;
	}
		
	////// building and setting tabs //////
	tabsStr = tabsStr || "";
	PostSave.Tabs.setTabs(PostSave.Activities.currentActivity, tabsStr);
	PostSave.GUI.buildTabs();
		
	////// Checking item string only if NOT sharing activity //////
	itemString = itemString || '';
	itemString = itemString.toString();
	var itemArr = itemString.split(",");
	if (tabsStr === PostSave.TABS.ACTIVITY_MAIL)
	{
		PostSave.shareActivityMode = true;
	}
	else
	{
		itemArr = itemArr || null;
		if (itemArr === null || !(itemArr instanceof Array))
		{
			PostSave.GUI.Popup.hide();
			return;
		}
		
		var l = itemArr.length;
		if (l === 0 || l>4)
		{
			PostSave.GUI.Popup.hide();
			return;
		}
	}
	
	/////// if the div is already shown ///////
	if (PostSave.GUI.isShown)
		return;
	PostSave.GUI.isShown = true;
	
	////// Checking which tab to show //////
	firstTabToShow = firstTabToShow || '';
	if (firstTabToShow !== '')
		PostSave.GUI.firstTabToShow = firstTabToShow;
	
	////// setting GAS dir //////
	PostSave.Stats.setDir(PostSave.Activities.currentActivity);
	
	////// running activity on show function //////
	PostSave.Activities.currentActivity.onShowFunction();
	
	////// showing please wait tab ///////
	PostSave.Tabs.GUI.showByName(PostSave.TABS.PLEASEWAIT);
	
	////// registering items download event //////
	if (!PostSave.itemDownloadHandler)
		PostSave.itemDownloadHandler = EVENTS.addEventListener(PostSave.Items.Download._event, PostSave.GUI.showAfterItems);
	
	////// proccessing items //////	
	PostSave.isItemsDownloaded = false;
	PostSave.Items.newItems(itemArr);
}

PostSave.GUI.showAfterItems = function()
{
	////// items downloaded flag ///////
	PostSave.isItemsDownloaded = true;
	
	////// updating your face link //////
	document.getElementById('yourFaceLink').href = PostSave.Items.array[0].large.src.split("?")[0];
	document.getElementById('yourFaceLink').onclick = function() { PostSave.Stats.send(PostSave.Tabs.GUI.currentTabName + '/seeCreation'); };
	document.getElementById('yourFaceLink').innerHTML = PostSave.Items.array[0].large.src.split("?")[0];
	
	////// dispatching show event ///////
	PostSave.showEvent.dispatch();
	
	////// showing tabs menu //////
	PostSave.GUI.buildMenu();
	
	////// showing the wanted tab //////
	if (PostSave.GUI.firstTabToShow === '')
	{
		/// [if share mode and there is firstTabshare] ///
		if (PostSave.shareMode && PostSave.Activities.currentActivity.firstTabShare !== '')
		{
			if (PostSave.Activities.currentActivity.firstTabShare === PostSave.TABS.DONE)
				PostSave.Tabs.GUI.showDoneTabFirst();
			else
				PostSave.Tabs.GUI.showByName(PostSave.Activities.currentActivity.firstTabShare);
			
			PostSave.Tabs.GUI.showByName(PostSave.Activities.currentActivity.firstTabShare);	
		}
		/// [if NOT in share mode and NOT in activity share mode and firstTab] ///
		else if (!PostSave.shareMode && !PostSave.shareActivityMode && PostSave.Activities.currentActivity.firstTab !== '')
		{
			if (PostSave.Activities.currentActivity.firstTab === PostSave.TABS.DONE)
				PostSave.Tabs.GUI.showDoneTabFirst();
			else
				PostSave.Tabs.GUI.showByName(PostSave.Activities.currentActivity.firstTab);
		}
		/// [other cases] ///
		else
		{
			PostSave.Tabs.GUI.showByIndex(0);
		}
	}
	else
		PostSave.Tabs.GUI.showByName(PostSave.GUI.firstTabToShow);
}


PostSave.GUI.hide = function(source)
{
	//// hiding post save popup ////
	PostSave.GUI.Popup.hide();
	
	////// running activity on show function //////
	PostSave.Activities.currentActivity.onHideFunction();
	
	try
	{
		//// hiding menu ////
		PostSave.GUI.Pointers.menuDiv.style.display = "none";
		
		/// hiding current tab ///
		PostSave.Tabs.GUI.hideByName(PostSave.Tabs.GUI.currentTabName);
	
		/// updating menu ///
		PostSave.GUI.updateMenu(PostSave.Tabs.GUI.currentTabName);
	}
	catch(e){}
	
	//// saving last itemString ////
	PostSave.Items.lastItemString = PostSave.Items.currItemString;
	
	//// saving last activity ////
	PostSave.Activities.lastActivityName = PostSave.Activities.currentActivity.name;
	
	/// saving last tab string ////
	PostSave.Activities.lastTabString = PostSave.Activities.currentActivity.tabString;
	
	/// clearing current tab name ///
	PostSave.Tabs.GUI.currentTabName = '';
	
	//// setting shown flag ////
	PostSave.GUI.isShown = false;
	
	//// setting shareActivity mode ////
	PostSave.shareActivityMode = false;
	
	//// dispatching hide event ////
	PostSave.hideEvent.dispatch(source);
	
	//// sending urchin ////
	PostSave.Stats.send(PostSave.Tabs.GUI.currentTabName + '/close');
}


PostSave.GUI.buildTabs = function()
{
	if (PostSave.GUI.Pointers.tabHolder === null)
		PostSave.GUI.Pointers.tabHolder = document.getElementById('postSave_tabDiv_round');
		
	//// adding a tab div if not created yet ////
	var l = PostSave.Activities.currentActivity.tabs.length;
	var tab = null;
	var divElem = null;
	for (var i=0; i<l; i++)
	{
		tab = PostSave.Tabs[PostSave.Activities.currentActivity.tabs[i]];
		/// if tab isn't created yet ///
		if (tab.pointer === null)
		{
			divElem	= document.createElement('div');
			divElem.setAttribute('id', 'postSave_tabDiv_' + tab.name);
			divElem.setAttribute('class','postSave_tabDiv');
			PostSave.GUI.Pointers.tabHolder.appendChild(divElem);
			tab.pointer = divElem;
		}
	}
}

PostSave.GUI.buildMenu = function()
{
	
	//// building menu table only if have too ////
	if (PostSave.Activities.currentActivity.name !==  PostSave.Activities.lastActivityName || PostSave.Activities.currentActivity.tabString !== PostSave.Activities.lastTabString)
	{
		var tabsLen = PostSave.Activities.currentActivity.tabs.length;
		var html = new Array();
		html.push("<table cellspacing='0' cellpadding='0' border='0'><tr>");
		
		var tabObject = null;
		var imgObjA = null;
		var imgObjB = null;
		var tabName;
		for (var i=0; i<tabsLen; i++)
		{
			tabName = PostSave.Activities.currentActivity.tabs[i];
			tabObject = PostSave.Tabs[tabName];
			
			if (tabObject.nameA !== '')
			{
				html.push("<td height='30px' id='postSave_TabTD_"+tabName+"' width='90px'>");
			
				imgObjA = PostSave.Images.array[tabObject.nameA];
				imgObjB = PostSave.Images.array[tabObject.nameB];
				
				html.push("<a href='javascript:PostSave.Tabs.GUI.showByName(\""+tabName+"\")' title='Go to "+tabObject.name+" tab'><img border='0' src='"+imgObjA.src+"'  id='postSave_TabImg"+tabName+"' onmouseover='javascript:this.src=\""+imgObjB.src+"\"' onmouseout='javascript:this.src=\""+imgObjA.src+"\"'/></a>");
				
				html.push("</td>");
				
				if (i !== tabsLen-1)
					html.push("<td width='5px'>&nbsp;</td>");
				else
					html.push("<td>&nbsp;</td>");
			}
		}
		html.push("</tr></table>");
			
		// setting menu pointer //
		if (PostSave.GUI.Pointers.menuDiv === null)
			PostSave.GUI.Pointers.menuDiv = document.getElementById('postSave_TabsDivTable');
		
		// innerHTMLing //
		PostSave.GUI.Pointers.menuDiv.innerHTML = html.join("");
		
		
		// setting menu td pointers //
		if (PostSave.GUI.Pointers.menuTds === null)
			PostSave.GUI.Pointers.menuTds = new Array();
		for (var i=0; i<tabsLen; i++)
		{
			var tabName = PostSave.Activities.currentActivity.tabs[i];
			if (PostSave.Tabs[tabName].nameA !== '') // adding only 'menued' pointers //
				PostSave.GUI.Pointers.menuTds[tabName] = document.getElementById('postSave_TabTD_' + tabName);
		}
	}
	PostSave.GUI.Pointers.menuDiv.style.display = "block";
}

PostSave.GUI.updateMenu = function(currentTabName, newTabName)
{
	currentTabName = currentTabName || '';
	newTabName = newTabName || '';
	
	var tabObject = null;
	var imgObjA = null;
	var imgObjB = null;
	
	// changing last tab menu appearance //
	if (currentTabName !== '')
	{
		tabObject = PostSave.Tabs[currentTabName];
		if (tabObject.nameA !== '')
		{
			imgObjA = PostSave.Images.array[tabObject.nameA];
			imgObjB = PostSave.Images.array[tabObject.nameB];
			PostSave.GUI.Pointers.menuTds[currentTabName].innerHTML = "<a href='javascript:PostSave.Tabs.GUI.showByName(\""+currentTabName+"\")' title='Go to "+tabObject.name+" tab'><img border='0' src='"+imgObjA.src+"'  id='postSave_TabImg"+currentTabName+"' onmouseover='javascript:this.src=\""+imgObjB.src+"\"' onmouseout='javascript:this.src=\""+imgObjA.src+"\"'/></a>";
		}
	}
	
	// setting new tab menu appearance //
	if (newTabName !== '')
	{
		tabObject = PostSave.Tabs[newTabName];
		if (typeof(PostSave.GUI.Pointers.menuTds[newTabName]) != 'undefined')
		{
			imgObjA = PostSave.Images.array[tabObject.nameA];
			imgObjB = PostSave.Images.array[tabObject.nameB];
			PostSave.GUI.Pointers.menuTds[newTabName].innerHTML = "<img border='0' src='"+imgObjB.src+"' id='postSave_TabImg"+newTabName+"'/>";
		}
	}
}



///////////////////////////////////////
/**************************************
ITEMS
**************************************/
///////////////////////////////////////
PostSave.Items = {};
PostSave.Items.perRow = 2;
PostSave.Items.newItems = function(itemArr)
{
	PostSave.Items.currItemString = itemArr.join(",");
	
	
	/// if there are items ///
	if (!PostSave.shareActivityMode)
	{
		// cleaning item array //
		if (PostSave.Items.array === null)
			PostSave.Items.array = new Array();
		else
			PostSave.Tools.cleanArray(PostSave.Items.array);		
		
		// adding items to array //
		var l = itemArr.length;
		for (var i=0;i<l;++i)
		{
			new PostSave.Items.item(itemArr[i], l);
		}
		
		// if there's a need to download items //
		if (PostSave.Activities.currentActivity.downloadItems)
			PostSave.Items.Download.process();
		// no need //
		else
			PostSave.loadHandler('Items');
	}
	else
	{
		PostSave.loadHandler('Items');
	}
}

PostSave.Items.lastItemString = '';
PostSave.Items.currItemString = '';
PostSave.Items.array = null;

////////////////////////////
///// ITEM OBJECT //////////
////////////////////////////
PostSave.Items.item = function(key, numOfItems)
{
	if (!key) return;
	
	var randomNum = Math.ceil(500*Math.random());
	var dirName = General.getDirName(key, PostSave.Activities.currentActivity.dirNameDepth);
	
	/// saving params ///
	this.key = key;
	
	if (PostSave.Activities.currentActivity.smallDir !== '')
	{
		this.smallSrc = PostSave.Activities.currentActivity.smallDir + dirName + "/" + key + PostSave.Activities.currentActivity.extension + "?rand=" + randomNum;
		/// in FDS and only 1 item ///
		if (PostSave.Activities.currentActivity.name === PostSave.ACTIVITIES.FDS && numOfItems === 1)
			this.smallSrc = this.smallSrc.replace(PostSave.Activities.currentActivity.smallDir, PostSave.Activities.currentActivity.largeDir);
	}
		
	if (PostSave.Activities.currentActivity.largeDir !== '')
	{
		this.largeSrc = PostSave.Activities.currentActivity.largeDir + dirName + "/" + key + PostSave.Activities.currentActivity.extension + "?rand=" + randomNum;
	}
	
	PostSave.Items.array.push(this);
}

PostSave.Items.item.prototype.key = -1;
PostSave.Items.item.prototype.largeSrc = '';
PostSave.Items.item.prototype.smallSrc = '';
PostSave.Items.item.prototype.large = '';
PostSave.Items.item.prototype.small = '';


///////////////////////////////////////
/**************************************
ITEMS DOWNLOAD
**************************************/
///////////////////////////////////////
PostSave.Items.Download = {};
PostSave.Items.Download._event = null;
PostSave.Items.Download.downloadedImages = 0;
PostSave.Items.Download.imagesToDownload = 0;
PostSave.Items.Download.handler = function()
{
	++PostSave.Items.Download.downloadedImages;
	PostSave.Tools._log.push("Item images downloaded: "+PostSave.Items.Download.downloadedImages+"/"+PostSave.Items.Download.imagesToDownload);
	if (PostSave.Items.Download.downloadedImages === PostSave.Items.Download.imagesToDownload)
	{
		PostSave.loadHandler('Items');
	}
		
			
}

PostSave.Items.Download.process = function()
{
	var l = PostSave.Items.array.length;
	
	/// counting ///
	PostSave.Items.Download.downloadedImages = 0;
	PostSave.Items.Download.imagesToDownload = 0;
	for (var i=0;i<l;++i)
	{
		PostSave.Items.Download.imagesToDownload = PostSave.Items.Download.imagesToDownload+1;
		//counter++;
		if (PostSave.Items.array[i].largeSrc)
			//counter++;
			PostSave.Items.Download.imagesToDownload = PostSave.Items.Download.imagesToDownload+1;
	}
	PostSave.Tools._log.push("images to download: " + PostSave.Items.Download.imagesToDownload);
	
	/// processing ///
	for (var i=0;i<l;++i)
	{
		PostSave.Tools._log.push("Item image #" + i + " (small) was added to the queue");
		PostSave.Items.array[i].small = new Image();
		PostSave.Items.array[i].small.src = PostSave.Items.array[i].smallSrc;
		PostSave.Items.array[i].small.onload = PostSave.Items.Download.handler;
		PostSave.Items.array[i].small.src = PostSave.Items.array[i].smallSrc;
		
		if (PostSave.Items.array[i].largeSrc)
		{
			PostSave.Tools._log.push("Item image #" + i + " (large) was added to the queue");
			PostSave.Items.array[i].large = new Image();
			PostSave.Items.array[i].large.src = PostSave.Items.array[i].largeSrc;
			PostSave.Items.array[i].large.onload = PostSave.Items.Download.handler;
			PostSave.Items.array[i].large.src = PostSave.Items.array[i].largeSrc;
		}
		else
		{
			PostSave.Items.array[i].large = PostSave.Items.array[i].small;
		}
	}
}



///////////////////////////////////////
/**************************************
USER
**************************************/
///////////////////////////////////////
PostSave.User = {};
PostSave.User.uidViewer = 0;
PostSave.User.uidType = '';
PostSave.User.ageViewer = 0;
PostSave.User.nicknameViewer = '';
PostSave.User.nicknameCreator = '';
PostSave.User.get = function()
{
	PostSave.User.uidType = General.Cookies._read('uidType') || '';
	if (PostSave.User.uidType == "FB")
	{
		PostSave.User.nicknameViewer = General.Cookies._read('fb_name') || '';
		PostSave.User.uidViewer = General.Cookies._read('fb_id') || 0;
	}
	else if (PostSave.User.uidType == "MWF")
	{
		PostSave.User.nicknameViewer = General.Cookies._read('displayName') || '';
		PostSave.User.uidViewer = General.Cookies._read('usrId') || 0;	
	}
}

PostSave.User.userChange = function(theType, uid)
{
	/// updating user info ///
	PostSave.User.get();
	
	/// updating registration teaser ///
	if (PostSave.GUI.isShown)
	{
		PostSave.Tabs[PostSave.Tabs.GUI.currentTabName].updateLeft();
	}
	
	
	
}

///////////////////////////////////////
/**************************************
IMAGES
**************************************/
///////////////////////////////////////
PostSave.Images = {};

PostSave.Images.downloadQueue = new Array();
PostSave.Images.addToQueue = function(key, src)
{
	PostSave.Images.downloadQueue.push({"key": key, "src": src});
}

PostSave.Images.setDownloadImages = function()
{
	PostSave.Images.addToQueue('closeA', 'http://home.mywebface.com/javascripts/postSave/images/close_a1.png');
	PostSave.Images.addToQueue('closeB', 'http://home.mywebface.com/javascripts/postSave/images/close_b1.png');
	
	PostSave.Images.addToQueue('printIcon', 'http://home.mywebface.com/javascripts/postSave/images/print_icon2.jpg');
	PostSave.Images.addToQueue('saveToPCIcon', 'http://home.mywebface.com/javascripts/postSave/images/saveToPC_icon1.gif');
	
	// twitter //
	PostSave.Images.addToQueue('twitterShare', 'http://home.mywebface.com/javascripts/postSave/images/twitter_share.gif');
	PostSave.Images.addToQueue('twitterAvatar', 'http://home.mywebface.com/javascripts/postSave/images/twitter_avatar.gif');
	
	/// mail tab ///
	PostSave.Images.addToQueue('emailIcon', 'http://home.mywebface.com/javascripts/postSave/images/email_icon1.gif');
	PostSave.Images.addToQueue('addressBook', 'http://home.mywebface.com/javascripts/postSave/images/adressbook.gif');
	PostSave.Images.addToQueue('sendEmail', 'http://home.mywebface.com/javascripts/postSave/images/send_email_btn_1.gif');
	PostSave.Images.addToQueue('copyToClipboard', 'http://home.mywebface.com/javascripts/postSave/images/copy2.gif');
	PostSave.Images.addToQueue('yahoo', 'http://home.mywebface.com/javascripts/postSave/images/yahoo.gif');
	PostSave.Images.addToQueue('hotmail', 'http://home.mywebface.com/javascripts/postSave/images/hotmail.gif');
	PostSave.Images.addToQueue('aol', 'http://home.mywebface.com/javascripts/postSave/images/aol.gif');
	PostSave.Images.addToQueue('gmail', 'http://home.mywebface.com/javascripts/postSave/images/gmail.gif');
	PostSave.Images.addToQueue('outlook', 'http://home.mywebface.com/javascripts/postSave/images/outlook.gif');
	
	/// download tab ///
	PostSave.Images.addToQueue('downloadIcon', 'http://home.mywebface.com/javascripts/postSave/images/download_icon.gif');
	PostSave.Images.addToQueue('downloadSave', 'http://home.mywebface.com/javascripts/postSave/images/savetodesktop3.gif');
	
	/// post tab ///
	PostSave.Images.addToQueue('postIcon', 'http://home.mywebface.com/javascripts/postSave/images/post_icon1.gif');
	
	/// facebook tab ///
	PostSave.Images.addToQueue('fbIcon', 'http://home.mywebface.com/javascripts/postSave/images/fb_icon.gif');
	PostSave.Images.addToQueue('fbOwnWall', 'http://home.mywebface.com/javascripts/postSave/images/post2wall.gif');
	PostSave.Images.addToQueue('fbFriendWall', 'http://home.mywebface.com/javascripts/postSave/images/send2friendswall.gif');
	PostSave.Images.addToQueue('fbCancel', 'http://home.mywebface.com/javascripts/postSave/images/fb_cancel.gif');
	PostSave.Images.addToQueue('fbFriends', 'http://home.mywebface.com/javascripts/postSave/images/facebook2.gif');
	PostSave.Images.addToQueue('postToAlbumBtn', 'http://home.mywebface.com/javascripts/postSave/images/post2album.gif');
	
	/// done tab ///
	PostSave.Images.addToQueue('doneIcon', 'http://home.mywebface.com/javascripts/postSave/images/done_icon1.gif');
	PostSave.Images.addToQueue('doneArrow', 'http://home.mywebface.com/javascripts/postSave/images/arrow.gif');
}

PostSave.Images.array = new Array();
PostSave.Images.processQueue = function()
{
	PostSave.Images.isLoaded = false;
	var _length = PostSave.Images.downloadQueue.length;
	PostSave.Images.imagesToDownload = PostSave.Images.downloadQueue.length;
	var key = '';
	for (var i=0;i<_length;++i)
	{
		key = PostSave.Images.downloadQueue[i]["key"];
		PostSave.Images.array[key] = new Image();
		//PostSave.Images.array[key].src = PostSave.Images.downloadQueue[i].src; //fixes IE7 bug
		PostSave.Images.array[key].onload = PostSave.Images.downloadHandler;
		PostSave.Images.array[key].src = PostSave.Images.downloadQueue[i]["src"];
	}
	
	// cleaning downloadArray //
	PostSave.Tools.cleanArray(PostSave.Images.downloadQueue);
	
}

PostSave.Images.downloadedImages = 0;
PostSave.Images.imagesToDownload = 0;
PostSave.Images.downloadHandler = function(event4ff)
{
	PostSave.Tools._log.push("Img: downloaded " + (PostSave.Images.downloadedImages+1) + "/" + PostSave.Images.imagesToDownload);
	if (++PostSave.Images.downloadedImages === PostSave.Images.imagesToDownload)
	{
		PostSave.loadHandler('Images');
	}
}


///////////////////////////////////////
/**************************************
STATISTICS
**************************************/
///////////////////////////////////////
PostSave.Stats = {};
PostSave.Stats.baseDir = '';
PostSave.Stats.setDir = function(activity)
{
	activity = activity || PostSave.Activities.currentActivity;
	
	
	/*** Statistics ***/
	var dir = activity.name;
	if (dir === PostSave.ACTIVITIES.BOUTIQUE)
		dir = 'boutique';
	PostSave.Stats.baseDir = '/GAS/afterSave/'+dir+'/';
}

PostSave.Stats.send = function(code)
{
	if (typeof(urchinTracker) !== 'undefined')
		urchinTracker(PostSave.Stats.baseDir + code);
}

///////////////////////////////////////
/**************************************
TOOLS
**************************************/
///////////////////////////////////////
PostSave.Tools = {}
PostSave.Tools.cleanArray = function(arr)
{
	if (arr instanceof Array)
	{
		var l = arr.length;
		for (var i=0;i<l;++i)
		{
			arr.pop();
		}
	}
}
PostSave.Tools._log = new Array();
