
// taking off the .00
function formatPrices(price){
	var newPrice = price.replace('.00','');
	return newPrice;
}

// handles the tab switching
function globalActions(){
	$('#tab-content div.container').hide();
	$('#tab-titles h4').each(function(){
		var titleHTML = $(this).html();
		if(titleHTML == ''){
			$(this).hide();
		};
	});
	
	// checks to see if tab 1 exists, otherwise it sets tab 2 to active
	var tab1Title = $('#tab-1-title').html();
	var tab2Title = $('#tab-2-title').html();
	var tab3Title = $('#tab-3-title').html();
	if(tab1Title == '' && tab2Title != ''){
		$('#tab-content #tab-2').show();
		$('#tab-2-title').addClass('active');
	} else if(tab1Title == '' && tab2Title == '' && tab3Title != ''){
		$('#tab-content #tab-3').show();
		$('#tab-3-title').addClass('active');
	} else if(tab1Title != ''){
		$('#tab-content #tab-1').show();
	};
	
	$('#tab-titles h4').click(function(){
		$('#tab-titles h4').removeClass('active');
		$(this).addClass('active');
		var tabID = $(this).attr('id');
		var clickedTitle = '#' + $(this).attr('id').substr(0,5);
		$('#tab-content div.container').hide();
		$(clickedTitle).show();		
		trackClick(tabID);
	});				
	
	$('.btn-add-cart input').hover(function(){
		$(this).fadeTo('fast', 0);
	}, function(){
		$(this).fadeTo('fast',  1);
	});
	
}
	
function productSmartphone(){
	
	// call the global actions
	globalActions();
	
	// system spec tab
	$('.req-content').hide();
	$('.system-req h3').click(function(){
		if(($(this).hasClass('active')) == false){
			$('.system-req h3').removeClass('active');
			$(this).addClass('active');
			var parentDiv = $(this).parent('.system-req');
			$('.req-content').slideUp('slow');
			$('.req-content', parentDiv).slideDown('slow');
		}
	});
	
	// slideshow product image						
	setInterval(function(){
		var currentPosition = $('#product-image').css('background-position-x');
		if(currentPosition == null){
			var currentPosition = $('#product-image').css('background-position');
		}
		//$('h1').html(currentPosition);
		$('.product-slideshow').fadeOut('slow', function(){
			if (currentPosition == '0px' || currentPosition == '0px 0px' || currentPosition == '0% 0%'){
				$('.product-slideshow').css('background-position', '-228px 0');
			} else {
				$('.product-slideshow').css('background-position', '0 0');
			}
			$('.product-slideshow').fadeIn('slow');
		});							
	}, 6000);
	
	// building the custom logos
	var wmFound = false;
	var bbFound = false;
	$('#custcol_os_matrix option').each(function(){
		var optionValue = $(this).attr('value');
		if(optionValue == '2'){
			$('#platform-logos').append('<img src="/images/logos/platforms/palm.gif" alt="Available for Palm OS" />');
		} else if ((optionValue == '3' || optionValue == '10') && (wmFound == false)){
			$('#platform-logos').append('<img src="/images/logos/platforms/wm.gif" alt="Available for Windows Mobile" />');
			wmFound = true;
		} else if ((optionValue == '1' || optionValue == '14') && (bbFound == false)){
			$('#platform-logos').append('<img src="/images/logos/platforms/blackberry.gif" alt="Available for Blackberry" />');
			bbFound = true;
		} else if (optionValue == '8'){
			$('#platform-logos').append('<img src="/images/logos/platforms/mobile.gif" alt="Available for Mobile" />');
		} else if (optionValue == '9'){
			$('#platform-logos').append('<img src="/images/logos/platforms/iphone.gif" alt="Available for iPhone" />');
		} else if (optionValue == '12'){
			$('#platform-logos').append('<img src="/images/logos/platforms/android.gif" alt="Available for Android" />');
		} else if (optionValue == '11'){
			$('#platform-logos').append('<img src="/images/logos/platforms/webos.gif" alt="Available for Palm webOS" />');
		} else if (optionValue == '17'){
			$('#platform-logos').append('<img src="/images/logos/platforms/hp-webos.gif" alt="Available for HP webOS" />');
		};
	});
	
	var operatingSystems = new Array();
	$('#custcol_os_matrix option').each(function(index){	
		var optionValue = $(this).val();
		operatingSystems[index] = optionValue;
	});
	
	// hiding system req if not in option dropdown
	//palm
	if(jQuery.inArray('2', operatingSystems) < 0){
		$('#system-req-palmos').hide();
	}
	// ppc
	if(jQuery.inArray('3', operatingSystems) < 0){
		$('#system-req-ppc').hide();
	}
	if(jQuery.inArray('10', operatingSystems) < 0){
		$('#system-req-wm6').hide();
	}
	if(jQuery.inArray('1', operatingSystems) < 0){
		$('#system-req-blackberry4').hide();
	}
	if(jQuery.inArray('14', operatingSystems) < 0){
		$('#system-req-blackberry5').hide();
	}
	if(jQuery.inArray('8', operatingSystems) < 0){
		$('#system-req-mobile').hide();
	}
	if(jQuery.inArray('9', operatingSystems) < 0){
		$('#system-req-iphone').hide();
	}
	if(jQuery.inArray('12', operatingSystems) < 0){
		$('#system-req-android').hide();
	}
	if(jQuery.inArray('11', operatingSystems) < 0){
		$('#system-req-webos').hide();
	}
	if(jQuery.inArray('17', operatingSystems) < 0){
		$('#system-req-hp-webos').hide();
	}
	
	
	// builds the custom selections
	$('#custom-os').append($('#custcol_os_matrix').html());
	$('#custom-os option:nth-child(2)').remove(); // removing the built in -select- option
	$('#custom-renew').html($('#custcol_auto_renew').html());

	// custom form submit
	$('#custom-form').submit(function(){
		var yearsValue = $('input[name=custom-years]:checked').val();
		var OSValue = $('#custom-os').val();
		var renewValue = $('#custom-renew').val();
		var quanityValue = $('#custom-quanity').val();
		$('#custcol_years_matrix_items').val(yearsValue);
		$('#custcol_os_matrix').val(OSValue);
		$('#custcol_auto_renew').val(renewValue);
		$('#qty').val(quanityValue);
		$('#addtocart').click();
		return false;
	});

	populateDatabases();
	tooltip();
	lcodOnlineBenefits();
}

function productHandbook(){
	
	// call the global actions
	globalActions();
	
	// custom form submit
	$('#handbook-form').submit(function(){
		var quanityValue = $('#custom-quanity').val();
		$('#qty').val(quanityValue);
		$('#addtocart').click();
		return false;
	});
	
}

function categorySmartphone(){

	globalActions();
	
	$('.btn-add-cart input').click(function(){
		var parentItem = $(this).parents('.item');
		var yearsValue = $('input[name=custom-years]:checked', parentItem).val();
		var OSValue = $('#custom-os', parentItem).val();
		var renewValue = $('#custom-renew', parentItem).val();
		var quanityValue = $('#custom-quanity', parentItem).val();
		$('#custcol_years_matrix_items', parentItem).val(yearsValue);
		$('#custcol_os_matrix', parentItem).val(OSValue);
		$('#custcol_auto_renew', parentItem).val(renewValue);
		$('#qty', parentItem).val(quanityValue);		
		$('#addtocart', parentItem).click();
	});
	$('.btn-small-blue').click(function(){
		var parentItem = $(this).parents('.item');
		$('.btn-small-blue').show();
		$('.item').each(function(){
			$('#product-options', this).hide();
		});
		$(this).hide();
		$('#product-options', parentItem).fadeIn('slow');
		return false;
	});
}

function productOnline(){
	
	// call the global actions
	globalActions();
	
	// builds the custom selections
	$('#custom-renew').html($('#custcol_auto_renew').html());

	// custom form submit
	$('#custom-form').submit(function(){
		var yearsValue = $('input[name=custom-years]:checked').val();
		var renewValue = $('#custom-renew').val();
		var quanityValue = $('#custom-quanity').val();
		$('#custcol_years_matrix_items').val(yearsValue);
		$('#custcol_auto_renew').val(renewValue);
		$('#qty').val(quanityValue);
		$('#addtocart').click();
		return false;
	});
}

function productBasic(){
	// call the global actions
	globalActions();
	
	// custom form submit
	$('form .btn-add-cart').click(function(){
		var quanityValue = $('#custom-quanity').val();
		$('#qty').val(quanityValue);
		$('#addtocart').click();
		return false;
	});
}

function categoryTabs() {
	$('.video a').click(function(){
		$('.video').delegate('.play-below a', 'click', function(){
			$('#mobile-video').hide();
			$('.video *').show();
			$('.play-below').hide();
		});
		$('#mobile-video').slideDown('fast');
		$('.video *').hide();
		$('.video').append('<p class="play-below">Play the video below,<br />or <a href="#">go back</a>.</p>')
		trackClick('mobile-video', 'Video');
		return false;
	});
	$('.newsletter a').click(function(){
		var url = $(this).attr('href');
		window.open(url, 'newsletter', 'scrollbars=yes, resizable=yes, height=500, width=500');
		trackClick('newsletter');
		return false;
	});
	
	
}
