function clearThis(e, val) {
	if($(e).attr("value") == val) {
		$(e).attr("value", "");
		$(e).parent(".error").removeClass("error");
	}
}

function closePopupVideo() {
	$("#popUpBg").fadeOut("normal");
	$("#popUpVideo").fadeOut("normal");
	$("#popUpVideo .video").html("");
}

function openPopupVideo() {
	$("#popUpBg").fadeIn("normal");
	$("#popUpVideo").fadeIn("normal");
	$("#popUpVideo .video").html('<object width="846" height="519"><param name="movie" value="http://www.youtube.com/v/65BIM2b42BU?version=3&autohide=1&showinfo=0&autoplay=1"></param><param name="allowScriptAccess" value="always"></param><param name="allowFullScreen" value="true"></param><embed src="http://www.youtube.com/v/65BIM2b42BU?version=3&autohide=1&showinfo=0&autoplay=1" type="application/x-shockwave-flash" allowscriptaccess="always" width="846" height="519" allowFullScreen="true"></embed></object>');	
	return false;
}

function kiireFormValidate() {

	noError = true;

	if($("#nimi").attr("value") == "") {
		$("#nimi").parent().parent(".row").addClass("error");
		noError = false;
	} else { $("#nimi").parent().parent(".row").removeClass("error"); }
	if($("#email").attr("value") == "") {
		$("#email").parent().parent(".row").addClass("error");
		noError = false;
	} else { $("#email").parent().parent(".row").removeClass("error"); }
	if($("#maara").attr("value") == "") {
		$("#maara").parent().parent(".row").addClass("error");
		noError = false;
	} else { $("#maara").parent().parent(".row").removeClass("error"); }
	if($("#pdf").attr("value") == "") {
		$("#pdf").parent().parent(".row").addClass("error");
		noError = false;
	} else { $("#pdf").parent().parent(".row").removeClass("error"); }
	
	return noError;
}

function twitterCallback2(twitters) {
  var statusHTML = [];
  for (var i=0; i<twitters.length; i++){
    var username = twitters[i].user.screen_name;
    var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
      return '<a href="'+url+'">'+url+'</a>';
    }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
      return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
    });
    statusHTML.push('<h3>'+relative_time(twitters[i].created_at)+'</h3><p>'+status+'</p>');
  }
  document.getElementById('twitter_update_list').innerHTML = statusHTML.join('');
}

function relative_time(time_value) {

	time_value = time_value.replace(/ \+[\d]{4}/, "");

	var d = new Date(time_value);
	
	var h = d.getHours();
	var m = d.getMinutes();
	
	if(h < 10) h = "0" + h; 
	if(m < 10) m = "0" + m; 
	

	return d.getDate() + "." + (d.getMonth() + 1) + "." + d.getFullYear() + " - " + h + ":" + m;
}

var currentPopup = null;

function showTooltip() {
	$("#pubItTooltip").fadeIn("fast");
}

function hideTooltip() {
	$("#pubItTooltip").fadeOut("fast");
}

function openNavi(e) {
	$("#navi .popup").hide();
	$("#navi a").removeClass("hover");
	
	$(e).addClass("hover");
	$(e).parent(".naviItem").children(".popup").show();
	$(e).parent(".naviItem").hover(function () {}, function() { 
		$(".popup").hide();
		$("#navi a").removeClass("hover");
	});
}

function findX(obj) {
	var val = 0;
	
	if (obj.offsetParent) {
		do {
			val += obj.offsetLeft;
		} while (obj = obj.offsetParent);
	}
	
	return val;
}

function findY(obj) {
	var val = 0;
	
	if (obj.offsetParent) {
		do {
			val += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	
	return val;
}


var languageContainer = 0;

function getPageSizeWithScroll() {
	var xScroll, yScroll;
		
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	return pageHeight;
}

function getPopUpTop() {

	var pageY;
	var scrollY;

	if (window.innerHeight) {	
		pageY = window.innerHeight;
	} else { // IE
		pageY = document.documentElement.clientHeight;
	}
	
	if(window.pageYOffset) {
		scrollY = window.pageYOffset;
	} else if(document.body.scrollTop) {
		scrollY = document.body.scrollTop;
	} else {
		scrollY = document.documentElement.scrollTop;
	}
	
	var topValue = scrollY + pageY/2 - document.getElementById("kiirePopup").offsetHeight/2
	
	if(document.getElementById("kiirePopup").offsetHeight > pageY) {
		topValue = scrollY + 10;
	}
	
	return topValue;
}

function openPopup() {
	document.getElementById("popUpBg").style.height = getPageSizeWithScroll() + "px";
	
	//document.getElementById("popUpBg").style.display = "block";
	//document.getElementById("kiirePopup").style.display = "block";
	
	$("#popUpBg").css("opacity","0");
	$("#popUpBg").show();
	$("#popUpBg").fadeTo("fast", 0.37);
	$("#kiirePopup").fadeIn("fast");
	
	document.getElementById("kiirePopup").style.top = getPopUpTop() + "px";
}

function closePopup() {
	//document.getElementById("popUpBg").style.display = "none";
	$("#popUpBg").fadeOut("fast");
	document.getElementById("kiirePopup").style.display = "none";
	document.getElementById("kiirePopup").style.top = "-9999px";
}
