
/*
 *	@Script			: agresso_corporate.js
 *	@Description	: General functions for the Agresso website. Require Mootools 1.2. And jQuery.
 */

//////////////////////////////////////////////////////////////////////////////////////
// JQUERY 																			//
//////////////////////////////////////////////////////////////////////////////////////

var tmpTextHolder = '';

// Turn on compatibility mode for jQuery 
jQuery.noConflict(); 

jQuery(document).ready(function() {
			
	// Add validation to all forms
	jQuery("form").each( function() {			  
		jQuery(this).validate();
	});
	
	// impliment ajax submission of forms
	jQuery("form.ajaxform #button").click(function() {
        var str = jQuery(this).closest("form.ajaxform").serialize();
		var formid = jQuery(this).closest("form.ajaxform").attr('id');

		jQuery(this).closest("form.ajaxform").validate();

		if(jQuery(this).closest("form.ajaxform").valid()){
	
			jQuery.ajax({
			   type: "POST",
			   url: "/resources_app/library/ajax/ajax_formsubmission.php",
			   data: str,
			   beforeSend: function(){ 
				  //jQuery('form.ajaxform button').after('<div id="loadingScreen"><img src="images/site/portfolio-loader.gif" /></div>');
			   },
			   dataType: "html",
			   success: function(response){
					jQuery('#' + formid).html(response);
				   //jQuery('#loadingScreen').remove();
				   animateResults();
			   }
			}); 
		}
		
		return false;
		
   	});
	
	animateResults();	
});


jQuery.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return jQuery.getUrlVars()[name];
  }
});

jQuery(".acc_link").click(function(event){
	window.location.hash=this.hash;
});


function animateResults(){
  jQuery(".graph div").each(function(){
      var percentage = jQuery(this).next().text();
      jQuery(this).css({width: "0%"}).animate({
				width: percentage}, 'slow');
  });
}


function jQuery_SetupHoverLinks()
{
  jQuery(".list_links_hover li a").hover(function(){
  	var jQuery_Span = jQuery(this).siblings('span.background');
  	jQuery(jQuery_Span).stop();
    jQuery(jQuery_Span).animate({"opacity":1});
  }, function(){
  	var jQuery_Span = jQuery(this).siblings('span.background');
    jQuery(jQuery_Span).stop();
    jQuery(jQuery_Span).animate({"opacity":0}, "slow");   
  });
}

// Moves a class between the elements with the tag within the container.
function toggleClass(newSelectedId, containerId, tagName, newClassName) {

	var elementsArray = jQuery(tagName, '#' + containerId);

	for (i = 0; i < elementsArray.length; i++) {
		if (elementsArray[i].id == newSelectedId) {
			// this is the selected one, add the class to it.
			jQuery(elementsArray[i]).addClass(newClassName);
		} else {
			// not the selected one, remove the class from it
			jQuery(elementsArray[i]).removeClass(newClassName);
		}
	}
}

// add a loading screen
function loadingScreen(element) {
	var div = jQuery("<div>").addClass('loadingScreen').text('');	
	// append it
	div.appendTo(element);
}

// remove the loading screen(s)
function removeLoadingScreen(element){
	if (element) {
		jQuery(".loadingScreen", element).remove();
	} else {
		// remove all loading screens
		jQuery(".loadingScreen").remove();
	}
}

