var counter = 1;
var mouseover = false;
function imgload(){
	if (counter > imgs.length-1) {
		counter = 0;
	}
    var oldcount = counter-1;
    if (counter == 0) oldcount = imgs.length - 1; 
    if (!mouseover){
	    jQuery("#imgt").ImageSwitch({
			Type:"FadeIn",
			NewImage:imgs[counter],
			OldImage:imgs[oldcount],
			Direction:"DownTop",
			EffectOriginal: false
		});

	    jQuery(".desc-frame").divSwitch({
	    	Type:"FadeIn",
	    	NewImage:divs[counter],
	    	OldImage:divs[oldcount],
	    	Direction:"DownTop",
	    	EffectOriginal: false
	    });
	    
	    jQuery("#nav-frame-link-"+(oldcount+1)).removeClass('active');
	    jQuery("#nav-frame-link-"+(counter+1)).addClass('active');
	    
		counter++;
    }
	var mytime=setTimeout('imgload()',4000);
}
function imgload_nottimed(oldcount){
	    jQuery("#imgt").ImageSwitch({
			Type:"FadeIn",
			NewImage:imgs[counter-1],
			OldImage:imgs[oldcount-1],
			Speed:500,
			Direction:"DownTop",
			EffectOriginal: false
		});
	
	    jQuery(".desc-frame").divSwitch({
	    	Type:"FadeIn",
	    	NewImage:divs[counter-1],
	    	OldImage:divs[oldcount-1],
			Speed:500,
	    	Direction:"DownTop",
	    	EffectOriginal: false
	    });

	    jQuery("#nav-frame-link-"+oldcount).removeClass('active');
	    jQuery("#nav-frame-link-"+counter).addClass('active');
	    	    
}

	
	
	
	 var Debug = function(mess, line){
			if(!jQuery("#Debug"))
			{
				jQuery("body").append("<div id='Debug'></div>");
			}
			if(line){
				jQuery("#Debug").html(jQuery("#Debug").html()+"<br/>"+mess);
			}
			else{
				jQuery("#Debug").html(jQuery("#Debug").html()+mess);
			}
		};
		(function(jQuery){
			jQuery.fn.divSwitch = function(Arguements, FuntionHandle) {
				var defaults = {
					Type: "FadeIn", // Type of effect to run the function
					NewImage: "", //The new image will be loaded in
					OldImage: "",
					EffectOriginal: true,
					Speed: 1000, //Speed of the effect
					StartLeft: 50, //The position the effect start compare to the original position could be (-)
					StartTop: 0,
					StartOpacity: 0, //Which start opacity it would be
					EndOpacity: 0 //Which start opacity it would be
				};
				
				var Args = jQuery.extend(defaults, Arguements);
				var Obj = this; // Just a way to reference to this obj in case we need to pass in another event handle
				//To specific which obj ID the effect will associate to
				var EffectID = 0;
				
				var EffectImageId;//The id of effect image layer : #GrpEffectImg + EffectID
				
		        var EndFunction = function(){
					Obj.data("imageswitch", -1);
				};		
				if(jQuery.isFunction(FuntionHandle)){
				    EndFunction = function(){		
						FuntionHandle();	
						Obj.data("imageswitch", -1);
					};
				}
				//-----------------------------------------------------------------------------------------------------------
				//The original image will be fade out when the new image will fade in
				var FadeDiv = function(){
					//Generate the effect map, move the effect map overlay the original map
					Obj.parent().append("<div id='"+EffectImageId.replace("#","")+"' class='desc-frame'></div>");
					jQuery(EffectImageId).html(Args.OldImage);
					jQuery(EffectImageId).css("position", "absolute");
					jQuery(EffectImageId).css("top", Obj.position().top);
					jQuery(EffectImageId).css("left", Obj.position().left);
					jQuery(EffectImageId).css("z-index", "30");
					jQuery(EffectImageId).css("opacity", 1);
					
					//Change image of the original map
					Obj.html(Args.NewImage);
					
					//Need something special when user want to keep no effect for the orignal
					if(Args.EffectOriginal)
					{
						//Set the start opacity, as the effect will fade out we set in start at 1, vice versa for the original
						Obj.css("opacity", Args.StartOpacity);		
						
						//Fade in the original image
						Obj.animate({"opacity":1}, Args.Speed);			
					}
					
					//Start effect animation
					jQuery(EffectImageId).animate({"opacity":0}, Args.Speed, function(){
							//Remove the effect image when finish the effect
							jQuery(EffectImageId).remove();
							EndFunction();
					});	
					
				};
				
				return this.each(function(){
					Obj = jQuery(this);		
					if(!Obj.ImageAnimating())
					{
						EffectImageId = "#header_sdiv";//The id of effect image layer : #GrpEffectImg- + EffectID
						
						var TempImg = jQuery(EffectImageId);
						TempImg.html(Args.NewImage);
						FadeDiv();
						jQuery(EffectImageId).remove();
					}
				});	
			};	
		})(jQuery);
		(function(jQuery){
			jQuery.fn.ImageSwitch = function(Arguements, FuntionHandle) {
				var defaults = {
						Type: "FadeIn", // Type of effect to run the function
						NewImage: "", //The new image will be loaded in
						OldImage: "",
						EffectOriginal: true,
						Speed: 1000, //Speed of the effect
						StartLeft: 50, //The position the effect start compare to the original position could be (-)
						StartTop: 0,
						StartOpacity: 0, //Which start opacity it would be
						EndOpacity: 0 //Which start opacity it would be
				};
				
				var Args = jQuery.extend(defaults, Arguements);
				var Obj = this; // Just a way to reference to this obj in case we need to pass in another event handle
				//To specific which obj ID the effect will associate to
				var EffectID = 0;
				
				var EffectImageId;//The id of effect image layer : #GrpEffectImg + EffectID
				
				var EndFunction = function(){
					Obj.data("imageswitch", -1);
				};		
				if(jQuery.isFunction(FuntionHandle)){
					EndFunction = function(){		
						FuntionHandle();	
						Obj.data("imageswitch", -1);
					};
				}
				
				//-----------------------------------------------------------------------------------------------------------
				//The original image will be fade out when the new image will fade in
				var FadeImage = function(){
					//Generate the effect map, move the effect map overlay the original map
					Obj.parent().append("<img id='"+EffectImageId.replace("#","")+"'/>");
					jQuery(EffectImageId).attr("src", Args.OldImage);
					jQuery(EffectImageId).css("position", "absolute");
					jQuery(EffectImageId).css("top", Obj.position().top);
					jQuery(EffectImageId).css("left", Obj.position().left);
					jQuery(EffectImageId).css("opacity", 1);
					
					//Change image of the original map
					Obj.attr("src", ""+Args.NewImage);
					
					//Need something special when user want to keep no effect for the orignal
					if(Args.EffectOriginal)
					{
						//Set the start opacity, as the effect will fade out we set in start at 1, vice versa for the original
						Obj.css("opacity", Args.StartOpacity);		
						
						//Fade in the original image
						Obj.animate({"opacity":1}, Args.Speed);			
					}
					
					//Start effect animation
					jQuery(EffectImageId).animate({"opacity":0}, Args.Speed, function(){
						//Remove the effect image when finish the effect
						jQuery(EffectImageId).remove();
						EndFunction();
					});			
				};
				
				return this.each(function(){
					Obj = jQuery(this);		
					if(!Obj.ImageAnimating())
					{
						EffectImageId = "#header_s";//The id of effect image layer : #GrpEffectImg- + EffectID
						
						var TempImg = new Image();
						TempImg.src = Args.NewImage;
						jQuery.ImagePreload(Args.NewImage,function(){
							switch(Args.Type){
							case "FadeIn":		FadeImage();	break;
							}
						});
						jQuery.each(jQuery.browser, function(i, val) {
							   if(i=="msie" || i == "opera")
								   jQuery(EffectImageId).remove();
						});
					}
				});	
			};	
		})(jQuery);

		//Check if a IS effect is running
		(function(jQuery){
			jQuery.fn.ImageAnimating = function(){
				if(this.data("imageswitch")>0){
					return true;
				}else{
					return false;
				}
			};
		})(jQuery);
		//Stop a specific the IS effect if it's running
		(function(jQuery){
			jQuery.fn.ImageStop = function(clearQueue, gotoEnd, EndFunction){
				return this.each(function(){
					if(jQuery(this).ImageAnimating()){
						var EffectID = jQuery.data(this,"imageswitch");
						jQuery("#header").stop(clearQueue, gotoEnd);
						jQuery(this).stop(clearQueue, gotoEnd);
						jQuery("#header").remove();
						if(jQuery.isFunction(EndFunction)){
							EndFunction();
						}
					}
				});
			};
		})(jQuery);
		//Stop all the IS effect running
		(function(jQuery){
			jQuery.ImageStopAll = function(clearQueue, gotoEnd, EndFunction){
				jQuery(".GrpEffectImg").stop(clearQueue, gotoEnd);
				jQuery(".GrpEffectDiv").stop(clearQueue, gotoEnd);
				jQuery(".GrpEffectDiv1").stop(clearQueue, gotoEnd);
				jQuery(this).stop(clearQueue, gotoEnd);
				jQuery(".GrpEffectImg").remove();
				jQuery(".GrpEffectDiv").remove();
				jQuery(".GrpEffectDiv1").remove();
				jQuery.data(this, "imageswitch", -1);
				if(jQuery.isFunction(EndFunction)){
					EndFunction();
				}
			};
		})(jQuery);
		//Preload a specific image
		(function(jQuery){
			jQuery.ImagePreload = function(FileName, EndFunction){
				var TempImage = new Image();
				TempImage.src = FileName;
				if(jQuery.isFunction(EndFunction)){
					jQuery(TempImage).load(EndFunction());
				}
			};
		})(jQuery);