/*

File:		functionality.js
Created:	11.06.2009
Author:		Sean Jones @ Equator	

Notes:


*/

$ektron(function(){
 
	if($ektron("#contactComponent").length) {
		categoryName = $ektron("#contactComponent dt").addClass("categoryName");
		subCategoryName = $ektron("#contactComponent dd > p").addClass("subCategoryName");
		subCategoryContent = $ektron("#contactComponent dd > ul").addClass("subCategoryContent");	
		
		$ektron(".categoryName,.subCategoryName").wrapInner("<a href='#'></a>");
		
		$ektron(subCategoryName).hide();
		$ektron(subCategoryContent).hide();
		
		$ektron(".categoryName a").click(function () {
										if ($ektron(".subCategoryContent:visible")) {
											$ektron(".subCategoryContent:visible").slideToggle();
										}
										$ektron(this).parent().toggleClass("open");
										$ektron(".subCategoryName").slideToggle();
										
										return false;
										});
		$ektron(".subCategoryName").click(function () {
										$ektron(this).toggleClass("open");
										$ektron(this).next().slideToggle();
										
										return false;
										});		
	}
	

$ektron(".freqAskedQ div div h3").addClass("show"); 
$ektron(".freqAskedQ .sectionQuestions .questionList h4").addClass("showQuestion"); 

$ektron(".freqAskedQ .sectionQuestions").addClass("showHideContent");
$ektron(".freqAskedQ .sectionQuestions .answer").addClass("showHideContent_Answer");

$ektron(".show,.showQuestion").wrapInner("<a href='#'></a>");

$ektron(".details .fullContent").addClass("showHideContent"); // show/hide products components

$ektron(".showHideContent,.showHideContent_Answer").hide();

	if ( $ektron(".ourPricesContent").length ) {
		$ektron(".ourPricesContent .fullContent").before("<p class='show'><a>View our tariff table</a></p>");
		$ektron(this).find(".productFullWidth:first .show").toggleClass("open");
		$ektron(this).find(".productFullWidth:first .show").text("Hide our tariff table");
		$ektron(this).find(".productFullWidth:first .showHideContent").slideToggle();
	}
	else {
		$ektron(".fullContent").before("<p class='show'><a>More</a></p>");
	}




// show/hide script	
	if ( $ektron(".showHideContent").length ) {
		
		$ektron(".show,.showQuestion").click(function () {
					$ektron(this).next().slideToggle();
					$ektron(this).toggleClass("open");
							if ( $ektron(".ourPricesContent").length ) {
								if( $ektron(this).text() == "View our tariff table" ) // toggle innerText 
									{
									$ektron(this).text("Hide our tariff table");
									}
								else if( $ektron(this).text() == "Hide our tariff table" )
									{
									$ektron(this).text("View our tariff table");
									}
								return false;	
							}
																
							else {
								if( $ektron(this).text() == "More" ) // toggle innerText 
									{
									$ektron(this).text("Less");
									}
								else if( $ektron(this).text() == "Less" )
									{
									$ektron(this).text("More");
									}
								return false;	
								}
							}
					);		
	}
	
// Create default text for Search text field
    $ektron(".siteSearch input[type=text]").each(function() {
        var labelVal = $ektron(".siteSearch label.searchLabel").text();

        $ektron(this).each(function() {
            createText(labelVal, $ektron(this));
        });
        // Removal of text on user-focus
        $ektron(this).focus(function() {
            removeText(labelVal, $ektron(this));
        });
        // Restoration of default text on input blur, if no user input.
        $ektron(this).blur(function() {
            restoreText(labelVal, $ektron(this));
        });
    });





// toolTip
if($ektron(".whatOurPeersSay").length) {
	$ektron(".peerTxt").wrap('<div class="peerTxtContainer"></div>');
	
	$ektron(".peerContent img").each( 
						   function() {
						   	$ektron(this).attr("alt",""); // remove IE alt tooltip 
						  }
						   );
	
	$ektron(".peerContent").hover(
				function() {				  
					$ektron(this).addClass("selectedToolTip");	
				},
				function() {
					$ektron(this).removeClass("selectedToolTip");	
				}
				);
}

// Search results page - give link urls max number of characters
	$ektron(".searchResults dl dd p a").each( function() {							
							var searchLink =  $ektron(this).eq(0); 
							var searchLinkText =  $ektron(this).eq(0).text(); 
							
							var maxChar = $ektron(this).text().substr(0,72);
							
						    if (searchLinkText.length > 70) { // check character length of each search result link
								$ektron(this).text(maxChar + "...");
							}
						   });


// FAQ list - compare query string with question IDs (category type and question) on FAQ page...
	if ($ektron(".freqAskedQ").length) {
		var categoryID = location.href.match(/#.+$ektron/);
		
		$ektron(".freqAskedQ .question").each( function() {
								
				questionAsked = $ektron(this).attr("ID");
				answer = $ektron(this).find(".showHideContent");
								
				if(categoryID == "#"+questionAsked) { // if question ID matches querystring select that question...
					selectCategory = $ektron(this).parents(".FAQcontent").find(".show").toggleClass("open");
					showQuestions = $ektron(this).parents(".showHideContent").show();
					selectAnswer = $ektron(this).find(".answer").show();
					location.href = location.href;
				}					
		});

	}
	
});


// Create default text for text field on page load
function createText(defVal, thisObj) {
    var inpType = thisObj.attr("type");
    //alert(inpType);
    
    if (inpType != "submit") {

        if (thisObj.attr("value")) {
            if (thisObj.attr("value") == defVal || thisObj.attr("value").length == 0) {

                thisObj.attr("value", defVal);
                thisObj.addClass("empty");
            }
        } else {

            thisObj.attr({ 
                value: defVal
            });
            thisObj.addClass("empty");
        }
        
        

       
    }
}

// Remove default text on focus. Ignore user-inserted text
function removeText(defVal, thisObj) {
    var inpType = thisObj.attr("type");
    if (inpType != "submit") {
        var currVal = thisObj.attr("value");
        if (currVal == defVal) {
            thisObj.attr("value", "");
            thisObj.removeClass("empty")
        }
    }
}

// Restore default text on focus. Ignore user-inserted text
function restoreText(defVal, thisObj) {
    var inpType = thisObj.attr("type");
    if (inpType != "submit") {
        var currVal = thisObj.attr("value");
        if (currVal != undefined && currVal != '') {
            thisObj.attr("value", currVal);
        }
        else if (currVal == undefined || currVal == '') {
            thisObj.attr("value", defVal);
            thisObj.addClass("empty");
        }
    }
}

/* This function toggles the tariff type / meter type based on the product selected */

function ToggleTariffsLayering() {
    var containerRef = document.getElementById("ctl00_ContentPlaceholder_productSignupControl_productTypesCheckBoxList");
    var inputRefArray = containerRef.getElementsByTagName("input");
    var checkCount = 0;
    var elecCheck = 4;
    var gasCheck = 2;
    var telCheck = 1;
    //addding 
    //elec
    if (inputRefArray[0].checked) {
        checkCount = checkCount + elecCheck;
    }
    //gas
    if (inputRefArray[1].checked) {
        checkCount = checkCount + gasCheck;
    }
    //tel
    if (inputRefArray[2].checked) {
        checkCount = checkCount + telCheck;
    }

    var sChargeContainer = document.getElementById("ctl00_ContentPlaceholder_productSignupControl_standingChargeRadioButtonList");
    var eMeterTypeContainer = document.getElementById("ctl00_ContentPlaceholder_productSignupControl_electricityMeterTypeRadioButtonList");

    var sChargeArray = sChargeContainer.getElementsByTagName("input");
    var eMeterTypeArray = eMeterTypeContainer.getElementsByTagName("input");


    if (checkCount == 7 || checkCount == 6 || checkCount == 5 || checkCount == 4 || checkCount == 0) {
        sChargeArray[0].disabled = false;
        sChargeArray[1].disabled = false;
        eMeterTypeArray[0].disabled = false;
        eMeterTypeArray[1].disabled = false;
    }

    if (checkCount == 3 || checkCount == 2) {
        //gas + tel or only gas
        sChargeArray[0].disabled = false;
        sChargeArray[1].disabled = false;
        eMeterTypeArray[0].disabled = true;
        eMeterTypeArray[1].disabled = true;
    }

    if (checkCount == 1) {
        //only tel
        sChargeArray[0].disabled = true;
        sChargeArray[1].disabled = true;
        eMeterTypeArray[0].disabled = true;
        eMeterTypeArray[1].disabled = true;
    }
}