var timerArray = false;
function setLoadArticleTimer(divid,articleid) {
//	if (!timerArray){
//		plantedTimers = new Array();
//		timerArray = true;
//	} 
//	plantedTimers[divid] = setTimeout(function() {
//								try {document.getElementById('article_nextId_'+articleid).onclick();}catch(e){}
//								}, 180000 + Math.floor(Math.random()*60000));	
}

var homepageSlideObjects = new Array();

var containers = new Array();


var homepageSlideTimer;

var exactTimeout = 30000;


function registerHomepageSlide (divid,articleid,timeout,nextarticle) {


	var div = [];
	div['divid'] = divid;
	div['articleid'] = articleid;
	div['timeout'] = timeout;
	div['nextarticle'] = nextarticle;


	var exists = false;	
	var art = {};
	art.divid = divid;

	for (var i=0; i<containers.length; i++) {
		//console.log("divid:"+containers[i].divid)
		if (containers[i].divid==divid) {
			art = containers[i];
			exists = true;
			break;
		}				
	}

	if (!exists ) {
		art.arr = [];
		containers.push(art);
	}

	art.arr.push(div);

	//console.log("art:"+art.arr[0].divid);
	
	
	if (!homepageSlideTimer) {
		homepageSlideTimer = setTimeout(function(){loadNextArticleHomepageSlide(divid,articleid);}, exactTimeout);
	}
}


function moveArticle(divid, width, articleid) {


	var speed = "5000";
	
	$('#'+divid).animate({"marginLeft": "-="+width+"px"}, speed, function() {
		
		document.getElementById(divid).parentNode.appendChild(document.getElementById(divid));
		document.getElementById(divid).style.marginLeft="0";
	});
	
	window.clearTimeout(homepageSlideTimer);

	var nextDivToPush;
	
	var divIdToCompare = divid.substring(divid.indexOf("_")+1);

	for (var i=0; i<containers.length; i++) {
		var cont = containers[i];
		if (cont.divid==divIdToCompare) {
			var arr = cont.arr;
			for (var j=0; j<arr.length; j++) {
				var div = arr[j];
				//console.log("div.nextarticle"+div.nextarticle);
				//console.log("articleid"+articleid);
				if (div.nextarticle == articleid) {
					arr.splice (j, 1);
					arr.push(div);
					if (i==containers.length-1) {
						nextDivToPush = containers[0].arr[0];	
					} else {
						nextDivToPush = containers[i+1].arr[0];
					}
					break;
				}
			}
		}
	}	


	homepageSlideTimer = setTimeout(function(){loadNextArticleHomepageSlide(nextDivToPush.divid,nextDivToPush.articleid);}, exactTimeout);
	
	
}



function loadNextArticleHomepageSlide (divid,articleid) {


	var stop = false;
	for (var i=0; i<containers.length; i++ ) {
		if (containers[i].divid==divid) {
			var arr = containers[i].arr;
			for (var j=0; j<arr.length; j++) {
				var div = arr[j];
				if (div.articleid == articleid) {
					document.getElementById('article_nextId_'+div.articleid).onclick();
					break;
				}
			}			
		}
	}
}


function loadurl(divid,url) {
    if (window.XMLHttpRequest) { 
    	xmlhttp = new XMLHttpRequest(); 
    } else if (window.ActiveXObject) { 
    	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.open("GET", url, false);
	xmlhttp.send(null);
	document.getElementById(divid).innerHTML = xmlhttp.responseText;
	cutArticleExcessContentTextAjaxSpec(findFirstParentWithClass(document.getElementById(divid), 'cloudContainer'), divid);
	Animation.fadeIn(document.getElementById(divid));
    return false;
}
/*
function loadarticle(divid,articleid) {
	Animation.fadeOut(document.getElementById('articleContent_'+divid), function() {
		loadurl('articleContent_'+divid,"tmpl/ptp-hr/jsload.jsp?id="+articleid+"&oid="+divid);
		//setLoadArticleTimer(divid,articleid);
		registerHomepageSlide (divid,articleid);
	});
}
*/
function loadpoll(oid, pollid, additional) {
	Animation.fadeOut(document.getElementById('anketa_'+oid), function() {
		loadurl('anketa_'+oid,"tmpl/ptp/jsload.jsp?type=anketa&id="+pollid+"&oid="+oid+additional);
	});
	
//	loadurl('anketa_'+oid,"tmpl/ptp-hr/jsload.jsp?type=anketa&id="+pollid+"&oid="+oid+additional);
//	fbPublishVote();
}

function fbPublishVote() {
		FB.api('/me', function(response) {
		var userName = response.name;
		var obj = 
		{
			display: "iframe",
			method: "stream.publish",
      	  	message: "",
      	  	attachment: 
          		{
            		name: "Press The President",
            		description: userName + " je upravo glasao.",
            		href: "http://"+window.location.host
        		},
        	action_links: 
            	[
            		{ 
            			text: "Press The President", 
                		href: window.location.href
            		}
        		],
        	user_prompt_message: "Objavi svoj komentar na Facebook:",
        	user_message_prompt: "Objavi svoj komentar na Facebook:"
		}
		streamPublish(obj, "fbPublishVoteLocal();");
	});

}

function fbPublishVoteLocal() {
	
}

function pollVote(oid, pollid, valueid, username) {
	if(username=='')
		openLoginBox();
	else
		loadpoll(oid, pollid, "&vote="+valueid+"&results=true");
}


function measureHeight(element) {

	var measurediv = document.createElement('div');

	measurediv.innerHTML = element.innerHTML;
	measurediv.style.height = '';
	measurediv.style.left = '-2000px';
	measurediv.style.position = 'absolute';
	var height = measurediv.offsetHeight;

	return height;
}

function reflowSlidingElements(layoutitemoid) {
	
	var cloudContainer = $("div.cloudContainer[layoutitemoid="+layoutitemoid+"]")[0];
	
	var first = $("div.cloudContainer[layoutitemoid="+layoutitemoid+"] .firstdiv")[0];
	var second = $("div.cloudContainer[layoutitemoid="+layoutitemoid+"] .seconddiv")[0];
	var third = $("div.cloudContainer[layoutitemoid="+layoutitemoid+"] .thirddiv")[0];
	
//	alert("FIRST PERO: "+$(first).height()+" = "+$(first).attr('id')+" // ZDENKO: "+$(cloudContainer).height()+" = "+$(cloudContainer).attr('id'));
//	alert($(second).height()>$(cloudContainer).height());
//	alert("THIRD PERO: "+$(third).height()+" = "+$(third).attr('id')+" // ZDENKO: "+$(cloudContainer).height()+" = "+$(cloudContainer).attr('id'));
	
//	setTimeout(function(){alert (measureHeight(cloudContainer));}, 10000);
	var firstContent = $("div.cloudContainer[layoutitemoid="+layoutitemoid+"] .firstdiv .content")[0];
	var secondContent = $("div.cloudContainer[layoutitemoid="+layoutitemoid+"] .seconddiv .content")[0];
	var thirdContent = $("div.cloudContainer[layoutitemoid="+layoutitemoid+"] .thirddiv .content")[0];

	try {
		//alert (cloudContainer.clientHeight + " = " + first.clientHeight + " x " + second.clientHeight + " x " + third.clientHeight);
		//console.log(cloudContainer.clientHeight + " = " + first.clientHeight + " x " + second.clientHeight + " x " + third.clientHeight);
		try {
			//console.log("\t\t\t --- " + cloudContainer.clientHeight + " // " + first.clientHeight-50);
			while ($(firstContent).html().length>50 && $(first).height()>$(cloudContainer).height()-15) {
				var htmlCont = $(firstContent).html();
				$(firstContent).html(htmlCont.substring(0, htmlCont.length-100)+" ...");
				//console.log("\t\t\t\t\t\t --- " + cloudContainer.clientHeight + " // " + first.clientHeight);
				
			}
		} catch (ex) {
	//		console.log(ex.message);
		}
		//-(cloudContainer.clientHeight/10)
		try {
			//console.log("\t\t\t --- " + cloudContainer.clientHeight + " // " + second.clientHeight);
			//if ($(second).attr('id') == "seconddiv_40496") {alert($(second).height()>$(cloudContainer).height())};
			while ($(secondContent).html().length>50 && $(second).height()>$(cloudContainer).height()-15) {
				var htmlCont = $(secondContent).html();
				$(secondContent).html(htmlCont.substring(0, htmlCont.length-100)+" ...");
				//if ($(second).attr('id') == "seconddiv_40496") {alert($(secondContent).html())};
				//console.log("\t\t\t\t\t\t --- " + cloudContainer.clientHeight + " // " + second.clientHeight);
			}
		} catch (ex) {
			//console.log(ex.message);
		}
		
		try { 
			//console.log("\t\t\t --- " + cloudContainer.clientHeight + " // " + third.clientHeight);

			//if ($(third).attr('id') == "thirddiv_40496") {alert($(third).height()>$(cloudContainer).height())};
			while ($(thirdContent).html().length>50 && parseInt($(third).height())>parseInt($(cloudContainer).height()-1)) {
				var htmlCont = $(thirdContent).html();
				$(thirdContent).html(htmlCont.substring(0, htmlCont.length-100)+" ...");
				//if ($(third).attr('id') == "thirddiv_40496") {alert($(thirdContent).html())};
				//console.log("\t\t\t\t\t\t --- " + cloudContainer.clientHeight + " // " + third.clientHeight);
			}
			//alert($(third).height() + " " + $(cloudContainer).height());
		} catch (ex) {
			//alert(ex.message);
		}
	} catch (e) {
		//console.error(e.message);
	}
//	console.log("layoutitemoid: "+$("div.cloudContainer[layoutitemoid="+layoutitemoid+"]").attr('layoutitemoid'));


/*
function setNextLoadArticleTimer(divid,articleid,timeout) {
	
	if (!timerArray){
		plantedTimers = new Array();
		timerArray = true;
	}
	
	clearTimeout(plantedTimers[divid]);	
	var exactTimeout = (Math.floor(15000 - Math.random()*parseInt(timeout)));
	plantedTimers[divid] = setTimeout(function() {
		try {
			document.getElementById('article_nextId_'+articleid).onclick();
		} catch(e){}
	}, exactTimeout);	
}

function loadurl(divid,url) {
    if (window.XMLHttpRequest) { 
    	xmlhttp = new XMLHttpRequest(); 
    } else if (window.ActiveXObject) { 
    	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.open("GET", url, false);
	xmlhttp.send(null);
	document.getElementById(divid).innerHTML = xmlhttp.responseText;
	cutArticleExcessContentTextAjax(findFirstParentWithClass(document.getElementById(divid), 'cloudContainer'));
	Animation.fadeIn(document.getElementById(divid));
    return false;
}

function loadNexturl(divid,url) {
    if (window.XMLHttpRequest) { 
    	xmlhttp = new XMLHttpRequest(); 
    } else if (window.ActiveXObject) { 
    	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.open("GET", url, false);
	xmlhttp.send(null);
	document.getElementById(divid).innerHTML = xmlhttp.responseText;
	cutArticleExcessContentTextAjaxSpec(findFirstParentWithClass(document.getElementById(divid), 'cloudContainer'), divid);
    return false;
}


function loadNextpicture(divid,url) {
    if (window.XMLHttpRequest) { 
    	xmlhttp = new XMLHttpRequest(); 
    } else if (window.ActiveXObject) { 
    	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.open("GET", url, false);
	xmlhttp.send(null);
	
	document.getElementById(divid).innerHTML = xmlhttp.responseText;
	
    return false;
}


function loadNextarticle(divid,articleid,width) {
	if (eval("nextDivToRender_" + divid).indexOf("next")!=-1) {
		loadNextpicture('nextitempicture_'+divid,"tmpl/ptp-hr/loadPicture.jsp?id="+articleid+"&oid="+divid);
		loadNexturl('nextitem_'+divid,"tmpl/ptp-hr/jsload.jsp?id="+articleid+"&oid="+divid);
	} else {
		loadNextpicture('currentitempicture_'+divid,"tmpl/ptp-hr/loadPicture.jsp?id="+articleid+"&oid="+divid);
		loadNexturl('currentitem_'+divid,"tmpl/ptp-hr/jsload.jsp?id="+articleid+"&oid="+divid);
	}

	setTimeout(function() {
		loadNextarticleInner(divid,articleid,width);
	}, 100);
}

function loadNextarticleInner(divid,articleid,width) {
	var speed = "5000";
	if (eval("nextDivToRender_" + divid).indexOf("next")!=-1) {
	//	alert('1');
		//console.log("currentitem_"+width);
		$('#currentitem_'+divid+', #currentitempicture_'+divid).animate({"left": "-="+width+"px"}, speed, function() {
		
			$('#currentitem_'+divid).css('left','+'+width+'px');
			$('#currentitem_'+divid).html("");

			$('#currentitempicture_'+divid).css('left','+'+width+'px');
			$('#currentitempicture_'+divid).html("");

		});


		$('#nextitem_'+divid).animate({"left": "-="+width+"px"}, speed);
		$('#nextitempicture_'+divid).animate({"left": "-="+width+"px"}, speed);

		eval("nextDivToRender_" + divid +"='currentitem_"+divid+"'");
	} else {
	//alert('2');

		//console.log("nextitem_"+width);

		$('#nextitem_'+divid+', #nextitempicture_'+divid).animate({"left": "-="+width+"px"}, speed, function() {
			$('#nextitem_'+divid).css('left','+'+width+'px');
			$('#nextitem_'+divid).html("");

			$('#nextitempicture_'+divid).css('left','+'+width+'px');
			$('#nextitempicture_'+divid).html("");

		});


		$('#currentitem_'+divid).animate({"left": "-="+width+"px"}, speed);
		$('#currentitempicture_'+divid).animate({"left": "-="+width+"px"}, speed);

		eval("nextDivToRender_" + divid +"='nexttitem_"+divid+"'");
	}
	registerHomepageSlide (divid,articleid);
	//setNextLoadArticleTimer(divid,articleid, homepage_article_timeout);

}


function loadnextarticle(divid,articleid) {
	loadurl('nextarticleContent_'+divid,"tmpl/ptp-hr/jsload.jsp?id="+articleid+"&oid="+divid);
}
*/
	
}

