// JavaScript Document
$(document).ready(function() {
						   
	$(document).pngFix();
	// simplest example
	$('#slidy, #logotext #logotext_spans').cycle({
		fx: 'fade',
		timeout: 6000,
		prev:    '.prev',
        next:    '.next',
		cleartype: 1,
		height: 'auto',
		containerResize: 1
	});
	
	$('#slidy2').cycle({
		fx: 'fade',
		timeout: 3000
	});
	
	$('#slidy3').cycle({
		fx: 'fade',
		timeout: 4500
	});
	
	$('#slidy2 div').each(function() {
		//var mTop = Math.round($(this).attr('height')/2) +'px';
    	//$(this).css({top: '50%', marginTop: '-'+mTop});
    }); 

	$("#righttop2, #righttop").tabs();
	
	$('#captcha1').simpleCaptcha({
   		numImages: 5,
    	introText: '<p>Please pick out the <strong class="captchaText"></strong> so that we can help reduce spam.</p>',
		scriptPath: 'js/captcha/simpleCaptcha.php'
	});
});

sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}

function submit_comment(obj){
	
	var bid = $('#comment_form #bid').val();
	var name = $('#comment_form #name').val();
	var email = $('#comment_form #email').val();
	var show_email = $('#comment_form #show_email:checked').val();
	var comment = $('#comment_form #comment').val();
	var captcha = $('input[name="captchaSelection"]').val();
	
	//alert('name: '+name+'  \nemail:'+email+' \nshow_email:'+show_email+' \ncomment:'+comment+' \ncaptcha:'+captcha);
	
	
	$(obj).empty();
	$(obj).append('submitting .');
	var dots = setInterval(function(){ $(obj).append('.'); }, 750);
	
	$.post("ajax/blog/add_comment.php", { bid:bid, name:name, email:email, show_email:show_email, comment:comment, captcha:captcha}, function(data){ 
		
		$('#messages').empty();
		clearInterval(dots);
		$('#messages').append(data);
		//$.scrollTo('#messages', 500, {offset:-10});
		
		$(obj).empty();
		$(obj).append('submit comment');
		
		if(substr(data, 12, 3)=="suc"){
			$('#comment_form, #addCmtTitle').remove();
			setTimeout(function(){ $('#messages .success').fadeOut(250);  }, 4000);
			setTimeout(function(){ $('#messages .success').remove();  }, 4250);
			$(obj).parent().parent().parent().parent().parent().slideUp(500);
			$('#addCmtTitle').slideUp(500);
			
			load_comments(bid);
		}
		
	});
		
}

function load_comments(bid){
	$.getJSON("ajax/blog/load_comments.php",'bid='+bid, function(json){

		$('#comments').empty();
		for(i in json){
			var comment = '<div class=\"comment\"></div>';
			$('#comments').append(json[i].comment);
			
		}
		
		
		
	}); //End json 	
}


function substr (f_string, f_start, f_length) {
    // *       example 1: substr('abcdef', 0, -1);
    // *       returns 1: 'abcde'
    // *       example 2: substr(2, 0, -6);
    // *       returns 2: ''
 
    f_string += '';
 
    if (f_start < 0) {
        f_start += f_string.length;
    }
 
    if (f_length == undefined) {
        f_length = f_string.length;
    } else if (f_length < 0){
        f_length += f_string.length;
    } else {
        f_length += f_start;
    }
 
    if (f_length < f_start) {
        f_length = f_start;
    }
 
    return f_string.substring(f_start, f_length);
}
