var selectedDiv = '';
var secondsx = 1;


function uncheck() {
  $('#like').attr('checked', false);
  $('#dislike').attr('checked', false);
  }


function clearInput(inputid) {
	inputdiv = document.getElementById(inputid);
	if (inputdiv.value == "Your message" || inputdiv.value == "Your Name" || inputdiv.value == "Your Email Address") {
	  inputdiv.value = "";
	  } else { return; } 
  }

function setLinksTarget(divid) {
  $('#' + divid + " a").each(function () {
    $(this).attr("target", "_blank");
    });
  }

function initLoaded() {  
  loadedDivs['about'] = false;
  loadedDivs['contact'] = false;
  loadedDivs['events'] = false;
  loadedDivs['projects'] = false;
/*
  loadedDivs['upcomingevents'] = false;
  loadedDivs['pastevents'] = false;
  loadedDivs['ongoing'] = false;
  loadedDivs['pastprojects'] = false;
*/
  loadedDivs['table'] = false;
  loadedDivs['news'] = false;

  }

var news = [];
var News = function(index) {
  this.index = index;
  this.id = 0;
  this.title = '';
  this.header = '';
  this.headerLen = 0;
  this.titleLen = 0;
  this.len = 0;
  this.right = (185 - $(window).width());
  //this.loop = 1000;
  this.width = 0;  
  this.maxLeft = 0;
  this.divid = '#news' + index;
  this.divid1 = '';
  this.divid2 = '';
  }

News.prototype.add = function(data) {
  this.id = data.id;
  this.divid1 = "#One" + this.index;
  this.divid2 = "#Two" + this.index;
  this.title = data.title;
  if (data.header == null || data.header == '') {
    this.header = ' ';
    } else {
    this.header = data.header;
    }
  this.headerLen = this.header.length;
  this.titleLen = data.title.length;
  this.len = this.headerLen + this.titleLen;
  this.appearInterval = Math.floor(Math.random() * 25000);
  }

News.prototype.getHtml = function(no) {
  var html = ''; 
  html += "<span class='singleMover' id='" + no + this.index + "'>";
  html += "<span class='title' id='title" + no + this.index + "'>";
  html += this.title;
  html += "</span> ";
  html += " <span class='header' id='header" + no + this.index + "'>";
  html += this.header.replace("<br />", "");
  html += "</span>";
  html += "</span>";
  return html;
  }

News.prototype.doCss = function() {
  var initRight = this.right + "px";
  $(this.divid).css("right", initRight);   
  }

News.prototype.doAnimate = function(currRight) {
  var thisNews = this;
  var diff = this.maxLeft - currRight;
  var timeTaken = Math.floor((diff / 2) * ((25 - this.index) * 3));
  /*if (thisNews.index == 7) {
    alert("Time to take: " + timeTaken + " MaxLeft " + thisNews.maxLeft + "currWidth= " + $(thisNews.divid).width() + " currRight=" + currRight);
    }*/
  $(thisNews.divid).animate({'right': thisNews.maxLeft + "px"}, timeTaken, "linear", function() {
    $("#One" + thisNews.index).remove();
    var move = 0 - thisNews.maxLeft;
    move += "px";
    $(thisNews.divid).css("right", "0px");
    $("#Two" + thisNews.index).attr("id", "One" + thisNews.index);
    $(thisNews.divid).append(thisNews.getHtml("Two"));
    /*if (thisNews.index == 1) {
      alert("maxLeft: " + thisNews.maxLeft + " currentRight= " + $(thisNews.divid).css("right"));
      }*/      
    thisNews.doAnimate(0);
    });
  }

function falseMouseOver(inc) {
  var currRight = $(news[inc].divid).css("right");
  currRight = parseInt(currRight.substring(0, currRight.length - 2));  
  news[inc].doAnimate(currRight);
  }

function trueMouseOver(inc) {
  $(news[inc].divid).stop();
  }
  
function stopNews() {
  $('.movingWrapper').stop();
  //$('#links').css("background", "");
  //$('#links').css("z-index", "0");
  //$('#background').show("slow");
  }

function showNews() {
  //alert(loadedDivs['news']);
  if (selectedDiv != 'news') {
    $('#' + selectedDiv).fadeOut();
    }
  selectedDiv = 'news';
  //$('#links').css("background", "#000000");
  //$('#links').css("z-index", "10");
  //$('#background').hide("slow");
  if (loadedDivs['news'] == true) {
    //alert(noOfNews);
    for (var t=0; t < noOfNews; t++) {
      falseMouseOver(t);
      }
    $('#news').fadeIn();
    } else {
    loadedDivs['news'] = true;
    var randomNo = Math.floor(Math.random() * 9);
    var windowWidth = $(window).width();
    $('#news').show();
    $.getJSON("getNewsJSON.php", {}, function(data) {
      noOfNews = data.length;  
      var i=0;    
      while (newsData = data[i]) {
        news[i] = new News(i);
        news[i].add(newsData);
        var newsHtml = '';
        newsHtml += "<div class='staticWrapper' id='static" + i + "' onmouseover='trueMouseOver(" + i + ")' onmouseout='falseMouseOver(" + i + ")'>";
        newsHtml += "<span class='movingWrapper' id='news" + i + "'>";
        newsHtml += news[i].getHtml("One");
        newsHtml += news[i].getHtml("Two");
        newsHtml += "</span>";
        newsHtml += "</div>";
        $('#news').append(newsHtml);
        var newMarginTop = 5 + (i * 2);
        newMarginTop += "px";
        var newFontSize = 12 + i;
        newFontSize += "px";
        //alert(news[i].divid);
        //$("#static" + i).css("margin-top", newMarginTop);
        //$(news[i].divid).css("font-size", newFontSize);
        news[i].width = $(news[i].divid).width();
        news[i].titleLen = $("#title" + i).width();
        news[i].maxLeft = Math.floor(news[i].width / 2);
        news[i].doCss();
        if (i == randomNo) {
          var appearInterval = 1;
          } else {
          var appearInterval = news[i].appearInterval;
          }
        var funcName = "news[" + i + "].doAnimate(" + news[i].right + ")";
        //eval(funcName);        
        window.setTimeout(funcName, appearInterval);
        i++;  
        }
      setLinksTarget("news");
      });
    }
  }

function fetchText(divid) {
  //$('#' + divid).fadeIn(150);  

  if (loadedDivs[divid] == false) {
    $('#' + divid).show();
    $('#' + divid).html("Loading...");    
    var url = "mainGets/" + divid + ".php";
    $.get(url, {}, function(Response) {
      if (selectedDiv != divid) {
        return;
        }
      $('#' + divid).hide();
      $('#' + divid).html(Response);
      if (divid == 'table') {
        $('#randomTitle').html(pageTitle);
        }
      $('#' + divid).slideDown("slow");
      loadedDivs[divid] = true;
      setLinksTarget(divid);
      if (divid == 'table') {
        $('#like').click(function() {
        doLike(this.checked, '<?php echo $title; ?>');
        });
        $('#dislike').click(function() {
        doDislike(this.checked, '<?php echo $title; ?>');
        });
        }
      });
    } else {
    $('#' + divid).fadeIn();
    }
  }
          
function showText(divid) {
	if (selectedDiv == divid && selectedDiv != "news") {
	  secondsy = new Date().getTime();
	  secondsbetweenclicks = secondsy - secondsx;
	  if (secondsbetweenclicks > 1500) {	
	    $('#' + divid).fadeOut(150);
	    selectedDiv = '';
	    }
	  } else if (selectedDiv != '') {
      if (selectedDiv == 'news') { stopNews(); }
	    $('#' + selectedDiv).fadeOut(150, function () {
        //$('#' + divid).slideDown("slow");
        if (divid != "news") {
          window.scrollTo(0,0);
          fetchText(divid);
          } else {
          showNews();
          }
        });
	    selectedDiv = divid;		
	    } else {
	    //$('#' + divid).slideDown("slow");
        if (divid == 'news') {
          showNews();
          } else {
          fetchText(divid);
          }
	      selectedDiv = divid;
      //fetchText(divid);
	    }
	secondsx = new Date().getTime();
  }


function doLike(checked, title) {
	if (checked == false) { return false; }
  if (title != "none") {
    title = pageTitle;
    } 	
  $('#pollq').slideUp('slow');
  $.post('liketitle.php', {
    'title': title
    },
  function (Response) {
    var html = "<br />Names people have liked are:<br /><br />" + Response;
    $('#likelist').html(html);
    $('#likelist').slideDown("slow", function() {
      window.scrollTo(0,500);
      });
    });
  }

//The PhP script knows to not add the title to the DB if title = "none"
function doDislike(checked, title) {
	doLike(checked, 'none');	
	}


function sendMail() {
  var contactName = $('#contactname').val();
  var contactEmail = $('#contactemail').val();
  var contactText = $('#contacttext').val();
  if (contactText == '' || contactText == "Your message.") {
    alert("Please leave a message!");
    return false;
    }
  $('#contactspan').html('Sending Message...');
  $.post('contactmail.php', {
    'name': contactName,
    'email': contactEmail,
    'text': contactText
    },
  function() {
    var html = "Email sent to info AT camputer DOT org <br /> Thanks for getting in touch!";
    $('#contact').html(html);
    });
  }


function doSearch() {
  var searchString = $('#searchInput').val();
    
  if (searchString != '') {  
    if (selectedDiv != '') {
      $('#' + selectedDiv).hide();
      if (selectedDiv == 'news') { stopNews(); }
      }  
    $('#search').show();
    selectedDiv = "search";
    $('#search').html("Searching...");      
    $.post("mainGets/search.php", {
      'search': searchString
      }, function(html) {
      $('#search').hide(5, function() {
        $('#search').html(html);  
        $('#search').slideDown();      
        });      
      });
    }
  }
