/**** Add application wide javascripts below this point  ******/
$(document).ready(function() {
  $("#companies_block .list .image_0").eq(0).attr("src", $(".image_0").attr("rel"));
  $("#companies_block .list .image_1").eq(0).attr("src", $(".image_1").attr("rel"));
});




secondary_backgrounds = []; //global one for all background images...
primary_backgrounds = [];
event_cost = 0;
//background swapping on home page
function home_page_backgrounds(backgrounds, prefix, size){
  if(typeof(backgrounds) != undefined && backgrounds.length){
    for(var div_id in backgrounds){
      if(!size) jQuery('#'+prefix+div_id).css('background', "url('"+backgrounds[div_id]+"') no-repeat center center");
      else jQuery('#'+prefix+div_id).css('background', "url('/show_image/"+backgrounds[div_id]+"/"+size+".jpg') no-repeat center center");
    }
  }
}

/** GOOGLE MAP **/
function googlemap(){
  if(jQuery('#googlemap').length && GBrowserIsCompatible() ){
    var g_map = new google.maps.Map2(document.getElementById('googlemap'));
    g_map.enableScrollWheelZoom();
		g_map.setUIToDefault();
		var local_search = new google.search.LocalSearch();
		local_search.setSearchCompleteCallback(null, function(){		  
		  if(local_search.results[0]){
		    var resultLat = local_search.results[0].lat, resultLng = local_search.results[0].lng;
		    var latLng = new GLatLng(resultLat, resultLng);
        g_map.setCenter(latLng, 15);
        var gicon = new GIcon(G_DEFAULT_ICON);
	      var marker = new GMarker(latLng, {icon:gicon, title:jQuery('h1').text()});		      
	      GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(jQuery('#event_page .location .copy').html());
        });
        g_map.addOverlay(marker);
		  }
		});
		local_search.execute(jQuery('address.postcode').html()+', UK');
  }
}


/** DIALOG **/
function email_friend_dialog(){
  jQuery('#email_a_friend').dialog({autoOpen:false,modal:true});
  jQuery('.send_to_friend').click(function(){
    jQuery('#email_a_friend').dialog('open');
    jQuery('#email_a_friend').dialog('option', 'title', 'Send to a Friend');
    jQuery('#email_a_friend form').show();
    email_friend_ajax_send();
    return false;
  });
}
function email_friend_ajax_send(){
  jQuery('#email_a_friend form').submit(function(){
    var form_fields = jQuery('#email_a_friend form').serialize();
    jQuery.ajax({
      type:jQuery('#email_a_friend form').attr('method'),
		  url:jQuery('#email_a_friend form').attr('action') + '.ajax',
  		data: form_fields,
  		success: function(response){
  			jQuery('#email_a_friend form').html(response);
  		},
  		error:function(response){
  		  jQuery('#email_a_friend form').html(response);
  		}
    });
    return false;
  });
}

function login_warning(){
  jQuery('a[href=#login_warning]').addClass('login_dialog');
  var introcopy = '<img src="/images/forms/traffic-cone.gif" alt="warning" class="left"/><p>In order to access this exclusive content, you must be an IiE Registered User. Enter your username and password now.</p>';
  var noaccess = '<p>You do not have access to this piece of content.</p>';
  if(jQuery('#iie_login form').length){
    var login_form = '<form id="iie_login_form_modal" method="post" action="">'+jQuery('#iie_login form').html()+'</form>';
    jQuery('#login_required_dialog').html(introcopy+login_form);
  }else jQuery('#login_required_dialog').html(noaccess);
  jQuery('#login_required_dialog').dialog({autoOpen:false,modal:true, title:'Attention'});
  jQuery('a.login_dialog').click(function(){
    jQuery('#login_required_dialog').dialog('open');
    return false;
  });
}


/**** EVENT FORM ****/
function show_hide_delegates(val){
  if(val == 0) jQuery('.delegate_form').hide();
  else{
    jQuery('.delegate_form').each(function(){
      var num = parseInt(jQuery(this).attr('id').replace("delegate_", ""));
      if(num >= val) jQuery(this).hide().find('input').attr('disabled', true);
      else jQuery(this).show().find('input').removeAttr('disabled');
    });
  }
  var new_cost = ((val+1)*event_cost).toFixed(2);
  jQuery('#total_cost span em').html(new_cost);
}
function event_form(){
  if(!jQuery('#main_registrar_register_lead_contact').is(":checked")) jQuery('.delegate').hide().find('input').attr('disabled', true);
  else show_hide_delegates(parseInt(jQuery('#main_registrar_delegates').val()));

  if(jQuery('#main_registrar_register_lead_contact_table').is(":checked")) jQuery('#total_cost span em').html(event_table_cost);

  jQuery('#main_registrar_register_lead_contact').click(function(){ 
    jQuery('#delegates_intro').show(); 
    show_hide_delegates(parseInt(jQuery('#main_registrar_delegates').val()));
  });
  jQuery('#main_registrar_register_as_individual').click(function(){
    jQuery('.delegate').hide().find('input').attr('disabled', true);
    jQuery('#total_cost span em').html(event_cost);
  });
	jQuery('#main_registrar_register_lead_contact_table').click(function(){
    jQuery('.delegate').hide().find('input').attr('disabled', true);
    jQuery('#total_cost span em').html(event_table_cost);
  });
  
  jQuery('#main_registrar_delegates').change(function(){ show_hide_delegates(parseInt(jQuery(this).val())); });
}

/** ajax calendar links **/
function ajax_calendar(){
  jQuery('.cal_options .previous a, .cal_options .next a').click(function(){
    jQuery('#calendar_block').fadeTo('fast', 0.6);
    var clicked = this.href.substring(this.href.indexOf('?'));
    jQuery.ajax({
      type:'post',
      url:'/calendar_list.ajax'+clicked,
      success:function(response){
        jQuery('#calendar_block').replaceWith(response);
        ajax_calendar();
      },
      error:function(){ajax_calendar();}
    });
    return false;
  });
}

/**** ****/
jQuery(document).ready(function(){
  if(jQuery('#primary_featured_articles').length){
    jQuery('#primary_featured_articles').switcher({item:'.primary_feature', pagination:'#paginated_features', auto_rotate:false, rotate_time:15000, swap_container_class:true,speed:0});
    home_page_backgrounds(primary_backgrounds, 'primary_feature_', false);
  }
  if(jQuery('div.secondary_feature').length) home_page_backgrounds(secondary_backgrounds, 'secondary_feature_', false);
  if(jQuery('#secondary_images').length && jQuery('#secondary_images img').length > 1) jQuery('#secondary_images').switcher({item:'.image_holder', auto_rotate:true, rotate_time:5000, speed:"slow"});
  googlemap();
  
  if(jQuery('#news_archive').length) jQuery('#news_archive').slidingtree();
  if(jQuery('#email_a_friend_form').length){
    jQuery('#email_a_friend_form').hide();
    email_friend_dialog();
  }
  if(jQuery('.flickr a.gallery').length) jQuery('.flickr a.gallery').lightBox();
  if(jQuery('#secondary_images a.gallery').length) jQuery('#secondary_images a.gallery').lightBox();  
  login_warning();
  if(jQuery('#register_form').length) event_form();
  if(jQuery('#calendar_block').length) ajax_calendar();


	$("#other_hear").hint();
	if(jQuery("#contact_us_hear_from").val() == 6 || jQuery("#main_registrar_hear_from").val() == 6 ){
		$("#other_hear").css({"display":"block","float":"right"});
	}else{
		$("#other_hear").css("display","none");
	}
	jQuery("#contact_us_hear_from, #main_registrar_hear_from").change(function(){
		if($(this).val() == 6){
			$("#other_hear").css({"display":"block","float":"right"});
		}else{
			$("#other_hear").css("display","none");
		}
	});
});



var block_items;
var current_block=0;
/***** Companies Block Rotator ************/
$(document).ready(function() {
    block_items = $("#companies_block .list img").hide().filter(":eq(0)").show().add("#companies_block .list img").size();
    if(block_items > 1) setInterval(auto_block,4000);
});

function auto_block() {
    $("#companies_block .list img:eq("+current_block+")").fadeOut("slow",function(){
      $(this).hide();
      current_block = ++current_block%block_items;
      ahead_block = current_block+1;
      $("#companies_block .list img:eq("+ahead_block+")").attr("src", $("#companies_block .list img:eq("+ahead_block+")").attr("rel"));
      $("#companies_block .list img:eq("+current_block+")").fadeIn("slow");
    });
}