function comic_switch()
{
  if (jQuery('#comic0').css('display')!='none') 
  {
    jQuery('#comic0').hide();
    jQuery('#comic1').fadeIn();    
  } else {
    jQuery('#comic1').hide();
    jQuery('#comic0').fadeIn();    
  } 
} 


jQuery(document).ready(function(){


  // ======================================
  // Preload images  
  // ======================================
  jQuery('<img>').attr('src', '/css/img/lodings.gif');



  // ======================================
  // Previous Issues Bar  
  // ======================================
  jQuery('#date')
    .mouseenter(function() { jQuery('#previous_issues').fadeIn('fast'); })
    .mouseleave(function() { jQuery('#previous_issues').fadeOut('fast'); })

    

  // ======================================
  // Comic animation  
  // ======================================
  if (jQuery('#comic0'))
  {
    setInterval("comic_switch()",4000);
  }

    

  // ======================================
  // Auto empty form fields  with .auto_empty   
  // ======================================
  jQuery('.auto_empty').focus( function()
    {
      if (!jQuery(this).data('done'))
      {
        jQuery(this).data('done', true);
        jQuery(this).val('');
      }
    });
  
  
  
  // ======================================
  // Vote / Poll 
  // ======================================
  jQuery('#vote_form').submit(function()
    {
      form = jQuery(this);

      // prevent duplicate submit  
      if(form.data('progress')) { return false; }
      form.data('progress', true);
      action = form.attr('action');     // get form action
      data = form.serializeArray(); // get all form fields
      
      form.html('<img src="/css/img/lodings.gif" alt="Loading ...">');

      // Submit form 
      jQuery.post(
        action, data,           
        function (data) {
          form.removeData('progress');
          form.html(data);  
        },
        "html"
      ); // Ajax post

      return false;
    });




  // ======================================
  // Article favorite    
  // ======================================
  jQuery('.article_favorite').click(function() {
    me = jQuery(this);    

    if (me.data('working'))  return; // prevent duplicate 
    me.data('working',true);       

    //me.html('saving ...'); 
	jQuery('#icon_msg').html('');
	me.html('<img src="/css/img/lodings.gif" />');
	me.removeClass('set1');

	//alert(230)
    jQuery.post(me.attr('href'), null, function(data) {
		if(data.Not_Signed==true){
		  me.html('<img src="/css/img/favorite1.gif" />');
		  
		  jQuery('#icon_msg').html('خدمة مفضلتي متاحة للإعضاء...<br><a href="/users/signup"> سجل معنا</a>');
		  jQuery('#icon_msg').show();
		  
		}else if (data.favorite==true) {
          //me.html('Star On');
		 if (me.is('.icone')) {
		me.html('مفضلة');
		me.addClass('set1');
		}else{
		  me.html('<img src="/css/img/favorite2.gif" />');
		  jQuery('#icon_msg').html('تمت إضافته في مفضلتي');
		  jQuery('#icon_msg').show();
		}
		  
        } else {
          //me.html('Star Off');
		  if (me.is('.icone')) {
			me.html('مفضلة');
			me.removeClass('set1');
		  }else{
		  me.html('<img src="/css/img/favorite1.gif" />');
		  jQuery('#icon_msg').html('تم إزالته من مفضلتي');
		  jQuery('#icon_msg').show();
		  }
        } 
        me.data('working', false);
      }, // end callback 
      "json"
    ); // end post 
    
    return false;
  
  }); // article_favorite click

  
  // ======================================
  // Article Star Rating     
  // ======================================
  jQuery('.rating a').click(function() {
    me = jQuery(this);    

    if (me.data('working'))  return; // prevent duplicate 
    me.data('working',true);
	jQuery('#rating_box span').html('انتظر قليلاً...')

    jQuery.post(me.attr('href'), null, function(data) {
       if( data.Not_Signed == true ){
		   jQuery('#rating_box span').html('تقييم المقالات متاح للأعضاء ... <br><a href="/users/signup"> سجل معنا</a>')
	   }else{
	   var star_class = ['nostar','onestar','twostar','threestar','fourstar','fivestar'];
        jQuery('.rating')
          .removeClass('nostar onestar twostar threestar fourstar fivestar')
          .addClass(star_class[data.stars]);
        me.data('working', false);
		jQuery('#rating_box span').html('شكراً لتقييمك')
		 
	   }}, // end callback 
      "json"
    ); // end post 
    
    return false;
  
  }); // article star rating 




  // ======================================
  // Article Comment Rating     
  // ======================================
  function comments_rating_init()
  {
	  
	jQuery('#rating_box').show();
	/*
    jQuery('.comment_rating.ghost').mouseout(function() {
      me = jQuery(this);
//	  me.html('ارجو التسجيل');

	}).mouseover(function() {
	  me = jQuery(this);
//	   me.siblings('dev').show();

	});
  */
  
    jQuery('.comment_rating a').click(function() {
      me = jQuery(this);
	  

	  
      if (me.data('working'))  return; // prevent duplicate 
	  me.addClass('rating_loading');
      me.data('working',true);
  
      jQuery.post(me.attr('href'), null, function(data) {
		if(data.Not_Signed == true){
			jQuery('#comment_rating_msg_'+data.comment_id).html('عفواً تقييم التعليقات متاحة للأعضاء فقط... <br> <a href="/users/signup">سجل معنا</a><br><br>')

		  me.removeClass('rating_loading');
		  me.css('cursor','default');

		}else{
          me.siblings('span').html(''+data.rating);

		  rating_class = '';
		  if( data.rating >  0 ) { rating_class = 'rating_up'; } else if( data.rating <  0 ) { rating_class = 'rating_down'; }
		  if( me.text() == 'UP' ) { rating_class_a = 'rating_up'; } else { rating_class_a = 'rating_down'; }
		  
		  me.siblings('span').removeClass('rating_up rating_down');
		  me.siblings('span').addClass(rating_class);
		  me.siblings('a').removeClass('rating_up rating_down');
		  me.removeClass('rating_loading');
		  me.addClass(rating_class_a);
		  
          me.data('working', false);
		}}, // end callback 
        "json"
      ); // end post 
      
      return false;
    
    }); // Article Comment Rating
  }
  comments_rating_init();  // called also from pager_click            




  // ======================================
  // Article Comments Toolbar (show/hide)   
  // ======================================
  function comments_toolbar_init()
  {
    jQuery('.comments_ul li')
      .mouseenter(function() { jQuery(this).find('.toolbar').show(); })
      .mouseleave(function() { jQuery(this).find('.toolbar').hide(); })
  }
  comments_toolbar_init(); // called also from pager_click     




  // ======================================
  // Article Comments Pager  
  // ======================================
  function pager_click()
  {
    jQuery('#article-comments ul').html('<div class="loading"></div>');
    url = jQuery(this).attr('href');
    jQuery.get(url, function(data) {
      jQuery('#article-comments').html(data);
      jQuery('#article-comments .pager a').click(pager_click);
      comments_toolbar_init();
      comments_rating_init(); 
    });
    return false;
  } // pager_click 
  jQuery('#article-comments .pager a').click(pager_click); 





  // ======================================
  // check user by cookies in HomePage
  // ======================================
  /*
	if( jQuery.cookie("rnp_id") ) {
		
	if ( jQuery.cookie("rnp_username") ){ 
	rnp_username = jQuery.cookie("rnp_username") 
	rnp_username = rnp_username.replace(/\+/ig, " ")
	}else{ rnp_username = ''}
	
	jQuery('#user_toolbar_home').html('مرحباً, <a href="users/edit">' + rnp_username + '</a> | <a href="users/edit"> لوحة التحكم </a> | <a href="users/comments"> تعليقاتي </a> | <a href="users/signout"> خروج </a>');
	}
	*/
  



  // ======================================
  // Text Zoom
  // ======================================
  jQuery('#text_zoom').click(function() {
	
	font_size = jQuery('#article_text').css('font-size');

	if( font_size == '13px' || font_size == '16px' ) { 
		new_font_size = 120
	}else{
		new_font_size = 100
		}
	
	jQuery('#article_text').css('font-size',new_font_size+'%');
	
	return false;
	});
  



  // ======================================
  // Article Quote
  // ======================================  
   jQuery(".quote").each(function (index, domEle) {
		jQuery(domEle).html('<div class="start"></div>' + jQuery(domEle).text() + '<div class="end"></div>');
   });




  // ======================================
  // Sort commints
  // ======================================  
   jQuery('.sort_comm_').click(function() {
		jQuery("#comments_sort").show();
		return false;
   });
 
 


  // ======================================
  // Get IP
  // ======================================  
	//jQuery.get("/script/rnp_net", function(data){
	   //jQuery('#testt').text(data);
	 //});
 
 
}); // document ready 



