// JavaScript Document

$j(document).ready(
	function() {
	
			$j('#tips').accordion({
			header: 'span.button',
			active: '.selected',
			alwaysOpen: false,
			autoheight: false,
			animated: 'easeslide'
		});

		if ($j(".roamingContentTabs").length) {
			
			var URLstr = window.location.toString();
			if (URLstr.indexOf('#', 0) >= 0 ) {
				$j(".if-js-hide").removeClass("if-js-hide");
			}
			$j("ul.tabs li").click(
				function (event) {
					event.preventDefault();
					var activeTab = $j(this).find("a").attr("href");
					$j.address.value(activeTab);  
					$j(this).addClass("active");
					return false;
				}
			);  
			
			$j("#travellerIntroLink").click(
				function (event) {
					event.preventDefault();
					$j.address.value('/#traveller');  
					return false;
				}
			); 

			$j("#standardIntroLink").click(
				function (event) {
					event.preventDefault();
					$j.address.value('/#standard');  
					return false;
				}
			);  
			
			$j.address.change(
				function (event) {
				
				switch (event.value) {
					case '/#prepay':
						$j(".tab-content").hide();
						$j(".roamingPrepayBanner").show();
						$j("ul.tabs li").removeClass("active");
						$j("ul.tabs li:eq(1)").addClass("active");
						$j(".tab-content:eq(1)").show();
						break;
					case '/#onaccount':
						$j(".tab-content").hide();
						$j(".roamingPrepayBanner").hide();
						$j("ul.tabs li").removeClass("active");
						$j("ul.tabs li:eq(0)").addClass("active");
						$j(".tab-content:eq(0)").show();
						break;
					case '/#business':
						$j(".tab-content").hide();
						$j(".roamingPrepayBanner").hide();
						$j("ul.tabs li").removeClass("active");
						$j("ul.tabs li:eq(2)").addClass("active");
						$j(".tab-content:eq(2)").show();
						break;
					case '/#traveller':
						//console.log('travel');
						$j(".tab-content").hide();
						$j("ul.tabs li").removeClass("active");
						$j("ul.tabs li:eq(0)").addClass("active");
						$j(".tab-content:eq(0)").show();
						break;
					case '/#standard':
					//console.log('standard');
						$j(".tab-content").hide();
						$j("ul.tabs li").removeClass("active");
						$j("ul.tabs li:eq(1)").addClass("active");
						$j(".tab-content:eq(1)").show();
						break;
					default:
					}		
				}
			); 
		}
		
		/* for the which roaming plan page 
    It looks up the table on the page, and parses it into a drop down menu which changes the p.rateDisplay html to display the correct rate, the table is hidden if js is active"
	*/
		$j(".rateFinder").prepend("<form action=''><p>What is your calling plan?</p><select><option value='null'>Please select calling plan</option></form>");
		$j("table.rateDisplay").find("td").each(function(intIndex) {
			if(intIndex%2 == 0) {
			$j(".rateFinder select").append("<option>"+ $j(this).html() +"</option>");
			} else {
			$j(".rateFinder select").find("option:last").attr('value', $j(this).html());
			}
			});
		//removes the first item in the dropdown list, which is the titlebar from the table.
		$j(".rateFinder select").find("option:eq(1)").remove();
		
		$j(".rateFinder select").change(function() {
			if($j(this).find("option:selected").attr('value') == 'null') { //check user hasn't picked default action
			 return false;
			} else {
			$j("p.rateDisplay").removeClass("hide").html("Your Standard Home Rate is<span>"+ $j(this).attr('value') +"</span>");
			}
		});

		
	$j("#expanderLink").click(function(){

			if($j(this).hasClass('expanderButtonClose')) {
			 $j("#expanderTable").slideDown();
			 $j(this).html('hide all destinations').removeClass("expanderButtonClose").addClass("expanderButtonOpen");
			} else {
			 $j("#expanderTable").slideUp();
			 $j(this).html('show all destinations').removeClass("expanderButtonOpen").addClass("expanderButtonClose");
			}
			return false;

		});	
	$j("#hider").click(function() {
			$j("#expanderTable").slideUp();
			$j("#expanderLink").removeClass("expanderButtonOpen").addClass("expanderButtonClose").html('See full list of countries with automatic roaming');
			return false;
	})
		
});

