// JavaScript Document
$(document).ready(function(){
	$.ajaxSetup ({
    	// Disable caching of AJAX responses */
    	cache: false
	});

	var newPhoto = new Image();
	newPhoto.src = '';
	$('.hover_img').each(imageHover); // call to imageHover function
	// big links
	$('ul.navi li').each(makeBigLink);
	$('ul.footer_list li').each(makeBigLink);
	$('.service_box ul li').each(makeBigLink);
	$('.serv_category ul li').each(makeBigLink);
	// ---------- counter for the comment forms
	$('textarea#com_detail').keyup(countChars);
	$('.hidden').hide();
	// for login page
	$('input#username').focus();
	$('td a#f_user').click(emailLogin);
	$('.re_act_link').click(reactivate);
	// REGISTRATION FORM ACTION - if the form was submitted, and the user is a client, open the slider
	var selectBox = $('#registration_form select#type').val();
	if(selectBox==2){
		$('#client_form').show();
	}
	else{
		$('#client_form').hide();
	}
	// for the profile sample list
	$('.sample_work_list').hide();
	$('#registration_form #type').change(selectChange);
	// *** -------------------- MORE REGISTRATION VALIDATION SCRIPTS  ***** -------------------------------
	// clean the username to remove spaces
	$('#registration_form #username').blur(regUserTb);	
	$('#registration_form #username').keyup(regUserKeyUp);	
	/* KEY EVENT WILL MONITOR THE INPUT IN THE PASSWORD FIELD AND CHECK AGAINST REGEX */	
	$('#registration_form #password').keyup(regPwdTb);
	$('#change_password #password').keyup(changePwd);
	// For the admin comments
	$('.reply_link').click(replyComment);
	

}); // end of document ready
// ******************* FUNCTIONS ******************* |
function getTwFeed(){
	$.getJSON('/functions/get_feed.php', processTweets);
}
function processTweets(tweets){
	if(tweets){
		var limit=5;
		var i=0;
		var html = '';
		$.each(tweets,function(tweet, tweetInfo){
			if(i < limit){
				// process the tweets
				html += '<div class="inner_tw_box">';
				html += '<p>&quot;'+tweetInfo.content+'&quot;</p>';
				html += '<p class="tweet_date">'+tweetInfo.pub_date+'</p>';
				html += '</div>';
			}
		});	
		$("#tw_feed_data").html(html);
	}
}

function scrollTwFeed(){
	var feedBox = $('#tw_feed_data');
	var currentPosition = feedBox.css('top');
	if(currentPosition != '5px'){
		feedBox.animate({ top: '+=150' }, 5000);	
	}
	else{		
		feedBox.animate({ top: '+=150', opacity: .4 }, 5000, function(){ // scroll down off the page & make opaque		
			feedBox.animate({ top: '-=750' }, 2500, function(){ // scroll them to the top,
				feedBox.animate({ opacity: 1 }, 2000); // bring the opacity to 100%		
			});
		});
	}	
}
function imageHover(){
	var imgFile = $(this).attr('src');
	var preloadImage = new Image();
	var imgExt = /(\.\w{3,4}$)/;
	preloadImage.src = imgFile.replace(imgExt, '-h$1');
	$(this).hover(		  
	function(){
		$(this).attr('src', preloadImage.src);
		$(this).css('cursor','pointer');
		// 
		/*
		if($(this).attr('class').match()){
			alert("Doin Stuff");
		}
		*/
	},
	function(){
		$(this).attr('src', imgFile);
	}
	); // end hover
}
function makeBigLink(){
	$(this).hover(
		function(){			
			var destination = $(this).find('a').attr('href');
			status = destination;	
			return false;
		},
		function(){
			status='';
			return false;
		}
	);
	$(this).click(function(){
		location = $(this).find('a').attr('href');				   
	});
	$(this).css('cursor','pointer');
}
function countChars(){
	var charLength = $(this).val().length;
	var charRemaining = 1650 - charLength;
	
	$('#charCount').html('<strong>' + charRemaining + ' characters remaining</strong>');

	if(charLength == 0){	
		$('#charCount').html('');
	}

	// return the message to a normal text
	if((charLength < 1650)&&(charLength > 0)){
		$('#charCount').removeClass('error');			
	}		
	
	// Alerts when 1650 characters is reached
	if(charLength > 1650){
		$('#charCount').html((charRemaining * -1) + ' characters over the 1650 max').addClass('error');			
		$('#submit').attr('disabled', true);
	}
	else{ $('#submit').attr('disabled', false); }
}
function emailLogin(){
	$('td#u_label').html('<label for="email">Email</label>');
	$('td#u_inp').html('<input type="text" name="email" id="email" class="big_text" maxlength="50" tabindex="1" /><br />');
	$('td#toggle_u').html('');
	$('input#email').focus();
	return false;
}

function reactivate(){
	var href = $(this).attr('href');
	var querystring = href.slice(href.indexOf('?')+1);
	$.get('reactivate.php', querystring, actCallback);
	return false;
} // end of reactivation link click.

function actCallback(data){
	$('#login_error').html(data);		
}

function selectChange(){
	var curr_val = $('#registration_form select#type').val();
	if(curr_val==2){
		// this is a client, show the extended form
		$('#client_form').slideDown('slow');
	}
	else{
		$('#client_form').slideUp();	
	}
}
function regUserTb(){
	var textbox = $(this);
	var regex = /[^A-Za-z0-9\.]/g;
	textbox.val(textbox.val().replace(/\s/g, ''));
	textbox.val(textbox.val().replace(regex,''));
}
function regUserKeyUp(){
	var name = $(this).val().length;
	if(name>=3){
		if(name<=20){
			var textbox = $(this);
			var regex = /[^A-Za-z0-9\.]/g;
			textbox.val(textbox.val().replace(regex,''));
			var user = $(this).serialize();
			$.post('username.php', user, userCallback);
		}
		else{
			$('#u_avail').html('Your username must be 20 or less characters');
			$('#u_avail').addClass('pass_error');
			$('#submit').attr('disabled', true);
		}
	}
	else{
		$('#u_avail').html('Your username must be 3 or more characters');
		$('#u_avail').addClass('pass_error');
		$('#submit').attr('disabled', true);
	}
}
function userCallback(user){
	// display pass or fail on username
	if(user=='success'){
		$('#u_avail').html('This username is available!');
		$('#u_avail').addClass('success');
		$('#u_avail').removeClass('pass_error');
		$('#submit').attr('disabled', false);
	}
	else if(user=='fail'){
		$('#u_avail').html('This username is already taken.');
		$('#u_avail').addClass('pass_error');
		$('#submit').attr('disabled', true);
		$('form #username').focus();
		//$('form #username').select();
	}
	else{
		$('#u_avail').html('<p>' + user + '</p>');
		$('#u_avail').removeClass('success');
		$('#u_avail').addClass('pass_error');
		$('#submit').attr('disabled', true);

	}
}
function regPwdTb(){							   
	var pcontent = $(this).val().length;
	var pass = $(this).serialize();
	$.post('../password.php', pass, passCallback);
	if((pcontent<6)||(pcontent>12)){
		$('#submit').attr('disabled', true);
	}
}
function passCallback(pass){
	if(pass=='success'){
		$('#p_good').html('Good password!');
		$('#p_good').addClass('success');
		$('#p_good').removeClass('pass_error');
		$('#submit').attr('disabled', false);
		$('form #password').focus();
	}
	else{
		$('#p_good').html('Your password needs:<br /><ul>'+ pass +'</ul>');
		$('#p_good').addClass('pass_error');
	}
}
function changePwd(){							   
	var pcontent = $(this).val().length;
	var pass = $(this).serialize();
	$.post('../../admin/password.php', pass, passCallback2S);
	if((pcontent<6)||(pcontent>12)){
		$('#change_password input #submit').attr('disabled', true);
	}
}
function passCallback2(pass){
	if(pass=='success'){
		$('#good_pass').html('Good password!');
		$('#good_pass').addClass('success');
		$('#good_pass').removeClass('pass_error');
		$('#change_password input #submit').attr('disabled', false);
		$('form #password').focus();
	}
	else{
		$('#good_pass').html('Your password needs:<br /><ul>'+ pass +'</ul>');
		$('#good_pass').addClass('pass_error');
	}
}
function replyComment(){		
	var idVal = $(this).attr('id');
	var id = idVal.slice(idVal.indexOf('_')+1);
	$('#orig_com_id').val(id);
	// $('#client_comment').focus(); // Doesn't work
}
