var $j = jQuery.noConflict();

jQuery.fn.slideFadeToggle = function(speed, easing, callback) {
	return this.stop().animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);  
};

function DivRotation(tabs, divclass) {
	this.tabs = $j(tabs);
	this.divclass= $j(divclass);
	this.firstTab = this.tabs.eq(0);
	
	this.closeDivs = function() {
		this.divclass.css('display','none');
		this.tabs.removeClass('current');
	}
	
	this.showDiv = function(id) {
		$j('#'+id).css('display','block');

		$j('#'+ id).find('.phone_rows').each( function() {
			$j(this).find('.phone:not(.height_adjusted)').equalHeights().addClass('height_adjusted');
		}) 
	}
	
	this.init = function() {
		var _this = this;
		
		this.tabs.click(function(){			
			var whichDiv = $j(this).attr('rel');	
			_this.closeDivs();
			
			$j(this).addClass('current');
			_this.showDiv(whichDiv)
		});
		
		this.closeDivs();
	
		this.firstTab.addClass('current');
		this.showDiv(this.firstTab.attr('rel'));
	}
	
	this.init();
}

function featureListBind() {
	var megaLoc = "138px";
	var curLoc = "138px";
	var newLoc = "";
	var arrowPos = {"broadband_feature_3g" : "138px", "broadband_feature_camera" : "167px", "broadband_feature_audio" : "196px", "broadband_feature_bluetooth" : "225px"};

	$j(".broadband_feature_list").find("li").find("a").mouseover(function () {
		curLoc = $j(".bubble_top").css("marginLeft");
		newLoc = arrowPos[$j(this).attr('class')];
		$j(".bubble_top").animate({
			"marginLeft" : newLoc
		}, 100);
	}).click(function() {
		newLoc = arrowPos[$j(this).attr('class')];
		curLoc = newLoc;
		megaLoc = curLoc;
		$j(".bubble_top").animate({
			"marginLeft" : newLoc
		}, 100);
		return false;
	});
	
	$j(".broadband_feature_list").mouseleave(function () {
		// exit entirely
		$j(".bubble_top").animate({
			"marginLeft" : megaLoc
		}, 100);
	});
}
	
$j(document).ready(function(){

	$j(document).pngFix(); 
	
	var bbRotation = new DivRotation('.tabnav li a', '.hidingblocks');
	featureListBind();
	setupFeatures();
	
	// setup Bubble Tool Tips on Plan Details Page
	$j('.bubbleTip').aToolTip2({});
	
	// toggle between Most popular and All Results links on Ask & Answer tab
	$j('.most_pop').addClass('selected');
	$j('.view_states a').click(function(){	
			$j('.most_pop').toggleClass('selected');
			$j('.all_results').toggleClass('selected');
	});
	
	// toggle the Expand All and Collapse All link on Ask & Answer tab
	$j('.expander a').html("Expand All");
	$j('.expander a').click(function(){
		$j('.expander a').toggleClass('selected')
			
		if($j('.expander a').hasClass('selected')) {
		   $j('.expander a').html("Collapse All");
		}
		else {
		   $j('.expander a').html("Expand All");
		}
	});
	
	// setup FAQ accordion
	$j('#faqList').setupAccordion();

	// setup Ask and Answers accordion
	$j('#askList').setupAccordion();
});

setupFeatures = function() { 
	$j('#fnav li').hover( function() {
		var index = $j('#fnav li').index($j(this));
		$j('#fnav li').removeClass('active').eq(index).addClass('active');
		$j('#features li').removeClass('active').eq(index).addClass('active');
	})
}

// Accordion
jQuery.fn.setupAccordion = function(el) {

	if(typeof document.body.style.maxHeight === "undefined") { // if IE6
		$j(this).find('h4 a').click(function() {
			if (!$j(this).parent().parent().hasClass('expanded')) { 
				$j(this).parent().parent().siblings().removeClass('expanded');
				$j(this).parent().parent().removeClass('expanded').find('.body').hide();
			} else {
				$j(this).parent().parent().addClass('expanded').find('.body').fadeIn(400).slideDown(400);
			};
			
			return false;
		});
	} 
	else { // if NOT IE6
		$j(this).find('h4 a').click(function() {
			targetListItem = $j(this).parents('li');
			if(targetListItem.hasClass('expanded')) {
				targetListItem.removeClass('expanded').css({
					'background' : 'url(../_img/beyond_talk/list_arrow.png) no-repeat scroll 2px 5px transparent'
				}).find('div.body').slideUp('slow');
			} else {
				targetListItem.addClass('expanded').css({
					'background' : 'url(../_img/beyond_talk/list_arrow_down.png) no-repeat scroll 0 8px transparent'
				}).find('div.body').slideDown('slow');
			}
			
			return false;
		});
	}
};

