var currentChar=1;
var currentText=1;
function updatehref() {
	$('#typewriter').attr('href', $("#newsTiker li a").eq(currentText-1).attr('href'));
}
function typewriter(){
	if(currentText>$("#newsTiker li a").size()){currentText = 1;updatehref();} 
	text = $("#newsTiker li a").eq(currentText-1).text();
	$('#typewriter').text(text.substr(0, currentChar)+'_');
	currentChar++
if (currentChar>text.length){
		$('#typewriter').text(text.substr(0, currentChar));
		currentText++;
		currentChar=1;
		updatehref();
		setTimeout("typewriter()", 6000);
	}else{
		setTimeout("typewriter()", 100);
	}
}

$(document).ready(function() {
	$("#newsTiker").hide();
	$('#books').cycle({
		fx: 'all', 
		delay:   -4000 
	});
	$('#pubs').cycle({
		fx: 'zoom,curtainX', 
		delay:   -2000,
		timeout:  10000
	});
	$('#vote').click(function(){
		v = $("input[name='data[Poll][val]']:checked").val()
		if(typeof(v)=='undefined'){
			$(this).colorbox({href:"#poll_err1", inline:true, open:true, title: 'نظر سنجی'});
			//$('#vote').colorbox({open: false});
			//alert('plz select option');
		}else{
		$("#pollbox").mask("در حال ارسال...");
		 $.ajax({
		   type: "POST",
		   url: $('#pollForm').attr('action'),
		   data: "data[Poll][val]="+v,
		   dataType: "json",
		   success: function(msg){
		     if(  msg.data =='seted'  ){
		    	 $(this).colorbox({href:"#poll_err2", inline:true, open:true, title: 'نظر سنجی'});
		     }else if(typeof(msg.data)=='number'){
		     	$('#pollTB').hide();
		     	$('#pcell'+v).html('<b> '+msg.data+' <b>');
		     	$('#pollTB').fadeIn(800);
		     }
		     $("#pollbox").unmask()
		   }
		 });
		}
	});	
	typewriter();
	$('#RenewCaptcha').live('click',function(){RenewCaptcha();});
	function RenewCaptcha(){
		rnd = Math.ceil(Math.random()*1000000000);
		capsrc = $("#captchaImage").attr('src').replace(/\/rnd:.*/, '');
		capsrc +='/rnd:'+rnd;
		$("#captchaImage").attr('src', capsrc);
	}
	$('#sbBtn').click(function(){
		q = $.trim($('#q').val())
		if(q.length>=3){
			newac = $('#Sfrm').attr('action')+ '' + q;
			$('#Sfrm').attr('action',newac)
		}else{
			alert('حداقل 3 حرف برای جستجو وارد نمایید');
			return false;
		}
	});
	$('.book a').click(function(){
		id = $(this).attr('id').replace('Book','');
		//text = $('#BookT'+id).text();
		$(this).colorbox({href:'#BookT'+id, inline:true, open:true, title: 'معرفی کتاب', width:480});
		return false;
	});
	$('#ViewComments').live('click',function(){
		if($('#CommentsList').is(':hidden')){
			var u=$(this).attr('rel');
			if($('#CommentsList input').size()==0) $.get(u,function(data){$('#CommentsList').html(data)});
			$('#CommentsList').slideDown();
			$('#ViewComments').addClass('avcom');
		}else{
			$('#CommentsList').hide();
			$('#ViewComments').removeClass('avcom');
		}
	});
	$('.clist').live('click',function(){
		var id = $(this).attr('id').replace('Cl','');
		if($('#Co'+id).is(':hidden')) 
			$(this).addClass('aclist');
		else
			$(this).removeClass('aclist');
		$('#Co'+id).slideToggle(700)
	});
	$("#CommentSubmit").live('click',function(){
		//$("#AContent").mask("در حال ارسال اطلاعات...");
		pd = {};
		$('#CommentForm input, #CommentForm textarea').each(function(){
			n = $(this).attr('name'); v = $(this).val(); 
			if( n!='' ){
				pd[n] = v;
			}
		});
		ac = $('#CommentForm').attr('action');
		$.post(ac, pd,function(data){
			if(data=='Eerror'){
				//$.fn.colorbox({width:350, height:130, inline:true, href:"#comment_Eerr",open: true});
				alert($('#comment_Eerr p').text());
			}else if(data=='Derror'){
				//$.fn.colorbox({width:350, height:130, inline:true, href:"#comment_Derr",open: true});
				alert($('#comment_Derr p').text());
			}else if(data=='cerror'){
				//$.fn.colorbox({width:350, height:130, inline:true, href:"#comment_cerr",open: true});
				alert($('#comment_cerr p').text());
			}else if(data == 'yes'){
				//$.fn.colorbox({width:350, height:130, inline:true, href:"#comment_success",open: true});
				alert($('#comment_success p').text());
				$('#CommentForm :text ,#CommentForm textarea').val('');
				$('#CommentForm select').selectedIndex = -1;
			}else{
				$.fn.colorbox({width:350, height:130, inline:true, href:"#Faq_cerr",open: true});
			}
			RenewCaptcha();
			});
		//$("#AContent").unmask();
		return false;
	});	
});

