//options
var domain = 'http://www.stutter.in/';

//function run on load
$(function(){
	//vert align
	$('.valign').vAlign();
	$('#regpreview').vAlign();
	$('.logobox').vAlign();
	$('.sideheader').vAlign();
	$('.settingsvalign').vAlign();
	$("#statusbar").charCount();
	
	$('input').customInput();
	
	$('.tooltip').tipTip(
		{
			position: "top",
			delay: 100,
			maxWidth: "260px"
		});
	
	//settings page tumblr
	$('#opentumblr').click(function(){
		$.jGrowl('close');
		var growlhtml = '<br /><form action="javascript:addservice(' + "'tumblr'" + ')" method="post" id="addtumblr" class="addacctinfo">Email:<input type="text" name="tumblremail" id="tumblremail" /><br />Password:<input type="password" name="tumblrpassword" id="tumblrpassword" /><br /><input type="submit" name="submit" value="add" class="lightbutton" /><br /><input type="hidden" name="action" value="tumblrauth"></form><br />';
		$.jGrowl(growlhtml, { sticky: true, header: 'tumblr login'});
	});
	
	//settings page plurk
	$('#openplurk').click(function(){
		$.jGrowl('close');
		var growlhtml = '<br /><form action="javascript:addservice(' + "'plurk'" + ')" method="post" id="addplurk" class="addacctinfo">Username:<input type="text" name="plurkusername" id="plurkusername" /><br />Password:<input type="password" name="plurkpassword" id="plurkpassword" /><br /><input type="submit" name="submit" value="add" class="lightbutton" /><br /><input type="hidden" name="action" value="plurkauth"></form><br />';
		$.jGrowl(growlhtml, { sticky: true, header: 'Plurk login'});
	});
	
	//default text box text
	fillertext();
	
	//form submits
	$('#loginform').submit(function(){
		strData = $(this).serialize();
		ajaxPost(strData, 'login.php', 'login');
		return false;
	});
	$('#register').submit(function(){
		strData = $(this).serialize();
		ajaxPost(strData, 'register.php', 'register');
		resetsettingfields();
		return false;
	});
	$('#statussubmit').submit(function(){
		if($('#googlebutton').attr('checked')){
			searchquery = $('#statusbar').val();
			searchlocation = 'http://www.google.com/search?hl=en&q=' + searchquery;
			//searchlocation = 'http://www.google.com/custom?hl=en&client=pub-2388949037677090&cof=FORID:13;AH:left;CX:Stutter%252Ein;L:http://www.google.com/intl/en/images/logos/custom_search_logo_sm.gif;LH:30;LP:1;VLC:%23551a8b;DIV:%23cccccc;&boostcse=0&q=' + searchquery;
			$.fn.colorbox({href:searchlocation, width:'80%', height:'70%', iframe:true});
		}else{
			$.jGrowl('Processing...');}
		strData = $(this).serialize();
		ajaxPost(strData, 'statusupdate.php', 'post');
		$('#statusbar').autofill({
			value: 'status update or search',
			defaultTextColor: '#999',
			activeTextColor: '#333'
		});
		$('span.counter').text('140');
		return false;
	});
	$('#defaultservices').submit(function(){
		strData = $(this).serialize();
		ajaxPost(strData, 'settings.php', 'defaultchange');
		return false;
	});
	$('#profilesettings').submit(function(){
		strData = $(this).serialize();
		ajaxPost(strData, 'settings.php', 'settingschange');
		resetsettingfields();
		return false;
	});
	$('#addtumblr').submit(function(){
		strData = $(this).serialize();
		ajaxPost(strData, 'callback/index.php', 'addtumblr');
		return false;
	});
	$('#forgotpwform').submit(function(){
		strData = $(this).serialize();
		ajaxPost(strData, 'forgot.php', 'forgotpw');
		return false;
	});
	
	//mousover
	$("#regpreview").mouseover(function(){
		$("#bubblemouseover").css('visibility','visible');
	}).mouseout(function(){
		$("#bubblemouseover").css('visibility','hidden');
	});

	//settings page mousover
	$('.delacctx').hide();
	$("div.acctchoice").mouseover(function(){
		$(this).next().show();
    	}).mouseout(function(){
		$(this).next().hide();
    	});
	$("a.delacctx").mouseover(function(){
		$(this).show();
		$(this).children().attr('src','images/cross.png');
   	}).mouseout(function(){
		$(this).hide();
		$(this).children().attr('src','images/gcross.png');
    	});

	//manage services mouseover
	$("#statusupdatebutton").mouseover(function(){
		$(this).attr('src','images/manageservices_button2.png');
    	}).mouseout(function(){
		$(this).attr('src','images/manageservices_button.png');
    	});

});


//ajax stuff
function ajaxPost(strData, postScriptURL, postType){
	if(postType == 'addacct'){
	}else{
		$.jGrowl('close');}
	$.ajax({
		type: 'POST',
		url: postScriptURL,
		data: strData,
		success: function(msg){
			msgtest = msg.substr(0,2);
			if(msgtest == 'OK') {
				if(postType == 'login'){
					window.location = "index.php";}
				else if(postType == 'register'){
					window.location = "index.php";}
				else if(postType == 'forgotpw'){
					$.jGrowl('An email has been sent to the provided email account with further information.');}
				else if(postType == 'addacct'){
					window.location.reload();}
				else if(postType == 'defaultchange'){
					$.jGrowl('Your service selection defaults have been saved.');}
				else if(postType == 'settingschange'){
					$.jGrowl('Your settings have been saved. Name changes will be seen upon page refresh.');}
				else if(postType == 'post'){
					$.jGrowl('Your update has been posted to selected services.');}
				else{
					$.jGrowl('Whatever happend, worked!');}
			}
			else if(msgtest != 'OK'){
				if(msgtest != 'NO'){
					if(postType == 'post'){
						var mySplitResult = msg.split(",");
						for(i = 0; i < mySplitResult.length; i++){							
							if(mySplitResult[i].length >= 3)							
								$.jGrowl(mySplitResult[i]);
						}
					}
					else{
					$.jGrowl(msg, {
						header: 'Houston, we have a problem!',
						sticky: true,
						glue: 'before'
					});}
				}
			}
		}
	});
}

function fillertext(){
	$('#emaillogbox').autofill({
		value: 'email',
		defaultTextColor: '#999',
		activeTextColor: '#333'
	});
	$('#pwlogbox').autofill({
		value: 'password',
		defaultTextColor: '#999',
		activeTextColor: '#333'
	});
	$('#regname').autofill({
		value: 'name',
		defaultTextColor: '#999',
		activeTextColor: '#333'
	});
	$('#regemail').autofill({
		value: 'email',
		defaultTextColor: '#999',
		activeTextColor: '#333'
	});
	$('#regpw').autofill({
		value: 'password',
		defaultTextColor: '#999',
		activeTextColor: '#333'
	});
	$('#regpw2').autofill({
		value: 'confirm password',
		defaultTextColor: '#999',
		activeTextColor: '#333'
	});
	if($('#statusbar').val() == '')	
	$('#statusbar').autofill({
		value: 'status update or search',
		defaultTextColor: '#999',
		activeTextColor: '#333'
	});
	$('#currpw').autofill({
		value: 'current password',
		defaultTextColor: '#999',
		activeTextColor: '#333'
	});
	$('#npwval').autofill({
		value: 'new password',
		defaultTextColor: '#999',
		activeTextColor: '#333'
	});
	$('#npwval2').autofill({
		value: 'confirm new password',
		defaultTextColor: '#999',
		activeTextColor: '#333'
	});
	$('#siacctemail').autofill({
		value: 'Stutter.in Email',
		defaultTextColor: '#999',
		activeTextColor: '#333'
	});
}

function resetsettingfields(){
	if($('#currpw').val() != '')
	$('#currpw').autofill({
      value: 'current password',
      defaultTextColor: '#999',
      activeTextColor: '#333'
    });
	if($('#npwval').val() != '')
	$('#npwval').autofill({
      value: 'new password',
      defaultTextColor: '#999',
      activeTextColor: '#333'
    });
	if($('#npwval2').val() != '')
	$('#npwval2').autofill({
      value: 'confirm new password',
      defaultTextColor: '#999',
      activeTextColor: '#333'
    });
	if($('#regpw').val() != '')
	$('#regpw').autofill({
      value: 'new password',
      defaultTextColor: '#999',
      activeTextColor: '#333'
    });
	if($('#regpw2').val() != '')
	$('#regpw2').autofill({
      value: 'confirm new password',
      defaultTextColor: '#999',
      activeTextColor: '#333'
    });
}

function addservice(servicetype){
	if (servicetype == 'tumblr'){
		strData = $('#addtumblr').serialize();
		ajaxPost(strData, 'callback/index.php', 'addacct');
	}else if (servicetype == 'plurk'){
		strData = $('#addplurk').serialize();
		ajaxPost(strData, 'callback/index.php', 'addacct');
	}else{
		$.jGrowl('Incorrect service type.');
	}
}

(function ($) {
// VERTICALLY ALIGN FUNCTION
$.fn.vAlign = function() {
	return this.each(function(i){
	var ah = $(this).height();
	var ph = $(this).parent().height();
	var mh = (ph - ah) / 2;
	$(this).css('margin-top', mh);
	});
}
})(jQuery);

