/**

 * @author Syl

 */



/***

 * Toggles the visibility of an HTML element

 *  

 * @param {String | HTMLElement | Array} el 

 * 

 */

function toggleVisibility(e, el)

{

	if (!YAHOO.util.Dom.hasClass(el,"hidden")) 

	{

		var attributes = {

			height: { to: 0 },

			opacity: { to: 0 }

		};

		//hide this stuff...		

		var myAnim = new YAHOO.util.Anim(el, attributes, .5, YAHOO.util.Easing.easeBoth);						

		myAnim.onComplete.subscribe( function(){			

			YAHOO.util.Dom.addClass(el,"hidden");

			}			

		);

		myAnim.animate();		

		

		var parentID = el.id.substring(0,el.id.indexOf("Content"));

		YAHOO.util.Dom.removeClass(parentID, "minus");

		

	}

	else 

	{

		var attributes = {			

			height: {from: 0, to: 100, unit: '%'},

			opacity: { from: 0, to: 1 }

		};

		//show this stuff...

		var myAnim = new YAHOO.util.Anim(el, attributes, .5, YAHOO.util.Easing.easeIn);				

		myAnim.onStart.subscribe( function(){

			YAHOO.util.Dom.removeClass(el,"hidden");				

			}			

		);

  		myAnim.animate();



		var parentID = el.id.substring(0,el.id.indexOf("Content"));

		YAHOO.util.Dom.addClass(parentID, "minus");	

	}

}



/***

 * Potential way to change the cookie if they've expanded out a section (not being used)

 * @param {HTML Element} el

 */

function changeCookie(el)

{

	if (el.id == "ConceptContent")

	{

		/*alert("cookie");*/

		YAHOO.util.Cookie.set("type", "Concept");		

	}

	if (el.id == "Technical")

	{

		YAHOO.util.Cookie.set("type", "Technical");

	}

	

	

}

YAHOO.example.init = function() 

{

	

	//add an action to submit the search

	var searchBox = YAHOO.util.Dom.get("cse-search-box");

	var searchButton = YAHOO.util.Dom.get("cse-search-submit");

	

	YAHOO.util.Event.addListener(searchButton, "click", submitButton, searchBox, true);



	//set cookie

	//YAHOO.util.Cookie.set("type", "Digest");





	//get cookie

	var cookie = YAHOO.util.Cookie.get("type");	

	//hide concept and technical divs if Digest user

	if (cookie == "Digest") {

		YAHOO.util.Dom.addClass("digest", "minus");

		YAHOO.util.Dom.addClass("conceptContent", "hidden");

		YAHOO.util.Dom.setStyle("conceptContent", "opacity", 0);		

		YAHOO.util.Dom.addClass("technicalContent", "hidden");

		YAHOO.util.Dom.setStyle("technicalContent", "opacity", 0);		

	}

	//hide just the technical div when Conecept user

	if (cookie == "Concept") {

		YAHOO.util.Dom.addClass("digest", "minus");

		YAHOO.util.Dom.addClass("concept", "minus");

		YAHOO.util.Dom.addClass("technicalContent", "hidden");

		YAHOO.util.Dom.setStyle("technicalContent", "opacity", 0);

	}

	if (cookie == "Technical") {

		YAHOO.util.Dom.addClass("digest", "minus");

		YAHOO.util.Dom.addClass("concept", "minus");

		YAHOO.util.Dom.addClass("technical", "minus");

		YAHOO.util.Dom.removeClass("digestlContent", "hidden");

		YAHOO.util.Dom.removeClass("conceptContent", "hidden");

		YAHOO.util.Dom.removeClass("technicalContent", "hidden");

	}

	

	





	//get all the "buttons" in the doc that toggle show/hide actions

	var buttons = YAHOO.util.Dom.getElementsByClassName("showhide");	

	

	//...and turn them into listeners

	for(var i=0; i < buttons.length; i++)

	{

		var content = YAHOO.util.Dom.get(buttons[i].id+"Content");

		YAHOO.util.Event.addListener(buttons[i], "click", toggleVisibility, content, true);

	}



	//set all page navigation links to have appropriate action

	var contextNav = YAHOO.util.Dom.get("context-nav");

	

	var navLinks = contextNav.getElementsByTagName("a");

	

	for(var i=0; i< navLinks.length; i++)

	{		

		YAHOO.util.Event.addListener(navLinks[i], "click", pageNavigation, navLinks[i], true);

	}	



	//check for hash in URL

	if (window.location.hash != '')

	{

		var anchor = window.location.hash;			

		linkToAnchor(anchor);

	}

	//if sections are already expanded out, then show the minus sign

	var mainContent = YAHOO.util.Dom.get("content");

	//var hiddenDivs = YAHOO.util.Dom.getElementsByClassName("hidden","div","content");

	var hiddenDivs = YAHOO.util.Dom.getElementsBy(isHidden, "div", mainContent);

	for(var i=0; i< hiddenDivs.length; i++)

	{

		var heading = YAHOO.util.Dom.getPreviousSibling(hiddenDivs[i]);

		YAHOO.util.Dom.addClass(heading, "minus");

	}

	

	//create the beta feedback popup thing

	var feedbackButtons = YAHOO.util.Dom.getElementsByClassName("feedback-icon","a");

	for(var i=0; i<feedbackButtons.length; i++)

	{

		YAHOO.util.Event.addListener(feedbackButtons[i], "click", feedback, feedbackButtons[i], true);

	}

	//add it to the tag, as well

	var feedBackTag = document.getElementById("feedback-tag");

	YAHOO.util.Event.addListener(feedBackTag, "click", feedback, feedBackTag, true);

	

	//create the close button functionality

	var closeButtons = YAHOO.util.Dom.getElementsByClassName("close-button","a");

	for(var i=0; i<closeButtons.length; i++)

	{

		YAHOO.util.Event.addListener(closeButtons[i], "click", closeButton, closeButtons[i], true);

	}

};



function closeButton(el, e)

{

	var	grandpa = YAHOO.util.Dom.getAncestorByClassName(e, "inline-popup");

	

/*	var attributes = {			

			opacity: { to: 0 }

		};

		//fade in window		

		var myAnim = new YAHOO.util.Anim(grandpa, attributes, .5, YAHOO.util.Easing.easeBoth);								

		myAnim.onComplete.subscribe( function(){			

			YAHOO.util.Dom.setStyle(grandpa,"display","none");

			YAHOO.util.Dom.setStyle(grandpa,"visibility","hidden");

			}			

		);

		myAnim.animate();	*/

				YAHOO.util.Dom.setStyle(grandpa,"display","none");

			YAHOO.util.Dom.setStyle(grandpa,"visibility","hidden");



}



function feedback(el)

{

	var form = YAHOO.util.Dom.get("feedback");

	YAHOO.util.Dom.setStyle(form,"display","block");

	YAHOO.util.Dom.setStyle(form,"visibility","visible");

	

}





function submitButton(e, el)

{

	el.submit();

}



function isHidden(el)

{

	if (YAHOO.util.Dom.hasClass(el,"hidden"))

		return false;

	else

		return true;

}



function highlight(e, el)

{

	el.focus();

	//el.select();

	el.value = "";

	

}

function pageNavigation(e, el)

{

	//get the anchor that is being linked

	var anchor = el.href.substring(el.href.indexOf("#"));		

	linkToAnchor(anchor);

	window.location = el.href;

}



function linkToAnchor(anchor)

{

	var anchorTag = document.getElementsByName(anchor.substring(1));

	var parentDiv = YAHOO.util.Dom.getAncestorByTagName(anchorTag[0], "div");

	//alert(parentDiv.id);



	if (YAHOO.util.Dom.hasClass(parentDiv,"hidden"))

	{

		toggleVisibility(parentDiv, parentDiv);

	}	

	if (parentDiv.id == "content")

	{

		var content = anchor.substring(1,2) + anchor.substring(2) + "Content";

		//alert(content);

		if (YAHOO.util.Dom.hasClass(content, "hidden")) 

		{

			toggleVisibility(content, content);

		}

	}

}



YAHOO.util.Event.onDOMReady(YAHOO.example.init);

//YAHOO.util.Event.onAvailable('content', YAHOO.example.init);
