// JavaScript Document
var map = null;

var headline_count;
var headline_interval;
var old_headline = 0;
var current_headline = 0;

var image_count;
var current_image = 0;
var image_interval;
var old_image = 0;

var c = 0;
var dom_interval;

$(document).ready(function()
{

	
	headline_count = $("div.headlines").size();
	$("div.headlines:eq(" + current_headline + ")").show();
	headline_interval = setInterval(headline_fade, 7000);
	
	image_count = $("div.gallery_image").size();
	$("div.gallery_image:eq(" + current_image + ")").css('top', '0px');
	image_interval = setInterval(image_rotate, 7000);
	
	//jCarousel Plugin  
    $('#carousel').jcarousel({  
        horizontal: true, //display vertical carousel  
        scroll: 3,  //auto scroll  
        auto: 2,    //the speed of scrolling  
        wrap: 'last',   //go back to top when reach last item
		btnNext: ".default .next",
		btnPrev: ".default .prev",
        initCallback: mycarousel_initCallback   //extra called back function  
    });  
  
    //Front page Carousel - Initial Setup  
    //set all the item to full opacity  
    $('div#slideshow-carousel a img').css({'opacity': '1.0','border':'0px'});  
      
    //readjust the first item to 50% opacity  
    $('div#slideshow-carousel a img:first').css({'opacity': '1.0'});
}); 
  
  
//Carousel Tweaking  
function mycarousel_initCallback(carousel) {  
      
    // Pause autoscrolling if the user moves with the cursor over the clip.  
    // resume otherwise  
    carousel.clip.hover(function() {  
        carousel.stopAuto();  
    }, function() {  
        carousel.startAuto();  
    });  
}  

function headline_fade()
{
	current_headline = (old_headline + 1) % headline_count; 
	$("div.headlines:eq(" + old_headline + ")").fadeOut('fast', function() {
		$("div.headlines:eq(" + current_headline + ")").fadeIn('fast');
	});
	old_headline = current_headline;
}

function headline_rotate()
{
  current_headline = (old_headline + 1) % headline_count; 
  $("div.headlines:eq(" + old_headline + ")").animate({top: -280},"slow", function() {
    $(this).css('top','280px');
    });
  $("div.headlines:eq(" + current_headline + ")").show().animate({top: 0},"slow");  
  old_headline = current_headline;
}

function image_rotate()
{
  current_image = (old_image + 1) % image_count; 
  $("div.gallery_image:eq(" + old_image + ")").animate({top: -280},"slow", function() {
    $(this).css('top','280px');
    });
  $("div.gallery_image:eq(" + current_image + ")").show().animate({top: 0},"slow");  
  old_image = current_image;
}


function initialise(lat, lng, zoom, north, south, east, west)
{
	if (GBrowserIsCompatible())
	{
		map = new GMap2(document.getElementById("map_canvas"));
		
		if (lat != "" && lng != "")
		{
			// Calculate proper zoom level
			var pointSW = new GLatLng(south, west);
			var pointNE = new GLatLng(north, east);
			var bounds = new GLatLngBounds(pointSW, pointNE);
			var zoom = map.getBoundsZoomLevel(bounds);
			
			// Display map
			map.setCenter(new GLatLng(lat, lng), zoom);
			map.setUIToDefault();

			// Create marker
			var point = new GPoint(lng, lat);
			var marker = new GMarker(point);
			map.addOverlay(marker);
		}
		else
		{
			// Display map
			map.setCenter(new GLatLng(0, 0), 0);
			map.setUIToDefault();
		}
	}
}

function changeTwitter()
{
	toggle("change_twitter_settings", "slide", focusTwitterSettings);
}

function focusTwitterSettings()
{
	var e = document.getElementById("change_twitter_settings");
	
	if (e.style.display != "none")
	{
		document.twitterForm.twitterUsername.select();
		document.twitterForm.twitterUsername.focus();
	}
}

function changeMap()
{
	toggle("change_map_settings", "slide");
}

function changeSnapshot()
{
	var div = document.getElementById('snapshot_outer');
	var visible = (div.style.display == "none" ? false : true);
	var expandedField = document.getElementById("snapshotExpanded");
	
	toggle('snapshot_outer', 'slide');
	
	if (visible)
	{
		expandedField.value = "0";
	}
	else
	{
		expandedField.value = "1";
	}
}

function changeAbout()
{
	var div = document.getElementById('about_outer');
	var visible = (div.style.display == "none" ? false : true);
	var expandedField = document.getElementById("aboutExpanded");
	
	toggle('about_outer', 'slide');
	
	if (visible)
	{
		expandedField.value = "0";
	}
	else
	{
		expandedField.value = "1";
	}
}

function changeEssay()
{
	var div = document.getElementById('essay');
	var visible = (div.style.display == "none" ? false : true);
	var expandedField = document.getElementById("essayExpanded");
	
	toggle('essay', 'slide');
	
	if (visible)
	{
		expandedField.value = "0";
	}
	else
	{
		expandedField.value = "1";
	}
}

function showAboutHeader()
{
	var div = document.getElementById('editAboutHeader');
	var visible = (div.style.display == "none" ? false : true);
	
	if (visible)
	{
		div.style.display = "none";
	}
	else
	{
		div.style.display = "block";
	}
}

function showBox1Header()
{
	var div = document.getElementById('editBox1Header');
	var visible = (div.style.display == "none" ? false : true);

	
	if (visible)
	{
		div.style.display = "none";
	}
	else
	{
		div.style.display = "block";
	}
}


function changeText()
{
	var div = document.getElementById('changeText');
	var visible = (div.style.display == "none" ? false : true);

	if (visible)
	{
		div.style.display = "none";
	}
	else
	{
		div.style.display = "block";
	}
}

function changeBox1()
{
	var div = document.getElementById('box1_outer');
	var visible = (div.style.display == "none" ? false : true);
	var expandedField = document.getElementById("box1Expanded");
	
	toggle('box1_outer', 'slide');
	
	if (visible)
	{
		$('#member_map').css('visibility', 'visible');
		expandedField.value = "0";
	}
	else
	{
		$('#member_map').css('visibility', 'hidden');
		expandedField.value = "1";
	}
}

function changeBox2()
{
	var div = document.getElementById('box2_outer');
	var visible = (div.style.display == "none" ? false : true);
	var expandedField = document.getElementById("box2Expanded");
	
	toggle('box2_outer', 'slide');
	
	if (visible)
	{
		expandedField.value = "0";
	}
	else
	{
		expandedField.value = "1";
	}
}

function changeDescription()
{
	var div = document.getElementById('description');
	var visible = (div.style.display == "none" ? false : true);
	var expandedField = document.getElementById("descriptionExpanded");
	
	toggle('description', 'slide');
	
	if (visible)
	{
		expandedField.value = "0";
	}
	else
	{
		expandedField.value = "1";
	}
}


function changeLogoPicture()
{
	var div = document.getElementById('edit_logo_picture');
	var visible = (div.style.display == "none" ? false : true);
	var expandedField = document.getElementById("logo_picture_expanded");

	toggle('edit_logo_picture', 'slide');
	
	if (visible)
	{
		expandedField.value = "0";
	}
	else
	{
		expandedField.value = "1";
	}
}

function changeBubblePicture()
{
	var div = document.getElementById('edit_bubble_picture');
	var visible = (div.style.display == "none" ? false : true);
	var expandedField = document.getElementById("bubble_picture_expanded");

	toggle('edit_bubble_picture', 'slide');
	
	if (visible)
	{
		expandedField.value = "0";
	}
	else
	{
		expandedField.value = "1";
	}
}


function funFactsAdd(question)
{
	var div = document.getElementById(question);
	var valueField = document.getElementById(question + "Value");
	var visible = (div.style.display == "none" ? false : true);
	id1 = document.getElementById('funFactId').value;
    
	//toggle(question, 'slide');
	show(question, 'slide');
	if (valueField.value == 1)
	{
		questionClicked(question, 1);
	}
	
	var expandedField = document.getElementById(question + "Expanded");
	document.getElementById('editHiddenImage').style.display = "none";
	
	if (visible)
	{
		if(id1 != document.getElementById('funFactId').value)
			expandedField.value = "0";
		else
			expandedField.value = "1";
	
	}
	else
	{
		expandedField.value = "1";
	}
	
}
function gAddImg()
{
	document.forms['addNewImg'].submit();
} 


function funFactsEdit(id)
{
	var called = document.getElementById('call').value;
	//funFactsAdd('funFact');
	document.getElementById('caption').value ="";
	if(called !="")
	{
		hide('funFact','slide');
		//hide('funFact','slide');
		called ="";
	}
	else
		show('funFact','slide');
		
	document.getElementById('editHiddenImage').style.display = "inline";
	document.getElementById('funFactCurrentImage').src=document.getElementById('editImage' + id).src;
	document.getElementById('action').value='updateFunFact';
	document.getElementById('funFactId').value=id;
	document.getElementById('caption').value = document.getElementById('editCaption' + id).innerHTML;
	called='true';
		
}

function funFactsDelete(id)
{
	document.getElementById('action').value='deleteFunFact';
	document.getElementById('funFactId').value=id;
	document.forms['funFactForm'].submit();
}

function galleryImageDelete(id)
{
	document.getElementById('galleryAction').value='deletegalleryImage';
	document.getElementById('galleryId').value=id;
	document.forms['galleryForm'].submit();
}

function galleryImageEdit(id)
{
	//funFactsAdd('gallery');
	show('gallery','slide');
	document.getElementById('editGalleryHiddenImage').style.display = "inline";
	document.getElementById('galleryCurrentImage').src=document.getElementById('editGalleryImage' + id).src;
	document.getElementById('galleryAction').value='updateGallery';
	document.getElementById('galleryId').value=id;
	document.getElementById('captionGallery').value = document.getElementById('editGalleryCaption' + id).innerHTML;
}

function questionClicked(question)
{
	var valueField = document.getElementById(question + "Value");
	var max = 5;
	var id = valueField.value;
	
	if (id <= max)
	{
		var url = "../../ajax/register.php?id=" + id + "&question=" + question;
		
		updateAllDOMFields(document.profileForm);
		loadXMLDoc(url, gotData, true);
	}
}

function openWindow()
{
	fmy_window = window.open ("","mywindow1","status=1,width=350,height=150");  
	my_window.document.write(document.getElementById('someDiv').innertHTML);   
}

function hide(id, mode, callback)
{
	var e = document.getElementById(id);
	
	if (e != null)
	{
		if (mode == undefined)
			e.style.display = "none";
		else if (mode == "slide")
			$("#" + id).slideUp('normal', callback);
		else if (mode == "fade")
			$("#" + id).fadeOut('normal', callback);
	}
}


function gotData(xmlDoc)
{
	var max = 5;
	var questionInfo = new Array();
	var html = "";
	
	if (xmlDoc != null)
	{
		var id = getXmlValue(xmlDoc, 'id', 0);
		
		// Build up array of event info
		var question = xmlDoc.getElementsByTagName('response');
		
		if (question.length > 0)
		{
			// Loop through each event
			for (var e = 0; e < question.length; e++)
			{
				var info = question[e];
				
				// Loop through event info nodes
				// and build up an array of event details
				var details = new Array();
				
				if (info.childNodes.length > 0)
				{
					for (var n = 0; n < info.childNodes.length; n++)
					{
						// firstChild of the node is simply the text of the element
						var value = info.childNodes[n].firstChild.nodeValue;
						
						// Store in array
						// (e.g. details["id"] = 1)
						details[info.childNodes[n].nodeName] = value;
					}
				}
				
				// Add event details to main array
				questionInfo[questionInfo.length] = details;
			}
		}
		
		// Build HTML
		if (questionInfo.length > 0)
		{
			
			for (var e = 0; e < questionInfo.length; e++)
			{
				var details = questionInfo[e];
				var html = details["html"];
				var question = details["info"];
			}
		}
		
		document.getElementById(question + "Value").value  = parseInt(document.getElementById(question + "Value").value) + 1;
		document.getElementById(question + "Div").innerHTML += html;
		
		if (id == max)
		{
			document.getElementById(question + "HREF").style.display = 'none';
		}
	}
}

function showCarouselHover(id)
{
	$('#carouselHover'+id).css('visibility','visible');
}

function hideCarouselHover(id)
{
	$('#carouselHover'+id).css('visibility','hidden');
}
