// JavaScript Document
var dU = {
	
	getStyle: function(el,styleProp) {
		var x = document.getElementById(el);
		if (x.currentStyle)
			var y = x.currentStyle[styleProp];
		else if (window.getComputedStyle)
			var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);		
		return y;
	},
	
	// getPageSize()
	// Returns array with page width, height and window width, height
	// Core code from - quirksmode.org
	// Edit for Firefox by pHaez
	//
	getPageSize: function() {
		
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}
	
	
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
		return arrayPageSize;
	}
}

var blogMgr = {
	
	//DOM form element id's (constants)
	blogContentID: "blog_leftContent",	
	blogPhotosID: "blog_leftPhotos",
	blogMainPhotoID: "blogMainPhoto",
	
	blogToggleID: 'postToggle',
	blogToggleLinkID: 'postLinkAnchor',
	blogToggleIconID: 'postLinkIcon',
	blogToggleTextID: 'postLinkText',	
	
	blogMainPhotoOBJ: "",
	blogPhotosOBJ: "",	
	chosenImgOBJ: "",	
	
	imgCaptionID: "pictureCaption",
	
	blogToggleOBJ: "",
	blogTogglePhotosOBJ: "",
	blogToggleContentOBJ: "",
	
	postOBJ: "",
	postID: "",
	
  	init: function() {		
		
    	if (!document.getElementById || !document.getElementsByTagName) {		
			return;
	  	} 
		
		if(!document.getElementById(blogMgr.blogToggleID)) {			
			return;
		}
		
		//testOBJ = document.getElementById('postTest');	
		
		//get current post ID
		blogMgr.postID = document.getElementById('postID').name;		
		
		//preload images
		blogMgr.preLoadImgs();		
		
		//set objects			
		blogMgr.blogToggleOBJ = document.getElementById(blogMgr.blogToggleID);	
		blogMgr.blogPhotosOBJ = document.getElementById(blogMgr.blogPhotosID);	
		blogMgr.blogMainPhotoOBJ = document.getElementById(blogMgr.blogMainPhotoID);
		
		//set toggle div elements (because Safari can't interpret a simple DOM tree and wants me to write more code)				
		//build toggle node for switching back to content
		blogMgr.buildToggleNodes();				
		
		//get current placeholder thumb img object and store it for swapping
		blogMgr.findChosenImg();				
		
		//attach events for IMG thumb links
		var thumbLinks = blogMgr.blogPhotosOBJ.getElementsByTagName('a');	
		
		for(var i = 0; i < thumbLinks.length; i++) 
		{				
			var currentLink = thumbLinks[i];
			
			if(currentLink.firstChild.nodeName == 'IMG' && currentLink.firstChild.src.search(/thumb_/) != -1)	{						
				addEvent(currentLink, 'click', blogMgr.changePic, false);
				currentLink.href = "#";
				addEvent(currentLink, 'click', cancelClick, false);	
				currentLink.onclick = cancelClickSafari;
			}
		}
		
		//attach events for toggling between blog photos and content
		blogMgr.addToggleEvents(blogMgr.blogToggleLinkOBJ);		
  	},
	
	addToggleEvents: function(targetOBJ) {
		//alert(targetOBJ.nodeName);
		addEvent(targetOBJ, 'click', blogMgr.toggleBlog, false);
		targetOBJ.href = "#";
		addEvent(targetOBJ, 'click', cancelClick, false);	
		targetOBJ.onclick = cancelClickSafari;	
	},
	
	buildToggleNodes: function () {
		blogMgr.blogToggleLinkOBJ = document.getElementById(blogMgr.blogToggleLinkID);	
		blogMgr.blogToggleIconOBJ = document.getElementById(blogMgr.blogToggleIconID);
		blogMgr.blogToggleTextOBJ = document.getElementById(blogMgr.blogToggleTextID);
		
		blogMgr.blogToggleContentOBJ = blogMgr.blogToggleLinkOBJ.cloneNode(true);
		blogMgr.blogTogglePhotosOBJ = blogMgr.blogToggleLinkOBJ.cloneNode(true);		
		
		blogMgr.addToggleEvents(blogMgr.blogToggleContentOBJ);
		blogMgr.addToggleEvents(blogMgr.blogTogglePhotosOBJ);		
		
		blogMgr.blogTogglePhotosOBJ.title = "Back to Posts";
		blogMgr.blogTogglePhotosOBJ.childNodes[0].src = blogMgr.blogTogglePhotosOBJ.childNodes[0].src.replace(/photos/,"back_to_post");
		blogMgr.blogTogglePhotosOBJ.childNodes[1].nodeValue = "BACK TO POSTS";		
		
	},
	
	toggleBlog: function(e) {		
		var newSrc = "";
		var local_target = findTarget(e);
		
		//traverse to get to link node (Safari issue)
		local_target = linkTraverse(local_target);		
		
		if(local_target.nodeName.toLowerCase() == 'body') {
			return;	
		}
		
		if(blogMgr.blogToggleOBJ.firstChild.firstChild.src.match(/photos/)) {
			
			blogMgr.blogToggleOBJ.replaceChild(blogMgr.blogTogglePhotosOBJ,blogMgr.blogToggleOBJ.firstChild);			
			
			Element.hide(blogMgr.blogContentID);
			Element.show(blogMgr.blogPhotosID);			
		} else {			
			
			blogMgr.blogToggleOBJ.replaceChild(blogMgr.blogToggleContentOBJ,blogMgr.blogToggleOBJ.firstChild);			
			
			Element.hide(blogMgr.blogPhotosID);
			Element.show(blogMgr.blogContentID);	
		}
		
		
	},
	
	//finds current img node in rows of thumbs where chosen img is
	findChosenImg: function() {
		allImgs = blogMgr.blogPhotosOBJ.getElementsByTagName('img');
		
		for(i = 0; i < allImgs.length; i++) {
			if(allImgs[i].src.match(/thumb_chosen.gif/)) {
				//alert('found placeholder');
				blogMgr.chosenImgOBJ = allImgs[i];	
			}
		}
	},
	
	//finds position in thumbs where img should be placed when removed from main img position
	findThumbPositionOBJ: function() {
		allImgs = blogMgr.blogPhotosOBJ.getElementsByTagName('img');
		
		for(i = 0; i < allImgs.length; i++) {
			if(allImgs[i].alt == blogMgr.blogMainPhotoOBJ.src) {
				//found spot of img in thumbs
				blogMgr.chosenImgOBJ = allImgs[i];	
			}
		}		
	},
	
	changePic: function(e) {
		var newSrc = "";
		var local_target = findTarget(e);
		
		//traverse to get to link node (Safari issue)
		local_target = linkTraverse(local_target);		
		
		if(local_target.nodeName.toLowerCase() == 'body') {
			return;	
		}
		
		//get img from childNode (strip thumb_)				
		newSrc = local_target.firstChild.src.replace(/blogs\/thumb_/,'blogs/');	
		
		//switch caption from alt tag (if caption exists)
		if(document.getElementById(blogMgr.imgCaptionID).hasChildNodes()) {
			document.getElementById(blogMgr.imgCaptionID).firstChild.nodeValue = local_target.firstChild.alt;
		}
		
		//find position for thumb
		blogMgr.findThumbPositionOBJ();
		
		//save current main img
		oldSrc = blogMgr.blogMainPhotoOBJ.src;
		
		//put placeholder thumb img in thumb's spot
		local_target.firstChild.src = blogMgr.chosenImgOBJ.src;
		
		//take src from main img spot and put it in place w/placeholder thumb img		
		blogMgr.chosenImgOBJ.src = oldSrc.replace(/images\/blogs\/(.+)/,'images/blogs/thumb_$1');
		
		//wrap link around thumb and attach event
		var container_a = document.createElement('a');
		container_a.title = local_target.title;
		container_a.href = "#";
		addEvent(container_a, 'click', blogMgr.changePic, false);	
		addEvent(container_a, 'click', cancelClick, false);	
		container_a.onclick = cancelClickSafari;		
		
		//insert container node before img node
		blogMgr.chosenImgOBJ.parentNode.insertBefore(container_a,blogMgr.chosenImgOBJ);
		
		//append/move img node to container a	
		container_a.appendChild(blogMgr.chosenImgOBJ);
		
		//swap img src into main img spot
		blogMgr.blogMainPhotoOBJ.src = newSrc;			
		
		//get new chosenImgOBJ
		blogMgr.findChosenImg();
		
		//remove anchor tag around thumb in location
		parentN = blogMgr.chosenImgOBJ.parentNode.parentNode;
		parentN.replaceChild(blogMgr.chosenImgOBJ,parentN.firstChild);
		
	},	
	
	preLoadImgs: function() {	
		
		var qs = '';						
		
		//build POST query string
		qs = 'PostID=' + blogMgr.postID;		
		
		new Ajax.Request('/preLoadImgsAJAX.cfm', {method:'post', postBody:qs, onSuccess:blogMgr.pLhandlerFunc, onFailure:blogMgr.errFunc, asynchronous:true});		
		
	},
	
	errFunc: function(t) {
		alert('Error ' + t.status + ' -- ' + t.statusText);
	},
	
	//For an XML response:	
	pLhandlerFunc: function(t) {		
		//alert(t.responseText);
		
		var xmlDoc = t.responseXML.documentElement;
		var responseBOOL;	
		var imgsARR;				
		
		//var validNodeARR = xmlDoc.getElementsByTagName('valid');
		responseBOOL = xmlDoc.getElementsByTagName('valid')[0].firstChild.nodeValue;				
		
		if(responseBOOL == "TRUE") {
			imgsARR = xmlDoc.getElementsByTagName('image');
			for(i = 0; i < imgsARR.length; i++) {
				img = new Image();	
				img.src = imgsARR[i].firstChild.nodeValue;
			}			
		}		
	},
	
	delay: function() {
		//alert('img loaded!');
		//alert('Width: ' + this.width + ', Height: ' + this.height);
		document.getElementById(slideShow.imgID).src = slideShow.newImageSRC;
    	//setTimeout("imgshow()", 3000);
	}
}

var cFlash = {
	listenID: 'commentListen_',
	closeID: 'commentClose_',
	
	flashContID: 'commentAudio_',
	audioID: 'audio_',
	commentID: '',
	
	init: function() {
		if(!document.getElementById) {
			return;	
		}	
		
		cFlash.audioLinkEvents();		
	},
	
	audioLinkEvents: function() {
		allLinksARR = document.getElementsByTagName('a');
		
		for(i=0; i< allLinksARR.length; i++) {
			currentLink = allLinksARR[i];
			
			if(currentLink.name.match(/commentListen_/)) {
				//alert(currentLink.name);
				addEvent(currentLink, 'click', cFlash.openPlayer, false);
				addEvent(currentLink, 'click', cancelClick, false);
				currentLink.onclick = cancelClickSafari;				
			} else if(currentLink.name.match(/commentClose_/)) {
				//alert(currentLink.name);
				addEvent(currentLink, 'click', cFlash.closePlayer, false);
				addEvent(currentLink, 'click', cancelClick, false);
				currentLink.onclick = cancelClickSafari;			
			}
		}
	},
	
	openPlayer: function(e) {
		var local_target = findTarget(e);
		
		//traverse to get to link node (Safari issue)
		local_target = linkTraverse(local_target);		
		
		if(local_target.nodeName.toLowerCase() == 'body') {
			return;	
		}
		
		//pageLoc = local_target.href.indexOf("page") + 5;
		lastUScore = local_target.name.lastIndexOf("_") + 1;		
		//lengthStr = (local_target.href - lastUScore);
		cFlash.commentID = local_target.name.substr(lastUScore);
		//alert(cFlash.commentID);
		Element.show(cFlash.flashContID + cFlash.commentID);
		Element.hide(document.getElementById(local_target.id).parentNode);
	},
	
	testFunc: function(commentID) {
		alert('testing: ' + commentID);	
	},
	
	closePlayer: function(commentID) {			
		Element.hide(cFlash.flashContID + commentID);
		Element.show(document.getElementById(cFlash.listenID + commentID).parentNode);
	}
	
	/*closePlayer: function(e) {
		var local_target = findTarget(e);
		
		//traverse to get to link node (Safari issue)
		local_target = linkTraverse(local_target);		
		
		if(local_target.nodeName.toLowerCase() == 'body') {
			return;	
		}
		
		//pageLoc = local_target.href.indexOf("page") + 5;
		lastUScore = local_target.name.lastIndexOf("_") + 1;		
		//lengthStr = (local_target.href - lastUScore);
		cFlash.commentID = local_target.name.substr(lastUScore);
		//alert(cFlash.commentID);
		Element.hide(cFlash.flashContID + cFlash.commentID);
	}*/
}

var cP = {
	commentsPaginationTopID: 'commentsPaginationTop',
	commentsPaginationBottomID: 'commentsPaginationBottom',
	
	commentsPaginationTopOBJ: '',
	commentsPaginationBottomOBJ: '',
	pageNumber: 1,
	
	//for link updating
	pageFormID: 'page',
	recentPostsID: 'recentPosts',
	postPaginationID: 'postsPagination',
	
	resultsID: 'commentsContent',
	resultsOBJ: '',	
	
	init: function() {	
		if(!document.getElementById(cP.resultsID)) {
			return;	
		}	
		
		/* PAGINATION ONCLICK LINKS */
		cP.addLinkEvents();	
		
	},
	
	addLinkEvents: function () {
		//alert('y');
		cP.commentsPaginationTopOBJ = document.getElementById(cP.commentsPaginationTopID);
		cP.commentsPaginationBottomOBJ = document.getElementById(cP.commentsPaginationBottomID);
		
		countTop = 0;
		countBottom = 0;
		
		//get child nodes of pagination paragraph element
		commentsTopLinkNodeARR = cP.commentsPaginationTopOBJ.childNodes;
		commentsBottomLinkNodeARR = cP.commentsPaginationBottomOBJ.childNodes;
		
		//assign top pagination event handlers
		for(i=0; i < commentsTopLinkNodeARR.length; i++) {			
			if(commentsTopLinkNodeARR[i].nodeName == 'A') {	
				//commentsTopLinkNodeARR[i].href = "#";
				addEvent(commentsTopLinkNodeARR[i], 'click', cP.grabPage, false);
				addEvent(commentsTopLinkNodeARR[i], 'click', cancelClick, false);
				commentsTopLinkNodeARR[i].onclick = cancelClickSafari;
				countTop++;
			}			
		}
		
		//assign bottom pagination event handlers
		for(j=0; j < commentsBottomLinkNodeARR.length; j++) {			
			if(commentsBottomLinkNodeARR[j].nodeName == 'A') {
				//commentsBottomLinkNodeARR[j].href = "#";
				addEvent(commentsBottomLinkNodeARR[j], 'click', cP.grabPage, false);
				addEvent(commentsBottomLinkNodeARR[j], 'click', cancelClick, false);
				commentsBottomLinkNodeARR[j].onclick = cancelClickSafari;
				countBottom++;
			}			
		}
		
		//alert('Top Links: ' + countTop + '. Bottom Link: ' + countBottom);
		
	},
	
	grabPage: function(e) {
		var local_target = findTarget(e);
		
		//travers to get to link node (Safari issue)
		local_target = linkTraverse(local_target);		
		
		if(local_target.nodeName.toLowerCase() == 'body') {
			return;	
		} else {
			pageLoc = local_target.href.indexOf("page") + 5;
			lastSlash = local_target.href.lastIndexOf("/");		
			lengthStr = (lastSlash - pageLoc);
			cP.pageNumber = local_target.href.substr(pageLoc, lengthStr);
			//alert('Length: ' + lengthStr + ', Pageloc: ' + pageLoc + ', lastSlash: ' + lastSlash);
			//alert(cP.pageNumber);
			cP.searchPostings();			
		}
	},
	
	searchPostings: function(e) {	
		var qs = '';	
		
		//build POST string
		qs = 'page=' + cP.pageNumber;
		
		new Ajax.Updater(cP.resultsID,'/commentsAJAX.cfm',{method:'post', postBody:qs,asynchronous:true, onSuccess: setTimeout(cP.reInit,450), evalScripts: true});		
		//alert(qs);			
	},
	
	reInit: function() {		
		cP.addLinkEvents();	
		cP.updateLinks();	
		cFlash.audioLinkEvents();
	},
	
	updateLinks: function() {
		
		//change value in comment posting form 'page'
		if(document.getElementById(cP.pageFormID))
			document.getElementById(cP.pageFormID).value = cP.pageNumber;
		
		//append all links in Recent Posts		
		rPLinksARR = document.getElementById(cP.recentPostsID).getElementsByTagName('a');
		
		for(i = 0; i < rPLinksARR.length; i++) {
			cLink = rPLinksARR[i];
			
			//page not exist
			if(!cLink.href.match(/page\/([0-9]+)/)) {			
				cLink.href = cLink.href + 'page/' + cP.pageNumber + '/';				
		   	} 
			//page exist? replace number
			else {				
				cLink.href = cLink.href.replace(/page\/([0-9]+\/?)/,'page/' + cP.pageNumber + '/');
			}
		}
		
		//append all links in post pagination
		pPLinksARR = document.getElementById(cP.postPaginationID).getElementsByTagName('a');		
		
		for(i = 0; i < pPLinksARR.length; i++) {
			cLink = pPLinksARR[i];
			
			//page not exist
			if(!cLink.href.match(/page\/([0-9]+)/)) {				
				cLink.href = cLink.href + 'page/' + cP.pageNumber + '/';				
		   	} 
			//page exist? replace number
			else {				
				cLink.href = cLink.href.replace(/page\/([0-9]+\/?)/,'page/' + cP.pageNumber + '/');				
			}
		}		
	}
}

<!-- CHART AJAX -->
var chartSwap = {
	paginationID: 'swap',	
	currentDate: '',
	
	resultsID: 'chart_wrap',
	resultsOBJ: '',	
	
	init: function() {	
		if(!document.getElementById || !document.getElementsByTagName || !document.getElementById(chartSwap.resultsID)) {return;}				
		/* PAGINATION ONCLICK LINKS */
		chartSwap.addLinkEvents();		
	},
	
	addLinkEvents: function (t) {					
		count = 0;
		paginationARR = $(chartSwap.paginationID).getElementsByTagName('select');
		for(i=0; i < paginationARR.length; i++) {									
			addEvent(paginationARR[i], 'change', chartSwap.changeList, false);
			addEvent(paginationARR[i], 'change', cancelClick, false);
			paginationARR[i].oncchange = cancelClickSafari;
			paginationARR[i].href = "#";
		}				
	},
	
	changeList: function(e) {
		var local_target = findTarget(e);
		
		//traverse to get to link node (Safari issue)
		local_target = linkTraverse(local_target);		
		
		var qs = '';			
			
		//build POST string
		var tmp = document.getElementById('brands');
		//qs = 'date=' + local_target.name + '&lastDate=' + chartSwap.getStickDate() + '&action=' + local_target.id;
		qs = 'brands=' + tmp.options[tmp.selectedIndex].value;
		
		if(window.location.href.match(/\?test=Y/)) {
			alert(qs);	
		}
		
		//new Ajax.Updater(chartSwap.resultsID,'/inc/inc_calendar_dyn.cfm',{method:'post', postBody:qs,asynchronous:true, onSuccess: setTimeout(chartSwap.reInit,600)});		
		new Ajax.Updater(chartSwap.resultsID,'/inc/charts/vs.cfm',{method:'post', postBody:qs,asynchronous:true, evalScripts: true, onSuccess: setTimeout(chartSwap.reInit,600)});		
	},
	
	dumpResult: function(t) {
		if(window.location.href.match(/\?test=Y/)) {
			//alert("failure1: "+t.responseText);	
		}
		//alert("failure2: "+t.responseText);	
	},
		
	reInit: function(t) {			
		chartSwap.addLinkEvents();	
	}	
}

addEvent(window, 'load', chartSwap.init, false);
addEvent(window, 'load', cP.init, false);
addEvent(window, 'load', cFlash.init, false);
addEvent(window, 'load', blogMgr.init, false);