// fdrlib.js - version 1.0
// FadeDropRaise Library for Javascript
// Nick O'Neill 2005

function fadeElementIn(id,time,begin){
   // <a href="javascript:fadeElementIn(myelementid,0.3)">fade in!</a>
   // ---- SET THESE IN YOUR SCRIPTS
   // id is the id of the element you're fading
   // time is the amount of time you'd like your element to spend fading in (try 0.3)
   
   // ---- DON'T SET THESE IN YOUR SCRIPTS
   // begin is the start time of your fade - it will be set automatically when the function is called
       
   var elem = getByID(id);
   
   var current = new Date().getTime();
   if (typeof(begin) == "number"){}
   else {begin = new Date().getTime()}
   
   var timeDiff = (current - begin) / 1000; // /
   
   if (timeDiff < time){
      
      var boxOpacity = (timeDiff/time)*(timeDiff/time);

      setOpacity(elem.id,boxOpacity);
   
      setTimeout("fadeElementIn(\'"+id+"\',"+time+","+begin+")",16);
   } else {setOpacity(elem.id,.99);return}
}

function fadeElementOut(id,time,begin){
   // <a href="javascript:fadeElementOut(myelementid,0.3)">fade out!</a>
   
   // ---- SET THESE IN YOUR SCRIPTS
   // id is the id of the element you're fading
   // time is the amount of time you'd like your element to spend fading out (try 0.3)
   
   // ---- DON'T SET THESE IN YOUR SCRIPTS
   // begin is the start time of your fade - it will be set automatically when the function is called

   var elem = getByID(id);
   
   var current = new Date().getTime();
   if (typeof(begin) == "number"){}
   else {begin = new Date().getTime()}
   
   var timeDiff = (current - begin) / 1000; // /
   
   if (timeDiff < time) {

      var boxOpacity = .99-((timeDiff/time)*(timeDiff/time));

      setOpacity(elem.id,boxOpacity);
   
      setTimeout("fadeElementOut(\'"+id+"\',"+time+","+begin+")",16);
   } else {setOpacity(elem.id,0);return}
}

function raiseElementUp(id,ascent,time,begin,modAscent,topOrig){
   // <a href="javascript:raiseElementUp(myelementid,-100,0.4)">raise up!</a>
   
   // ---- SET THESE IN YOUR SCRIPTS
   // id is the id of the element you're raising
   // ascent is the amount of pixels the element will lift
   // time is the amount of time in seconds that you'd like your element to spend getting to it's final position (try 0.4)
   
   // ---- DON'T SET THESE IN YOUR SCRIPTS
   // begin is the start time of your raise - it will be set automatically when the function is called
   // modAscent is a used in calculating distance and should not be set when calling
   // topOrig is the original 'top' value of the element as set by setTop() or your own code

   var elem = getByID(id);
   
   if (elem.style.top == ""){setTop(elem.id,0)}
   if (typeof(topOrig) == "undefined"){topOrig = antipixel(elem.style.top)}
   var current = new Date().getTime();
   
   if (typeof(begin) != "number"){begin = new Date().getTime()}

   var timeDiff = (current - begin) / 1000; // /
   
   if (timeDiff < time) {
      var topPx = topOrig+(ascent*((timeDiff/time)*(timeDiff/time)));
      
      elem.style.top = pixel(topPx);
      setTimeout("raiseElementUp(\'"+id+"\',"+ascent+","+time+","+begin+","+modAscent+","+topOrig+")",16);

   } else {setTop(elem.id,topOrig+ascent);return}
}

function dropElementDown(id,decent,time,begin,modDecent,topOrig){
   // <a href="javascript:dropElementDown(myelementid,100,0.4)">drop down!</a>
   
   // ---- SET THESE IN YOUR SCRIPTS
   // id is the id of the element you're dropping
   // decent is the amount of pixels the element will drop
   // time is the amount of time in seconds that you'd like your element to spend getting to it's final position (try 0.4)
   
   // ---- DON'T SET THESE IN YOUR SCRIPTS
   // begin is the start time of your drop - it will be set automatically when the function is called
   // modDecent is a used in calculating distance and should not be set when calling
   // topOrig is the original 'top' value of the element as set by setTop() or your own code
   
   var elem = getByID(id);

   if (elem.style.top == ""){setTop(elem.id,0)}
   if (typeof(topOrig) == "undefined"){topOrig = antipixel(elem.style.top)}
   var current = new Date().getTime();

   if (typeof(begin) != "number"){begin = new Date().getTime()}

   var timeDiff = (current - begin) / 1000; // /

   if (timeDiff < time) {
      var topPx = topOrig-(decent*((timeDiff/time)*(timeDiff/time)));
      
      elem.style.top = pixel(topPx);
      setTimeout("raiseElementUp(\'"+id+"\',"+decent+","+time+","+begin+","+modDecent+","+topOrig+")",16);

   } else {setTop(elem.id,topOrig-decent);return}
}

function setOpacity(id,opacity){
   var toSet = getByID(id);
   
   if (toSet.style.MozOpacity != null){toSet.style.MozOpacity = opacity;}
   if (toSet.style.opacity != null){toSet.style.opacity = opacity;}
   if (toSet.style.filter != null){toSet.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+(opacity*100)+")";}
}
function setTop(id,top){
   getByID(id).style.position = 'relative';
   getByID(id).style.top = pixel(top);
}
function getByID(n){
	var d = window.document;
	if (d.getElementById)
		return d.getElementById(n);
	else if (d.all)
		return d.all[n];
}
function pixel(px){
   return px+"px";
}
function antipixel(px){
   return parseInt(px.replace(/px/,''));
}

	
	function send_vote(item_id, vote, user_id)
	{

	fadeElementOut("score_"+item_id, .4)	
//	fadeElementOut("up_down_"+item_id, .3)	
	setTimeout("xajax_cast_vote("+item_id+", "+vote+", "+user_id+ ")",0);;
	setTimeout("fadeElementIn('score_'+"+item_id+", .4)",500);	
//	setTimeout("fadeElementIn('up_down_'+"+item_id+", .3)",300);

	}	            


function send_post_vote(post_id, vote, user_id)
	{
	fadeElementOut("post_score_"+post_id, .4)	
	setTimeout("xajax_cast_post_vote("+post_id+", "+vote+", "+user_id+ ")",0);;
	setTimeout("fadeElementIn('post_score_'+"+post_id+", .4)",500);	
	}	       
	
function vote_helpful_review(post_id, review, user_id)
	{
	fadeElementOut("post_score_"+post_id, .4)	
	setTimeout("xajax_vote_review_up("+review+", "+user_id+ ")",0);;
	setTimeout("fadeElementIn('post_score_'+"+post_id+", .4)",500);	
	}	       
	
	
function send_tag_vote(tag_id, vote)
	{
	fadeElementOut("tag_score_"+tag_id, .4)	
	setTimeout("xajax_cast_tag_vote("+tag_id+", "+vote+")",0);;
	setTimeout("fadeElementIn('tag_score_'+"+tag_id+", .4)",500);	
	}	       
function toggleLayer(whichLayer)
{
if (document.getElementById)
{
// this is the way the standards work
var style2 = document.getElementById(whichLayer).style;
style2.display = style2.display? "":"block";
}
else if (document.all)
{
// this is the way old msie versions work
var style2 = document.all[whichLayer].style;
style2.display = style2.display? "":"block";
}
else if (document.layers)
{
// this is the way nn4 works
var style2 = document.layers[whichLayer].style;
style2.display = style2.display? "":"block";
}
}


function Cities() {
	this.flag = 0;
	this.toggle =  toggle_cities;

	 
function toggle_cities(){
	
	//var mySlider = new Fx.Slide('city_selection', {duration: 300}).hide();			
	mySlider.toggle();
	
	}
}


var flag = false;

function country_city_dropdown()
{	
		if( document.add_item.country.value != "" )
		{
				xajax_get_cities(document.add_item.country.value);
				
				if (!flag)
				{
						mySlider.toggle();
						flag = true;
				}
		}
}

function populate_dropdown( dropdown, new_elements_texts, new_elements_values )
{
		
		dropdown.options.length = 0;
		
		for (var i = 0; i < new_elements_texts.length; i++) {
				var newOption = document.createElement("OPTION");
				dropdown.options.add(newOption);
				newOption.text = new_elements_texts[i];
				newOption.value = new_elements_values[i];
		}
		
}


// trim spaces 
// #12 From http://blog.stevenlevithan.com/archives/faster-trim-javascript
// Thanks Steve
function trim (str) {
	var	str = str.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}

function vote(nt, nid, v)
{
		
		
		
}

//From
//http://techpatterns.com/downloads/javascript_cookies.php
function set_cookie( 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
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

str = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
( ( path ) ? ";path=" + path : "" ) + 
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );

document.cookie = str;
}

// this fixes an issue with the old method, ambiguous values 
// with this test document.cookie.indexOf( name + "=" );
function get_cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}


