var CharCounter = {
	msgMaxLen: 60,
	textAreadId: null,
	counterTextId: null,
	countDirection: 'down',
	init: function(textAreaId, counterTextId, direction) {
		this.textAreaId = textAreaId;
		this.counterTextId = counterTextId;
		this.countDirection = direction ? direction : 'down';
		$(textAreaId).observe('keyup', CharCounter.Events.processInput);
		$(textAreaId).observe('blur', CharCounter.Events.processInput);
		if(this.countDirection == 'down') {
			$(textAreaId).value = $(textAreaId).value.substr(0, CharCounter.msgMaxLen);
			$(counterTextId).innerHTML = CharCounter.msgMaxLen - $(textAreaId).value.length;		
		} else {
			$(counterTextId).innerHTML = $(textAreaId).value.length;		
		}
	},
	Events: {
		processInput: function() {
			var message = $(CharCounter.textAreaId).value;
			if(CharCounter.countDirection == 'down') {
				$(CharCounter.textAreaId).value = message.substr(0, GiftCert.msgMaxLen);
				if(message.length <= GiftCert.msgMaxLen) {
					$(CharCounter.counterTextId).innerHTML = GiftCert.msgMaxLen - message.length;
				} else {
					$(CharCounter.counterTextId).innerHTML = GiftCert.msgMaxLen - $(CharCounter.textAreaId).value.length;		
				}
			} else {
				$(CharCounter.counterTextId).innerHTML = $(CharCounter.textAreaId).value.length;
			}
		}
	}
};


function getCookie(c_name) {
	if(document.cookie.length > 0) {
  		c_start = document.cookie.indexOf(c_name + "=");
  		if(c_start != -1) { 
    		c_start = c_start + c_name.length + 1; 
    		c_end = document.cookie.indexOf(";", c_start);
    		if(c_end == -1) c_end = document.cookie.length;
    		return unescape(document.cookie.substring(c_start, c_end));
    	} 
  	}
	return "";
}
function setCookie(name, value, expires, path, domain, secure) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24 
	// expires = expires * 1000 * 60 * 60 * 24;
	*/
	if(expires) {
		expires = expires * 1000 * 60;
	}
	var expires_date = new Date(today.getTime() + (expires));   
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : ";path=/" ) +
	( ( domain ) ? ";domain=" + domain : ";domain=.gocoffeego.com" ) +
	( ( secure ) ? ";secure" : "" );
	
}


function toggleFAQ(id) {
    Effect.toggle('answer'+id, 'blind', { duration: .2 });
    $('toggler'+id).innerHTML = !$('answer'+id).visible() ? 'Hide Answer' : 'See Answer'; 
	if(!$('answer'+id).visible()) {
		$('toggler'+id).hide();
		//$('toggler'+id).innerHTML = 'Hide Answer'; 
		$('bottom_hide'+id).show();
		$('toggler_bottom'+id).innerHTML = 'Hide Answer';		
	} else {    
		//$('toggler'+id).innerHTML = 'See Answer';
		$('bottom_hide'+id).hide();
		$('toggler_bottom'+id).innerHTML = 'See Answer';		
		$('toggler'+id).show(); 
		jQuery.scrollTo('#faq'+id, { duration: 100 });
	}
}  

function toggleRoasterDesc(roasterName) {     
	roasterName = roasterName ? roasterName : 'roaster';
	var readMore = 'Read more about '+roasterName+'.';
	var readLess = 'Read less about '+roasterName+'.';
    Effect.toggle('desc_hidden', 'blind', { duration: .2 });
    $('toggler_bottom').innerHTML = !$('desc_hidden').visible() ? readLess : readMore; 
	if(!$('desc_hidden').visible()) {
		//$('toggler_link').hide();
		//$('toggler'+id).innerHTML = 'Hide Answer'; 
		//$('bottom_hide').show();
		$('toggler_bottom').innerHTML = readLess;		
	} else {    
		//$('toggler'+id).innerHTML = 'See Answer';
		//$('bottom_hide').hide();
		$('toggler_bottom').innerHTML = readMore;		
		//$('toggler_link').show(); 
	}	
}  

function toggleFactiods() {     
	if($('factiod_link').innerHTML == 'Click for more factoids.') {
		Effect.BlindDown('hidden_factiods', { duration: .2 });
		$('factiod_link').innerHTML = 'Click to hide factoids.'; 	
	} else {  
		Effect.BlindUp('hidden_factiods', { duration: .2 });  
		$('factiod_link').innerHTML = 'Click for more factoids.';		
	}  
}

function toggleCoffeeDesc() {
    //Effect.toggle('desc_hidden', 'blind', { duration: .2 });
    $('toggler_link').innerHTML = $('dots').innerHTML == '...' ? 'read less' : 'read more'; 
	if($('dots').innerHTML == '...') {
		$('toggler_link').hide();
		$('dots').innerHTML = $('desc_hidden').innerHTML;
		//$('toggler'+id).innerHTML = 'Hide Answer'; 
		$('bottom_hide').show();
		$('toggler_bottom').innerHTML = 'read less';		
	} else {    
		//$('toggler'+id).innerHTML = 'See Answer';  
		$('dots').innerHTML = '...';
		$('bottom_hide').hide();
		$('toggler_bottom').innerHTML = 'read more';		
		$('toggler_link').show(); 
	}	
}


// Additions to the Array Object

Array.prototype.inArray = function(value) {
	for(var i in this) { 
		if(this[i] === value) {
			return true; 
		}
	}
	return false;
}

Array.prototype.isArray = function() {
  return this && this.constructor === Array;
}

Array.prototype.arrayKeyExists = function(key) {    
    if(!this || (this.constructor !== Array && this.constructor !== Object)) {  
        return false;  
    }  
    return key in this;  
}

Array.prototype.remove = function(value) {
	for(var i = 0; i < this.length; i++) {
		if(value === this[i]) {
			this.splice(i, 1);
		}
	}
}

Array.prototype.removeKey = function(key) {
	var j = 0;
	for(var i in this) { 
		if(i === key) {
 			this.splice(j, 1);
		}
		j++;
	}
}

Array.prototype.arraySearch = function(needle, strict) {  
    // http://kevin.vanzonneveld.net  
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)  
    // *     example 1: array_search('zonneveld', {firstname: 'kevin', middle: 'van', surname: 'zonneveld'});  
    // *     returns 1: 'surname'  
    var strict = !!strict;  
    for(var key in this) {  
        if((strict && this[key] === needle) || (!strict && this[key] == needle)) {  
            return key;  
        }  
    }  
    return false;  
}  

Array.prototype.arrayKeys = function(search_value, argStrict) {
    var tmp_arr = {}, strict = !!argStrict, include = true, cnt = 0;
    var key = '';
    
    for (key in this) {
        include = true;
        if (search_value != undefined) {
            if (strict && input[key] !== search_value) {
                include = false;
            } else if (input[key] != search_value) {
                include = false;
            }
        }
        
        if (include) {
            tmp_arr[cnt] = key;
            cnt++;
        }
    }
    
    return tmp_arr;
}



String.prototype.nl2br = function() {  
    var breakTag = '<br>';    
    return (this + '').replace(/([^>]?)\n/g, '$1'+ breakTag +'\n');  
}

String.prototype.stripbr = function() {     
    return (this + '').replace(/<br\s*\/?>/mg,""); 
}  

String.prototype.trim = function() {
	return this.replace(/^\s*/,'').replace(/\s*$/,'').replace(/[\n\r]*$/,'');
} 
/*
String.prototype.trim = function(str, charlist) {
    // Strips whitespace from the beginning and end of a string  
    var whitespace, l = 0, i = 0;
    str += '';
    
    if (!charlist) {
        // default list
        whitespace = " \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000";
    } else {
        // preg_quote custom list
        charlist += '';
        whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '$1');
    }
    
    l = str.length;
    for (i = 0; i < l; i++) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(i);
            break;
        }
    }
    
    l = str.length;
    for (i = l - 1; i >= 0; i--) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(0, i + 1);
            break;
        }
    }
    
    return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}
*/




