var ajax_error_count = 9;

function twitter_error(){
  if(jQuery('#twitter-ajax .loading').length && (ajax_error_count > 0)){
    ajax_error_count--;
    setTimeout(load_twitter, 10000);
  }
}

function load_twitter(){
	jQuery.ajax({
		type:'post',
		url:'/page/_twitter.ajax',
		timeout: 5000,
		success:function(response){
      if(response.length) jQuery('#twitter-ajax').replaceWith(response);
      twitter_error();
		},
		error:twitter_error
	});
}

function flickr_error(){
  if(jQuery('#flickr-ajax .loading').length && (ajax_error_count > 0)){
    ajax_error_count--;
    setTimeout(load_flickr, 10000);
  }
}

function load_flickr(){
	jQuery.ajax({
		type:'post',
		url:'/page/_flickr.ajax',
		timeout: 5000,
		success:function(response){
      if(response.length) jQuery('#flickr-ajax').replaceWith(response);
      flickr_error();
		},
		error:flickr_error
	});
}

function vimeo_error(){
  if(jQuery('#vimeo-ajax .loading').length && (ajax_error_count > 0)){
    ajax_error_count--;
    setTimeout(load_vimeo, 10000);
  }
}

function load_vimeo(){
	jQuery.ajax({
		type:'post',
		url:'/page/_vimeo.ajax',
		timeout: 5000,
		success:function(response){
      if(response.length) jQuery('#vimeo-ajax').replaceWith(response);
      vimeo_error();
		},
		error:vimeo_error
	});
}

function form(){
	$("#newsletter_form input#newsletter_other,#newsletter_form input#other_title, #newsletter_form input.other").hint();
	$("#newsletter_form #newsletter_media,#newsletter_form #newsletter_web_search,#newsletter_form #newsletter_other,#newsletter_form input.other").attr("disabled","disabled");

	if($("#newsletter_form select#newsletter_title").val() == "Other"){
		$("#newsletter_form select#newsletter_title").parent().children("#other_title").attr("disabled","");
	}
	if($("#newsletter_form select#newsletter_media").val() == "5"){
		$("#newsletter_form select#newsletter_media").parent().children("#other_media").attr("disabled","");
	}
	if($("#newsletter_form select#newsletter_web_search").val() == "5"){
		$("#newsletter_form select#newsletter_web_search").parent().children("#other_search").attr("disabled","");
	}
	
	$("#newsletter_form select#newsletter_title").change(function(){
		if($(this).val() == "Other") $(this).parent().children("#other_title").attr("disabled","");
		else $(this).parent().children("#other_title").attr("disabled","disabled");
	});
	$("#newsletter_form select#newsletter_media").change(function(){
		if($(this).val() == "5") $(this).parent().children("#other_media").attr("disabled","");
		else $(this).parent().children("#other_media").attr("disabled","disabled");
	});
	$("#newsletter_form select#newsletter_web_search").change(function(){
		if($(this).val() == "5") $(this).parent().children("#other_search").attr("disabled","");
		else $(this).parent().children("#other_search").attr("disabled","disabled");
	});
	
	$("#newsletter_form .other_info input.info").each(function(){
		if($(this).attr("checked")==true) $(this).parent().children(".input_field").attr("disabled","");
	});
	$("#newsletter_form .other_info input.info").change(function(){
		if($(this).attr("checked")==true) $(this).parent().children(".input_field").attr("disabled","");
		else $(this).parent().children(".input_field").attr("disabled","disabled");
	});
}

jQuery(document).ready(function(){
  jQuery("#social-media").accordion({autoHeight:false});
  if(jQuery("ul.news-archive")){
    jQuery("ul.news-archive li").accordion({header:".month",autoHeight:false});
    jQuery("ul.news-archive").accordion({header:".year",autoHeight:false});
    jQuery("a.news-nav-active").closest("ul").prev().trigger("click").closest("ul").prev().trigger("click");
  }
  if(jQuery("ul.topics-list").length){
    jQuery("ul.topics-list a.topic-group").click(function(){
      jQuery(jQuery(this).attr("href")).slideToggle();
      return false;
    });
  }
  if(jQuery("#twitter-ajax .loading").length) load_twitter();
  else setTimeout(load_twitter, 30000);
  if(jQuery("#flickr-ajax .loading").length) load_flickr();
  else setTimeout(load_flickr, 30000);
  if(jQuery("#vimeo-ajax .loading").length) load_vimeo();
  else setTimeout(load_vimeo, 30000);
	if($("#newsletter_form").length) form();
	
	if(jQuery("#disqus_thread").length){
    (function() {
     var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
     dsq.src = 'http://birminghamculturetest.disqus.com/embed.js';
     (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();
	}
	
	if(jQuery("#dialog-open-on-load").length){
	  var dialog_load_on_open = jQuery("#dialog-open-on-load").dialog({"width":380, "modal":true});
	  jQuery("#dialog-open-on-load a.close-button").click(function(){
	    dialog_load_on_open.dialog("close");
	  });
	}
});