﻿function $(id) { return document.getElementById(id); }
function $$(strClassName, strTagName, oElm){var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);var arrReturnElements = new Array();strClassName = strClassName.replace(/\-/g, "\\-");var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");var oElement;for(var i=0; i<arrElements.length; i++){oElement = arrElements[i];if(oRegExp.test(oElement.className)){arrReturnElements.push(oElement);}}return (arrReturnElements)}

var contentslider = {

    init:function(config)
    {   
        this.$SliderContainer   = $(config.ContainerID);
        this.$Sliders           = $$(config.SliderClass,config.SliderTagName,this.$SliderContainer);
        this.$Pagers            = $$(config.PagerClass,config.PagerTagName,$(config.PagerContainerID));
        this.$PagerClass        = config.PagerClass;
        this.$PagerSelectedClass= config.PagerSelectedClass;
        this.$CurrentID         = -1;
        this.$SelectedID        = Math.abs(config.Selected)
        this.$Count             = (this.$Pagers.length > this.$Sliders.length)?this.$Sliders.length:this.$Pagers.length;
        this.$Animating         = false;
        this.$Timer             = null;
        this.$CookieTime		= Math.abs(config.CookieTime) || 300;
        this.$Step              = Math.abs(config.Step) || 5;
        this.$Time              = Math.abs(config.Time) || 10;
        this.$Interval          = Math.abs(config.Interval) || 3000;
        this.$AutoStart         = config.AutoStart || true;
        this.$AutoSlideTimes    = Math.abs(config.AutoSlideTimes) || 1;
        this.$CurrentWidth 		= 0;
        
        this.DocReady();
  
    },
    checkOrientAndLocation:function()
	{
		  if(this.$Animating) return;
		  if (document.body.clientWidth != this.$CurrentWidth)
		  {   
		  	this.SetupChannels();
		    this.$CurrentWidth = document.body.clientWidth;
		    var orient = this.$CurrentWidth == 320 ? "profile" : "landscape";
		    document.body.setAttribute("orient", orient);
		    //setTimeout(scrollTo, 100, 0, 1);
		  }

	},

    DocReady:function()
    {
        var oThis = this;
        clearTimeout(this.$Timer);
        if(document.readyState == "complete")
        {
            this.SetupChannels();
            this.SetupPagers();
            setInterval(function(){oThis.checkOrientAndLocation();},500);
            var pagenumber = getCookie("pagenumber");
            if(pagenumber != null && pagenumber != "" && pagenumber > -1)
			{
				this.$AutoSlideTimes = 0;
				this.$SelectedID = pagenumber;
		    }
			this.Start();

        }
        else
        {
            this.$Timer = setTimeout(function(){oThis.DocReady();},10);
        }
    },
    
    SetupPagers:function()
    {
        var oThis = this;
        for(var i=0;i<oThis.$Count;i++)
        {
            this.$Pagers[i].setAttribute("pageIndex",i)
            this.$Pagers[i].onclick = function(){
                if(oThis.$Animating) return;
                oThis.$AutoSlideTimes = 0;
                oThis.$SelectedID = this.getAttribute("pageIndex");
                SetCookiee("pagenumber",this.getAttribute("pageIndex"),oThis.$CookieTime);
                oThis.Start();
            };
        }
        
    },
    
    SetupChannels:function()
    {
        var oThis = this;
        for(var i=0;i<oThis.$Count;i++)
        {
            this.$Sliders[i].style.width = this.$SliderContainer.offsetWidth + "px";
        }
    },
    
    Start:function(){
        
        if(this.$SelectedID == this.$CurrentID || this.$Animating)
        {
            return;
        }
        this.$Animating = true;
        
        if(this.$SelectedID < 0)
        {
            this.$SelectedID = this.$Count - 1;
        }
        else
        {
            var t = this.$SelectedID / this.$Count;
            this.$SelectedID = this.$SelectedID % this.$Count
            
            if(t == 1) this.$AutoSlideTimes --;
        }

        var slider = this.$Sliders[this.$SelectedID];
        
        var pager  = this.$Pagers[this.$SelectedID];
        
        pager.className = this.$PagerSelectedClass;
        
        if(this.$CurrentID > -1)
        {
            var opager = this.$Pagers[this.$CurrentID];
            opager.className = this.$PagerClass;
        }
        
        this.$SliderContainer.style.height = slider.offsetHeight+"px";
        slider.style.visibility = "visible";
        slider.style["left"] = this.GetDistance()+"px";
        slider.style.width = this.GetDistance()+"px";
        slider.style.zIndex = 100;
        
        this.Move();
    },
    
    Move:function(){  
        clearTimeout(this.$Timer);
        var oThis = this;
        var slider = this.$Sliders[this.$SelectedID];
        
        iNow = parseInt(slider.style["left"]) || this.GetDistance();
        iStep = this.GetStep(iNow); 
        if(iStep != 0){
            
            slider.style["left"] = (iNow + iStep) + "px";
            this.$Timer = setTimeout(function(){oThis.Move();},this.$Time);
        }
        else
        {
            slider.style["left"] = "0px";
            this.Finished();
        }
    },
    
    Finished:function(){
        clearTimeout(this.$Timer);
        var oThis = this;
        if(this.$CurrentID > -1)
        {
            var oslider = this.$Sliders[this.$CurrentID];
            oslider.style["left"] = this.GetDistance()+"px";
            oslider.style.visibility = "hidden";
        }
        
        var nslider = this.$Sliders[this.$SelectedID];
        nslider.style.zIndex = 99;
        this.$CurrentID = this.$SelectedID; 
        
        this.$Animating = false;
        
       
       // if(this.$AutoStart && this.$AutoSlideTimes > 0)
       // {
       //     this.$Timer = setTimeout(function(){ oThis.$SelectedID++; oThis.Start();},this.$Interval);
       // } 
    },
    
    GetDistance:function()
    {
        return this.$SliderContainer.offsetWidth;
    },
    
    GetStep:function(iNow)
    { 
        var iStep = ( 0 - iNow) / this.$Step;
        
        if(iStep == 0) return 0;
        
        if (Math.abs(iStep) < 1) return 0; //(iStep > 0 ? 1 : -1);
        
        return iStep;
        
    }
}


function SetCookiee(name,value,expires)
{
	if(expires == null || expires < 0 || expires == "") expires = 300;
	setCookie(name,value,expires);
}

function setCookie(name, value) 
{ 
	var argv = setCookie.arguments; 
	var argc = setCookie.arguments.length; 
	var expires = (argc > 2) ? argv[2] : null; 
	if(expires!=null) 
	{ 
		var LargeExpDate = new Date (); 
		LargeExpDate.setTime(LargeExpDate.getTime() + (expires*1000)); 
	} 
	document.cookie = name + "=" + escape (value)+((expires == null) ? "" : ("; expires=" +LargeExpDate.toGMTString()))+"; path=/"; 
}
 
function getCookie(Name) 
{ 
	var search = Name + "="; 
	if(document.cookie.length > 0) 
	{ 
		offset = document.cookie.indexOf(search) 
		if(offset != -1) 
		{ 
			offset += search.length 
			end = document.cookie.indexOf(";", offset) 
			if(end == -1) end = document.cookie.length 
			return unescape(document.cookie.substring(offset, end)) 
		} 
		else return "" 
	} 
} 

