var oDiv = document.createElement('div');
var oSpan = document.createElement('span');
var oA = document.createElement('a');
var oP = document.createElement('p');
var oImg = document.createElement('img');
var oH3 = document.createElement('h3');
var oH4 = document.createElement('h4');
var oH5 = document.createElement('h5');
var oUl = document.createElement('ul');
var oLi = document.createElement('li');

var SortArchive = {

	/** start of editable items **/
	iThumbWidth : 80,  // width of thumbnails
	iThumbHeight : 80,  // height of thumbnails
	iOnOpacity : 100,  // opacity for on state of stories
	iOffOpacity : 10,  // opacity for off state of stories
	iTeaserLength : 97,  // length of description teaser
	iTitleCutoff : 25,  // length of title, if longer add ...
	iHeadlineCutoff : 27,  // length of headline, if longer add ...
	sUlMargin : '10px 5px',  // margin for unordered lists of stories when month is open
	sLiHeight : '100px',  // height of stories
	/* month backgrounds opened and closed states */
	sMonthOpenBg : 'url(/euro/main/js/pro/images/header_opened20060405.gif) no-repeat',
	sMonthCloseBg : 'url(/euro/main/js/pro/images/header_closed20060405.gif) no-repeat',
	iMonthsToDisplay : 3,  // number of months opened on loading of page
	sSeminarsLink: 'seminars.html',  // link to Seminars See All
	/** end of editable items **/
	
	aMonthNames : new Array(
						'January',
						'February',
						'March',
						'April',
						'May',
						'June',
						'July',
						'August',
						'September',
						'October',
						'November',
						'December'
					),
	oResults : new Object(),
	aStories : new Array(),
	aStoriesHash: new Array(),
	sType : new String(),
	sNav : new String(),
	sXMLFile : new String(),
	sContentId : new String(),
	aSections : new Array(),
	aMonths : new Array(),
	aNavItems: new Array(),
	bOnFlag : false,
	iCheckedNavItems: 5,
	sImgChecked: 'http://images.apple.com/euro/main/js/pro/images/check_on20060502.gif',
	sImgUnchecked: 'http://images.apple.com/euro/main/js/pro/images/check_off20060502.gif',
	
	fInit : function(type,xmlfile,content) {
		this.sType = type;
		this.sXMLFile = xmlfile;
		this.sContentId = content;
		this.fShowAll();
		this.fDisplayContent();
	},
	fDisplayContent : function() {
		AppleJAX.getXML(this.sXMLFile,function(){
			SortArchive.oResults = results;
			SortArchive.fDisplayYears();
			SortArchive.fDisplayStories();
		});
	},
	fDisplayStories : function() {
		var oContent = new HTMLObject($(this.sContentId));
		var curDocumentPagename = document.location.href.substring(document.location.href.lastIndexOf('/') + 1);
		var oImgClone = oImg.cloneNode(true);
		oImgClone.style.height = this.iThumbHeight+'px'
		oImgClone.style.width = this.iThumbWidth+'px';
		this.aStories = this.oResults.getElementsByTagName('story');
		var oArchiveLi = oLi.cloneNode(true);
		var oArchiveLiLink = oA.cloneNode(true);
		var sArchiveTitle = document.createTextNode('Weitere Tipps');
		oArchiveLi.className = 'archive';
		oArchiveLiLink.href = '/de/pro/tips/';
		oArchiveLiLink.appendChild(sArchiveTitle);
		oArchiveLi.appendChild(oArchiveLiLink);
		
		for(var i = 0; i < 6; i++) {
			var oStory = this.aStories[i];
			var oTitle = oStory.getAttribute('title');
			var oDate = oStory.getElementsByTagName('date')[0];
			var oURL = oStory.getElementsByTagName('url')[0];
			var oStoryLi = new HTMLObject(oLi.cloneNode(true)); // oLi.cloneNode(true);
			var oTitleLink = oA.cloneNode(true);
			var oSpanPart = oSpan.cloneNode(true);
			
			if(i == 0)
			{
				oStoryLi.className = 'new';
			}
			
			if(this.sType == 'tips' || this.sType == 'resources' || this.sType == 'tipsmini') {
				this.iTitleCutoff = oTitle.length;
			}
			if(oTitle.length > this.iTitleCutoff) {
				oTitle = oTitle.substring(0,(this.iTitleCutoff-3));
				oTitle = oTitle.substring(0,oTitle.lastIndexOf(' '));
				oTitle += '...';
			}
			var sTitleEnd = '';
			var sTitle = document.createTextNode(oTitle+sTitleEnd);
			var sYear = oDate.firstChild.data.split('.')[0];
			var sMonth = oDate.firstChild.data.split('.')[1];
			var sDay = oDate.firstChild.data.split('.')[2];
			var sShortYear = sYear.substring(2);
			var sDateSeparator = ' ';
			var sDate = document.createTextNode(sDay+'.'+sMonth+'.'+sShortYear+sDateSeparator);
			
			// if story link contains reference to a current page, don't display link (the else condition)
			if(oURL.firstChild.data.indexOf(curDocumentPagename) == -1)
			{
				oTitleLink.href = oURL.firstChild.data;
				oTitleLink.appendChild(sDate);
				oTitleLink.appendChild(sTitle);
				oStoryLi.appendChild(oTitleLink);
			}
			else
			{
				oStoryLi.appendChild(sDate);
				oStoryLi.appendChild(sTitle);
			}
			
			oStoryLi.id = 'story_'+i; 
			var oMonthUl = $('subnav').getElementsByTagName('ul')[0];
			oMonthUl.appendChild(oStoryLi);
			
			
			Object.extend(oStoryLi,WhizBang.prototype);
			this.aStoriesHash[i] = {
				id: oStoryLi.id,
				obj: oStoryLi,
				height: oStoryLi.offsetHeight,
				width: oStoryLi.offsetWidth,
				top: oStoryLi.offsetTop,
				left: oStoryLi.offsetLeft,
				titlelink: oTitleLink,
				opacity: this.iOnOpacity,
				disciplines: oStory.getAttribute('disciplines'),
				href: oURL.firstChild.data
			};
		}
		oMonthUl.appendChild(oArchiveLi);
		for(var i=0; i<this.aStoriesHash.length; i++) {
			this.aStoriesHash[this.aStoriesHash[i].id] = this.aStoriesHash[i];
		}
	},
	fDisplayYears : function() {
		var oContent = new HTMLObject($(this.sContentId));
		this.aStories = this.oResults.getElementsByTagName('story');
		
		for(var i=0;i<this.aStories.length;i++) {
			var oDate = this.aStories[i].getElementsByTagName('date')[0];
			var sYear = oDate.firstChild.data.split('.')[0];
			var sMonth = oDate.firstChild.data.split('.')[1];
			var sYearMonth = sYear.toString()+'/'+sMonth.toString();
			if(this.aMonths.toString().indexOf(sYearMonth) < 0) {
				this.aMonths.push(sYear+'/'+sMonth);
			}	
		}
		
		this.aMonths.sort().reverse();
		//var oMonthDiv = oDiv.cloneNode(true);
		//oMonthDiv.id = 'subnav';
		var oMonthUl = oUl.cloneNode(true);
		//oMonthDiv.appendChild(oMonthUl);
		//oContent.appendChild(oMonthDiv);
		oContent.appendChild(oMonthUl);
	},
	fShowAll : function() {
		var oShowAll = SortArchive.aNavItems[SortArchive.sNav+'_showall'];
		for(var i=0;i<SortArchive.aNavItems.length;i++) {
			var oNavItem = SortArchive.aNavItems[i];
			if(oNavItem != oShowAll) {
				if(oNavItem.anchor.hasClass('hi')) { oNavItem.anchor.removeClass('hi'); }
				if(!oNavItem.anchor.hasClass('all')) { oNavItem.anchor.addClass('all'); }
				oNavItem.checked = false;
				oNavItem.showing = true;
				oNavItem.showall = true;
				oNavItem.anchor.removeChild(oNavItem.img);
			} else {
				if(!oShowAll.anchor.hasClass('hi')) { oShowAll.anchor.addClass('hi'); }
				oShowAll.img.src = SortArchive.sImgChecked;
				oShowAll.checked = true;
				oShowAll.showing = true;
				oShowAll.showall = true;
			}
		}
	}
};

//addEventToObject(window,'onload',SortArchive.fInit('nav','filter_text.xml','content'));
