﻿(function($){
	
	var Banner = function(elm,data){
		this.autoCount = 0;
		this.curSlide = 0;
		this.data = data.tab1.children;
		this.fadeSpeed = 500;
		this.imgCache = [];
		this.interval = null;
		this.location = document.createElement("span");
		this.navLinks = [];
		this.slides = [];
		this.slideCount = 0;
		this.subTitle = document.createElement("p");
		this.wrap = $(elm);
		
		this.init();
	};
	
	var PublicMethod = Banner.prototype,
	PrivateMethod = {};
	
	PublicMethod.showSlide = function(i){
		    
        if(i > this.slideCount-1) {
            i = 0;
        } else if(i < 0) {
            i = this.slideCount-1;
        };
        
        $(this.slides[this.curSlide]).stop().animate({"opacity":0},this.fadeSpeed,function(){
            this.style.display = "none";
        });
        $(this.slides[i]).stop().css("display","block").animate({"opacity":1},this.fadeSpeed);

        this.location.innerHTML = this.data["data" + (i+1)].children.location3.nodeText;
        this.curSlide = i;
    };
	
	PublicMethod.init = function(){
		
		var me = this;	
		
		//data handling
		for(var x in this.data){
			var dRow = this.data[x].children,
			slide = new PrivateMethod.Slide(dRow),
			tmpImg = new Image();
			tmpImg.src = slide.image;
			this.imgCache.push(tmpImg);
			this.slides.push(slide.element);
			this.slideCount++;
		};

		// inital values/attributes
		this.location.setAttribute("id","bannerCaption");
		
	    // append elems
	    this.wrap.append(this.slides, this.location);

		this.showSlide(0);
				
		// start interval
		if(this.slideCount > 1) {
			this.interval = setInterval(function(){
				me.autoCount < (me.slideCount-1) ? me.autoCount++ : me.autoCount = 0;
				me.showSlide(me.autoCount);
			},4000);
		};
	};
	
	PrivateMethod.Slide = function(data){
	    var dataElm = data;
		for(var x in dataElm) {
			this[x] = dataElm[x].nodeText;
		};
		this.element = document.createElement("div");
		this.textElm = document.createElement("div");
		this.titleElem = document.createElement("h1");
		this.moreLink = document.createElement("a");
		
		this.element.setAttribute(classAttr,"slide");
		this.textElm.setAttribute(classAttr,"bannerText");
		this.moreLink.setAttribute(classAttr,"moreBtn");
				
		this.titleElem.innerHTML = this.title1;
		this.moreLink.innerHTML = "Read more";
		this.moreLink.href = this.link4;
		
		this.textElm.appendChild(this.titleElem);
		this.textElm.innerHTML += this.subText2;
		//this.textElm.appendChild(this.moreLink);
		this.element.style.backgroundImage = "url('/images/FlashImages/" + this.image5 + "')";
		
		$(this.element).append(this.textElm).css("opacity",0);
	};
	
	window.Banner = Banner;
		
})(window.jQuery);
