app = {};

app.init = function(){
  
  ignore_regex = new RegExp(/(header|footer|nav)/);
  $.ajax({
    url: '/png_fix_selectors.json',
    type: 'GET',
    dataType: 'json',
    beforeSend:function(){},
    success: function(selector){
      $(document).pngFix({ignore_regex: ignore_regex, selector: selector.join(', ')});
    },
    error: function(){}
  })
  
	// $('div#logo').click(function(){
	//     window.location = '/';
	//   });
  
  $('a.contact').click(function(){
    $.overalls($('div#contact_container').html(), {blank: true});
    $('img#loader').hide();
    $('div#contact_form form.form_for_contact').find('input:submit').attr({disabled: false});
    $('div#contact_form form.form_for_contact').find("#lead_message").keyup(function(e){
      $('span#chars_left').html(140-$(this).val().length)
      if($(this).val().length >=140){
        if(e.keyCode >= 48 ){
          $(this).val($(this).val().slice(0,140))
          $('span#chars_left').html(140-$(this).val().length)
          return false;
        }
      }
    });
  });
  
  $('a.show_login_menu').click(function(){
    $('ul.login_menu').toggle()
    return false;
  });
  
  
  $('div#contact_form form.form_for_contact').live('submit', function(){
    var $form = $(this);
    var data = $form.serialize();
    $.ajax({
          url: $form.attr('action'),
          type: 'POST',
          data: data,
          dataType: 'html',
          beforeSend:function(){
            $('img#loader').show();
            $form.find('input:submit').attr({disabled: true});
          },
          success: function(html){
            $form.parents('div#contact_form').replaceWith(html);
          },
          error: function(){
            $('img#loader').hide();
            $form.find('input:submit').attr({disabled: false});
            $form.append("<span class='icon ui-icon ui-icon-alert'/>Contact info was not sent. <br/> Looks like we're having a problem on our end <br/> Feel free to email us <a href='mailto:info@collectiveintellect.com'>info@collectiveintellect.com")
          }
        });
    return false;
  });
  
  // Bind clicks to .insight_trial to load #insight_trial_form into Overalls
  $('a.insight_trial').live('click', function(){
    $.overalls($('div#insight_trial_container').html(), {blank: true});
    $('img#insight_trial_loader').hide();
    $('div#insight_trial_form form').find('input:submit').attr({disabled: false});
    return false;
  });
  
  $('div#insight_trial_form form').live('submit', function(){
    var $form = $(this);
    var data = $form.serialize();
    $.ajax({
      url: $form.attr('action'),
      type: 'POST',
      data: data,
      dataType: 'html',
      beforeSend:function(){
        log('before send')
        $('img#insight_trial_loader').show();
        $form.find('input:submit').attr({disabled: true});
      },
      success: function(html){
        log('html')
        log('success')
        $form.parents('div#insight_trial_form').replaceWith(html);
      },
      error: function(){  
        log('error!')
        $('img#insight_trial_loader').hide();
        $form.find('input:submit').attr({disabled: false});
        $form.append("<span class='icon ui-icon ui-icon-alert'/>Trial info was not sent. <br/> Looks like we're having a problem on our end <br/> Feel free to email us <a href='mailto:info@collectiveintellect.com'>info@collectiveintellect.com")
      }
    })
    return false;
  });
  
  
  
  
  $('a.define').click(function(){
    id = $(this).attr('href');
    $.overalls($(id).html());
    Cufon('div#overalls-overlay h2');
    return false;
  });
  
  $('div.content_type.socialsite').click(function(){
    window.open('http://www.facebook.com/pages/Collective-Intellect/143449332335329');
    return false;
  });
  
  $('div.content_type.microblog').click(function(){
    window.open('http://www.twitter.com/collectual');
    return false;
  });
  
  
  
};

app.subnav = function(){
  
  
  var path = window.location.pathname.split('/');
  if(path.length > 2){
    var subpage = path[path.length - 1];
    new_page = window.location.pathname.replace('/'+subpage,'');
  
    if(subpage == "management_team"){
      subpage = "management"
    }
    if(subpage == "board_of_directors"){
      subpage = "board"
    }
    if(["self_service", "full_service", "syndicated" , "topic_definition"].indexOf(subpage) != -1){
      subpage = "insight"
    }
    if(subpage == "our_technology"){
       subpage = "insight"
     }
    
    
    
    log(subpage)
    log(subpage)
    $.bbq.pushState({page: subpage});
    // window.location.pathname = new_page;
  }
  
  $(window).bind( 'hashchange', function( event ) {
    var subpage = app.default_subpage();
    $('div.subpage:visible').hide();
    $('div#'+subpage).show();
    $('ul.subnav li').removeClass('active');
    $('li.'+subpage).addClass('active');
    Cufon.refresh('ul.subnav');
  });
  
  

  
  var subpage = app.default_subpage();
  $('div#'+subpage).show();
  $('li.'+subpage).addClass('active');
  Cufon('ul.subnav');
  $.bbq.pushState({page: subpage});
  
	
  $('ul.subnav li a').click(function(){
    var id = $(this).attr('href');
    $('div.subpage:visible').hide();
    $('div'+id).show();
    $('ul.subnav li').removeClass('active');
    $('li'+id.replace('#','.')).addClass('active');
    Cufon.refresh('ul.subnav');
    $.bbq.pushState({page: id.replace('#','')});
    return false;
  });

};

app.default_subpage = function(){
    var default_subpage = window.location.pathname.match('about') ? 'contact_info' : 'insight';
    var subpage = $.bbq.getState('page')  || default_subpage;
  return subpage;
};

app.customer_benefits = function(){
  if(!$.browser.msie){
    Cufon('div.benefit');
  }
  $('div.benefits').fadeIn('slow');
  $('div.slider').easySlider({
    continuous: true,
    prevId: 		'slider_go_left',
		prevText: 		'',
		nextId: 		'slider_go_right',	
		nextText: 		''
  });
  $('div.slider').find('.slider_control').fadeIn('slow');
  
  $('div.logo_slider').easySlider({
    pause:     8000,
    speed: 1600,
    auto:true,
    continuous: true,
    numeric: true
  });
  
};

app.init_screencasts = function(){
  	flowplayer("insight_video",  {src: "/screencasts/flowplayer-3.2.2.swf", wmode: 'transparent'}, {clip:{autoPlay: false, autoBuffering: true}, onLoad: function() {this.setVolume(15);}});
  	flowplayer("listen_video",   {src: "/screencasts/flowplayer-3.2.2.swf", wmode: 'transparent'}, {clip:{autoPlay: false, autoBuffering: true}, onLoad: function() {this.setVolume(15);}});
  	flowplayer("semantic_video", {src: "/screencasts/flowplayer-3.2.2.swf", wmode: 'transparent'}, {clip:{autoPlay: false, autoBuffering: true}, onLoad: function() {this.setVolume(15);}});
};


function log(){
  if(window.console) {
    console.log.apply(console, arguments);
  }
}