/*********************************************************/
function targetBlank() {
	$('a.target_blank').attr("target","_blank");
}

function stringReplaceAll(Source,stringToFind,stringToReplace){
	  var temp = Source;
	  var index = temp.indexOf(stringToFind);
	  while(index != -1){
		  temp = temp.replace(stringToFind,stringToReplace);
		  index = temp.indexOf(stringToFind);
	  }
	  return temp;
}

/*********************************************************/
function initFormsVerifyEvent() {
	var reqFieldsForm=$('form.req_fields');
	if(reqFieldsForm.length>0) {
		$.each(reqFieldsForm,function() {
			$(this).submit(function(event) {
				var theForm=$(this);				
				var permitSubmit=false;				
				var parameters=theForm.serialize();
				parameters+='&fid='+theForm.attr("id");
				var theRequest = $.ajax({ url: "form_process_reqfields.php",
					async: false,
					type: 'post',
					dataType: 'script',					
					data: parameters,
					success: function(response) {
      					if(response.substring(0,10)!="mustStop=1")
      						permitSubmit=true;
      				}
      			});
      			return permitSubmit;      			
			});
		});
	}
}

/*********************************************************/
function scrollOnTopPage() {
	$('html, body').animate({scrollTop: 50}, 'slow');
}

function scrollToElement(element,speed) {
   if(!speed)
   	speed=500;
   var x = element.offset().top - 100; // 100 provides buffer in viewport
   $('html,body').animate({scrollTop: x}, speed);
}

function initBlinkObjects() {
	if($('.blink').length>0)
		$('.blink').blink();
}

/*********************************************************/
function getFlashVersion(){
  // ie
  try {
    try {
      // avoid fp6 minor version lookup issues
      // see: http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/
      var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
      try { axo.AllowScriptAccess = 'always'; }
      catch(e) { return '6,0,0'; }
    } catch(e) {}    
    return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1];
  // other browsers
  } catch(e) {
    try {
      if(navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin){
        return (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace(/\D+/g, ",").match(/^,?(.+),?$/)[1];     
      }      
    } catch(e) {}
  }  
  return '0,0,0';
}

/*********************************************************/
function initMainMenu() {
	$("ul#topnav li").hover(function() { //Hover over event on list item
		$(this).find("span.sub_menu").show();
	} , function() {
		$(this).find("span.sub_menu").hide();
	});
	$("ul#topnav li span.sub_menu span.sub_menu_item").hover(function() {
		$(this).find("span.sub_sub_menu").show();
	} , function() {
		$(this).find("span.sub_sub_menu").hide();
	});
}

function initButtonsRollover() {
	$("button").hover(
		function(event) {
			var actualBg=$(this).css("background-image");
			var newBg=actualBg.replace(".gif","_s.gif");
			$(this).css("background-image",newBg);
		},
		function(event) {
			var actualBg=$(this).css("background-image");
			var newBg=actualBg.replace("_s.gif",".gif");
			$(this).css("background-image",newBg);			
		}
	);
}

function initContentScrollbar() {
	if($("#content_scrolling").is(":visible"))
		$("#content_scrolling").mCustomScrollbar("vertical",300,"easeOutCirc",1.05,"auto","yes","yes",15);
}

/* function to fix the -10000 pixel limit of jquery.animate */
$.fx.prototype.cur = function(){
    if ( this.elem[this.prop] != null && (!this.elem.style || this.elem.style[this.prop] == null) ) {
      return this.elem[ this.prop ];
    }
    var r = parseFloat( jQuery.css( this.elem, this.prop ) );
    return typeof r == 'undefined' ? 0 : r;
}

/* function easing equations */
$.easing['BounceEaseOut'] = function(p, t, b, c, d) {
	if ((t/=d) < (1/2.75)) {
		return c*(7.5625*t*t) + b;
	} else if (t < (2/2.75)) {
		return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
	} else if (t < (2.5/2.75)) {
		return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
	} else {
		return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
	}
};
/*********************************************************/
$(document).ready(function () {
	targetBlank();
	initBlinkObjects();
});

$(window).load(function () {
	initContentScrollbar();
	initMainMenu();
	initButtonsRollover();
	initFormsVerifyEvent();
});
