$(document).ready(function(){
	var last = 3;
	var HeadNewsSize = $('div.news').size();
	var ton = false;
	function changeHNewsFocus(){
		if(!last) last = 0;
		tid = last +1
		if(tid >3 ) tid = 1;
		$('div.news').removeClass('hn');$('#news'+tid).addClass('hn');$('#nMainImage').html($('#ni'+tid).html())
		last = tid;
		var t = window.setTimeout(changeHNewsFocus,2500);
	}
	changeHNewsFocus();
	$('div.news').livequery('mouseover',function(){
//		window.clearTimeout(t);
		$('.news').removeClass('hn');
		$('#nMainImage').html($('#ni'+$(this).attr('id').replace('news','')).html())
		$(this).addClass('hn');
	});
	//$('div.news').livequery('mouseout',function(){
	//	var t = window.setTimeout(changeHNewsFocus,2500);
	//});

		//
		/*function onAfter(curr,next,opts) {
			//$('#yimages').cycle(opts.currSlide);
			tid = opts.currSlide + 1;
			$('div.news').removeClass('hn');
			$('#news'+tid).addClass('hn');
		}	
		$('#nMainImage').cycle({ 
		    fx:         'fade', 
		    timeout:     2000, 
		    //pager:      '.news', 
		    //pagerEvent: 'mouseover', 
		    //fastOnEvent: true ,
		    delay:   100 ,
		    speed:   100,
		    after:     onAfter
		});*/
		/*$('.news').livequery('hover',function(){
			$('.news').removeClass('hn');
			//var nid = parseInt((this).attr('id').replace('news','')) -1;
			alert('xxxx')
			$(this).addClass('hn');
			$('#nMainImage img').hide();
			$('#nMainImage img').eq(id).show();
			$('#nMainImage').cycle('pause'); 
		});
		$('div#HeadNews').livequery('mouseout',function(){
			$('#nMainImage').cycle('resume'); 
		});*/
		function hideSearchOptions(){
			$('#searchOp').slideUp();
		}
		function showSearchOptions(){
			var offset = jQuery('#q').offset();
			var left = offset.left - 77;
			var top  = offset.top + 18;
			$('#searchOp').css({'left':left,'top':top});
			$('#searchOp').slideDown();
		}

		
		$('#q').hover(function(){
			showSearchOptions();
		},function(){
			t1 = window.setTimeout(hideSearchOptions,2000);
		});
		$('#q').focus(function(){
			showSearchOptions()
		},function(){
			t1 = window.setTimeout(hideSearchOptions,2000);
		});
		$('#searchOp').hover(function(){
			clearTimeout(t1);
		},function(){
			$('#searchOp').slideUp();
		});
		function timer(){
			window.setTimeout(timer,1000);
			var lTime = $('#bakuTime').text();
			picks = lTime.split(":");
			var re=/^0.$/; 
			if( re.test(picks[0]) ){picks[2] = picks[0].replace('0','');}if( re.test(picks[1]) ){picks[2] = picks[1].replace('0','');}if( re.test(picks[2]) ){picks[2] = picks[2].replace('0','');}
	    	h = parseInt(picks[0]);m = parseInt(picks[1]);s = parseInt(picks[2]);
			var nm = m;var nh = h;var ns = s+1;
			if(ns==60){
	    		ns = 0;
	    		nm +=1;
			}
	    	if(nm==60){
	    		nm = 0;
	    		nh +=1;
			}
	    	if(nh==24) window.location.reload();
			if(ns<10) ns = '0'+ns;
			if(nm<10) nm = '0'+nm;
			if(nh<10) nh = '0'+nh;
			$('#bakuTime').html(nh+':'+nm+':'+ns);
		}
		timer();
	    
		$('#date').cycle({
			fx: 'blindZ',
		    delay:   -3000 ,
		    speed:   1000,
            timeout: 10000 
		});
		$("#yimages").jCarouselLite({
			auto: 9020,
		    speed: 2000,
			btnNext: "#Yprev",
	        btnPrev: "#Ynext",
	        visible: 4
	    });

		$("#yazarts").jCarouselLite({
			auto: 9020,
		    speed: 2000,
			btnNext: "#Yprev",
	        btnPrev: "#Ynext",
	        visible: 4,
	        vertical: true
	    });
		/*$('#yimages').cycle({ 
		    fx:      'scrollHorz', 
		    speed:    1000, 
		    timeout:  6500 ,
	        next:   '#Yprev', 
	        prev:   '#Ynext',
	        after:     onAfter
		});
		$('#yazarts').cycle({ 
		    fx:      'scrollHorz', 
		    speed:    900, 
		    timeout:  6500 ,
	        next:   '#Yprev', 
	        prev:   '#Ynext'
		});*/
		
		$('#ads').livequery(function(){
			$(this).cycle({
				fx: 'fade',
			    delay:   -3000, 
			    speed:   6000,
	            timeout: 15000 
			});		
		});

	    $('#books').cycle({
			fx: 'all', 
			delay:   -4000 
		});
		$('#yenilar').cycle({
			fx: 'zoom,curtainX', 
			delay:   -2000,
			//speed:    1000,
			timeout:  10000
		});
		$('#qtype1').cycle({
			fx: 'shuffle,blindX', 
			delay:   -2000,
			//speed:    1000,
			timeout:  10000
		});
		//$("#loading").ajaxStart(function(){
		//   $(this).show();
		//});
		//$("#loading").ajaxStop(function(){
		//   $(this).hide();
		//});		
		function ap(u){
			$("#content").mask("Yüklənir...");
			$.ajax({
			  url: u,
			  cache: true,
			  error: function(){
			  	$("#content").html('<div id="AContent"><h1 align="center">Error: Can not Load Page!</h1></div>');
			  	$("#content").unmask();
			  },
			  success: function(html){
			  	$("#content").html(html);
			  	$("#content").unmask();
			  }
			});	
		}
		$.historyInit(ap, "home/");
		$('#vote').click(function(){
			v = $("input[name='data[Poll][val]']:checked").val()
			if(typeof(v)=='undefined'){
				$.fn.colorbox({width:"400", height:150, inline:true, href:"#poll_err1", title:'Sorğu', open: true});
				//$('#vote').colorbox({open: false});
				//alert('plz select option');
			}else{
			$("#pollbox").mask("Yüklənir...");
			 $.ajax({
			   type: "POST",
			   url: $('#pollForm').attr('action'),
			   data: "data[Poll][val]="+v,
			   dataType: "json",
			   success: function(msg){
			     if(  msg.data =='seted'  ){
			     	$.fn.colorbox({width:"400", height:150, inline:true, href:"#poll_err2", title:'Sorğu',open: true});
			     }else if(typeof(msg.data)=='number'){
			     	$('#pollTB').hide();
			     	$('#pcell'+v).html('<b> '+msg.data+' <b>');
			     	$('#pollTB').fadeIn(800);
			     }
			     $("#pollbox").unmask()
			   }
			 });
			}
		});
		$('a.ext').attr('target', '_blank');
		$('a#crylicSite').livequery('click',function(){window.location.search = 'crylic'});
		$('a#latinSite').livequery('click',function(){window.location.search = 'latin'});
		$('a:not(.box,.gallery,.starRating,.ext,#inlineCrylic2,[rel=GI])').livequery('click',function(){
			l = $(this).attr('href');
			cls = $(this).attr('class')
			isJs = /^javascript.*/i.test(l);
			var hn = window.location.hostname;
			isHttp = /^http:\/\/.*/i.test(l);
			if(l=='/' ){
				l='home/'
			}
			if(!isJs && l!='' && cls!='box' && cls!='ext'){
				hash = l.replace(/^.*#/, '');
				$.historyLoad(hash);
				//ap(l);
				return false;
			}else{
				if(cls=='ext')
					return true;
				else
					return false;
			}
		});
		$('#cboxLoadedContent #AContent a:not(#R1,#R2,#R3,#R4,#R5,#ViewComments,.starRating, #RenewCaptcha)').livequery('click',function(){
//			$(this).colorbox({iframe:false, width:750, height:585,scrolling: false,opacity: 0.5,open: true});//$.fn.`({html:'<p>Wait</p>',open:true});
			cls = $(this).attr('id');
			if(cls != 'GoHome'){
				u = $(this).attr('href');
				$('#NaC').html('');
				$.fn.colorbox({width:750, height:585,scrolling: false,opacity: 0.5,open: true, inline:true, href:"#NaC"}, function(){$.get(u,function(data){$('#NaC').html(data)});});
				return false;
			}else{
				$.fn.colorbox.close();
			}
		});
		$("a.gallery").livequery('click',function(){
			$(this).colorbox({scrolling: false,opacity: 0.5,open: true});
			return false;
		});
		$("a[rel=GI]").livequery('click',function(){
			$(this).colorbox({scrolling: false,opacity: 0.5,open: true});
			return false;
		});		
		$("a.box").livequery('click',function(){
			u = $(this).attr('href');
			$('#NaC').html('');
			ttl = $(this).text();
			$(this).colorbox({width:750, height:585,scrolling: false,opacity: 0.5,open: true, inline:true, href:"#NaC",title:ttl}, function(){$.get(u,function(data){$('#NaC').html(data)});});
			return false;
		});
		var fontSizes = [11,12,14,16,18];
		$("a.rsz").livequery('click',function(){
			i = $(this).attr('id').replace('R','');
			i = Math.floor(i);
			if(i<=fontSizes.length && i >=1)
				$('#cText').css('font-size',fontSizes[i-1]);
		});
		$('#searchForm').submit(function(){
			q   = $('#q').val()
			if($.trim(q)!=''){
				se = false;
				index = 0;
				$("#searchForm input:radio").each(function (i){
					ch = $(this).attr('checked');
					if(ch){
						se = true;index = i;return false;
					}
				});
				if(!se)
					$("#searchForm input:radio:eq(0)").attr('checked', true);
				
				sps = new Array('News/', 'Articles/', 'faqs/1/', 'faqs/2/');
				if(sps[index] && sps[index]!=''){ 
					fnc = sps[index];act = $('#searchForm').attr('action');q = q.replace('+',' AND ');act+=fnc+q;hash = act;$.historyLoad(hash);
				}else
					return false;
			}else{
				$.fn.colorbox({width:350, height:130, inline:true, href:"#SeaechErr1",open: true, title:''});
			}
			return false;
			
		});
		function RenewCaptcha(){
			rnd = Math.ceil(Math.random()*1000000000);
			capsrc = $("#captchaImage").attr('src').replace(/\/rnd:.*/, '');
			capsrc +='/rnd:'+rnd;
			$("#captchaImage").attr('src', capsrc);
		}
		$("#faqSubmit").livequery('click',function(){
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			if(reg.test($('#FaqEmail').val()) == false){
				$.fn.colorbox({width:350, height:130, inline:true, href:"#Faq_Eerr",open: true, title:''});
				
				return false;
			} 
			if($('#FaqFaqDepartmentId').size()==1){
				if($('#FaqFaqDepartmentId').val()==""){
					$.fn.colorbox({width:350, height:130, inline:true, href:"#Faq_Merr",open: true, title:''});
					return false;
				}
			}
			if($.trim($('#FaqSubject').val())==''){
				$.fn.colorbox({width:350, height:130, inline:true, href:"#Faq_Serr",open: true, title:''});
				return false;
			}
			if($.trim($('#FaqQuestion').val())==''){
				$.fn.colorbox({width:350, height:130, inline:true, href:"#Faq_Qerr",open: true, title:''});
				return false;
			}
			if($.trim($('#FaqCaptcha').val())==''){
				$.fn.colorbox({width:350, height:130, inline:true, href:"#Faq_cerr",open: true, title:''});
				return false;
			}
			$("#content").mask("Yüklənir...");
			pd = {};
			$('#faqForm input, #faqForm textarea, #faqForm select').each(function(){
				n = $(this).attr('name'); v = $(this).val(); 
				if( n!='' ){
					pd[n] = v;
				}
			});
			ac = $('#faqForm').attr('action');
			$.post(ac, pd,function(data){
				if(data=='cerror'){
					$.fn.colorbox({width:350, height:130, inline:true, href:"#Faq_cerr",open: true, title:''});
				}else if(data == 'yes'){
					$.fn.colorbox({width:350, height:130, inline:true, href:"#Faq_success",open: true, title:''});
					$('#faqForm :text ,#faqForm textarea').val('');
					$('#faqForm select').selectedIndex = -1;
					//document.getElementById('faqForm').reset();
				}else{
					alert(data)
					$.fn.colorbox({width:350, height:130, inline:true, href:"#Faq_cerr",open: true, title:''});
				}
				RenewCaptcha();
   			});
			$("#content").unmask();
			return false;
		});

		$("#CommentSubmit").livequery('click',function(){
			$("#content").mask("Yüklənir...");
			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();
   			});
			$("#content").unmask();
			return false;
		});
		
		$("#ContactSubmit").livequery('click',function(){
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			if(reg.test($('#ContactEmail').val()) == false){
				$.fn.colorbox({width:350, height:130, inline:true, href:"#Contact_Eerr",open: true});
				return false;
			} 
			if($.trim($('#ContactMessage').val()) == ''){
				$.fn.colorbox({width:350, height:130, inline:true, href:"#Contact_Merr",open: true});
				return false;
			} 
			
			$("#content").mask("Yüklənir...");
			pd = {};
			$('#ContactForm input, #ContactForm textarea').each(function(){
				n = $(this).attr('name'); v = $(this).val(); 
				if( n!='' ){
					pd[n] = v;
				}
			});
			ac = $('#ContactForm').attr('action');
			$.post(ac, pd,function(data){
				if(data=='Eerror'){
					//$.fn.colorbox({width:350, height:130, inline:true, href:"#comment_Eerr",open: true});
					alert($('#Contact_Eerr p').text());
				}else if(data=='merror'){
					//$.fn.colorbox({width:350, height:130, inline:true, href:"#comment_Derr",open: true});
					alert($('#Contact_Merr p').text());
				}else if(data=='cerror'){
					//$.fn.colorbox({width:350, height:130, inline:true, href:"#comment_cerr",open: true});
					alert($('#Contact_cerr p').text());
				}else if(data == 'yes'){
					//$.fn.colorbox({width:350, height:130, inline:true, href:"#comment_success",open: true});
					alert($('#Contact_success p').text());
					$('#ContactForm :text ,#ContactForm textarea').val('');
					$('#ContactForm select').selectedIndex = -1;
				}else{
					$.fn.colorbox({width:350, height:130, inline:true, href:"#Contact_cerr",open: true});
				}
				RenewCaptcha();
				});
			$("#content").unmask();
			return false;
		});
		
		$("#GBookSubmit").livequery('click',function(){
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			if(reg.test($('#GuestbookEmail').val()) == false){
				$.fn.colorbox({width:350, height:130, inline:true, href:"#Guestbook_Eerr",open: true});
				return false;
			} 
			if($.trim($('#GuestbookMessage').val()) == ''){
				$.fn.colorbox({width:350, height:130, inline:true, href:"#Guestbook_Merr",open: true});
				return false;
			} 
			
			$("#content").mask("Yüklənir...");
			pd = {};
			$('#GuestbookForm input, #GuestbookForm textarea').each(function(){
				n = $(this).attr('name'); v = $(this).val(); 
				if( n!='' ){
					pd[n] = v;
				}
			});
			ac = $('#GuestbookForm').attr('action');
			$.post(ac, pd,function(data){
				if(data=='Eerror'){
					//$.fn.colorbox({width:350, height:130, inline:true, href:"#comment_Eerr",open: true});
					alert($('#Guestbook_Eerr p').text());
				}else if(data=='merror'){
					//$.fn.colorbox({width:350, height:130, inline:true, href:"#comment_Derr",open: true});
					alert($('#Guestbook_Merr p').text());
				}else if(data=='cerror'){
					//$.fn.colorbox({width:350, height:130, inline:true, href:"#comment_cerr",open: true});
					alert($('#Guestbook_cerr p').text());
				}else if(data == 'yes'){
					//$.fn.colorbox({width:350, height:130, inline:true, href:"#comment_success",open: true});
					alert($('#Guestbook_success p').text());
					$('#GuestbookForm :text ,#GuestbookForm textarea').val('');
					$('#GuestbookForm select').selectedIndex = -1;
				}else{
					$.fn.colorbox({width:350, height:130, inline:true, href:"#Guestbook_cerr",open: true});
				}
				RenewCaptcha();
				});
			$("#content").unmask();
			return false;
		});		
		
		
		$('#RenewCaptcha').livequery('click',function(){RenewCaptcha();});
		$("#ShowSearchTip").livequery('click',function(){$.fn.colorbox({width:350, height:130, inline:true, href:"#SearchTips",open: true});});
		$('#ViewComments').livequery('click',function(){
			if($('#CommentsList').is(':hidden')){
				$("#content").mask("Yüklənir...");
				var u=$(this).attr('rel');
				if($('#CommentsList input').size()==0 || 1==1) $.get(u,function(data){$('#CommentsList').html(data)});
				$('#CommentsList').slideDown();
				$("#content").unmask();
				$('#ViewComments').addClass('avcom');
			}else{
				$('#CommentsList').hide();
				$('#ViewComments').removeClass('avcom');
			}
		});
		$('.clist').livequery('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)
		});
		var closeit = false;
		function closeCbox(ct){
			if(closeit){
				setTimeout($.fn.colorbox.close, ct);
			}
			closeit = false;
		}
		if($('#Notify').size()>0){
			Ntitle = $('#NotifyTitle').text();
			nw = $('#NotifyTitle').attr('w');
			nh = $('#NotifyTitle').attr('h');
			ct = $('#NotifyTitle').attr('ct');
			di = $('#NotifyTitle').attr('di');
			bi = $('#NotifyTitle').attr('bi');
			bp = $('#NotifyTitle').attr('bp');
			br = $('#NotifyTitle').attr('br');
			if(!nw || nw=="") nw = 350;
			if(!nh || nh=="") nh = 130;
			
			$('#Notify').colorbox({width:nw, height:nh, inline:true, href:"#Notify",open: true,title :Ntitle});
			if($('#NotifyTitle').attr('b')!="")$('#cboxContent, #cboxLoadedContent').css('background-color', "#"+$('#NotifyTitle').attr('b'))
			
			if(bi!="") {
				$('#cboxContent, #cboxLoadedContent').css('background-image', 'url('+bi+')' );
				if(bp) $('#cboxContent, #cboxLoadedContent').css('background-position', bp );
				if(br) $('#cboxContent, #cboxLoadedContent').css('background-repeat', br );
			}
			$('#Notify').css('margin','5px')

			$(document).bind('cbox_closed', function(){
				$('#cboxContent, #cboxLoadedContent').css('background-color', '#fff');
				$('#cboxContent, #cboxLoadedContent').css('background', '#fff');
			});
			if(ct && ct!="" && ct!=0){
				closeit = true;
				$(document).bind('cbox_complete', function(){
					closeCbox(ct,true);
				});
			}
		}
});
