function changeBG(strDiv, strColor){
	var divStyle = getStyleObject(strDiv);		
	divStyle.backgroundColor = strColor;
}

function moveSlider(direction){  
  archive_cal.hideCalendar();
  
  if((direction == -1 && slide_pos < slide_max - 1) || (direction == 1 && slide_pos > 0)){
    slide_pos += (direction * -1);	  
    startMoving(direction);
  }
}

function startMoving(direction){
  strip_info[(slide_max-1) - slide_pos][2] = 0;
  
  clearTimeout(tId);
  
  var stripDate = new Date(strip_info[(slide_max-1) - slide_pos][0]);
  document.getElementById("title").innerHTML = '<h1>' + stripDate.format("fullDate") + '</h1>';
  isSunday = stripDate.format("ddd") == 'Sun';
  
  if(!isSunday) changeHeight(def_height, min_height);
  
  
  var divStyle = getStyleObject("usercomments");
  setCommentLink(strip_info[(slide_max-1) - slide_pos][1], divStyle.display);
  //showShide("usercomments", "none");
  strForm  = '<form name="frmComment">';
  strForm += '<div class="frmtext"><label for="frmname">Your Name</label><input type="text" name="name" id="frmname" maxlength="50" /></div>';
  strForm += '<div class="frmtext"><label for="frmemail">Your Email</label><input type="text" name="email" id="frmemail" maxlength="50" /></div>';
  strForm += '<div class="frmtext"><label for="frmtitle">Comment Title</label><input type="text" name="title" id="frmtitle" maxlength="50" /></div>';
  strForm += '<div class="frmtextarea"><label for="frmcommentext">Your Comment</label><textarea rows="4" cols="40" name="commentext" id="frmcommentext" /></textarea></div>';
  strForm += '<div class="frmsubmit"><input type="button" name="submitButton" value="Submit Comment" onclick="this.disabled=true;submitComment();" /><input type="button" name="cancelButton" value="Cancel" onclick="toggleComments(\'off\');" /></div>';
  strForm += '</form>';
  
  document.getElementById('form_wrapper').innerHTML = strForm;
  document.getElementById("allcomments").innerHTML  = '<div class="normal commentitem"><p>No comments. Be the first to leave a comment!</p></div>';    
  toggleComments('off');
  
  if(slide_max - 1 == 0){
    arrow_state('next', dis_alpha);
    arrow_state('previous', dis_alpha);
  }else if(slide_pos == slide_max - 1){
    arrow_state('next', dis_alpha);
    arrow_state('previous', 100);
  }else if(slide_pos == 0){
    arrow_state('next', 100);
    arrow_state('previous', dis_alpha);
  }else{
    arrow_state('next', 100);    
    arrow_state('previous', 100);
  }  
  
  slideIt(direction, -(slide_pos * slide_width));  
}

function toggleComments(state){
	var divStyle = getStyleObject("comment_link");
	divStyle.display = state == 'on' ? 'none' : 'block';
	
	divStyle = getStyleObject("form_wrapper");
	divStyle.display = state == 'on' ? 'block' : 'none';	
}

function arrow_state(strDiv, dblOpacity){
  var divStyle = getStyleObject(strDiv);		
  divStyle.opacity      = (dblOpacity / 100);
  divStyle.MozOpacity   = (dblOpacity / 100);
  divStyle.KhtmlOpacity = (dblOpacity / 100);
  divStyle.filter       = "alpha(opacity=" + dblOpacity + ")";
  divStyle.cursor       = (dblOpacity == 100 ? "pointer" : "default");
}

function slideIt(direction, x_target){
  startX = x_target - (Math.floor(Math.abs(x_target - startX) / slower) * direction);
  moveXY('container', startX, startY);
  if(startX != x_target)
  {
    tId = setTimeout('slideIt(' + direction + ', ' + x_target + ')', speed);	  
  }
  else{
    if(isSunday) changeHeight(sun_height, min_height + sun_height - def_height);
    loadComments();
  }
}

function changeHeight(content_height, wrap_height){
  document.getElementById("content").style.height = content_height + 'px';
  document.getElementById("wrap").style.minHeight = wrap_height + 'px';    
}

function print(){
  var stripDate = strip_info[(slide_max-1) - slide_pos][0];
  stripDate = stripDate.replace(/\//g, "_");
    
  window.open('/print.asp?' + stripDate);
}

function send(){
  var stripDate = strip_info[(slide_max-1) - slide_pos][0];
  stripDate = stripDate.replace(/\//g, "_");
  
  document.location = 'mailto:?subject=' + escape('Attica Comic') + '&body=' + escape('Have a look at this comic strip: ') + document.location + '?' + stripDate;
}

function subscribe() {
	document.location = '/subscribe.asp';
}

function archive(){
  var divStyle = getStyleObject("calendar");
  //alert(divStyle.visibility);
  divStyle.display = "block";
  if(divStyle.visibility == "visible"){
    archive_cal.hideCalendar();
  }else{
    archive_cal.showCalendar('archive_link');
  }
}

function set_archive(y, m, d){
  var sel_date = m + '/' + d + '/' + y;

_gaq.push(['_trackPageview', sel_date]);
  
  for(x=0;x<strip_info.length;x++){
    if(strip_info[x][0] == sel_date){
      new_pos = x;
      break;
    }
  }
  
  new_pos   = (slide_max-1) - new_pos;
  direction = (new_pos < slide_pos) ? 1 : -1;
  slide_pos = new_pos;

  startMoving(direction);
}
 
function showShide(strDiv, strDisplay){
	commentChange();
	var divStyle = getStyleObject(strDiv);
	
  archive_cal.hideCalendar();
	
	if(!strDisplay) strDisplay = divStyle.display == 'block' ? 'none' : 'block';
	divStyle.display = strDisplay;
	
	setCommentLink(strip_info[(slide_max-1) - slide_pos][1], divStyle.display);
	
	numOfComments = 0;
	
	loadComments();
}

function loadComments(){
  var divStyle = getStyleObject("usercomments");
  
	if(divStyle.display == "block" && strip_info[(slide_max-1) - slide_pos][1] && !strip_info[(slide_max-1) - slide_pos][2]){
	  strip_info[(slide_max-1) - slide_pos][2] = 1;
	  document.getElementById("allcomments").innerHTML = '<div class="normal commentitem"><p>loading...</p></div>';
	  
    xmlHttp = GetXmlHttpObject();
  	
 	  var stripDate = strip_info[(slide_max-1) - slide_pos][0];
    stripDate     = stripDate.replace(/\//g, "_");
  
    if (xmlHttp!=null){
      xmlHttp.onreadystatechange = commentsStateChanged;
      xmlHttp.open("GET","_load_comments.asp?" + stripDate);
      xmlHttp.send(null);    
    }
	}else if(strip_info[(slide_max-1) - slide_pos][1] == 0){
    document.getElementById("allcomments").innerHTML = '<div class="normal commentitem"><p>No comments. Be the first to leave a comment!</p></div>';
  }  
}

function commentsStateChanged(){
  if(xmlHttp.readyState==4){
 		var strHTML   = '';    
    var xmlDoc    = xmlHttp.responseXML.documentElement;
    var strStatus = xmlDoc.getElementsByTagName("status")[0].childNodes[0].nodeValue;

    if(strStatus == "ERROR"){
      strHTML = '<div class="normal commentitem"><p>An error occured, please try again later!</p></div>';
    }else{    
      var allComments = xmlDoc.getElementsByTagName("item");
      numOfComments   = allComments.length;
      
      for (i=0; i<allComments.length; i++) {
        rowstyle = (i % 2) ? 'alt' : 'normal';
        
      	datetime = allComments[i].getElementsByTagName("when")[0].childNodes[0].nodeValue;
      	isjeff   = allComments[i].getElementsByTagName("isjeff")[0].childNodes[0].nodeValue;
      	user     = allComments[i].getElementsByTagName("user")[0].childNodes[0].nodeValue;
      	hdr      = allComments[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
      	comment  = allComments[i].getElementsByTagName("comment")[0].childNodes[0].nodeValue;
      	
      	fromstyle = (isjeff == "True") ? ' class="author"' : '';

        strHTML += '<div class="' + rowstyle + ' commentitem">';
  			strHTML += '<h2>' + hdr + '</h2>';
  			strHTML += '<p>' + comment + '</p>';
  			strHTML += '<p class="info">Submitted by <strong'+fromstyle+'>' + user + '</strong> on ' + datetime + '</p>';
  			strHTML += '</div>';         
      }
  	}  
  	
		document.getElementById('allcomments').innerHTML = strHTML;				
  }
}

function submitComment(){
  user     = document.forms.frmComment.name.value;
  email    = document.forms.frmComment.email.value;
  hdr      = document.forms.frmComment.title.value;
  comment  = document.forms.frmComment.commentext.value;
  
	var msg='';
	
	if(user==''){msg+='\n  -Your Name';}
	if(hdr==''){msg+='\n  -Comment Title';}
	if(comment==''){msg+='\n  -Your Comment';}
	
	if(msg!=''){		
		alert('You forgot to fill the following fields:\n' + msg);
		document.forms.frmComment.submitButton.disabled=false;		
		return;
	}		
  
  datetime = new Date();
  datetime = datetime.format("shortDate");  
  strHTML  = '';
  rowstyle = (numOfComments++ % 2) ? 'alt' : 'normal';


  xmlHttp = GetXmlHttpObject();
	
  var stripDate = strip_info[(slide_max-1) - slide_pos][0];
  stripDate     = stripDate.replace(/\//g, "_");

  if (xmlHttp!=null){
    xmlHttp.onreadystatechange = saveStateChanged;
    xmlHttp.open("GET","_save_comments.asp?strip=" + stripDate + "&usr=" + user + "&email=" + email + "&hdr=" + hdr + "&comment=" + comment);
    xmlHttp.send(null);    
  }

  strHTML += '<div class="' + rowstyle + ' commentitem">';
	strHTML += '<h2>' + hdr + '</h2>';
	strHTML += '<p>' + comment + '</p>';
	strHTML += '<p class="info">Submitted by <strong>' + user + '</strong> on ' + datetime + '</p>';
	strHTML += '</div>';         

  var divStyle = getStyleObject("usercomments");
  setCommentLink(strip_info[(slide_max-1) - slide_pos][1], divStyle.display);
	document.getElementById('allcomments').innerHTML += strHTML;				
	document.getElementById('form_wrapper').innerHTML = "<p>Saving your comment, please wait...</p>";				
	document.forms.frmComment.reset();
}    	  

function saveStateChanged(){
  if(xmlHttp.readyState==4){
 		var strHTML   = '';    
    var xmlDoc    = xmlHttp.responseXML.documentElement;
    var strStatus = xmlDoc.getElementsByTagName("status")[0].childNodes[0].nodeValue;

    if(strStatus == "ERROR"){
      strHTML = "<p>An error occured, please try again later!</p>";
    }else{    
      strHTML = "<p>Comment saved successfully, thank you for your support.</p>";
  	}  
  	
		document.getElementById('form_wrapper').innerHTML = strHTML;				
  }
}

function setCommentLink(intNum, strState){
  document.getElementById("comments").innerHTML = '<a href="" onclick="return false;" onmouseup="showShide(\'usercomments\');">Comments (' + intNum + ')&nbsp;&nbsp;|&nbsp;&nbsp;' + (strState == "none" || strState == "" ? "SHOW [ + ]" : "HIDE [ - ]") + '</a>';
}

function GetXmlHttpObject(){    
  var xmlHttp=null;
  
  try{    // Firefox, Opera 8.0+, Safari    
    xmlHttp=new XMLHttpRequest();
  }catch (e){    // Internet Explorer
    try{
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }catch (e){
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        
    }
  }
	
	return xmlHttp;
}
