window.addEventListener('load', function(event) {
	var click = function(parent) {
		var search = [parent];
		while(search.length > 0) {
			var node = search.pop();
			if (node.nodeType == Node.ELEMENT_NODE) {
				if ((node.nodeName+"").toLowerCase() == "a" && node.href) {
					window.location.href = node.href;
					return;
				}
				for (var child = node.firstChild; child != null; child = child.nextSibling) {
					search.push(child);
				}
			}
		}
		alert('no link found');
	};
	var nodes = document.querySelectorAll(".boxlink");
	for (var i = 0; i < nodes.length; i++) {
		(function(){
			var node = nodes[i];
			node.addEventListener('click', function (event) {
				click(node)
			}, true);
		})();
	}
}, false);

// -------------------------------
// function that load next picture
// -------------------------------
function loadNextPhoto(page, next_photo_id, max_visual) {

	if (next_photo_id!='null')
	{
		window.currentVisual = next_photo_id;
	}
	/*if (typeof window.currentVisual == "undefined") {
		// first time calles --> believe parameter, but check that it's in range ( 1 to n)
		next_photo = (next_photo + max_visual - 1) % max_visual + 1;
		window.currentVisual = next_photo;
	} else {
		// for later calls ignore parameter
		window.currentVisual = (window.currentVisual + max_visual) % max_visual + 1;
		next_photo = window.currentVisual;
	} */
	return loadBiscuits(page, window.currentVisual);

/*clickNextBtn();
	if ((next_photo != 1) && ((next_photo -1) % 5 == 0)) {
		return loadBiscuits('_ad_'+page, parent_id,next_photo,max_visual);
	} else {
		return loadBiscuits(page, parent_id, next_photo,max_visual);
	}*/
}

// -----------------------------------
// function that load previous picture
// -----------------------------------
function loadPreviousPhoto(page, prev_photo_id, max_visual) {

	if (prev_photo_id!='null')
	{
		window.currentVisual = prev_photo_id;
	}


	/*
	if (typeof window.currentVisual == "undefined") {
		// first time calles --> believe parameter, but check that it's in range ( 1 to n)
		prev_photo = (prev_photo-1 + max_visual) % max_visual + 1;
		window.currentVisual = prev_photo;
	} else {
		// for later calls ignore parameter
		window.currentVisual = (window.currentVisual - 2 + max_visual) % max_visual + 1;
		prev_photo = window.currentVisual;
	}*/
	
	return loadBiscuits(page, window.currentVisual);

/*clickPrevBtn();
	if ((prev_photo != 1) &&((prev_photo - 1) % 5 == 0)) {
		return loadBiscuits('_ad_'+page, parent_id, prev_photo,max_visual);
	} else {
		return loadBiscuits(page, parent_id, prev_photo,max_visual);
	}*/
}


// ------------------------------------
// loads the biscuits into the new page
// ------------------------------------
function loadBiscuits(page,next_photo_id){
	if (document.getElementById('photoSpot').childNodes[0] != null) {
		document.getElementById('photoSpot').childNodes[0].id='shaded';
	}
	var bid_arr = ['contentSpot','photoSpot','photoCredit','navSpot1','navSpot2','socials','message'];

	var loadingurl = page+'?guid='+next_photo_id;
	//if (gup('pfd')!=null) loadingurl = loadingurl +'&pfd='+gup('pfd');

	getRemoteBiscuit_mod(bid_arr, loadingurl);
	window.location.hash="top_";
	window.location.hash="top";
	return false;
}


function ajaxloaderStart(replaceimg,componentid)
{
	if (document.getElementById(componentid) != null)
	{
		var nsrc = document.getElementById(componentid).childNodes[0].src;
		nsrc = nsrc.replace(replaceimg,"loader.gif");
		nsrc = nsrc.replace("format=jpg","format=gif");
		document.getElementById(componentid).childNodes[0].src = nsrc;
		setTimeout(function(){
			ajaxloaderStop(replaceimg,componentid);
		},10000);
	}
}
function ajaxloaderStop(recoverimg,componentid)
{
	if (document.getElementById(componentid) != null)
	{
		var nsrc = document.getElementById(componentid).childNodes[0].src;
		nsrc = nsrc.replace("loader.gif",recoverimg);
		nsrc = nsrc.replace("format=gif","format=jpg");
		document.getElementById(componentid).childNodes[0].src = nsrc;
	}
}

// ------------------------------------
// loads the biscuits into the new page
// ------------------------------------
function expandSocials(page){

	var bid_arr = ['socials'];
	var item,id;
	if (typeof window.currentVisual == "undefined")
	{
		item = gup('guid');
		id = gup('id');
	}
	else
	{
		item = window.currentVisual;
		id = gup('id');
	}
	var loadingurl = page+'?guid='+item+'&id='+id+'&sv=1';
	//if (gup('pfd')!=null) loadingurl = loadingurl +'&pfd='+gup('pfd');

	ajaxloaderStart('toolsave.png','loadingShare');

	getRemoteBiscuit_mod(bid_arr, loadingurl);
	return false;
}

// ------------------------------------
// loads the biscuits into the new page
// ------------------------------------
function expandSocialsEmail(page){

	var bid_arr = ['socials'];
	var item,id;
	if (typeof window.currentVisual == "undefined")
	{
		item = gup('guid');
		id = gup('id');
	}
	else
	{
		item = window.currentVisual;
		id = gup('id');
	}
	var loadingurl = page+'?guid='+item+'&id='+id+'&sv=2';
	//if (gup('pfd')!=null) loadingurl = loadingurl +'&pfd='+gup('pfd');

	ajaxloaderStart('toolemail.png','loadingEmail');

	getRemoteBiscuit_mod(bid_arr, loadingurl);
	return false;
}

// ------------------------------------
// loads the biscuits into the new page
// ------------------------------------
function expandSocialsSMS(page){

	var bid_arr = ['socials'];
	var item,id;
	if (typeof window.currentVisual == "undefined")
	{
		item = gup('guid');
		id = gup('id');
	}
	else
	{
		item = window.currentVisual;
		id = gup('id');
	}
	var loadingurl = page+'?guid='+item+'&id='+id+'&sv=3';
	//if (gup('pfd')!=null) loadingurl = loadingurl +'&pfd='+gup('pfd');

	ajaxloaderStart('toolsms.png','loadingSMS');

	getRemoteBiscuit_mod(bid_arr, loadingurl);
	return false;
}

// ------------------------------------
// loads the biscuits into the new page
// ------------------------------------
function compactSocials(page){

	var bid_arr = ['socials'];
	var item, id;
	if (typeof window.currentVisual == "undefined")
	{
		item = gup('guid');
		id = gup('id');
	}
	else
	{
		item = window.currentVisual;
		id = gup('id');
	}
	var loadingurl = page+'?guid='+item+'&id='+id;
	//if (gup('pfd')!=null) loadingurl = loadingurl +'&pfd='+gup('pfd');
	getRemoteBiscuit_mod(bid_arr, loadingurl);
	return false;
}

// --------------------------
// modified platform function
// --------------------------
//function getRemoteBiscuit_mod(id, page, biscuitid, loadcontent, parent_id, next_photo, max_visual) {
function getRemoteBiscuit_mod(ids, page) {

	//var origin_page = page;
	var xhr = getXHRObject();
	xhr.open('get',page,true);

	if(xhr.setRequestHeader) {
		xhr.setRequestHeader("Cache-Control", "no-transform");
	}

	xhr.onreadystatechange = function() {

		if(xhr && xhr.readyState === 4){
			if (xhr.status === 200 || xhr.status === 304) {
				var response = xhr.responseText;
				for(var i = 0, len = ids.length; i < len; ++i)
				{
					var curElement = ids[i];
					var element = Netbiscuits.core.helpers.getElement(curElement);
					//element.innerHTML = getBiscuitContentById(curElement, response);
					var newelement = getBiscuitContentById(curElement, response);
					if (element != null && newelement != null)
					{						
						/*if (curElement == 'content')
						{
							//alert("skip elemeent?: "+newelement.innerHTML.length);
							if (newelement.innerHTML.indexOf('http://s0.2mdn.net/viewad/817-grey.gif') > 0 ||
								newelement.innerHTML.indexOf('decodeURIComponent(\'\')') > 0 ||
								newelement.innerHTML.length <= 60)
								{
								//alert('skip');
								var nextpage = origin_page.substring(4, page.length);
								switchAdToPic(nextpage, parent_id, next_photo, max_visual);
								break;
							}
						}*/
						//alert('[-]'+newelement.innerHTML);
						element.parentNode.replaceChild(newelement, element);
					} /*else {
						if (element = null) alert('element is null: '+curElement);
						else alert('newelement is null: '+curElement);
					}*/
				//Netbiscuits.core.ajax.trigger(element);
				}
			}
		}

	};

	xhr.send(null);
}

//----------------------------------------------
getXHRObject = function () {
	var xhr = null;
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		} catch(e1) {
			try {
				xhr = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e2) {
				try {
					xhr = new ActiveXObject("Msxml2.XMLHTTP.4.0");
				} catch(e3) {
					try {
						xhr = new ActiveXObject("Msxml2.XMLHTTP.5.0");
					} catch(e4) {
						Netbiscuits.core.helpers.handleError("Error - ActiveX component not found");
						xhr = null;
					}
				}
			}
		}
	} else {
		//device does not provide a xmlhttprequest object
		Netbiscuits.core.helpers.handleError("Device is not ajax capable");
	}
	return xhr;
}

// parse recieved response and find correct div
function getBiscuitContentById(id, response)
{
	var cc = document.createElement("div");
	//cc.innerHTML = xhr.responseText;
	cc.innerHTML = response;
	var search = [cc];
	var newcontent = null;
	for (newcontent = search.shift(); newcontent != null; newcontent = search.shift()) {
		if (newcontent.nodeType == newcontent.ELEMENT_NODE) {
			if (newcontent.id == id) {
				break;
			} else {
				for (var child = newcontent.firstChild; child != null; child = child.nextSibling) {
					search.push(child);
				}
			}
		}
	}
	return newcontent;
}

//-------------------------------------------
function gup( name )
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
		return "";
	else
		return results[1];
}
