(function($){  

	$(function(){
       //cycle [home,category]
    $('#ask_slides').cycle({ 
        fx:     'fade', 
        speed:  'fast', 
        timeout: 0, 
        next:   '#ask_next', 
        prev:   '#ask_prev'
    });
    // create this cycle on load as there are images
    $(window).bind('load', function(){
      $('#savemore_slides').cycle({ 
          fx:     'fade', 
          speed:  'fast', 
          timeout: 0, 
          next:   '#savemore_next', 
          prev:   '#savemore_prev',
          containerResize: true
      });
    });
    $('.cycle').cycle({ 
        fx:     'fade', 
        speed:  'fast', 
        timeout: 10000
    });
    $('.center_column ul.cycle').width(240).height(50);
    
    //corners [article,tags,category,page]
    if(! navigator.userAgent.toLowerCase().match('msie 6.0')){
      $('.tags ul li a').corner('5px');
      $('.tabs ul li a').corner('5px top');
      $('#comment_form').corner('5px');
      $('#comment_container ol .meta').corner('3px');
    }
    // tabs
    function initTabs(){
      //start with first tab
      var tab = $('.tabs li:eq(0) a').attr('href');
      //does url end in /comments/?
      var parts = location.href.split('/');
      if(parts[parts.length-1] == 'comments' || parts[parts.length-2] == 'comments'){
        tab = 'comments';
      }
      //hash overrides all
      if(location.hash){
        tab = location.hash;
      }
      if(typeof(tab) != 'undefined'){
        tab = tab.replace('#', '');
        showTab(tab);
      }
      return false;
    }
    initTabs();
    $(window).bind('hashchange', initTabs);
    $('.tabs li a').click(function(){
      showTab($(this).attr('href').replace('#', ''));
      return false;
    })
    
    // auto-popup login screen where needed
    $('.login-required:eq(0)').each(function(){
      $(window).bind('load', function(){setTimeout(function(){$('a.rpxnow:eq(0)').trigger('click');}, 10);});
    });
    
    // relative dates
    $('.save-and-share span.date, .center_column .cycle span.date').toRelativeTimeFromAttr('title');
    
    // embed code highlights
    $('textarea.embed-code').click(function(){$(this).select();})

    // inject 'back to mobile' link on iphone standard view
    if(navigator.userAgent.toLowerCase().match(/(iphone|ipod|android|opera mini)/)){
      $('.footer_links ul').append('<li class="switch_theme"><a class="last" href="?switch_theme">Mobile Version</a></li>')
        .find('li.switch_theme').prev('li').find('a').removeClass('last');
    }
    
    
    //infinite scroll for s-a-s
    if(typeof(sas_use_infinite_scroll)!='undefined' && sas_use_infinite_scroll===true){
      $('ol#comment-list-save-and-share').after('<p class="loading"><img src="/_base/img/ajax-loader.gif" width="32" height="32" /></p>');
      $(window).scroll(function(){
        if ($(window).scrollTop() >= ($(document).height() - $(window).height() - $('.interior_footer_wave').height() - $('.footer').height()) && typeof(sas_next_page_url)!='undefined' && sas_next_page_url!=null){
          $('p.loading').fadeIn('fast');
          $.get(sas_next_page_url, function(data, response){
            if(response!=''){
              $('ol#comment-list-save-and-share').append(data)
                .next('p.loading').hide();
              $('.save-and-share span.date, .center_column .cycle span.date').toRelativeTimeFromAttr('title');
            }else{
              $('p.loading').hide();
            }
          });
        }
      });

      //get first data batch
      $.get('/partials/comment_list_save_and_share/c/', function(data, response){
        if(response!=''){
          $('ol#comment-list-save-and-share').append(data)
            .next('p.loading').hide();
          $('.save-and-share span.date, .center_column .cycle span.date').toRelativeTimeFromAttr('title');
        }else{
          $('p.loading').hide();
        }
      });

    }

  });
  
})(jQuery);

function mySetCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
