var questions = document.getElementsByTagName('dt');
var answers = document.getElementsByTagName('dd');

function displayToggle(){

     toggleAllOff(); 

     for (var m=0; m<questions.length; m++) {
         questions[m].onclick=function() {
             var qnext = this.nextSibling;
             while(qnext.nodeType != 1) qnext=qnext.nextSibling; 
             qnext.className=((qnext.className=="hide") ? "show" : "hide");
          }
      }
}

window.onload=function() {
     displayToggle();
}



function toggleAllOff(){
     for (var g = 0; g < answers.length; g++) {
        answers[g].className = 'hide';
     }
}
        
function toggleAllOn(){
     for (var y = 0; y < answers.length; y++) {
        answers[y].className = 'show';
     }
}

/*	
	This function finds all links to the Attractions Ticketing website on any page and applies an onClick function 
	to them that registers the hit to the 3rd party website in Google Analytics, allowing Moody to track ROI across
	multiple sites. [ASR - 3/31/2010]
*/
$(document).ready(function(){
	var links = $("a[href*='gatewayticketing.com']")
	links.attr('target','_blank')
	links.attr('onClick','window.open(pageTracker._getLinkerUrl(this.href),this.target); return false;')
 });



var buffer = false;
var loaded = false;
var aG = [];
function loadGallery(){
aG.push({
	totalW: totalW,
	totalH: totalH,
	path: path
});
$(function(){
	if(loaded) return false;
	loaded = true;
	for(var p in aG){
		var gSlideshow = $('div.slideshow:eq('+p+')')
		gSlideshow.data('mP',p);
		gSlideshow.height(aG[p].totalH);
		$.ajax({
			url: aG[p].path+'myimages.xml',
			dataType: 'xml',
			context: gSlideshow,
			success: function(data){
				var slideshow = $(this);
				var mP = slideshow.data('mP');
				var dataArray = [];
				slideshow.after('<table cellpadding=0 cellspacing=0 class="gallerylinks copy" width="100%" height="19" bgcolor="000000" style="color:#fff;font-family:arial;text-transform:uppercase;font-size:10px;line-height:19px;cursor:pointer;font-weight:bold;"><tbody><tr></tr></tbody></table>');
				var gallerylinks = slideshow.next('.gallerylinks').find('tr');
				$(data).find('data').each(function(){
					dataArray.push({img:$(this).attr('img'),imgcaption:$(this).attr('imgcaption'),imglink:$(this).attr('imglink')});
				});
				slideshow.append(
					'<img index="'+0+'" sr'+'c="'+aG[mP].path
					+dataArray[0].img
					+'" imagelabel="'
					+dataArray[0].imgcaption
					+'" imglink="'
					+dataArray[0].imglink
					+'" style="position: absolute;display: none; ">'
				);
				if(dataArray[0].imgcaption.replace(/ /g,'')!='')
				gallerylinks.append(
					'<td align="center" index="'+0+'" imglink="'
					+dataArray[0].imglink
					+'">'
					+dataArray[0].imgcaption
					+'</td>'
				);
				gallerylinks.find('td[index='+0+']').css({'color':'#ff66cc'});
				var loaded = false;
				/* As soon as the first image is loaded, we may begin */
				slideshow.find('img:first').load(function(){
					if(loaded) return false;
					loaded = true;
					for(var i=1;i<dataArray.length;i++){
						slideshow.append(
							'<img index="'+i+'" sr'+'c="'+aG[mP].path
							+dataArray[i].img
							+'" imagelabel="'
							+dataArray[i].imgcaption
							+'" imglink="'
							+dataArray[i].imglink
							+'" style="position: absolute;display: none; ">'
						);
						if(dataArray[i].imgcaption.replace(/ /g,'')!='')
						gallerylinks.append(
							'<td align="center" index="'+i+'" imglink="'
							+dataArray[i].imglink
							+'">'
							+dataArray[i].imgcaption
							+'</td>'
						);
					}
					
					gallerylinks.find('td').click(function(){
						var $t = $(this);
						if($t.attr('imglink').length!=0)
						document.location.href=$t.attr('imglink');
					});
					/* Variables */
					var transitionTime = 5000;
					
					/* Show First Image / Gallery / Hide Play Button */
					var n = slideshow.find('img:first');
					$('.descriptionText').html(n.attr('imageLabel').replace(/ /g,'')==''?' ':n.attr('imageLabel'));
					n.show();
					slideshow.css({'cursor':'auto'}).unbind();
					if(n.attr('imglink')){
						slideshow.css({'cursor':'pointer'}).click(function(){
							document.location.href = n.attr('imglink');
						});
					}
					if(buffer){
						$('.buffer').css({
							'height':n.height(),
							'width':n.width(),
							'margin-left' : (aG[mP].totalW-n.width())/2,
							'margin-top' : (aG[mP].totalH-n.height())/2
						}).fadeOut(1000,function(){
							n.css('z-index','5');
							$('.buffer').show();
						});
						n.css({
							position : 'absolute',
							'margin-left' : (aG[mP].totalW-$(this).width())/2,
							'margin-top' : (aG[mP].totalH-$(this).height())/2
						});
					}
					
					$('.play').hide();
					var animating = false;
					var nextSlide = function(next){
						/* Make sure we're not already in transition */
						if(animating) return false;
						animating = true;
						var f = slideshow.find('img:visible');
						if(next){
							var n = slideshow.find('img:visible').prev('img');
							if(n.length==0) n = slideshow.find('img:last');
						}else{
							var n = slideshow.find('img:visible').next('img');
							if(n.length==0) n = slideshow.find('img:first');
						}
						/* Make sure the next image is loaded before proceeding (don't worry, the loop event will re-fire) */
						if(!n[0].complete) return false;
						

						slideshow.css({'cursor':'auto'}).unbind();
						if(n.attr('imglink')){
							slideshow.css({'cursor':'pointer'}).click(function(){
								document.location.href = n.attr('imglink');
							});
						}
								
						/* Buffered Animation */
						if(buffer){
							$('.buffer').css({
								'z-index':'4',
								'height':f.height(),
								'width':f.width(),
								'margin-left' : (aG[mP].totalW-f.width())/2,
								'margin-top' : (aG[mP].totalH-f.height())/2
							});
							f.css('z-index','5').fadeOut(600,function(){
								$('.buffer').animate({
									'height':n.height(),
									'width':n.width(),
									'margin-left' : (aG[mP].totalW-n.width())/2,
									'margin-top' : (aG[mP].totalH-n.height())/2
								},300,'swing',function(){
									n.css({
										position : 'absolute',
										'margin-left' : (aG[mP].totalW-$(this).width())/2,
										'margin-top' : (aG[mP].totalH-$(this).height())/2,
										'z-index' : 3
									}).show();
									animating=false;
									/* Alter Text */
									$('.descriptionText').html(n.attr('imageLabel').replace(/ /g,'')==''?' ':n.attr('imageLabel'));
									
									$('.buffer').fadeOut(1000,function(){
										n.css('z-index','5');
										$('.buffer').show();
									});
								});
							});
						}else{
							/* Non Buffered Animation */
							f.css('z-index',5);
							n.css('z-index',4).show();
							var i = n.attr('index');
							gallerylinks.find('td').css({'color':'#ffffff'});
							gallerylinks.find('td[index='+i+']').css({'color':'#ff66cc'});
							f.fadeOut(1500,function(){
								animating=false;
							},'swing');
						}
					};
					var looping=true;
					var t = setTimeout(function(){beginLoop();},transitionTime);
					var beginLoop = function(){
						nextSlide();
						t = setTimeout(function(){beginLoop();},transitionTime- -1500);
					};
					var play = function(){
						clearTimeout(t);
						$('.play').hide();
						$('.pause').show();
						beginLoop();
					};
					var pause = function(){
						clearTimeout(t);
						$('.play').show();
						$('.pause').hide();
					};
					$('.play').click(play);
					$('.pause').click(pause);
					$('.back').click(function(){nextSlide(true);play();});
					$('.next').click(function(){nextSlide();play();});
				/* If the image is loaded from cache, the load event doesn't fire, this checks if it's already loaded and the load event missed */
				}).each(function(){
					if(this.complete) $(this).trigger("load");
				});
			}
		});
	}
});
}

$(document).ready(function(){

    $.getJSON(
        'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=moody_gardens&count=5&callback=?',
        function(e){
        var i = 0;
            var $t = $('<div style="height:192px;width:144px;overflow:hidden;"></div>');
            $('.twitterContent').append($t);


    var relative_time = function (time_value) {
      var parsed_date = Date.parse(time_value.replace(/^([a-z]{3})( [a-z]{3} \d\d?)(.*)( \d{4})$/i, '$1,$2$4$3'));
      var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
      var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
      var r = '';
      if (delta < 60) {
	r = delta + ' seconds ago';
      } else if(delta < 120) {
	r = 'a minute ago';
      } else if(delta < (45*60)) {
	r = (parseInt(delta / 60, 10)).toString() + ' minutes ago';
      } else if(delta < (2*60*60)) {
	r = 'an hour ago';
      } else if(delta < (24*60*60)) {
	r = '' + (parseInt(delta / 3600, 10)).toString() + ' hours ago';
      } else if(delta < (48*60*60)) {
	r = 'a day ago';
      } else {
	r = (parseInt(delta / 86400, 10)).toString() + ' days ago';
      }
      return 'about ' + r;
    }

            var add = function(){
                var $c = $('<div style="font-size:10px;font-color:#333333;border-bottom:1px solid #dff2f6;margin:0 6px;padding: 5px 0;display:none;">'+e[i].text.replace(/((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi,'<a target="_blank" href="$1" style="font-size:10px;">$1</a>').replace(/(^|[^\w]+)\@([a-zA-Z0-9_]{1,15})/g, function(m, m1, m2) {
            return m1 + '@<a href="http://twitter.com/' + m2 + '" target="_blank" style="color:#2d647f;font-size:10px;">' + m2 + '</a>';
          })+'<br><span style="color:#999999;">'+relative_time(e[i].created_at)+'</span></div>');
                i++;
                if(i>=e.length)i=0;
                $t.prepend($c);
                $c.fadeIn();
                setTimeout(add,5000+Math.ceil(Math.random()*5000));
            };
            add();
        }
    );

});
