//globals
var g_bannerImages = new Array();
// the index of the currently displayed banner
var g_currentIndex = 0;
// boolean variable that indicates whether the carousel is paused or not
var g_pause = false;
var g_CarouselTimer;
//var g_BannerInterval = 6000;
//for partner's banners
var g_partnerId;
/*init*/
window.onload = preloadCarousel;
var loadingAnimationImage = null;
var preloadImg = new Image();
var browserType = "";
var g_carouselDelay = 4500;
var g_pixelsize=133;
var g_banner_height=168;
var g_banner_width=350;
var g_carousel_left_limit=0;
var clip = document.getElementById('thumbnailBarThumbs');
var g_scroll_amt = 350;
var g_cant_autoscroll_anymore = false;
var g_CarouselTimer;
//returns the css style an element
function getStyle(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}
function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;
	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}
//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style;
	//var object = thumb.style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}
//scrolls the thumbs to the Right
function clickScrollRight(){
  clearInterval(g_CarouselTimer);
  scrollRight(0);
  //done scrolling re-enable the timer
  g_CarouselTimer = setInterval('autoScrollCarousel()',g_carouselDelay);
}
function clickScrollLeft(){
  clearInterval(g_CarouselTimer);
  scrollBack(0);
  //done scrolling re-enable the timer
  g_CarouselTimer = setInterval('autoScrollCarousel()',g_carouselDelay);
}
function scrollRight(pixels) {
  //makes sure it doesn't scroll in the middle
  g_carousel_left_limit = parseInt(document.getElementById('carouselContainerDiv').style.width)-(g_banner_width*(g_bannerImages.length));
  if (parseInt(clip.style.left) > (g_carousel_left_limit)) {
  	if (pixels <= (g_scroll_amt)) {
		clip.style.left = (parseInt(clip.style.left)-15)+'px';
		setTimeout("scrollRight("+(pixels+15)+")", 6);
		//setTimeout(scrollRight('pixels'),2); // call doMove in 20msec
		//document.getElementById('statusDiv').innerHTML = clip.style.left;
	}
	
  }else{
	clip.style.left=g_carousel_left_limit + 'px';
	//document.getElementById('statusDiv').innerHTML = clip.style.left;
	g_cant_autoscroll_anymore=true;
	}
}
function scrollBack(pixels) {
  if (parseInt(clip.style.left) < (0)) {
  	if (pixels <= g_scroll_amt) {
		clip.style.left = (parseInt(clip.style.left)+15)+'px';
		setTimeout("scrollBack("+(pixels+15)+")", 6);
		//document.getElementById('statusDiv').innerHTML = clip.style.left;
	}
  }else{
	//alert('done');
	clip.style.left=0;
	g_cant_autoscroll_anymore=false;
  }
}
function highlightThumb(thumb) {
	changeOpac(80,thumb.id);
	var arg = 'changeOpac(100,"'+thumb.id+'")';
	setTimeout(arg,500);
}
function unhighlightThumb(thumb) {
}
function unhighlightAllCarouselThumbs(thumb) {
}
function initCarousel() {
	resize_carousel();
	//loadingAnimationImage = new Image();
	//loadingAnimationImage.src = "flower_load.gif";
	
	if (navigator.userAgent.search("MSIE") >= 0) {
		browserType = 'ie';
	}else{
		browserType = 'ff';
	}
	//dynamically add images using DOM
	var i=0;
	var thumbnailBar = document.getElementById('thumbnailBarThumbs');
	thumbnailBar.appendChild(document.createTextNode(' '));
	
	for (i=1;i<=g_bannerImages.length;i++)
	{
		//creat the link
		var anchor = document.createElement('a');
		if(g_bannerImages[i-1].target == '') {
			anchor.setAttribute('href','eventdetails.php?id='+g_bannerImages[i-1].eventid);
		}else {
			anchor.setAttribute('href',g_bannerImages[i-1].target);
		}
		anchor.setAttribute('className','toggleopacity');
		anchor.setAttribute('class','toggleopacity');
	
		//create the banner img	
		var img = document.createElement('img');
	
		img.setAttribute('src', ""+g_bannerImages[i-1].displayinfo);
		img.setAttribute('id', 'pic' + i);
		img.setAttribute('title',g_bannerImages[i-1].tagline);
		img.style.position = 'relative';
		img.style.cursor = 'pointer';
		if (i==1){
			img.style.borderLeft = '1px solid black';
		}
		
		if (browserType!='ie')
			img.style.borderTop = '1px solid black';
		img.style.borderBottom = '1px solid black';
		if (i==g_bannerImages.length){
			img.style.borderRight = '1px solid black';
		}
		
		
		
		img.style.height = g_banner_height+'px';
		img.style.width = g_banner_width+'px';
		img.style.display='block';
		img.style.textDecoration='none';
		//img.style.top = '1%';
		var eventTextTable = document.createElement('table');
		var eventTextTbody = document.createElement('tbody');
		var eventTextFirstTR = document.createElement('tr');
		var eventTextSecondTR = document.createElement('tr');
		var eventTextFirstTD = document.createElement('td');
		var eventTextSecondTD = document.createElement('td');
		
		//add image to first td
		anchor.appendChild(img);
		anchor.style.marginBottom='-4px';
		eventTextFirstTD.appendChild(anchor);
		
		//create body
		eventTextTable.appendChild(eventTextTbody);
		eventTextTbody.appendChild(eventTextFirstTR);
		//add first td to first tr
		eventTextFirstTR.appendChild(eventTextFirstTD);
		
		//add the second row
		eventTextTbody.appendChild(eventTextSecondTR);
		//second cell
		eventTextSecondTD.style.fontSize='12px';
		eventTextSecondTD.style.textAlign='center';
		//eventTextSecondTD.style.background='black';
		eventTextSecondTD.style.color='white';
		
		//create the text underneath the banner in the carousel
		var eventText = document.createElement('div');
		var link = document.createElement("a");
		link.style.color='white';
		link.style.textDecoration='none';
		link.setAttribute('href','eventdetails.php?id='+g_bannerImages[i-1].eventid);
		if (g_bannerImages[i-1].tagline != null && g_bannerImages[i-1].tagline != '') {
			link.appendChild(document.createTextNode(g_bannerImages[i-1].tagline));
		}else {
			link.appendChild(document.createTextNode(g_bannerImages[i-1].eventname));
		}
		eventText.appendChild(link);
		eventText.style.position='relative';
		eventText.style.top='-17px';
		eventText.style.background='black';
		eventText.style.opacity = 0.75;
		eventText.style.filter = 'alpha(opacity=' + 75 + ')';
		eventText.style.width='100%';
		
		eventTextSecondTD.appendChild(eventText);
		eventTextSecondTR.appendChild(eventTextSecondTD);
		
		//override stylesheet
		if (browserType == 'ie') {
			eventTextTable.style.display='inline';
		}
		eventTextTable.setAttribute('className','featuredEventTextTable');
		eventTextTable.setAttribute('class','featuredEventTextTable');
		eventTextTable.style.borderSpacing='0px';
		//eventTextTable.setAttribute('border','1');
		if(browserType=='ie') {
			eventTextTable.style.marginLeft='-6px';
			eventTextTable.style.marginBottom='5px';
			}
		else
			eventTextTable.style.marginLeft='-2px';
		thumbnailBar.appendChild(eventTextTable);
		
	}
	thumbnailBar.style.height='164px';
	if (browserType=='ie') {
		thumbnailBar.style.marginTop='-2px';
	}
	//start the timer
	
	//show the carousel
	document.getElementById('carouselContainerTable').style.visibility='visible';
	g_CarouselTimer = setInterval('autoScrollCarousel()',g_carouselDelay);
}
function autoScrollCarousel() {
	scrollRight(0);
	if(g_cant_autoscroll_anymore) {
		//rewind
		clip.style.left=0+'px';
		g_cant_autoscroll_anymore=false;
	}
}
function resize_carousel() {
	//alert(document.body.clientWidth);
	if (document.body.clientWidth > 800) {
		//alert(document.body.clientWidth);
		var delta = 90;
		if (document.body.clientWidth <= 1280) {
			delta=77;
		}
		
		if (browserType == 'ie') {
			delta +=5;
		}
		
		document.getElementById('carouselContainerDiv').style.width=document.body.clientWidth-delta+ 'px';
		
	}else { 
		//less than 1000
		document.getElementById('carouselContainerDiv').style.width=document.body.clientWidth-delta + 'px';
	}
}
/*
* Function that preloads the banner carousel images
*/
function preloadCarousel() {

//adjust window size
self.moveTo(0,0);self.resizeTo(screen.availWidth,screen.availHeight);
	
var url='browse.php?sortby=Featured';
if (g_partnerId!=null)
	url+='&partner='+g_partnerId;
	
try {
		var req = new XMLHttpRequest(); /* e.g. Firefox */
	} catch(e) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
			/* some versions IE */
		} catch (e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
				/* some versions IE */
			} catch (E) {
			req = false;
			}
		}
	}
	req.open("POST",url,true);
	
	//the ajax target will return an array of event objects
	req.onreadystatechange = function() {
		if(req.readyState == 4) {
			if(req.status == 200) {
				//evaluate the array object
				var JSONarray=eval(req.responseText);
				g_bannerImages = JSONarray;
				
				//show the images
				
				//preloading
				for (i = 0; i < g_bannerImages.length; i++) {
					var img = new Image();
					img.src = g_bannerImages[i].displayinfo;
				}
				
				if (g_bannerImages.length > 0){
					//randomize the array
					g_bannerImages.sort(function() {return 0.5 - Math.random()})
					//buffer the ajax a little
					setTimeout("initCarousel()",320);
					
					
				}else {
					
				}
				
			}
		}
	};
	req.send(null);
	
}
/*
* Function that pauses/resumes the banner carousel
*/
function pause(pauselink) {
if (g_pause) {
	g_pause = !g_pause; //set to play
	pauselink.innerHTML = 'Pause';
	setTimeout('swapImages('+g_currentIndex+')',g_BannerInterval);
}else {
	g_pause = !g_pause; //set to pause
	pauselink.innerHTML = 'Play';
}
}
/*function initCarousel(partnerId) {
	g_partnerId = partnerId;
	preload();
}*/
//should only be called after ajax response
function swapImages(index,manual) {
	//if paused, don't advance to the next ad
	if (g_pause && !manual) {
		g_currentIndex = index;
		return;
	}
	
	
	//prepare next and previous links for navigation controls below
	var next = index+1;
	var prev = index-1;
	if (index > g_bannerImages.length-1) {
		index = 0;
		next = 1;
		prev = g_bannerImages.length-1;
	}else if (index == 0) {
		prev = g_bannerImages.length-1;
	}
	
	//insert the featured Ad Banner and Link	
	var cell =document.getElementById('bannercell');
	var bann_link =document.getElementById('featuredAdLink');
	bann_link.href='eventdetails.php?id='+g_bannerImages[index].eventid;
	//display info holds url
	var bann =document.getElementById('featuredAdBanner');
	bann.src=g_bannerImages[index].displayinfo;
	bann.style.border='1px solid #69A4CC';
	bann.style.visibility='visible';
	cell.style.width='533px';
	cell.style.height='240px';
	
	//insert Caption text and navigation controls	
	var text = document.getElementById('text');
	text.style.background='black';
	text.style.backgroundRepeat='repeat-x';
	if (g_bannerImages[index].fullname.length > 45){
		text.innerHTML = g_bannerImages[index].fullname.substring(0,45) + "...";
	}else {
		text.innerHTML = g_bannerImages[index].fullname;
	}
	
	
	
	var pausetext ='Pause';
	//handles case where carousel is paused and the user is manually swapping images
	if (g_pause && manual) {
		pausetext ='Play';
	}
	
	
	var featuredAdControls = document.getElementById('featuredAdControls');
	featuredAdControls.style.background='black';
	featuredAdControls.style.backgroundRepeat='repeat-x';
	featuredAdControls.style.textAlign='right';
	featuredAdControls.innerHTML = "<a style='color:white' href='#' id='pauselink' onclick='pause(this);return false'>"+pausetext +"</a>"
		+ "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + "<a style='color:white' href='#' onclick='clearTimeout(g_CarouselTimer);swapImages(" + prev + ",true);return false;'>&laquo;</a> "
		+ (index+1) + " of " + g_bannerImages.length
		+ " <a style='color:white' href='#' onclick='clearTimeout(g_CarouselTimer);swapImages(" + next + ",true);return false;'>&raquo;</a> ";
		
	//advance automatically to the next slide
	index++;
	//store the timer globally
	g_CarouselTimer = setTimeout('swapImages('+index+')',g_BannerInterval);
}
//var g_images = new Array("_AU10803.jpg","_AU11032.jpg","_AU13629.jpg","_AU11022.jpg","n774105023_4350478_9859.jpg");
/*function galleryObj(img_arr,galleryid,eventid) {
	this.images=img_arr;
	this.id=galleryid;
	this.eventid=eventid;
}
var g_collage_images = new Array("1.jpg","2.jpg","3.jpg","4.jpg","5.jpg","7.jpg","6.jpg","8.jpg","9.jpg","10.jpg","11.jpg","12.jpg");
var gal = new galleryObj(g_collage_images,35,262);*/
function initDatePicker(){
	//disable event loading
	g_dateBox = "startDate";
	toggleEventLoading(false);
	placeCalendar();
	//calendar that is 200 wide, made up of boxes 25x25
	resize_divs(15,175,15);
	
	//set end date to be the same as
	document.getElementById('endDate').value = 	document.getElementById('startDate').value; 
}
function switchDateChoice() {
	deselectCurrentDay();
	if(g_dateBox == 'startDate') {
		g_dateBox = 'endDate';
	}else{
		g_dateBox = 'startDate';
	}
}

