// http://www.switchonthecode.com/tutorials/javascript-tutorial-how-to-auto-ellipse-text
function autoEllipseText(element, text, width) {
	 if(element.offsetWidth > width) {
			var i = 1;
			element.innerHTML = '';
			while(element.offsetWidth < (width) && i < text.length) {
				 element.innerHTML = text.substr(0,i) + '...';
				 i++;
			}
			returnText = element.innerHTML;
			return returnText;
	 }
	 return text;
}

function rounds(context) {
	$('.box', context).each(function() {
		//if ($(this).parents('.rounded').length == 0) {
			if ($(this).hasClass('box-gray')) {
				$(this).wrap('<div class="rounded rounded-gray"></div>').parent().prepend('<div class="top"><div class="tl"><div class="tr"></div></div></div>').append('<div class="bottom"><div class="bl"><div class="br"></div></div></div>');
			} else if ($(this).hasClass('box-dark')) {
				$(this).wrap('<div class="rounded rounded-dark"></div>').parent().prepend('<div class="top"><div class="tl"><div class="tr"></div></div></div>').append('<div class="bottom"><div class="bl"><div class="br"></div></div></div>');
			} else if ($(this).hasClass('box-blue')) {
				$(this).wrap('<div class="rounded rounded-blue"></div>').parent().prepend('<div class="top"><div class="tl"><div class="tr"></div></div></div>').append('<div class="bottom"><div class="bl"><div class="br"></div></div></div>');
			} else if ($(this).hasClass('box-pink')) {
				$(this).wrap('<div class="rounded rounded-pink"></div>').parent().prepend('<div class="top"><div class="tl"><div class="tr"></div></div></div>').append('<div class="bottom"><div class="bl"><div class="br"></div></div></div>');
			} else if ($(this).hasClass('box-yellow')) {
				$(this).wrap('<div class="rounded rounded-yellow"></div>').parent().prepend('<div class="top"><div class="tl"><div class="tr"></div></div></div>').append('<div class="bottom"><div class="bl"><div class="br"></div></div></div>');
			} else {
				$(this).wrap('<div class="rounded rounded-white"></div>').parent().prepend('<div class="top"><div class="tl"><div class="tr"></div></div></div>').append('<div class="bottom"><div class="bl"><div class="br"></div></div></div>');
			}
		//}
	});
}

$(function(){

	$('.overlay-trigger').overlay();
	
	rounds();

	$('.ellipse').each(function (i) {
		var width = $(this).parent().width();
		autoEllipseText(this, $(this).text(), width);
	});
	
	$('#change-password form').passroids({ 
		main: '#create-password'
	});
	
	/* VERIFY IF PASSWORDS MATCH */	   
	$("#change-password #verify-password").keyup(function(){
		if ($("#create-password").val() == $("#verify-password").val()) {
			$("#match").show();
			$("#sendButton").removeAttr("disabled");
		} else {
			$("#match").hide();
			$("#sendButton").attr("disabled","disabled");
		}
	});

});

$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});

/*
 * Auto-binds XML data from AJAX to either HTML Div ID's or form elements
 * 
 * xmlobject -> A XML Document Object
 * tagName -> Top-level node of XML Document to start traversing
 * type -> [content | form] - whether binding to HTML content or a Form
 */
function bindXml(xmlobject, tagNames, type) {
	
	var nodeNameList = tagNames.split(",");
	
	for(var n = 0;n<nodeNameList.length;n++){
	    var tagName = nodeNameList[n];
	
		//console.log("processing tag name: " + tagName);
	
	    var recordsContainer = xmlobject.getElementsByTagName(tagName);
	    var tot_records = recordsContainer.length;
	    
	    //console.log("total records: " + tot_records);
	    
	    //if (DEBUG)  alert(tot_records);
	    if (tot_records == 0) return 0;
	    
	       // Populate the First Record
	       var record = recordsContainer[0];
	       if (record.childNodes.length == 0) return false;
	       
	       //console.log("child nodes: " + record.childNodes.length);
	       
	       for(j=0;j<record.childNodes.length;j++) {
	        var field = record.childNodes[j];
	        //alert(field);
	        if (typeof field != "undefined") {
	            var fieldName = field.nodeName;
	            var fieldValue = "";
	            if (field.hasChildNodes()) {
	                fieldValue = field.childNodes[0].nodeValue;
	            }
	            
	            //console.log(fieldName + ":" + fieldValue);
	
	            if (type == 'content') {
	                $('#' + fieldName).html(fieldValue);
	            } else  {
	                $('#' + fieldName).val(fieldValue);
	            }
	
	        } else {
	            //break;
	        }
	        
	    }
	}

	return tot_records;
}

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}


/*
 * Auto-selects a HTML select box based on a value
 * in_element -> element ID
 * in_value -> the value to be selected
 */
function SetIndex(in_element, in_value) {
    var list = document.getElementById(in_element);
    if(list&&list.options.length){
        for(var i=0; i<list.options.length; i++){
            if(list.options[i].value == in_value){
                list.selectedIndex = i;
                return;
            }
        }
    }
} 
