var colorArray = [
	"#ed008c",
	"#00ed61",
	"#ee8301",
	"#edca00",
	"#ed0000",
	"#7b00ed",
	"#0011ed",
	"#00c3ed"
];
var colorArrayLength = colorArray.length;


// toggle showing of poll
function pollClick() {
// show/hide options
if($("#polls").hasClass("active")) {
    $("#polls").removeClass("active");
} else {
    $("#polls").addClass("active");
}
}

// send poll answer
function pollAnswer(event) {
	var url = $(this).find(".actions span + span").text();
	// disable further clicks
	$("#polls dd").unbind("click");

	
	$.getJSON(url, null, convertJSON2XML);
}

// This function takes the JSON object containing poll results, and converts it into an XML object
// to the pre-existing pollAnswerSuccsss() function.
function convertJSON2XML(rawJSON) {
	var xml = document.implementation.createDocument("", "poll", null);
	for (var i = 0; i < rawJSON[0].question.choice.length; i++) {
		var resultElement = document.createElement("result");
		var percentElement = document.createElement("percent");
		percentElement.appendChild(document.createTextNode(rawJSON[0].question.choice[i].percent + '%'));
		resultElement.appendChild(percentElement);
		var colorElement = document.createElement("color");
		colorElement.appendChild(document.createTextNode(colorArray[i % colorArrayLength]));
		resultElement.appendChild(colorElement);
		
		xml.firstChild.appendChild(resultElement);
	}

	pollAnswerSuccess(xml);
}


// successfully loaded data
function pollAnswerSuccess(xml) {
//insert poll results
$(xml).find("percent").each(function(i) {
    $("#polls dd").eq(i).find("p").eq(2).append($(this).text());
});
// set the width and color of each result
$("#polls dd > p span").each(function(i) {
    $(this).css("width", parseInt($(this).parent("p").text()) * 1.5 + "px");
    $(this).css("background-color", $(xml).find("color").eq(i).text());
});
}


// this is to populate the poll section with poll question stem and poll choices into the polls DIV tag
function getPoll(pollURL) {
	  
    $.getJSON(pollURL, getPollObj);
}

function getPollObj(data) {

	var thePollDiv = document.getElementById('polls');	

	$.each(data, function(index, pollObj){	

		var numChoices = pollObj.question.choices;
		var pollText = pollObj.question.text;
		var submitLink = pollURL + "&pass=1&Q1=";

		var outerdl = document.createElement("dl");
		var thedt = document.createElement("dt");

		var theQuestionSpan = document.createElement("span");

		theQuestionSpan.appendChild(document.createTextNode(pollText));
		thedt.appendChild(theQuestionSpan);
		var emptyspan = document.createElement("span");
		thedt.appendChild(emptyspan);
		outerdl.appendChild(thedt);

		// start with A as first letter
		var optionLetter = 65;

		var choiceNum = 0;
		var choiceText = "";

		for (i = 0; i < numChoices; i++) { 

			choiceNum = i + 1;
			choiceText = pollObj.question.choice[i].text;

			var theDD = document.createElement("dd");
			var theDIV = document.createElement("div");
			theDIV.className = "actions";
			var linkP = document.createElement("p");
			var letterSPAN = document.createElement("span");
			letterSPAN.appendChild(document.createTextNode(String.fromCharCode(optionLetter)));
			linkP.appendChild(letterSPAN);
			var linkSPAN = document.createElement("span");

			linkSPAN.appendChild(document.createTextNode(decodeURIComponent(submitLink + choiceNum)));

			linkP.appendChild(linkSPAN);
			theDD.appendChild(theDIV);
			theDIV.appendChild(linkP);

			var choiceP = document.createElement("p");
			choiceP.appendChild(document.createTextNode(choiceText));
			theDD.appendChild(choiceP);
			var blankP = document.createElement("p");
			var blankSPAN = document.createElement("span");
			blankP.appendChild(blankSPAN);
			theDD.appendChild(blankP);

			outerdl.appendChild(theDD);

			optionLetter = optionLetter + 1;

			outerdl.appendChild(theDD);
		}

		thePollDiv.appendChild(outerdl);

		$("#polls dt").click(pollClick);
		$("#polls dd").click(pollAnswer);

	});

}
	
// some utility function that may be of use by some functions to convert Amp into the char code
function insertAmp() {
	return String.fromCharCode(38);
}



// load data
function loadData() {
$.ajax({
url: $("#content .active .more span").text(),
type: "GET",
dataType: "xml",
timeout: 4000,
error: loadDataError,
success: loadDataSuccess
});
}

// error loading data
function loadDataError() {
alert("Error Loading");
}

// successfully loaded data
function loadDataSuccess(xml){
//remove the more button
$("#content .active .more").remove();
//append new content to end of content
$("#content .active ul").append($("response", xml).text());
//set listener for new more button
$("#content .active .more div").click(loadData);
// resize content to fit screen
resizeContent();
}

// sets active tab and content
function activeTab(pointer) {
// remove all previous active classes
$("#content > dl").removeClass("active");
// set current active class
pointer.addClass("active");
// initialize active tab boolean
var bTabActive = false;
// loop through each tab and associated content
$("#content > dl").each(function(i) {
    // determine if active tab has been reached yet
    bTabActive = ($(this).hasClass("active") || bTabActive) ? true : false;
    // set z-index according to whether we've reached the active tab yet
    $(this).children("dt").css("z-index", (bTabActive) ? $("#content > dl").length * 2 - i : i);
});
// resize content to fit screen
resizeContent();
}

// resizes content to fit screen properly
function resizeContent() {
// set the height of the content because we're using absolute positioning
$("#content").css("height",$("#content dl.active > dd").height() + 50);
// clean links on click
$("a").click(function(event) {
    event.preventDefault();
    window.location = $(this).attr("href").replace(/&#38;/g, "&");
});
// listen for clicks on videos bar
$("#content .active li dl").click(gotoVideo);
}

// cleans links
function cleanLinks() {
// clean links on click
$("a").click(function(event) {
    event.preventDefault();
    if($(this).attr("href").indexOf("tel:") != -1) {
        disclaimerPop($(this).attr("href"));
    } else {
        window.location = $(this).attr("href").replace(/&#38;/g, "&");
    }
});
}

// goes to link for video
function gotoVideo() {
// go to video on click
var link = $(this).siblings("div").eq(0).find("a").attr("href");
if(link.indexOf("tel:") != -1) {
        disclaimerPop(link);
    } else {
        window.location = link.replace(/&#38;/g, "&");
    }
}

// pops up a disclaimer for tel: links
function disclaimerPop(link) {
    var response = confirm("Clicking on OK will prompt you to make a phone call. Ask a parent's permission before calling. Standard calling rates apply.\n\nThe Disney Mobile Web site is data intensive. This link requires a phone with voice and data capabilities. By clicking on the link, you will initiate a phone call to a number that plays audio and also uses minutes from your calling plan. Standard data and airtime carrier fees may apply and will depend on the specific terms and conditions of your voice and data plan. If you are under 18, please get your parent's permission before using this feature. When you are done listening or would like to exit, you can click on \"End Call\" to return to the Disney Mobile Web.");
    if(response) {
        window.location = link;
    }
}

// listen for iPhone rotation
window.onorientationchange = orientChange;

// hide top bar
function orientChange() {
window.scrollTo(0, (window.pageYOffset != 0) ? window.pageYOffset : 1);
}