var total = 0;
var cntclcks = 1;

// function to check checkbox when image link is clicked
// also keep track of the amount of checked boxes and take right actions
function check(id) {
 // enter the number of photos to pick
 var chooseNm = 12;
 var checkBox = document.getElementById("checkbox" + id);
 var link = document.getElementById("link" + id)
 	if (checkBox.checked == false) {
 		checkBox.checked = true;
 		link.className = 'on';
 		total += 1;
 	}
 	else {
 		checkBox.checked = false;
 		link.className = 'off';
 		total -= 1;
 	}
 	
 	// count the amount of checked images and make different sentances for amounts
 	var toGo = chooseNm - total;
 	if (toGo == 0) {
 		document.getElementById('counter').innerHTML = "You need to pick " + toGo + " more photos. ";
 	}
 	else if (toGo == 1){
 	  document.getElementById('counter').innerHTML = "You still need to pick " + toGo + " more photo. ";
 	} 
 	else if (toGo > 0) {
 	  document.getElementById('counter').innerHTML = "You still need to pick " + toGo + " more photos. ";
 	}
 	else if (toGo < 0) {
 		document.getElementById('counter').innerHTML = "You picked too many photos. ";
 	}
 	
 	// enable submit button at right amount
 	if (total == chooseNm){
 	  document.getElementById("submit").disabled = false;
 	  document.getElementById("submit").className = 'submitenabled';
 	} else {
 	  document.getElementById("submit").disabled = true;
 	  document.getElementById("submit").className = 'submitdisabled';
 	}
 }
 
 //change class and size of image when hover over
 function mover(id){
   var image = document.getElementById("image" + id)
   var link = document.getElementById("link" + id)
   
   image.width='112';
   image.height='78';
   image.className = 'over';
 }
 //change class and size back on hover out
 function mout(id){
   var image = document.getElementById("image" + id)
   var link = document.getElementById("link" + id)
   image.width='100';
   image.height='66';
   image.className = 'out';
 }

// Written by Rob Schmitt, The Web Developer's Blog
// http://webdeveloper.beforeseven.com/

// The following variables may be adjusted
var active_color = '#000'; // Colour of user provided text
var inactive_color = '#ccc'; // Colour of default text

//No need to modify anything below this line
window.onload = formDefaultValues;

function formDefaultValues() {
  var fields = getElementsByClassName(document, "input", "default-value");
  if (!fields) {
    return;
  }
  var default_values = new Array();
  for (var i = 0; i < fields.length; i++) {
    fields[i].style.color = inactive_color;
    if (!default_values[fields[i].id]) {
      default_values[fields[i].id] = fields[i].value;
    }
    fields[i].onfocus = function() { 
      if (this.value == default_values[this.id]) {
        this.value = '';
        this.style.color = active_color;
		if (this.id == "barpassword"){
			this.type = 'password';
		}
      }
      this.onblur = function() {
        if (this.value == '') {
          this.style.color = inactive_color;
          this.value = default_values[this.id];
		  if (this.id == "barpassword"){
			this.type = 'text';
		  }
        }
      }
    }
  }
}

//getElementsByClassName()
//Written by Jonathan Snook, http://www.snook.ca/jonathan
//Add-ons by Robert Nyman, http://www.robertnyman.com

function getElementsByClassName(oElm, strTagName, strClassName){
  var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
  var arrReturnElements = new Array();
  strClassName = strClassName.replace(/\-/g, "\\-");
  var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
  var oElement;
  for (var i = 0; i < arrElements.length; i++) {
    oElement = arrElements[i];
    if (oRegExp.test(oElement.className)) {
      arrReturnElements.push(oElement);
    }
  }
  return (arrReturnElements);
}

function resize(size) {
	var bluediv = document.getElementById('scaleable');
	var textlink = document.getElementById('scalelink');
	var images = document.getElementById('scaleable2');
	if (size == "big") {
		bluediv.className = "big";
		textlink.innerHTML = '<a href="#" onclick="javascript:resize(\'small\')">hide &raquo;</a>';
		images.className = "hide";
	} else if (size == "small") {
		bluediv.className = "small";
		textlink.innerHTML = '<a href="#" onclick="javascript:resize(\'big\')">&laquo; show</a>';
		images.className = "show";
	}
}

function profDescrip() {
	var line = document.getElementById('editdescript');
	var textarea = document.getElementById('edit');
	var textdiv = document.getElementById('justtext');
	// change textarea style
	textarea.className = "editon";
	textdiv.className = "justtextoff";
	line.innerHTML = '<input class="edit" type="submit" value="submit" /> or <a href="./index.php?p=profile#profile">cancel</a>';
}

function showSubmit(id) {
	var text = document.getElementById("respons" + id);
	var submitdiv = document.getElementById("submitmssg" + id);
	
	text.className = "writemessage";
	submitdiv.className = "submitshow";
}

function cancelSubmit(id) {
	var text = document.getElementById("respons" + id);
	var submitdiv = document.getElementById("submitmssg" + id);
	
	text.className = "wrtmessage";
	text.value = "";
	submitdiv.className = "submithide";
}

function showReaction(id) {
	var reaction = document.getElementById("writemessage" + id);
	reaction.className = "show";
}

function clearValue() {
	var textarea = document.getElementById("wrtmessage");
	if (cntclcks == 1) {
		textarea.value = "";
		cntclcks++;
	}
	textarea.className = "clear";
}

function concept(display) {
	var fade = document.getElementById('fade');
	var message = document.getElementById('concept_message');
	if (display == "show") {
		fade.className = "popup_show";
		message.className = "popup_show";
	} else if (display == "block") {
		fade.className = "popup_block";
		message.className = "popup_block";
	}
}



function control(kind) {
	if (kind == "username") {
		//check username
		//document.getElementById("username").value = "username";
	}
}



addEvent(window, "load", makeNiceTitles);

var XHTMLNS = "http://www.w3.org/1999/xhtml";
var CURRENT_NICE_TITLE;
var browser = new Browser();

function makeNiceTitles() {
    if (!document.createElement || !document.getElementsByTagName) return;
    // add namespace methods to HTML DOM; this makes the script work in both
    // HTML and XML contexts.
    if(!document.createElementNS)
    {
        document.createElementNS = function(ns,elt) {
            return document.createElement(elt);
        }
    }

    if( !document.links )
    {
        document.links = document.getElementsByTagName("a");
    }
    for (var ti=0;ti<document.links.length;ti++) {
        var lnk = document.links[ti];
        if (lnk.title) {
            lnk.setAttribute("nicetitle",lnk.title);
            lnk.removeAttribute("title");
            addEvent(lnk,"mouseover",showNiceTitle);
            addEvent(lnk,"mouseout",hideNiceTitle);
            addEvent(lnk,"focus",showNiceTitle);
            addEvent(lnk,"blur",hideNiceTitle);
        }
    }
    var instags = document.getElementsByTagName("ins");
    if (instags) {
    for (var ti=0;ti<instags.length;ti++) {
        var instag = instags[ti];
        if (instag.dateTime) {
            var strDate = instag.dateTime;
            var dtIns = new Date(strDate.substring(0,4),parseInt(strDate.substring(4,6)-1),strDate.substring(6,8),strDate.substring(9,11),strDate.substring(11,13),strDate.substring(13,15));
            instag.setAttribute("nicetitle","Added on "+dtIns.toString());
            addEvent(instag,"mouseover",showNiceTitle);
            addEvent(instag,"mouseout",hideNiceTitle);
            addEvent(instag,"focus",showNiceTitle);
            addEvent(instag,"blur",hideNiceTitle);
        }
    }
    }
}

function findPosition( oLink ) {
  if( oLink.offsetParent ) {
    for( var posX = 0, posY = 0; oLink.offsetParent; oLink = oLink.offsetParent ) {
      posX += oLink.offsetLeft;
      posY += oLink.offsetTop;
    }
    return [ posX, posY ];
  } else {
    return [ oLink.x, oLink.y ];
  }
}

function showNiceTitle(e) {
    if (CURRENT_NICE_TITLE) hideNiceTitle(CURRENT_NICE_TITLE);
    if (!document.getElementsByTagName) return;
    if (window.event && window.event.srcElement) {
        lnk = window.event.srcElement
    } else if (e && e.target) {
        lnk = e.target
    }
    if (!lnk) return;
    if (lnk.nodeName.toUpperCase() != 'A') {
        // lnk is not actually the link -- ascend parents until we hit a link
        lnk = getParent(lnk,"A");
    }
    if (!lnk) return;
    nicetitle = lnk.getAttribute("nicetitle");
    
    var d = document.createElementNS(XHTMLNS,"div");
    d.className = "nicetitle";
    tnt = document.createTextNode(nicetitle);
    pat = document.createElementNS(XHTMLNS,"p");
    pat.className = "titletext";
    pat.appendChild(tnt);
    d.appendChild(pat);
    if (lnk.href) {
        tnd = document.createTextNode(lnk.href);
        pad = document.createElementNS(XHTMLNS,"p");
        pad.className = "destination";
        pad.appendChild(tnd);
        d.appendChild(pad);
    }
    
    
    STD_WIDTH = 30;
    //if (lnk.href) {
    //    h = lnk.href.length;
    //} else { h = nicetitle.length; }
    if (nicetitle.length) {
      t = nicetitle.length;
    }
    //h_pixels = h*6;
    t_pixels = t*10;
    
    //if (h_pixels > STD_WIDTH) {
    //    w = h_pixels;
    //} else if ((STD_WIDTH>t_pixels) && (t_pixels>h_pixels)) {
        w = t_pixels;
    //} else if ((STD_WIDTH>t_pixels) && (h_pixels>t_pixels)) {
    //    w = h_pixels;
    //} else {
    //    w = STD_WIDTH;
    //}
        
    d.style.width = w;    
	

	
    /*
    mx = lnk.offsetLeft;
    my = lnk.offsetTop;
    */
    mpos = findPosition(lnk);
    mx = mpos[0];
    my = mpos[1];
    //xy = getMousePosition(e);
    //mx = xy[0]; my = xy[1];
    
    d.style.left = (mx+15) + 'px';
    d.style.top = (my+35) + 'px';
    if (window.innerWidth && ((mx+w) > window.innerWidth)) {
        d.style.left = (window.innerWidth - w - 25) + "px";
    }
    if (document.body.scrollWidth && ((mx+w) > document.body.scrollWidth)) {
        d.style.left = (document.body.scrollWidth - w - 25) + "px";
    }
    
    document.getElementsByTagName("body")[0].appendChild(d);
    
    CURRENT_NICE_TITLE = d;
}

function hideNiceTitle(e) {
    if (!document.getElementsByTagName) return;
    if (CURRENT_NICE_TITLE) {
        document.getElementsByTagName("body")[0].removeChild(CURRENT_NICE_TITLE);
        CURRENT_NICE_TITLE = null;
    }
}



// Add an eventListener to browsers that can do it somehow.
// Originally by the amazing Scott Andrew.
function addEvent(obj, evType, fn){
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
  } else if (obj.attachEvent){
	var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
	return false;
  }
}

function getParent(el, pTagName) {
	if (el == null) return null;
	else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase())	// Gecko bug, supposed to be uppercase
		return el;
	else
		return getParent(el.parentNode, pTagName);
}

function getMousePosition(event) {
  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }
  return [x,y];
}

// Determine browser and version.

function Browser() {
// blah, browser detect, but mouse-position stuff doesn't work any other way
  var ua, s, i;

  this.isIE    = false;
  this.isNS    = false;
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

