﻿// JScript File
        var notPrev;
        var notNext;

        var ListControl;
        function ListingItems(col)
        {
            this.collection = col;
            this.numByPage = 9;
            this.currentPageIdx = 1;
            this.totalPages = (col.length/this.numByPage);
            this.currentPage = document.createElement("div");
            this.numVisible = 0;
        }
        
        function LoadPage()
        {
            if ( ListControl.currentPage.hasChildNodes() )
			{
				while ( ListControl.currentPage.childNodes.length >= 1 )
				{
					ListControl.currentPage.removeChild( ListControl.currentPage.firstChild ); 
    			} 
			}
			
			var idxInternal = (ListControl.currentPageIdx-1)*ListControl.numByPage;
			var idx = 0;
			ListControl.numVisible = 0;
            while(idxInternal < ListControl.collection.length && idx < ListControl.numByPage )
            {
                if( (ListControl.collection[idxInternal].item.style.visibility == "") || (ListControl.collection[idxInternal].item.style.visibility =="hidden"))
                {
                    ++idxInternal;
                }
                
                else
                {
                    ListControl.currentPage.appendChild(ListControl.collection[idxInternal].item);
                    ++ListControl.numVisible;
                    ++idx;
                    ++idxInternal;
                }
            }
            if(idxInternal == ListControl.collection.length)
            {
                //nextLabel.className="PagDisable";
                nextLabel.style.visibility = "hidden";
                return;
            }
                
            while(idxInternal < ListControl.collection.length && idx < ListControl.numByPage )
            {
                if( (ListControl.collection[idxInternal].item.style.visibility != "") && (ListControl.collection[idxInternal].item.style.visibility !="hidden"))
                {
                    //nextLabel.className="PagDisable";
                    nextLabel.style.visibility = "hidden";
                    return;
                    
                }
            }
        }
        
        function Prev()
        {
            if(ListControl.totalPages > 1)
                //nextLabel.className = "PrintText";
                nextLabel.style.visibility = "visible";
            if(ListControl.currentPageIdx <= 1)
            {   
                //prevLabel.className="PagDisable";
                prevLabel.style.visibility = "hidden";
                return;
            }
            
            --ListControl.currentPageIdx;
            LoadPage();
            if(ListControl.currentPageIdx <= 1)
            {   
                //prevLabel.className="PagDisable";
                prevLabel.style.visibility = "hidden";
            }
        }
        
        function Next()
        {
            //prevLabel.className = "PrintText";
            prevLabel.style.visibility = "visible";
            if(ListControl.currentPageIdx >= ListControl.totalPages)
                return;
            if(ListControl.currentPageIdx+1 >= ListControl.totalPages)
            {
                //nextLabel.className="PagDisable";
                nextLabel.style.visibility = "hidden";
            }
            var idxInternal = (ListControl.currentPageIdx)*ListControl.numByPage;
			var idx = 0;
            while(idxInternal < ListControl.collection.length && idx < ListControl.numByPage )
            {
                if( (ListControl.collection[idxInternal].item.style.visibility == "") || (ListControl.collection[idxInternal].item.style.visibility =="hidden"))
                {
                    ++idxInternal;
                }
                else
                {
                    ++ListControl.currentPageIdx;
                    LoadPage();
                    return;
                }
            }
            
        }

        function selectByCategory(categSelected){
            for(var idx =0; idx < ListControl.collection.length; ++idx)
			{
		        ListControl.collection[idx].item.style.visibility = "visible";
		        ListControl.collection[idx].item.style.height = "95px";
		        for(var i = 0; i <ListControl.collection[idx].item.childNodes.length; ++i )
		        {
			        ListControl.collection[idx].item.childNodes[i].style.visibility = "visible";
			        ListControl.collection[idx].item.childNodes[i].style.height = "";
			    }
			}
			ListControl.currentPageIdx = 1;
            if(categoryElem.selectedIndex == 0)
            {
                LoadPage();
                return;
            }
                categSelected = new String()+categSelected;
                categSelected = categSelected.toUpperCase();
			for(var idx =0; idx < ListControl.collection.length; ++idx)
			{
			    if(ListControl.collection[idx].category != categSelected){
			        ListControl.collection[idx].item.style.visibility = "hidden";
			        ListControl.collection[idx].item.style.height = "0px";
			        for(var i2 = 0; i2 <ListControl.collection[idx].item.childNodes.length; ++i2 )
			        {
			            ListControl.collection[idx].item.childNodes[i2].style.visibility = "hidden";
			            ListControl.collection[idx].item.childNodes[i2].style.height = "0%";
			        }
			    }
			}
			//LoadItems();
			LoadPage();
		}

        function changeCategory()
        {
            var categSelected = categoryElem[categoryElem.selectedIndex].value;
            selectByCategory(categSelected);
        }


        function itemClick(item)
        {
            alert('inside click'+item);
        }


		function createItem(date, dateSeparator, weekday, categorySeparator, category, title, description, img, entry) 
		{
			var _item = document.createElement("div");
			
			_item.removeAttribute("class");
			_item.className="Items";
            
            var _imgDiv =  document.createElement("div");
            _imgDiv.removeAttribute("class");
			_imgDiv.className="imgDiv";
			
			this.imgDiv = _imgDiv;
            if(img.length >0)
            {
			    var _img = document.createElement("img");
			    _img.setAttribute("src", img);
			    _img.removeAttribute("class");
			    _img.className="itemImg";
			    _imgDiv.style.width = _img.style.width;
//			_img.onclick = function(){ itemClick(_item); };
//			_img.ondblclick = function(){itemDblClick(entry);};
            
			
			//_item.appendChild(_img);
			    _imgDiv.appendChild(_img);
			
			}
			_item.appendChild(_imgDiv);
			
			var _header = document.createElement("div");
			_header.removeAttribute("class");
			_header.className="headerItem";
			var _date = document.createElement("span");
			_date.appendChild(document.createTextNode(date));
			_date.removeAttribute("class");
			_date.className="EventDate";
			_header.appendChild(_date);
			
			var _dateSeparator = document.createElement("span");
            _dateSeparator.appendChild(document.createTextNode(dateSeparator));
			_dateSeparator.removeAttribute("class");
			_dateSeparator.className="EventDetaidDayWeek";
			_header.appendChild(_dateSeparator);
			
			var _weekday = document.createElement("span");
            _weekday.appendChild(document.createTextNode(weekday));
			_weekday.removeAttribute("class");
			_weekday.className="EventDetaidDayWeek";
			_header.appendChild(_weekday);

			
			var _categorySeparator = document.createElement("span");
            _categorySeparator.appendChild(document.createTextNode(categorySeparator));
			_categorySeparator.removeAttribute("class");
			_categorySeparator.className="DetailEventCategory";
			_header.appendChild(_categorySeparator);
			
			var _category = document.createElement("span");
            _category.appendChild(document.createTextNode(category));
			_category.removeAttribute("class");
			_category.className="DetailEventCategory";
			_header.appendChild(_category);
			
		
			_item.appendChild(_header);
			
			
			var _body = document.createElement("div");
			_body.removeAttribute("class");
			_body.className="bodyItem";
			
			var _name = document.createElement("div");
			
			var _nameLink = document.createElement("a");
			
			//_name.appendChild(document.createTextNode(title));
			_nameLink.appendChild(document.createTextNode(title));
			//_nameLink.href = "./EventDetails.aspx?itemId="+entry.id;
			_nameLink.href = entry.url;
			_nameLink.appendChild(_name);
			_nameLink.removeAttribute("class");
			_nameLink.className="EventTitle2";
			
			_name.removeAttribute("class");
			_name.className="EventTitle2";
			_name.onclick = function(){ itemClick(entry.id); }
			_body.appendChild(_nameLink);
			
			var _subt = document.createElement("div");
			_subt.appendChild(document.createTextNode(description));
			_subt.removeAttribute("class");
			_subt.className="";
			
			_body.appendChild(_subt);
			_item.appendChild(_body);
			var _division = document.createElement("div");
			_division.removeAttribute("class");
			_division.className="DivisionItem";
			_item.appendChild(_division);
			
//            var itemOuter = document.createElement("div");
//            itemOuter.removeAttribute("class");
//            itemOuter.className="headerItemEnc";
//            itemOuter.appendChild(_item);
            	
			return _item;
		}
		
			
		function ItemShow(date, dateSeparator, weekday, categorySeparator, category, title, description, img, url){
			this.date = date;
			this.dateSeparator = dateSeparator;
			this.weekday = weekday;
			this.categorySeparator = categorySeparator;
			this.category = category;
			this.title = title;
			this.description = description;
			this.img = img;
			this.url = url;
			this.item = createItem(date, dateSeparator, weekday, categorySeparator, category, title, description, img, this);
		}

		function LoadItems(){
			var content = document.getElementById("contentDetails");
			if ( content.hasChildNodes() )
			{
				while ( content.childNodes.length >= 1 )
				{
					content.removeChild( content.firstChild );       
				} 
			}
			
			ListControl = new ListingItems(Items);
			selectByCategory('');
			LoadPage();
            //prevLabel.className="PagDisable";
            prevLabel.style.visibility = "hidden";
            if(ListControl.totalPages <= 1)
            {
                //nextLabel.className="PagDisable";
                nextLabel.style.visibility = "hidden";
            }

			//for(var i = 0; i < Items.length; ++i)
			//	ListControl.currentPage.appendChild(Items[i].item);
            content.appendChild(ListControl.currentPage);
			
		}

