// JavaScript Document
// Bas Zurburg 20090402


/**********************************************************
 * slideSwitch (function)
 *  - Used for the fading images on the homepage
 */

function slideSwitch() {
    var $active = $('#slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');
    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');

    // uncomment the 3 lines below to pull the images in random order
    
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );

    $active.addClass('last-active');
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}
	
$(document).ready(function(){
	// This code executes after domready
   
/**********************************************************
 * Navigation functionality
 *
 *  - Megamenu
 *  - IE6 Helpers
 *  - Sitemap expand functionality
 *  
 */
      function addMega(){
        $(this).addClass("hovering");
        }
      function removeMega(){
        $(this).removeClass("hovering");
        }
    $('li.mega').hoverIntent({
         interval: 200,
         sensitivity: 2,
         over: addMega,
         timeout: 300,
         out: removeMega
    });
   // End Mega menu.

   // Fix hover problem with IE6 z-index problem.
   $('li.mega div').bgiframe();

   // IE navigation helper (because IE6 does not support hover on li)
	$('#header ul.menu li').hover(
		function(){
			$(this).addClass('iehover');
		},
		function () {
			$(this).removeClass('iehover');
		}				
	);


	// Sitemap toggle class when expanding items
	$('#sitemap-columns li a.expand').click(function() {
			$(this).parent('li').toggleClass('open');
			return false;
		});

	// Language Switch - Hide the language switch functionality when there is notjing to switch	
	$('#language:has(a)').removeClass('hidden');


/**********************************************************
 * Autocomplete functionality and other forms functionality
 *
 */

  $("select.country_select").select_autocomplete();

  //fake button behaviour
  $("div.wrapSelect").hover(function () {
  	$(this).children(".ac_input").click();
  }); 
  

  // clears the autocomplete input when it gets focus and simulates a click
  $("input.ac_input").focus(function () {
  	$(this).attr({value: ''});
  	//$(this).click();
  }); 

	

  // creates a jumpbox (actually has nothing to do with autocomplete)
  $("select.country_simple").change( function () { 
      location.href = $(this).val();
    });



 
/**********************************************************
 * Image Rollover functionality
 *
 * 	Each image with a class of rollover will rollover with an image -active
 *    currently only works for gif's
 *
 */

   // Rollover
    $(".rollover").hover(
		function(){
			if($(this).attr("src").indexOf("-active") == -1) {
				var newSrc = $(this).attr("src").replace(".gif","-active.gif");
				$(this).attr("src",newSrc);
			}
		},
		function(){
			if($(this).attr("src").indexOf("-active.gif") != -1) {
				var oldSrc = $(this).attr("src").replace("-active.gif",".gif");
				$(this).attr("src",oldSrc);
			}
		}
	);


/**********************************************************
 * All javascript for buttons
 *
 *
 */

		//all hover and click logic for buttons
		$(".fg-button:not(.ui-state-disabled)")
		.hover(
			function(){ 
				$(this).addClass("ui-state-hover"); 
			},
			function(){ 
				$(this).removeClass("ui-state-hover"); 
			}
		)
		.mousedown(function(){
				$(this).parents('.fg-buttonset-single:first').find(".fg-button.ui-state-active").removeClass("ui-state-active");
				if( $(this).is('.ui-state-active.fg-button-toggleable, .fg-buttonset-multi .ui-state-active') ){ $(this).removeClass("ui-state-active"); }
				else { $(this).addClass("ui-state-active"); }	
		})
		.mouseup(function(){
			if(! $(this).is('.fg-button-toggleable, .fg-buttonset-single .fg-button,  .fg-buttonset-multi .fg-button') ){
				$(this).removeClass("ui-state-active");
			}
		});

/**********************************************************
 * Make JavaScript Unobtrusive functions
 *
 *
 */
	// hide items only when there is Javascript!
     $('.tohide').removeClass('tohide').addClass('hidden');
	// make items visible when there is Javascript!
     $('.unhide').removeClass('unhide');


/**********************************************************
 * Toggle the lists and archives functions
 *
 */

     $('#content a.toggle').toggle(
          function(){
			$(this).parent().next('div.year').slideDown('slow');
			$(this).removeClass('down').addClass('up');
				}
		  ,
		  function () {
			$(this).parent().next('div.year').slideUp('normal');
			$(this).removeClass('up').addClass('down');
			}				
	);

/**********************************************************
 *  Ajax Functionality 
 *
 * 	Each link (<a>) with a class of ajax will get a click event
 *  	and the result will be loaded in the container specified by the target)
 *      To Do: provide example
 *      To Do: create a 'post'  call
 */
	$('a.ajaxcall').click(function(){
		var ajaxurl = $(this).attr('href');
		var ajaxtarget = $('div#'+ $(this).attr('target'));
		ajaxtarget.addClass('ajaxloading');
		ajaxtarget.load(ajaxurl);
		ajaxtarget.removeClass('ajaxloading');
		return false;
	});


   // To-Top
   //	Not to display to-Top when too high in the page (only below 920 px).
   //   This value has been chosen because of the list pages.
	if ($('a.to-top').length){
	var totop = $('a.to-top');
	alert("totop" + totop);
		var pos = totop.position().top;
		if (pos < 920) {
			totop.hide();
			$('<br />').insertAfter(totop); 
			}
		}

/**********************************************************
 *  Fancybox functionality 
 *
 */

   // fancybox-iframe

   $('a.iframe.popup').fancybox({
		'frameWidth': 500,
		'frameHeight': 500,
		'centerOnScroll': false,
        'easingIn': 'easeOutBack',
		'easingOut': 'easeInBack'
	});	 

   $('a.iframe.calculator').fancybox({
		'frameWidth': 640,
		'frameHeight': 600,
		'centerOnScroll': false,
		'easingIn': 'easeOutBack',
		'easingOut': 'easeInBack'
	});	 


/**********************************************************
 *  Cluetip
 *  
 *  The idea was to use this for the module popup.
 *  perhaps use tools overlay? 
 */

  // Cluetip 
  $('a.moduletip').cluetip({
    cluetipClass: 'module', 
	width: '360px',
    positionBy: 'bottomTop',
    dropShadowSteps:  3,
	local: true,
	sticky: true,
    closePosition: 'title',
	mouseOutClose: true,
	//delayedClose: 25000,
	hoverIntent: {    
      sensitivity:  3,
      interval:     200,
      timeout:      200
    },
    fx: {             
       open:       'slideDown', // can be 'show' or 'slideDown' or 'fadeIn'
       openSpeed:  300
    }
  });

/**********************************************************
 *  Date picker (mainly used in forms) 
 *
 */
	// Datepicker defaults
	
	$.datepicker.setDefaults({ dateFormat: 'yy-mm-dd',
								changeMonth: true,
								changeYear: true								
	 })

	// A generic datepicker e.g. used in the forms
	$(".datepicker").datepicker();


 // Activate Homepage slideshow
 
 	if ($('#slideshow').length){
 		setInterval( "slideSwitch()", 5000 );
	}

// Form validation
	// only do this when there is some validation class that has attributes - 
	// (I probably do something wrong here - need to learn more about this plugin) 
	if ($('.atradiusForm .validate').length){
		$.metadata.setType("attr", "validate");
	}

 // Form validation
	$("#AtradiusFormJS").validate();

/**********************************************************
 * External links (display with an extra icon) 
 * The color and the context can be passed as parameters. 
 */

$('#content').extlink("red");
$('.threecol').extlink("red");

 // end code to be executed when document ready
});

