/**** Add application wide javascripts below this point  ******/

$(document).ready(function() {
	
	if(jQuery("ul.cat-archive").length){
    jQuery("ul.cat-archive").accordion({header:".cat",autoHeight:false,active:false,animated:false,collapsible:true});
  }
	
	if(jQuery("ul.news-archive").length){
    jQuery("ul.months").accordion({header:".month",autoHeight:false,animated:false});
    jQuery("ul.news-archive").accordion({header:".year",autoHeight:false,animated:false,active:false});
		if(jQuery("a.news-nav-active").closest("ul").children("li").length == 4 && !jQuery("a.news-nav-active").closest("ul").children("li.hide").length){
			jQuery("a.news-nav-active").closest("ul").children("li:last-child").hide();
		}
		jQuery("a.news-nav-active").closest("ul").prev().closest("ul").prev().trigger("click");
		jQuery("a.news-nav-active").closest("ul").prev().trigger("click");
  }
	
	$(".press_nav li.hide").hide();
	$(".press_nav li.hidden").hide();
	$(".press_nav li.show").show();
	
	var switch_var = 0;
	$(".press_nav li a.read_more").click(function(){
		
		if(!switch_var){
			$(this).parent().parent().children("li.hide").slideDown("fast");
			switch_var = 1;
			if($(this).html() == "Click for more articles..." && (switch_var == 2 || switch_var == 1 && !$(this).parent().parent().children("li.hidden").length)){
				$(this).html("Click for less articles...");
				switch_var = 2;
			}else $(this).html("Click for more articles...");
			return false;
		}
		if(switch_var == 1 && $(this).parent().parent().children("li").length > 5){
			$(this).parent().parent().children("li.hidden").slideDown("fast");
			switch_var = 2;
			if($(this).html() == "Click for more articles..." && (switch_var == 2 || switch_var == 1 && !$(this).parent().parent().children("li.hidden").length)){
				$(this).html("Click for less articles...");
				switch_var = 2;
			}else $(this).html("Click for more articles...");
			return false;
		}
		if(switch_var == 2){
			$(this).parent().parent().children("li.hidden, li.hide").slideUp("fast");
			switch_var = 0;
			if($(this).html() == "Click for more articles..." && (switch_var == 2 || switch_var == 1 && !$(this).parent().parent().children("li.hidden").length)){
				$(this).html("Click for less articles...");
				switch_var = 2;
			}else $(this).html("Click for more articles...");
			return false;
		}
	});
	
	$("form.validate").validate();
	$("#contact input").focus(function(){ $(this).parent().addClass("selected_field")});
  $("#contact input").blur(function(){ $(this).parent().removeClass("selected_field")});
  $("#pulse_subscribe input").focus(function(){ $(this).parent().addClass("selected_field")});
  $("#pulse_subscribe input").blur(function(){ $(this).parent().removeClass("selected_field")});
  $("#report_form input").focus(function(){ $(this).parent().addClass("selected_field")});
  $("#report_form input").blur(function(){ $(this).parent().removeClass("selected_field")});	
  $("#report_form select").focus(function(){ $(this).parent().parent().addClass("selected_field")});
  $("#report_form select").blur(function(){ $(this).parent().parent().removeClass("selected_field")});
	
  $("#contact_name").example('Name', {
  	hide_label: true
  });
  $("#contact_telephone").example('Telephone', {
  	hide_label: true
  });
  $("#search_field").example('Search', {
  	hide_label: true
  });
  $("#contact_me").example('Telephone or email', {
  	hide_label: true
  });
	autobrowse = setTimeout("default_image()", 25000);
  
  $("#home_buttons div").hover(
    function() { 
			clearTimeout(autobrowse);
		  autobrowse = setTimeout("default_image()", 25000);
      $("#homepage_content").attr("class","clearfix");
      $("#home_buttons div").removeClass("highlighted");
      $("#page_content").css("background-image", "url(/images/"+home_images[$(this).attr("id")]+")");
			area = "#"+$(this).attr("id") +"_text";
			new_content = $(area).html();
			$("#homepage_content").html(new_content);
			$("#homepage_content").addClass("normal");
			$("#page_content p").css("color", "#767676");
			$("#page_content h1").css("color", "#767676");
			$("#page_content h2").css("color", "#767676");
      $(this).addClass("highlighted");
    },
    function() { 
    }
  );
  if($("#gmap").length) { 
    map = new GMap2(document.getElementById("gmap"));
    maplocation = new GLatLng(52.271031, 0.492778);
    map.setCenter(maplocation, 13);
		var eicIcon = new GIcon(G_DEFAULT_ICON);
		eicIcon.image = "/images/map_pointer.png";
		eicIcon.iconSize = new GSize(53, 53);
		var marker = new GMarker(maplocation, eicIcon);
    map.addOverlay(marker);
		map.addControl(new GSmallMapControl());
   
    $("#g_norwich").click(function(){
      get_directions_from("Norwich");
      return false;
    });
    $("#g_brum").click(function(){
      get_directions_from("Birmingham, UK");
      return false;
    });
    $("#g_london").click(function(){
      get_directions_from("London, UK");
      return false;
    });
  }

});

function get_directions_from(place) {
  map.clearOverlays();
  gdir = new GDirections(map);
  var direct = "from "+place+" to "+maplocation.lat()+", "+maplocation.lng();
  gdir.load(direct);
}

home_images = {
  "risk_man_button":      "home_fireater.jpg",
	"portfolio_button":  		"home_portfolio.jpg",
  "market_intel_button":  "home_target.jpg",
  "procurement_button":   "home_jump.jpg",
  "data_button":          "home_data.jpg",
  "carbon_button":        "home_carbon.jpg",
	"water_button": 				"home_water.jpg" 
}


function default_image() {
  if($("#home_buttons")) {
    $("#home_buttons .highlighted").each(function(){
      $(this).removeClass("highlighted");
			$("#page_content").css("background-image", "url(/images/energy2010_event_header.jpg)");
			$("#page_content p").css("color", "#EBEBEB");
			$("#page_content h1").css("color", "#EBEBEB");
			$("#page_content h2").css("color", "#EBEBEB");
			area = "#original_text";
			new_content = $(area).html();
			$("#homepage_content").attr("class","clearfix");
			$("#homepage_content").html(new_content);
    });
  }

}

