var DPCD = {};
DPCD.isIE6 = (jQuery.browser.msie && jQuery.browser.version < 7);

DPCD.initPage = function()
{
	// Fix background image flickering in IE 6
	if (DPCD.isIE6) document.execCommand('BackgroundImageCache', false, true);

	// Add text shadows
	jQuery('#base_panels h2, #main_content div.box h2').css('text-shadow', 'rgba(0, 0, 0, 0.2) 1px 2px 2px');

	// Set article grid panel heights
	jQuery('ul.article_list.grid .article_inner').each(function(index)
	{
		if (index % 2 == 1)
		{
			var item = jQuery(this);
			var previousItem = item.parent().prev().find('.article_inner');
			var itemHeight = item.height();
			var previousItemHeight = previousItem.height();				
			if (itemHeight < previousItemHeight)
				item.height(previousItemHeight);
			else
				previousItem.height(itemHeight);
		}
	});

	// Set base panel heights
	var tallest = 0;
	jQuery('#base_panels > li').each(function()
	{
		var height = jQuery(this).height();
		if (height > tallest) tallest = height;
	}).height(tallest);
};

function stripSub(str) {
  var parts = str.split('.');
  parts.shift();
  return parts.join('.');
}

jQuery(document).ready(function()
{
	DPCD.initPage();
         //resize the menu items based on the number of list items in the menu
	var contentWidth = parseInt($("#wrapper").css("width"));
	var totalAmount = $("#top_nav li").size();
	var newSize = Math.floor(contentWidth/totalAmount);
	var childSize = newSize - 1 + "px";
	newSize = newSize + "px";
	$("#top_nav li").each(function(){
	  $(this).css("width", newSize);
	  //IE7 fix to ensure borders print
	  $(this).children().css("width", childSize);
	});	
         //paint the first list item white to stop the IE bug
         //$("#top_nav li:first").css("background-color", "white");
         //paint the first list item white to stop the IE bug
         $("#top_nav li:first").css("background-color", "white");
         //css fix for people without javascript is removed for those with
	$("#top_nav li a span").css("padding", "0px");
         // javascript corners for the menu
         $("#top_nav").corner("bl br 8px");
	$("#top_nav").corner("tr tl 6px");
	$("#top_nav li:first").corner("tl 2px");
	$("#top_nav li:last a").corner("br tr 5px");
	$("#top_nav li:first a").corner("bl tl 5px");
         	// Insert resize links
	$('#header_tools ul').prepend('<li id="header_tools_downsize"><a href="#"><span class="hidden">Decrease text size</span></a></li><li id="header_tools_upsize"><a href="#"><span class="hidden">Increase text size</span></a></li><li id="header_tools_print"><a href="#" onClick="window.print()"><span class="hidden">Print this page</span></a></li>');

  $('#content').textResize({
    plus: '#header_tools_upsize a',
    minus: '#header_tools_downsize a',
    reset: '',
    pace: 1,
    original: 11,
    limit: [8,14]
  });

  //if submenu has more than 5 items, add to the section nav container
    if($('#nav_column > ul > li').length > 5 ) {
	//show heading
	$("#section-heading").show();
    //create section nav container
    $("#nav_column").append('<div id="section_nav"><ul></ul></div>');
    $("#section_nav ul").append($('#nav_column > ul > li:gt(4)'));
  }

  //external links
  // Content External Links
	$('#main_content a[href^=http]').each(
		function(){
		    /* commented out to hardcode sub domains
			var siteDomain = stripSub(location.hostname);*/
			if(this.href.indexOf("private.vic.gov.au") == -1 && this.href.indexOf("dpcd.vic.gov.au") == -1) {
				$(this).attr('target', '_blank')
					.attr('class', 'external-link')
					.attr('title', 'External link (will open in new window)');
			}

		});

   //alternate colors for search result rows
    $("#search_results li:odd").addClass("even");

  /* *** Home slide show *** */
function formatText(index, panel) {
 return index + "";
 }
 
 $("#main_content .anythingFader ul").css("position","absolute");
 $("#main_content .anythingFader ul").css("padding","0");
 $("#main_content .anythingFader ul li").css("display","none");
 $("#main_content .anythingFader ul li").css("position","absolute");
 $("#main_content .anythingFader ul li").css("height","218px");
 $("#main_content .anythingFader ul li").css("margin-bottom","0");
 $(".slideshow_content").css("position","absolute");
 $(".slideshow_content").css("height","135px");
 $(".slideshow_image").css("position","absolute");

$('.anythingFader').anythingFader({
 autoPlay: true, // This turns off the entire FUNCTIONALY, not just if it starts running or not.
 delay: 5000, // How long between slide transitions in AutoPlay mode
 startStopped: false, // If autoPlay is on, this can force it to start stopped
 animationTime: 500, // How long the slide transition takes
 hashTags: true, // Should links change the hashtag in the URL?
 buildNavigation: true, // If true, builds and list of anchor links to link to each slide
 pauseOnHover: true, // If true, and autoPlay is enabled, the show will pause on hover
 startText: "Play", // Start text
 stopText: "Pause", // Stop text
 navigationFormatter: formatText // Details at the top of the file on this use (advanced use)
}); 

 $("#slide-jump").click(function(){
 $('.anythingfader').anythingFader(6);
 });

  // Urban Design Guidelines chapter hide show
  // find the active element page and add highlight classes for the tree
  var $match;
  $(".rhs_toolbar .right_panel_content ul:first").find("a").each(function(){
    if (($(this).attr("href")) == ($(location).attr("href"))) {
      $match = 1;
      $(this).parents("li").addClass("current");
      $(this).parents("ul").addClass("visible");
      $(this).parent("li").children("ul").addClass("visible");
    }
  });
  // hide the element structures that are not currently part of the hierarchy
  if ($match === 1) {
    $(".rhs_toolbar .right_panel_content ul").each(function(){
      if(!($(this).hasClass("visible"))){
        $(this).hide();
      }
    }); 
  }

});

