var Scroll;

window.addEvent('domready', function() {
 if($('walkthroug')) {
 Scroll = new Fx.Scroll('walkthroug', {
     wait: false,
     duration: 1000, wheelStops: false,
     transition: Fx.Transitions.Quad.easeInOut
    });
  }	
});

function scroll(numScroll) {
    Scroll.scrollTo(numScroll * 610, 0);
}


function prev_month(username, month, year, type) {
  if(!month || !year) return false; 
  
  if(month == 1) {
    month = 12;
    year--;
  }else{
    month--;
  }
  
  var data = 'username='+username+'&month=' + month + '&year=' + year;
  new Ajax('/browsemainmonths', {update: $('calendar'), data: data, method: 'post', evalScripts: true}).request();
}

function next_month(username, month, year, type) {
  if(!month || !year) return false; 
  
  if(month == 12) {
    month = 1;
    year++;
  }else{
    month++;
  }
  var data = 'username='+username+'&month=' + month + '&year=' + year;

  new Ajax('/browsemainmonths', {update: $('calendar'), data: data, method: 'post', evalScripts: true}).request();
}

function redirect(url){
	window.location = url;
}

function nospam(user, domain) {
	locationstring = "mailto:" + user + "@" + domain;
	window.location = locationstring;
}

function externalLink() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank";
		}
	}
}

function vote(postid, score) {
  var data = 'postid='+postid+'&score='+score;
  new Ajax('/vote', {update: $('response-'+postid),  evalScripts:true, data: data, onComplete: function(s) {
	if (s != '' && s != null) {
  	var w = (s/5)*100;
		$('qwe-'+postid).innerHTML = $('qwe-'+postid).innerHTML*1 + 1;
	    $('current-rating-'+postid).setStyle('width', w+'%');
	    $('response-'+postid).setHTML('Currently '+s+'/5 stars');
	    $('response-'+postid).setStyle('display', 'block');
	}
    }}).request();  
}

function soundoff() {
  new Ajax('/sound?' + new  Date().getTime(), {update: $('response'),  method: 'get', evalScripts:true, data: $('soundoff-form')}).request(); 
  return false; 
}

function getPageSize(){
	var xScroll, yScroll;
	
	if(window.innerHeight && window.scrollMaxY){	
		xScroll = document.body.scrollWidth;
		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
		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 = windowWidth;
	}else{
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function initOverlay(){
	var arrayPageSize = getPageSize();
	
	if($('notification'))
		$('notification').style.height = arrayPageSize[1]+"px";
		
	if($('screencast'))
		$('screencast').style.height = arrayPageSize[1]+"px";
	
}

function toggleNotification(){
	//new Effect.toggle('notification', 'appear', {duration:1.0, transition: Effect.Transitions.sinoidal});
}

function toggleScreencast(){
	$('terms').style.display = "none";
	$('policy').style.display = "none";
	if($('screencast').style.display == "none"){
		$('screencast').style.display = "block";
		$('walkthrough').style.display = "block";
	}else{
		$('screencast').style.display = "none";
		$('walkthrough').style.display = "none";
	}
}


function toggleScreencastTerms(){
	$('walkthrough').style.display = "none";
	$('policy').style.display = "none";
	if($('screencast').style.display == "none"){
		$('screencast').style.display = "block";
		$('terms').style.display = "block";
	}else{
		$('screencast').style.display = "none";
		$('terms').style.display = "none";
	}
}

function toggleScreencastPolicy(){
	$('walkthrough').style.display = "none";
	$('terms').style.display = "none";
	if($('screencast').style.display == "none"){
		$('screencast').style.display = "block";
		$('policy').style.display = "block";
	}else{
		$('screencast').style.display = "none";
		$('policy').style.display = "none";
	}
}

function materialize_response() {
  new Effect.Appear('feedback');
}

function windowOnload(){
  externalLink();
}

function restart(formid) {
  Form.reset(formid);
}

function remove_post(postid, is_user_page) {
  if(is_user_page) director = "/1";
  else if(!is_user_page) director = "";
  if(confirm("Are you sure you want to delete this post?")){
    window.location.href = "http://www.thinkpost.com/removepost/" + postid + director;
  }
}

function remove_postpt(postid) {
  if(confirm("Are you sure you want to delete this post?")){
    window.location.href = "http://www.thinkpost.com/removepostpt/" + postid;
  }
}

function remove_comment(commentid, is_user_page) {
  if(is_user_page) director = "/1";
  else if(!is_user_page) director = "";
  if(confirm("Are you sure you want to delete this comment?")){
    window.location.href = "http://www.thinkpost.com/removecomment/" + commentid + director;
  }
}

function remove_sound(id) {
  if(confirm("Are you sure you want to delete this soundoff?")){
    window.location.href = "http://www.thinkpost.com/removesoundoff/" +id;
  }
}

function remove_news(newsid, is_user_page) {
  if(is_user_page) director = "/1";
  else if(!is_user_page) director = "";
  if(confirm("Are you sure you want to delete this news post?")){
    window.location.href = "http://www.thinkpost.com/removenews/" + newsid + director;
  }
}

function remove_happiness(happinessid, is_user_page) {
  if(is_user_page) director = "/1";
  else if(!is_user_page) director = "";
  if(confirm("Are you sure you want to delete this answer?")){
    window.location.href = "http://www.thinkpost.com/removehappiness/" + happinessid + director;
  }
}

function remove_happiness_comment(happiness_commentid, is_user_page) {
  if(is_user_page) director = "/1";
  else if(!is_user_page) director = "";
  if(confirm("Are you sure you want to delete this comment?")){
    window.location.href = "http://www.thinkpost.com/removehappinesscomment/" + happiness_commentid + director;
  }
}

function remove_special_comment(commentid, is_user_page) {
  if(is_user_page) director = "/1";
  else if(!is_user_page) director = "";
  if(confirm("Are you sure you want to delete this comment?")){
    window.location.href = "http://www.thinkpost.com/removespecialcomment/" + commentid + director;
  }
}

//function showTour(id){
//	new Effect.toggle(id,'appear',{duration:0.5});
//}

//function insert_tag() {
//  // Execute the mceTemplate command without UI this time
//  tinyMCEPopup.execCommand('mceTemplate', false, $('data').innerHTML);
//
//  // Close the dialog
//  tinyMCEPopup.close();
//}
//
//if ((document.getElementById || document.all) && document.images) {
//	window.onload = windowOnload();
//}
