var $j = jQuery.noConflict();

$j(document).ready(function () {

	$j(document).pngFix(); 
								
	// setup tabs
	// $j('.tabnav').setupTabs();
	
	// setup FAQ accordion
	$j('#faqList').setupAccordion();
	
	// setup Ask and Asnwers accordion
	$j('#askList').setupAccordion();
	
	// setup Bubble Tool Tips on Plan Details Page
	$j('.bubbleTip').aToolTip({});
	
	
	//add the sale and bundle tags on the phones tab
	$j('#phone_rumor2').addClass('sale_item');
	$j('#phone_rumor_touch').addClass('bundle_item');
	
	
	// 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");
		}
	});
	
	var bbRotation = new DivRotation('.tabnav li a', '.hidingblocks');

	setupColorSwitch($j('.colors'));
	
	plandetailBind();

});// end of dom ready
	
function plandetailBind() {
	$j("#page_paylo_00").find("#plandetails").find("a").click(function() {
		$j("#tab2").click();
	});
}
	
setupColorSwitch = function(colors) { 

	colors.each( function(i) {

		var LIs = $j(this).find('.color_nav li, .color_nav a');
	
		LIs.click( function() {
												
			clickedClass = $j(this).attr('class');
	
			colors[i].className = colors[i].className.replace(/\bcolor_.*?\b/g, 'color_' + clickedClass);

		})
													 
	})


}

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(){			
														 
			// alert('tab clicked');
			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();
}
	
	
	
	// Tabs
	jQuery.fn.setupTabs = function() {
		// show first tab on load
		$j('div.tabContent').hide();
		$j('div.tabContent:first').show();
		$j('ul.tab_bar li:first a').addClass('active');
		
		this.find('li a').click(function() {
			// hide all tab content, deactivate all tabs
			$j('div.tabContent').hide();
			$j('ul.tab_bar li a').removeClass('active');
			
			// tab ID is tab_contentIdentifier; div id is tabContent_contentIdentifier
			var tabID = $j(this).attr('id');
			tabID = tabID.replace('tab_','');
			
			// show the relevant tab, place active on the proper tab
			$j('#tabContent_'+tabID).show();
			$j('#tab_'+tabID).addClass('active');
			
			return false;
		});
	}


	// 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().removeClass('expanded');
				} else {
					$j(this).parent().parent().siblings().removeClass('expanded');
					$j(this).parent().parent().addClass('expanded');
				};
				
				return false;
			});
		} 
		else { // if NOT IE6
			$j(this).find('h4 a').click(function() {
				if (!$j(this).parent().parent().hasClass('expanded')) { 
					$j(this).parent().parent().siblings().removeClass('expanded').find('.body').hide(); 
					$j(this).parent().parent().addClass('expanded').find('.body').hide().fadeIn(400).slideDown(400); 
				} else {
					$j(this).parent().parent().removeClass('expanded').find('.body').hide(); 
				};
				
				return false;
			});
		}
	};	


					
	bvShowTab = function() {
		clickTab('tab5');
	}





