$(document).ready(function() {		
     
     ChiliBook.lineNumbers = true; 
     $('div#code').chili();
    
    /*
    $('pre').each(function() { //on each code box do
    $(this)
      .before('<a href="#" class="codeswitch">View this source in Plain Text</a>') //write a code right before the code-box
      .after('<textarea rows="' + ($(this).children().html().split("\n").length-1) + '" cols="50">' + $(this).children().html() + '</textarea>'); //write a textarea with the content of the code-box after it
  });
  
  $('.codeswitch').toggle(function() { //hide code-box and show textarea
    $(this)
      .text('View this source as Highlighted Code') //change text of the link
      .next().hide().next().show(); //first next is code-box, second is textarea
  }, function() { //hide textarea and show code box
    $(this)
      .text('View this source in Plain Text')
      .next().show().next().hide();
  });
*/


     function replaceURLWithHTMLLinks(text) {
            var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
            return text.replace(exp,"<a class=\"linkInTweeter\" href='$1'>$1</a>");
         }
     
     twitter=function(){
     $('#loadingTwitter').show();
     var url = "http://twitter.com/status/user_timeline/kurniawanaja.json?count=3&callback=?"; 
       $.getJSON(url, 
        function(data){ 
            $('#loadingTwitter').hide();	
            $("#tweets").html("");
            $.each(data, function(i, item) { 
                 css=(i%2==0)?"odd":"even";
                 waktu=item.created_at;                 
                $("#tweets").append("<li class="+css+">"+replaceURLWithHTMLLinks(item.text)+"<br /><em>("+waktu+")</em></li>"); 

            }); 
        }); 
    
      }  
      
      loadDetikinet=function(){          
          var i=0;
          $('#loadingDetik').show();
          urlx="/detikinet.php";
          $.ajax({
            type: "GET",
      			url: urlx,
      			cache:false,
      			dataType: "xml",
      			success: function(xml) {
      			  $('#loadingDetik').hide();	
      			  $('#detikinet').html("");
      			  $(xml).find('item').each(function(){
        			    i++;
        			    var title = $(this).find('title').text();
        			    var url=$(this).find('link').text();
        			    var style=(i%2==0)?" even":" odd";
        			  //  alert(style);
        			    if(i<4)
        					{
                   $('#detikinet').append("<li><a class='linkDetikinet"+style+"'  href='"+url+"'>"+title+"</a></li>");
        					}
      		   	});      		   	
  			  }
  		   });
          		
       }
       
       loading=function(status){
        if(status=='show'){
          sedangProses=true;
          $('.loadingContent').fadeIn();        
        }
        else{
          sedangProses=false;
          $('.loadingContent').fadeOut(); 
        }
       }
       
       loadHal=function(obj,url){       
           $.ajax({
            url: url,
            cached:false,
            success: function(html) {
                $(obj).html(html);
                loading('hide');
            },
            error: function() {
                alert("Error saat loading data");                
            }
        });
       }
       
       lokertwitter=function(){          
          var ii=0;
          $('#loadingLokerTwitter').show();
          urlx="/lokertweeter.php";
          $.ajax({
            type: "GET",
      			url: urlx,
      			cache:false,
      			dataType: "xml",
      			success: function(xml) {      			 
      			  $('#loadingLokerTwitter').hide();	
      			  $('#tweetsloker').html("");
      			  $(xml).find('entry').each(function(){
        			    ii++;
        			    css=(ii%2==0)?"odd":"even";
        			    var judul=$(this).find('content').text();
                                    var updated=$(this).find('updated').text(); 
                                    var author=$(this).find('author name').text(); 
                                    var uriauthor=$(this).find('author uri').text(); 
                                    var imgx="<img src='"+$(this).find('link:last').text()+"'>";
        			     judul=judul.replace("a href=","a class='linkInTweeter' href=");
        			     updated=updated.replace("T"," ").replace("Z","");
                   $('#tweetsloker').append("<li class="+css+">"+judul+"("+updated+")</li>");
      		   	});      		   	
  			  }
  		   });
          		
       }
      
      
      lokertwitter();
      twitter();
    //  loadDetikinet(); 
    
    
      initArtikel=function(){
        loading('show');
        loadHal('.contentAjax','/ajax/artikel/list');          
      }
    
  //   initArtikel();
    
      var sedangProses=false;
      var pakeAjax=false;
      pakeAjax=true;
      $('a.ajaxMainContent').live('click',function(){
        
        if(sedangProses==true)
        {
         alert('Maaf, sedang merequest data');
         return false;
        }        
        if(pakeAjax==true)
         {
          loading('show');
          loadHal('.contentAjax',$(this).attr('href').replace('/index/','/ajax/'));    
          return false;  
         } 
      })
      
      
      idTabSkrg="home";
      $('.navTop a.tab').click(function(){
        idTabBaru=$(this).attr('href').replace('/kur.web.id/','');    
         idTabBaru=$(this).attr('href').replace('/','');  
        
        // kalo HOME
         if(idTabBaru=='home')
         {
           initArtikel();
         }
           
         $('#tab'+idTabSkrg).fadeOut(500,function(){
           $('#tab'+idTabBaru).fadeIn(); 
            $('a#link'+idTabSkrg).removeClass('active');
           $('a#link'+idTabBaru).addClass('active');
           idTabSkrg=idTabBaru;     
        });   
      return false;       
      })
      
      $('#tab'+idTabSkrg).show();
      
      
      // switch AJAX
      
      $('.toggleAJAX a').click(function(){
       // status=$('.toggleAJAX a span');
        //alert($('.toggleAJAX a span').html());
        if($('.toggleAJAX a span').html()=='ON')
        {
         $('.toggleAJAX a span').html('OFF');
         pakeAjax=false;
         $.cookie("statusAJAX", "OFF");

        }
        else{
         $('.toggleAJAX a span').html('ON');
         $.cookie("statusAJAX", "ON");
         pakeAjax=true;
        }
        return false;
      })
      
     statusAJAX=($.cookie("statusAJAX")==null)?"ON":$.cookie("statusAJAX");
     $('.toggleAJAX a span').html(statusAJAX);
     if(statusAJAX=='ON'){
      pakeAjax=true;
     }
     else{
      pakeAjax=false;
     }
     $('.gallery a,#gallery a').lightBox();

});
