function training() {

    if ($(".pageid_1646").length > 0) {
      $("#q1647_q17").val($(document).getUrlParam("course").replace(/%20/g," "));
      $("#q1647_q15").val($(document).getUrlParam("date").replace(/%20/g," "));
      $("#q1647_q16").val($(document).getUrlParam("location").replace(/%20/g," "));
    }

}; // end of training()

function navAnimation() {

	$('#region_nav a').hover(function() { //mouse in  
		$(this).animate({ paddingTop: '8px', paddingBottom: '2px' }, 300);  
	}, function() { //mouse out  
		$(this).animate({ paddingTop: 0, paddingBottom: '10px' }, 300);  
	});

}; // end of navAnimation()

function tabArea() { //seems a little hackish - can we improve this?

	// home page tab area
	// first remove a tags
	$("ul#right-menu a").each(function() {
		var tmp = $(this).text();
		$(this).parent().html(tmp);
	});
        // load tabs
	$("ul#right-menu li").click(function () {
                var content_hide = $(".active").attr("id");
                var content_show = $(this).attr("id");
		$(".active").removeClass("active");  
		$(this).addClass("active");
		$("#content-"+content_hide).slideUp();
		$("#content-"+content_show).slideDown();
	});
}; //end tabArea

function prevNext() {

    $("#page_nav .nojs").fadeOut("def", function() {
      $("#page_nav .js").fadeIn("def");
    });

    var total_news_items = $(".jsLoad div").length;
    var news_item_shown = 1;

    $("#page_nav .prev").click( function() {
       if (news_item_shown > 1) {
          $("#news_"+news_item_shown).fadeOut("def", function() {
             news_item_shown = news_item_shown -1;
             $("#news_"+news_item_shown).fadeIn("def");
          });
        }
       return false;
    });

    $("#page_nav .next").click( function() {
       if (news_item_shown < total_news_items) {
          $("#news_"+news_item_shown).fadeOut("def", function() {
             news_item_shown = news_item_shown +1;
             $("#news_"+news_item_shown).fadeIn("def");
          });
        }
       return false;
    });

}; // end of prevNext()

function focus() {

    $("*:input").focus(function () {
      $(this).addClass("focus");
    });
    $("*:input").blur(function () {
      $(this).removeClass("focus");
    });


}; // end of focus()


//Page Specific Code

function govisEntry(){
   $('.govis-entry #govis-terms-control').html('Do you agree to the <a class="govis-terms-control-link" href="#">terms & conditions</a>?');   
   var terms = $('#govis-terms').html();
   $('.govis-entry #govis-terms').remove();
   $('.govis-entry #govis-terms-control').after('<div id="govis-terms" style="display:none;">' + terms + '</div>');
   $(".govis-terms-control-link").click(function(){
      $('.govis-entry #govis-terms').slideDown();
      return false;
   })

}

//Portfolio Nav

function portfolioAccordion(){
		
	$('#portfolio-sidenav>li').each(function(){
		if($(this).find('ul').length > 0){
			$(this).prepend('<a href="#" class="portfolio-sidenav-control">&uarr;</a>');
		}
	})
	
	//bind toggle function
	$('a.portfolio-sidenav-control').toggle(
		function () {
			tel = $(this).parent('li');
			$(tel).children('ul').hide();
			$(tel).children('.portfolio-sidenav-control').html('&darr;');
		},
		function () {
			tel = $(this).parent('li');
			$(tel).children('ul').show();
			$(tel).children('.portfolio-sidenav-control').html('&uarr;');
		}
	)
	
	//hide everything
	$('a.portfolio-sidenav-control').click();
	
	//Change colour of current link - i know its hacky :)
	var currentPage = $('body').attr('id').substr(4);
	$("li.active>a, li.hierarchy>a").css('color', '#000');
	
	//show if we're on the current one or its hierarchy
	$('#portfolio-sidenav>li').each(function(){
	
		var $this = $(this).find('.portfolio-sidenav-control');
	
		if($(this).attr('class') == "hierarchy" || $(this).attr('class') == "active" ){
			$this.click();
		} 

	})
	
}	

/* Homepage slideshow - pretty specific, will need to make more generic to use elsewhere */
function slideShow(el){
             
                var containers = $('#main_content_home a[class^=container]').length - 1
                
                
                $('.container-0').css({ "z-index" : "3"});
                
                i = 0;
                slideShowTimer = window.setInterval(function(){ 
                		
                		if(i==containers){ //if at end of cycle start again from beginning
                                $('.container-0').css({ "z-index" : "2"});
                        } else {
                        	$('.container-'+(i+1)).css({ "z-index" : "2"});
                        }
                		
                		$('.container-'+i).fadeOut('slow', function(){
                        
	                        $('.container-'+i).css({ "z-index" : "1"}).show();
                        
                         	if(i==containers){ //if at end of cycle start again from beginning 
                                i = 0;
                                $('.container-'+i).css({ "z-index" : "3"});
                                
                        	} else {
                        		i++;
                        		$('.container-'+i).css({ "z-index" : "3"});
                       	 	}
                      
                      });   
                        
                },5000); // time delay in milliseconds     
        
};	



//Call it all


/* Now that the page is ready lets do all the js magic we feel like */
$(document).ready(function(){
	
	//animate links at top
	navAnimation();

	//homepage tab area
	tabArea();

        //enable previous and next buttons
        prevNext();

        //add focus and blur classes
        focus();

        //add training defaults
        if($('#page1646')){
             training()
        }

      if ($("#page4644, #page4622").length > 0) {
$('.seminar-cms .seminar-location').click(function(){
    $('.seminar-cms').fadeOut(function(){
$(this).html('<h2>Prefered Location</h2><p>The MySource Matrix Enterprise CMS seminar is held in Wellington as well as in Auckland. Please select your prefered location:</p><ul><li><a href="./?a=4451">Auckland Tue 17 Nov</a></li><li><a href="./?a=2614">Wellington Wed 18 Nov</a></li></ul>').fadeIn();
})
    return false;
})

$('.seminar-funnel .seminar-location').click(function(){
    $('.seminar-funnel').fadeOut(function(){
$(this).html('<h2>Prefered Location</h2><p>The Funnelback Enterprise Search seminar is held in Wellington as well as in Auckland. Please select your prefered location:</p><ul><li><a href="./?a=4684">Auckland Tue 17 Nov</a></li><li><a href="./?a=4144">Wellington Wed 18 Nov</a></li></ul>').fadeIn();
})
    return false;
})
}
      if ($(".pageid_4144").length > 0) {
          $('#q4145_q10 option[value*=' + $(document).getUrlParam("location") + ']').attr('selected', 'selected');
      }

        if($('body#Home')){
             slideShow($('#main_content_home a img'), "http://www.squiz.co.nz/home/homepage_image_list/_nocache")
        }

        /* if($('#portfolio-sidenav')){
             portfolioAccordion();

             //in portfolio also activate lightbox
             $("a[rel^='lightbox']").slimbox({/* Put custom options here */}, null, function(el) {
		return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
	     });
        } */

      /* if (!($.browser.msie && $.browser.version.substr(0, 1) < 7)) {
          //Hide the accordion before doc load for seemless loading
         var accordion = $('.accordion');
          accordion.hide();
          //Bind the accordion initialision to document load
         $(function() {
             accordion.accordion({ header: 'h3', autoHeight: false, collapsible: true, active: false });
             accordion.show();
         });
       } */ 

	//load superfish
	$('ul#nav').supersubs({
		minWidth:    8,   // minimum width of sub-menus in em units
		maxWidth:    20,   // maximum width of sub-menus in em units
		extraWidth:  1     // extra width can ensure lines don't sometimes turn over due to slight rounding differences and font-family
	}).superfish({ 
		animation:   {opacity:'show',height:'show'},  // fade-in and slide-down animation 
                speed:       'fast',                          // faster animation speed 
                autoArrows:  false,                           // disable generation of arrow mark-up 
                dropShadows: false                            // disable drop shadows 
	});
  


  //govis entry form
  govisEntry();



});     