var objInterval;
var displayTime = 12000;
var bannerLock = false;
var transactionId = 0;

(function(jQuery){
		

		

	jQuery.fn.start = function(){

		var elements = jQuery(".main-banner ul li");
		jQuery(".banner-inner").hide();
					
		if(elements.length > 0){
					
			jQuery(".main-banner ul li a").mouseover(function(){
				bannerLock = true;
			});
						
			jQuery(".main-banner ul li a").mouseout(function(){
				bannerLock = false;
			});
					

			jQuery(".banner-selector").append("<ul></ul>");
				for(i=0;elements.length > i;i++){
					jQuery(".banner-selector ul").append("<li><a href='#'>" + (i+1) + "</a></li>");
				}
						
				nextBanner();
						
			
			
		}else {
		
				if(jQuery(".banner-inner").length > 0){
					jQuery("#header-banner-in").css("background-image", 'url(' + jQuery("img",".banner-inner").attr("src") + ')');
				}
			}
		}
			

				
				

	
	
	jQuery.fn.startBackgroundPosition = function(){
		jQuery(window).resize(function(){jQuery.fn.loadBackgroundPosition();});
		jQuery(window).load(function(){jQuery.fn.loadBackgroundPosition();});
	}
	
	jQuery.fn.loadBackgroundPosition = function(){
		var headerLeft = jQuery(".header").offset().left;
		jQuery(".header-banner-bg").css("background-position", headerLeft + 'px top');
	}

	
	jQuery.fn.changeImage = function(){
		jQuery(".#mainBanner a").click(function(){
			jQuery.fn.loadImage(this.href, this.rel);
			return false;
		})
	}
	
	jQuery.fn.startTabs = function() {

	//When page loads...
	jQuery(".tab_content").hide(); //Hide all content
	jQuery("ul.tabs li:first").addClass("active").show(); //Activate first tab
	jQuery(".tab_content:first").show(); //Show first tab content

	//On Click Event
	jQuery("ul.tabs li").click(function() {

		jQuery("ul.tabs li").removeClass("active"); //Remove any "active" class
		jQuery(this).addClass("active"); //Add "active" class to selected tab
		jQuery(".tab_content").hide(); //Hide all tab content

		var activeTab = jQuery(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		jQuery(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});

};

	jQuery.fn.startStates = function(){
		jQuery("div.states div").hide();
		jQuery("div.state-map area").click(function(){
			jQuery("div.states div").hide();
			var activeState = jQuery(this).attr("href");
			jQuery(activeState).fadeIn();
			return false;
		});
	}
	
	jQuery.fn.startCountries = function(){
		jQuery("div.countries div").hide();
		jQuery("div.world-map select").change(function(){
			jQuery("div.countries div").hide();
			var activeCountry = "#" + jQuery(this).attr("value");
			jQuery(activeCountry).fadeIn();
			return false;
		});
		

		
	}

})(jQuery);


function nextBanner(){
						
	var links = jQuery(".banner-selector ul li a");
	var selected = jQuery(".banner-selector ul li a.selected").html();
	var nextIndex = 1;
		if(links.length > 0){
		if(selected!=null){nextIndex = (parseInt(selected) + 1); }
		if(nextIndex > links.length){nextIndex = 1;}
							
			changeBannerByIndex(nextIndex);
		}
						
}
					
								
				function changeBannerByIndex(nIndex){
									
					if(bannerLock==false){
						transactionId++;
						var numTrans = transactionId;
					
						jQuery(".banner-selector ul li a").bind('click',function(){		
								return changeBannerByIndex(jQuery(this).html());
							});
						
					
						clearBanner();	
						setSelectedBanner(getBannerByIndex(nIndex));
						showBannerByIndex(nIndex,numTrans);
					}
					return false;
				}
					
				function getBannerByIndex (nIndex){
					var links = jQuery(".banner-selector ul li a");
					if(nIndex == 0){return false;}
					if(nIndex > links.length){return false;}
					return links[nIndex - 1];
				}
				
				function showBannerByIndex(nIndex, numTrans){
					var banners = jQuery(".main-banner ul li");
					if(nIndex == 0){return false;}
					if(nIndex > banners.length){return false;}
					
					var imgPath = jQuery("a", banners[nIndex - 1]).attr("rel");
					if(imgPath.length>0){
					var img = new Image();
					jQuery(".main-banner ul").css("background-image", 'url(ajax-loader.gif)');
					
					img.onload=function(){
					
						jQuery(".main-banner ul").css("background-image", 'none');
						jQuery("#header-banner-in").css("background-image", 'url(' + imgPath + ')');
						jQuery("#header-banner-in").fadeIn(1500, function(){
							animateBanner(nIndex,numTrans);
	
						});
					
					};
					
					img.src = imgPath;
					}else{
						animateBanner(nIndex, numTrans);
					}
				}
				
				function animateBanner(nIndex, numTrans){
					
					if(numTrans==transactionId){
						var banners = jQuery(".main-banner ul li");
						jQuery(banners[nIndex - 1]).css("margin-left","-1000px");
						jQuery(banners[nIndex - 1]).show();
						jQuery(banners[nIndex - 1]).animate({marginLeft:0},1000);
					}
				}
					
					
				function setSelectedBanner(banner){
					
						jQuery(".banner-selector ul li a").removeClass("selected");
						jQuery(banner).addClass("selected");
						clearInterval(objInterval);
						objInterval = setInterval('nextBanner()', displayTime);
					
						jQuery(banner).unbind('click');
					
						return false;
				}
				
				function clearBanner(){
					jQuery(".main-banner ul li").hide();
					jQuery("#header-banner-in").hide();
					jQuery("#header-banner-in").css("background-image", 'none');
				}

