var run=false;
//var url_root='/landc/html/';
var url_root='/';
var cnt=0;
var c_i=0;
var total;
var INTERVAL_T=7000;
var fb_text= new Array();
var fb_text_stated= new Array();
var fb_text_top= new Array();
var fb_text_left= new Array();
var fb_text_width= new Array();
var fb_text_class= new Array();
var fb_text_link= new Array();
var fb_text_ddtext= new Array();
var timer;
var noFade=false;

(function($) {
	$.fn.customFadeIn = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeOut = function(speed, callback) {
		$(this).fadeOut(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
})(jQuery);

$(document).ready(function(){
	cur_i=0;
	/*
	$("#fb_nav ul li a").click(function(){
		clearInterval(timer);
		update_fb(this, true);
		return false;
	});*/
	if ( $.browser.msie ) {
		  if( parseInt($.browser.version) <9 ){
			  noFade=true;
		  }
		}
	jQuery.getJSON(url_root+"ajax/ajaxGetFeatureBanners/0/ajaxGetFeatureBanners.htm",
			function(data){
	    		jQuery.each(data, function(i,item){   
	    			fb_text.push(item.content);
	    			fb_text_stated.push(item.line1+"<br/>"+item.line2);
	    			fb_text_top.push(item.top);
	    			fb_text_left.push(item.leftpos);
	    			fb_text_width.push(item.width);
	    			fb_text_class.push(item.classy);
	    			fb_text_link.push(item.link);
	    			fb_text_ddtext.push(item.dropdown_text);
	    			total=i;     
	    		}); 
    		}); 
		});

setTimeout(function() {
			run=true;
			//timer = setInterval("changeMain('null')",INTERVAL_T);
			$(".bgimg").css({"display":"block"});
		},1000);


function changeMain(){
	if(c_i==total){
		var ele=$("#fb_nav_"+c_i);
		c_i=0;
	}
	else{
		var ele=$("#fb_nav_"+(parseFloat(c_i)+1));
	}
	update_fb(ele, false);
}
function update_fb(ele, si){
	if(run==true){
		run=false;
		var sel=$(ele).attr("id");
		var bits = sel.split("_");
		var i = bits[2];
		if($(ele).hasClass("selected")){
			update_text_box(fb_text[0],fb_text_top[0],fb_text_left[0],fb_text_width[0],fb_text_class[0],fb_text_stated[0]);
			update_img("#bg_img_0","#bg_img_"+cur_i);
			c_i = 0;
			cur_i=0;
		}
		else{
			update_img("#bg_img_"+i,"#bg_img_"+cur_i);
			update_text_box(fb_text[i],fb_text_top[i],fb_text_left[i],fb_text_width[i],fb_text_class[i],fb_text_stated[i]);
			cur_i=i;
			c_i = i;
		}
		$("#fb_drop_text .fb_text").fadeOut(300,function(){
			if($(ele).hasClass("selected")){
				$("#fb_drop_text").slideUp(500,function(){
					run=true;
					if(si){
						timer = setInterval("changeMain('null')",INTERVAL_T);
					}
				});
				$("#fb_nav ul li a").removeClass("selected");
			}
			else{
				$("#fb_nav ul li a").removeClass("selected");
				$(ele).addClass("selected");
				$("#fb_drop_text").slideUp(500,function(){
					$("#fb_drop_text .fb_text").css({"display":"block"});
					$("#fb_drop_text").slideDown(500,function(){
						$("#fb_drop_text .fb_text span").html(fb_text_ddtext[i]);
						$("#fb_drop_text a.fb_text_link,a.fb_text_link_big").attr("href",fb_text_link[i]);
						$("#fb_drop_text").fadeIn(300,function(){
							run=true;
							if(si){
								timer = setInterval("changeMain('null')",INTERVAL_T);
							}
						});
					});
				});
			}
		});
	}
}

function update_text_box(text,top,left,width,cls,stated){
	if(noFade){
		$("#fb_text .text,#fb_text .stated").fadeOut(1500,function(){
			$("#fb_text .text").html(text);
			$("#fb_text .stated").html(stated);
			$("#fb_text").hide(0);
			$("#fb_text").css({'margin-top':top+"px",'margin-left':left+"px",'width':width+"px"}).removeClass().addClass(cls).show(0);
			$("#fb_text .text,#fb_text .stated").fadeIn(500);
		});	
		
	}
	else{
		$("#fb_text").customFadeOut(500,function(){
			$("#fb_text .text").html(text);
			$("#fb_text .stated").html(stated);
			$("#fb_text").css({'margin-top':top+"px",'margin-left':left+"px",'width':width+"px"}).removeClass().addClass(cls).customFadeIn(500);
		});	
	}
}

function update_img(bg_img,cur_bg_img){
	$(bg_img).css({'z-index':'-2'});
	$(cur_bg_img).fadeOut(1000,function(){
		$(cur_bg_img).css({'z-index':'-3','display':'block'});
		$(bg_img).css({'z-index':'-1'});
	});
}
