// thumb image scroll bar
$(document).ready(function() {
	var firstItemIndex = 0;
	var lastItemIndex  = $(".scrollItem:visible").size() - 1;
	var numElements = $(".scrollItem").size();
	function ScrollThumbImags(prev)
	{
		firstItemIndex += (prev ? 1 : -1);
		lastItemIndex  += (prev ? 1 : -1);
		$(".scrollItem").each(function(index) {
            if(index >= firstItemIndex && index <= lastItemIndex) {
				$(this).show();
			}
			else if( index > lastItemIndex || index < firstItemIndex) {
				$(this).hide();
			}
	    });
		$("#btnScrollU").css('visibility', (firstItemIndex>0 ? 'visible' : 'hidden'));
		$("#btnScrollD").css('visibility', (lastItemIndex < numElements-1 ? 'visible' : 'hidden'));
	}
	
	$("#btnScrollD").click(function(){ ScrollThumbImags(1); return false; });
	$("#btnScrollU").click(function(){ ScrollThumbImags(0); return false; });
	$(".scrollBtnImg").mouseover(function(){  var src = $(this).attr("src").replace(".gif", "_on.gif"); 
            							    $(this).attr("src", src); });
    $(".scrollBtnImg").mouseout(function(){  var src = $(this).attr("src").replace("_on.gif", ".gif"); 
            							   $(this).attr("src", src); }); 

    $(".scrollItem").mouseover(function() {
        $('.scrollItem').removeClass('current');
        $(this).addClass('current');
        $('.largeBox').hide();
        if ($(this).attr('id') == 'thumbBox_defaultVideo') {
            $('.imageContainer').css('z-index', -2000);
            $('.largeBox').hide();
        }
        else {
            $('#largeBox_'+ ($(this).attr('id').replace('thumbBox_',''))).show();
            $('.imageContainer').css({'background-color' : "white", 'z-index' : 10});
        }
    });
});

// thumb image scroll bar
$(document).ready(function() {
	var firstItemIndex = 0;
	var lastItemIndex  = $(".vwdItem:visible").size() - 1;
	var numElements = $(".vwdItem").size();
	function ScrollPurchasedItems(prev)
	{
		firstItemIndex += (prev ? 1 : -1);
		lastItemIndex  += (prev ? 1 : -1);
		$(".vwdItem").each(function(index) {
            if(index >= firstItemIndex && index <= lastItemIndex) {
				$(this).show();
			}
			else if( index > lastItemIndex || index < firstItemIndex) {
				$(this).hide();
			}
	    });
		$("#btnScrollL").css('visibility', (firstItemIndex>0 ? 'visible' : 'hidden'));
		$("#btnScrollR").css('visibility', (lastItemIndex < numElements-1 ? 'visible' : 'hidden'));
	}
	$("#btnScrollR").click(function(){ ScrollPurchasedItems(1); return false; });
	$("#btnScrollL").click(function(){ ScrollPurchasedItems(0); return false; });
});

function SetBreakdownChargesToggleLink() {
    $('a.pricingTableToggleLink').click(function() {
		if( $('#breakdownCharges').hasClass('closed') ) {
			$('#breakdownCharges').removeClass('closed').addClass('open');
			$('#breakdownCharges tbody[class~="prices"]').show();
			$('#breakdownCharges tbody[class~="pricetoday"]').show();
		} else {
			$('#breakdownCharges').removeClass('open').addClass('closed');
			$('#breakdownCharges tbody[class~="prices"]').hide();
			$('#breakdownCharges tbody[class~="pricetoday"]').hide();
			$('#flipswapRows').show(); // keep flip swap shown if exists
		}
		// UpdateBreakdownChargesRowsVisibility();
		return false;
	})
}

$(document).ready(SetBreakdownChargesToggleLink);

// tabs
$(document).ready(function() {
    $('#contentTabs a').click(function() {
	    $('#contentTabs a').removeClass('current');
		$(this).addClass('current');
		$('.tabContents').hide();
		$('#'+ ($(this).attr('id').replace('title_',''))).show();
		return false;
	});
});

// when adding a phone, also add accessories that are checked in the recommended accessory tab
$(document).ready(function() {
  $('#addPhoneForm').submit(function() {
	var prIdStr = '';
	$('#addAccessoryForm input:checkbox:checked').each(function(index) { prIdStr += (index ? '_' : '') + $(this).val() });
	$('#addPhoneForm input[name=addAccessoryPrIds]').val(prIdStr);
	return true;
  });
});

// if popup, anchor tags with class "openInMainWindow" should go to the parent window
$(document).ready(function() {
  if( PDAttributes.popup ) {
	var windows = new Array();
	var win = window;
	var index = 0;
	while(win) {
		windows[index++] = win;
		win = win.opener;
	}
    $('a.openInMainWindow').click(function() {
	  win = windows[windows.length - 1];
	  win.location.href = $(this).attr('href');
	  win.focus();
	  for(index = 0; index < windows.length-2; index++) {
		  windows[index].close();
	  }
	  return false;
	});
	
	$('a.openInNewTab').click(function() {
		window.open($(this).attr('href'),'_blank');
	    return false;
	});
  }
});

// HACK for friendly URL not supporting POST
$(document).ready(function() {
  $("form").each(function() {
      if($(this).attr('action')=='') {
		  $(this).attr('action', PDAttributes.default_form_action_handler);
	  }
  });
});

// resize window for popup
$(document).ready(function() {
  if( PDAttributes.popup ) {
	  ResizeWindow(735, 800);
  }
});

$(document).ready(function() {
    $('#title_exptRevs').click(function(){
        UserTrackPageView('PD3TD', PDAttributes.prId);
        return false;
        });
        
    $('#title_phnFeatures').click(function(){
        UserTrackPageView('PD3TF', PDAttributes.prId);
        return false;
        });
        
    $('#title_phnDemo').click(function(){
        UserTrackPageView('PD3TV', PDAttributes.prId);
        return false;
        });
        
    $('#title_custPnRevs').click(function(){
        UserTrackPageView('PD3TR', PDAttributes.prId);
        return false;
        });
        
    $('#title_inTheNews').click(function(){
        UserTrackPageView('PD3TN', PDAttributes.prId);
        return false;
        });
          
    $('#title_accXsell').click(function(){
        UserTrackPageView('PD3TA', PDAttributes.prId);
        return false;
        });
        
    $('#title_phnOverview').click(function(){
        UserTrackPageView('PD3TS', PDAttributes.prId);
        return false;
        });
        
    $('#title_recPlans').click(function(){
        UserTrackPageView('PD3TP', PDAttributes.prId);
        return false;
        });
        
    $('.openInNewTab').click(function(){
        UserTrackPageView('PD3EX', PDAttributes.prId);   
        });
                       
    });

