var globalposturl = "/update_account_js";
var oldcontent;
var newentry;


var redirectUser = function(){
    if($('is_album')) window.location = "/album/"+this;
    else window.location = "/videos/"+this;
}

function show_input (type, input_id, div){ // type of input, input id, div to update, link to change
    
    var div = $(div);
    var oldstatus = div.get('html');
	
	if(type == 'textarea') {
		// set the text area var
		var field = "<textarea rows=\"10\" id="+input_id+"></textarea>";
	} else {
		// set the input var
		var field = "<input type=\"text\" style=\"width:250px;\" id="+input_id+" />";
	}

	// get the old content first
	oldcontent = $(div).get('html'); 
	// set the old content to an input or text area
	var newcontent = $(div).set('html', field);
	
	$(input_id).value = oldcontent;
	$(input_id).focus();
	$(input_id).select();
	
	$(input_id).addEvents({
	    blur: function () {
		    $('current_status').addEvents({
		        click: function () {
        	        show_input ('input', 'new_status', this);
        	        this.removeEvents('click');
        	    }
	        });
		    var newstatus = $(input_id).get('value');
		    if(newstatus == oldstatus) {
		        div.set('html', oldstatus);
		    } else {
		        update_current_status();
		    } 
		    $(div).set('html', newstatus);
		}, 
		keydown: function (event) {
		  if(event.key == 'enter') {		      
              $('current_status').addEvents({
                  click: function () {
                      show_input ('input', 'new_status', this);
                      this.removeEvents('click');
                  }
              });
              update_current_status();
		      var newstatus = $(input_id).get('value');
		      $(div).set('html', newstatus);
		  }
		}
	});
	
}


window.addEvent('domready',function(){
    
    // hover for status border
    if($('current_status')) 
        var el = $('current_status'), color = el.getStyle('color');
    
    if($('who')) 
        var who = $('who').get('html');
    
    if(who == 'owner') {    
        $('current_status').addEvents({
        	mouseenter: function(){
        		this.morph({
        			'opacity'   : 0.5, 
        			'border'    : '1px solid #000', 
        			'padding'   : '1px'
        		});
        	},
        	mouseleave: function(){
        		this.morph({
        			opacity: 1, 
        			'border'    : '0px', 
        			'padding'   : '0'
        		});
        	},
        	click: function () {
        	    show_input ('input', 'new_status', this);
        	    this.removeEvents('click');
        	}
        });
    }
	
	if($('devnotice')) TB_show('Important Notice', '#TB_inline?height=400&width=400&inlineId=devnotice', '', '/assets/images/trans.gif');
	
    var Tips1 = new Tips($$('.Tips1'));
	
	// ********** tabbed content js

	/*if($('profile_content')) {	

		// profile tabs
		var profA = $('profile_content');
		var profB = $('friends_content');
		var profC = $('comments_content');
		var profD = $('photos_content');
		$('profile_tab').addEvent('click', showFunction.bind(profA));
		$('friends_tab').addEvent('click', showFunction.bind(profB));
		$('comments_tab').addEvent('click', showFunction.bind(profC));
		$('album_tab').addEvent('click', showFunction.bind(profD));
		
		[$('profile_tab'), $('friends_tab'), $('comments_tab'), $('album_tab')].each(function(item, index){
			item.addEvent('click', function(e){ 
				current_element = e.target;
				$$('.active').addClass('inactive');
				$$('.active').removeClass('active');
				current_element.addClass('active');
				current_element.setStyle('display', 'block');
			});
		});
		// set the first content to display
		//profA.removeClass('hidden');
		profA.setStyle('display', 'block');
	}*/

	if($('settings_info')) {
		// account settings tabs
		var setA = $('settings_info');
		var setB = $('privacy_info');
		var setC = $('billing_info');
		$('settings_tab').addEvent('click', showFunction.bind(setA));
		$('privacy_tab').addEvent('click', showFunction.bind(setB));
		$('billing_tab').addEvent('click', showFunction.bind(setC));
		
		[$('settings_tab'), $('privacy_tab'), $('billing_tab')].each(function(item, index){
			item.addEvent('click', function(e){ 
				current_element = e.target;
				$$('.tabs').addClass('inactive');
				$$('.tabs').removeClass('active');
				current_element.addClass('active');
				current_element.removeClass('inactive');
			});
		});
		// set the first content to display
		setA.setStyle('display', 'block');
	}

	
	// drop down menu
	//$('drop_down_menu').getElements('li.menu').each( function( elem ){
	//	var list = elem.getElement('ul.links');	
	//	var myFx = new Fx.Slide(list).hide();			
	//	elem.addEvents({
	//		'mouseenter' : function(){ 					
	//			myFx.cancel();
	//			myFx.slideIn();					
	//		},
	//		'mouseleave' : function(){ 
	//			myFx.cancel();
	//			myFx.slideOut();					
	//		}
	//	});	
	//})
});

var showFunction = function() {
	$$('.hidden').setStyle('display', 'none');
	this.setStyle('display', 'block');
}



function getURL(url, vars){
    if(vars == undefined) window.location="/"+url;
    else window.location="/"+url+""+vars;
}

function showInput (type, input_id, div, linkid){ // type of input, input id, div to update, link to change

	if(type == 'textarea') {
		// set the text area var
		var field = "<textarea rows=\"10\" id="+input_id+"></textarea>";
	} else {
		// set the input var
		var field = "<input type=\"text\" id="+input_id+" />";
	}

	// get the old content first
	oldcontent = $(div).get('html'); 
	// set the old content to an input or text area
	var newcontent = $(div).set('html', field);
	if(oldcontent == newcontent) {
		return;
	}
	// set the old content into the text area
	$(input_id).value = oldcontent;
	$(input_id).focus();
	$(input_id).select();
	
	// change the link from edit to save
	$(linkid).setProperty('onclick', 'update_'+div+'(\''+div+'\', \''+input_id+'\');return false;');
	$(linkid).innerHTML = "[ save ]";
	
}



// this function does not allow charater input at all
// INPUT must have onkeypress="return isNumberKey(event)" 
function isNumberKey(evt){ 
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57))
	   return false;
	
	return true;
}

// this function checks for numeric input AFTER it is submitted.
function forceNumeric(input) { 
	var numericExpression = /^[0-9]+$/;
	if(input.match(numericExpression)) return true;
	else return false;
}



// function update_current_status (div, input_id) {
//  var newentry = $(input_id).value;
//  if(newentry == oldcontent) {
//      $(div).innerHTML = newentry;
//      $('edit_status').setProperty('onclick', 'showInput(\'input\', \'status_update\', \'current_status\', \'edit_status\');return false;');
//      $('edit_status').innerHTML = "[ edit ]";
//      return;
//  }
//  $(div).addClass('waitNoBor');
//  $(div).addClass('topmar');
//  $(div).innerHTML = "Updating...";
// 
//  var request = new Request.JSON({
//          'url' : globalposturl,
//          'method' : "post",
//          'data' : {
//              'task'      : 'updateStatus',
//              'status'    : newentry 
//          },
//          'onComplete' : function(responseText)
//          {
// 
//              if(responseText.result == true) {
//                  $(div).removeClass('waitNoBor');
//                  $(div).innerHTML = newentry;
//                  $('edit_status').setProperty('onclick', 'showInput(\'input\', \'status_update\', \'current_status\', \'edit_status\');return false;');
//                  $('edit_status').innerHTML = "[ edit ]";
//                  $('ajax_currentstatus_date').innerHTML = "1 seconds(s) ago";    
//              } else {
//                  $(div).removeClass('waitNoBor');
//                  $(div).addClass('error');
//                  $(div).innerHTML = responseText.result;
//              }
//          }
//      }).send();
// }

function update_current_status () {
    
	var newstatus = $('new_status').get('value');
	if(newstatus == '') return;
	if(newstatus == 'Update your status here') return;
	
	var request = new Request.JSON({
			'url' : globalposturl,
			'method' : "post",
			'data' : {
				'task'  	: 'updateStatus',
				'status' 	: newstatus 
			},
			'onComplete' : function(responseText)
			{
				if(responseText.result) {
				    var html = '<ul><li><a href="/profile/'+responseText.id+'">'+responseText.name+'</a> changed status to '+newstatus+'<br><span class="grey">1 second ago</span></li></div><div class="clear"><br /><br />';
				    var newDiv = new Element('div', {id: 'update_posts'});
				    newDiv.set('html', html);
				    if($('new_status')) $('new_status').set('value', 'Update your status here');
					newDiv.inject($('update_date_block'), 'top');
				} 
			}
		}).send();
}

function updateAlbumCoverId(id){
	$('album_cover').setAttribute('value', id);
}

/*
preload_full = new Image();
preload_full.src = "/assets/images/icons/video_rating_full.gif";
preload_partial = new Image();
preload_partial.src = "/assets/images/icons/video_rating_part.gif";
preload_empty = new Image();
preload_empty.src = "/assets/images/icons/video_rating_none.gif";


function rating_over(rating) {
  for(var x=1; x<=5; x++) {
    if(x <= rating) {
      $('rate_'+x).src = preload_full.src;
    } else {
      $('rate_'+x).src = preload_empty.src;
    }
  }
}

function rating_out() {
	// if(x <= {/literal}{$video_info.video_rating_full}{literal}) {
	// 						      $('rate_'+x).src = preload_full.src;
	// 						    } else if({/literal}{$video_info.video_rating_part}{literal} != 0 && x == {/literal}{math equation='x+1' x=$video_info.video_rating_full}{literal}) {
	// 						      $('rate_'+x).src = preload_partial.src;
	// 						    } else {
	// 						      $('rate_'+x).src = preload_empty.src;
	// 						    }
  for(var x=1; x<=5; x++) {
  	$('rate_'+x).src = preload_empty.src;
  }
}
	  
	  
function rate(rating, user_id) {
    $('video_rating').onmouseout = null;
    $('video_rating').set('html', 'Submitting vote...');
    var request = new Request.JSON({
        'url' : 'video.php',
        'method' : "post",
        'data' : {
        	'task'  : 'vote',
            'user_id'  : user_id,
            'votes' : rating
        },
        'onComplete' : function(responseText)
        {
        	if(responseText.result == true) {
        	    $('user_rating').set('html', 'Thank you! Your vote has been recorded!');
        	} else {
        	    $('user_rating').set('html', '');
        	    $('rating_result').set('html', 'You already voted for this portfolio today. You can vote again ' + responseText.result);
        	}
        }
    }).send();
}

	
	*/
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	