﻿/*

File:		eqtr_functionality.js
Created:	06.04.2009
Author:		Sean Jones @ Equator	

Notes:


*/

$(function(){
		   

/////// Show/Hide component ///////
	if($(".sectionTipContainer").length) {
		
		$(".sectionTipHeader h3,.question p").wrapInner("<a href='#'></a>");
		
		$(".sectionTipHeader,.question").live( "click" , function () {
							$(this).next().slideToggle();
							
							if(".sectionTipHeader") {
								$(this).find("h3").toggleClass("selected");
							}
							if(".question") {
								$(this).find("p").toggleClass("selected");
							}							
							return false;
							});


			var firstTipID = $(":not(.FAQs) > .sectionTipContainer .sectionTipHeader > div").attr("id");
			//Takes the ID from the URL and displays the saving tip.
			//If there is no tip id in the URL it will open tip 1
			var tipID = location.href.match(/#.+$/) || ("#"+firstTipID);

			$(tipID + " h3").addClass("selected").parents(".sectionTip").find(".sectionTipContent").show();

			if (location.href.match(/#/)) {
				location.href = location.href;
			}
			
		
	}
	
/////// Cosmetic stuff ///////	
$(".sellingPointsContent > div:last,.sectionTipContainer .categoryTipText:last-child,.articleList:last").addClass("last"); 


/////// FAQ PAGE ///////

// FAQ list - take ID from url and compare with question IDs (category type and question) on FAQ page...
	if ($(".FAQs").length) {
	
		$(".questionAnswerContainer").each( function() {
								
				questionAsked = $(this).attr("ID");
				answer = $(this).find(".answer")
			
				if(tipID == "#"+questionAsked) { // if question ID matches querystring select that question...
					selectCategory = $(this).parents(".sectionTip").find(".sectionTipHeader h3").toggleClass("selected");
					showQuestions = $(this).parents(".sectionTipContent").show();
					selectAnswer = answer.show();
					location.href = location.href;
				}					
		});

	}

	/////// NEWS PAGE ///////
	if( $(".newsArticles").length )
	{
		initNews();
	}

});



/////// NEWS PAGE INIT FUNCTION ///////

function initNews()
{
	$(".newsArticles").click( function(evt) {
		var news = $(this);
		var el = evt.target;
		if( el.tagName == "A" && el.href.match(/page=/) )
		{
			var href = el.href;
            $.ajax({
                url: href.replace(/#.+$/,""),
                cache: true,
                dataType: "html",
                success: function(html) {
                    var content = $("<div/>").append(html.match(/<!--ARTICLES_START-->([.\s\S]*)<!--ARTICLES_END-->/i)[1]);
//                    contentContainer.append(content);
					news.html( content );
//                    showTab();
                },
                error: function() {
                    location.href = href;
                }
            });

			return false;
		}
	});
}
