var file = window.location.pathname.split('/');

function refreshCaptcha() {
	$('#captcha').attr('src','/style/imgs/captchaImage.png?img=' + Math.random());	
}
function visitorFormFields(){
	var varName = new Array(), varValue = new Array(), combinedValues = new Array();

	jQuery.each($('#signBox :input'),function(id,va){
		name = $(this).attr('name');
		if($(this).is(':checkbox')){
			val = $(this+':checked').val();
		}
		else {
			val = $(this).val();
		}
		varName.push(name);
		varValue.push(val);
	});

	combinedValues[0] = varName;
	combinedValues[1] = varValue;

	return combinedValues;
}

$('a[name*="visitorLink"]').live('click',function(){

	var windowTitle, targetURL = $(this).attr('href');

	switch($(this).attr('id')){
		case "signInWindow":
			windowTitle = "Sign In!";
		break;
		case "signUpWindow":
			windowTitle = "Sign Up!";
		break;
		default:
			return false;
	}

	if($("#signBox").exists()){
		if($("#signBox").dialog("isOpen")){
			$("#signBox" ).dialog( "destroy" );
		}
		$("#signBox" ).remove();
		return false;
	}
	else {
		$('body').append('<div id="signBox" title="'+windowTitle+'"><div id="loader" style="text-align:center; display:block;"><img src="/style/imgs/processingLoader.gif" /></div></div>');
		$( "#signBox" ).dialog({
			modal: true,
			autoOpen: false,
			width: 620,
			position: ['center',80],
			closeText :'',
			resizable: false,
			close: function(){$('#signBox').remove();}
		});

		$("#signBox").load( targetURL + ' #form', function(){	$('#loader').remove();	});
		$("#signBox" ).dialog( "open" );
	}
	return false;
});

function signIn(){

	if(file[1] == 'do')
		return true;


	var vars = visitorFormFields(), varName = vars[0], varValue = vars[1], linkPath = $('form[name="signInForm"]').attr('action');

	$('#form').loading({'position':'after'});

	$.post(linkPath, {

		username: null,
		varName: varName,
		varValue: varValue

		},function(result){

			if(result.failure == true) {
				$('#loader').remove();
				$('button[type=submit]').processingButton({'action':'stop'});
				$('.error').errorMessage({'message':result.message,'dialog':true,'duration':300000});
			}
			else {
				$('#header').load(window.location.pathname+' #headerContent');
				if($('.commentFormBox').exists()){
					$('.commentFormBox').load(window.location.pathname+' #commentBox');
				}
				$('a[name="visitorLink"][id="signInWindow"]').click();
			}
	},'json');

	return false;
}

function signUp(){

	if(file[1] == 'do')
		return true;

	var vars = visitorFormFields(), varName = vars[0], varValue = vars[1], linkPath = $('form[name*="signUpForm"]').attr('action');

	$('#form').loading({'position':'after'});

	$.post(linkPath, {

		username: null,
		varName: varName,
		varValue: varValue

		},function(result){

			if(result.failure == true) {
				$('#loader').remove();
				$('button[type=submit]').processingButton({'action':'stop'});
				$('.error').errorMessage({'message':result.message,'dialog':true,'duration':300000,'dialog':true});
			}
			else {
				$('a[name="visitorLink"][id="signUpWindow"]').click();
				$('.success').successMessage({'message':result.message,'position':'inside','duration':300000,'dialog':true});
			}
	},'json');

	return false;
}
var postComment = function(){

	var varName = new Array(), varValue = new Array();

	jQuery.each($('form[name="postCommentForm"] :input'),function(id,va){
		name = $(this).attr('name');
		if($(this).is(':checkbox')){
			val = $(this+':checked').val();
		}
		else {
			val = $(this).val();
		}
		varName.push(name);
		varValue.push(val);
	});

	var linkPath = $(this).attr('action');
	
	$.post('/comments/postComment/', {

		key: null,
		varName: varName,
		varValue: varValue

		},function(result){

			if(result.failure == true) {
				$('.error').errorMessage({'message':result.message,'dialog':true,'duration':5000});
			}
			else {
				$('.comments').load(window.location.pathname+' #commentsList');
				$('.commentFormBox').slideUp('fast');
			}
	},'json');


	return false;
}
$(document).ready(function(){

	$.each($('.codeHighlighter'),function(id,val){
		var line = '';
		$(this).attr('id',id);
		$.each($('.codeHighlighter:eq('+id+') ol li'),function(id2,value){
			$('.coderExp a').live('click',function(){$(this).attr('href','#');return false;});
			if(id2%2 == 0) {
				line += '<div class="codeLine-e"><table><tbody><tr><td class="lineNumber">'+(id2+1)+'</td><td class="lineCode" id="codeHighlighterLine:'+id+'-'+id2+'">'+$(this).html()+'</td></tr></tbody></table></div>';
			}
			else {
				line += '<div class="codeLine-o"><table><tbody><tr><td class="lineNumber">'+(id2+1)+'</td><td class="lineCode" id="codeHighlighterLine:'+id+'-'+id2+'">'+$(this).html()+'</td></tr></tbody></table></div>';
			}
		});
		
		$(this).html(line);
					
	});
	

	$('#embedTutorial').live('click',function(){
		document.getElementById('embedTutorial').focus();
		document.getElementById('embedTutorial').select();
	});

});
