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");
	  });
	}
	
	if(jQuery(".homepage-holder").length){
    //background switching
    var url = jQuery(".homepage-headline").eq(0).attr("data-bg");
    if(url && typeof url != "undefined") jQuery(".homepage-holder").css("background","transparent url("+jQuery(".homepage-headline").eq(0).attr("data-bg")+") center center no-repeat");
    
    //height fix for headlines
    var max_height = 420;
    jQuery(".homepage-headline").each(function(){
      var nh = jQuery(this).height(), bg = jQuery(this).attr('data-bg');
      if(nh > max_height) max_height = nh;
      if(!jQuery(this).hasClass('homepage-headline-0')) jQuery(this).removeClass("hide").css('background', "url('"+bg+"')").hide(100, function(){jQuery(this).css("background", "").css("display", "")});
    }).closest(".homepage-headline-tabs").css("height",(max_height)+"px");
    
    var after_class_add = function(event, ui){
      jQuery(this).find(".ui-tabs-nav li").removeClass("ui-tabs-after-selected");
      jQuery(ui.tab).parent().next().addClass("ui-tabs-after-selected");
      var bg = jQuery(jQuery(ui.tab).attr("href")).attr("data-bg");
      if(bg) jQuery(".homepage-holder").css("background","transparent url("+bg+") center center no-repeat");
    };
    jQuery(".homepage-headline-tabs").tabs({select:after_class_add}).tabs("rotate", 10000, true);
  }
});
