$(function()
{
	$('#upload-tags').autocomplete('/tips/uploads_autocomplete.php',
	{
		matchContains: true,
		minChars: 0,
		multiple: true
	});
	
	$('#uploads-search').autocomplete('/tips/uploads_autocomplete.php',
	{
		matchContains: true,
		minChars: 0
	});
	
	$('#article-tags').autocomplete('/tips/article_autocomplete.php',
	{
		matchContains: true,
		minChars: 0,
		multiple: true
	});
	
	$('#shredaton-faq a.head').click(function()
	{
		$(this).next().slideToggle('slow');
		return false;
	});
	
	$("#corner").hover(
		function()
		{
			$("#corner img , #corner_content").stop().animate({
				width: '307px',
				height: '319px'
			}, 500);
		},
		function()
		{
			$("#corner img").stop().animate({
				width: '50px',
				height: '52px'
			}, 220);
			$("#corner_content").stop().animate({
				width: '50px',
				height: '50px'
			}, 200);
		}
	);
	
	$.fn.qtip.styles.iotrak = {
		name: 'light',
		background: '#F1F1F1',
		border: {
			color: '#CFD7DC',
			radius: 6,
			width: 1
		},
		color: 'black',
		padding: 5,
		textAlign: 'justify',
		tip: 'bottomMiddle',
		width: 300
	}

	$('.articletip').each(function()
	{
		$(this).qtip({
			content: {
				url: '/tips/article_tooltip.php',
				data: {
					id: $(this).attr('rel')
				}
			},
			style: 'iotrak',
			show: 'mouseover',
			hide: 'mouseout',
			position: {
				corner: {
					target: 'topMiddle',
					tooltip: 'bottomMiddle'
				}
			}
		});
	});
	
	$('.bubble').each(function()
	{
		$(this).qtip({
			content: $(this).attr('alt'),
			style: 'iotrak',
			show: 'mouseover',
			hide: 'mouseout',
			position: {
				corner: {
					target: 'topMiddle',
					tooltip: 'bottomMiddle'
				}
			}
		});
	});
	
	$('.kodak-scanner').each(function()
	{
		var src = $(this).attr('src');
		var parts = src.split(".");
		var desc = parts[0] + '-desc.jpg';
		$(this).qtip({
			content: '<img src="' + desc + '" alt="' + desc + '" />',
			style: {
				height: 122,
				width: 530,
				padding: 5
			},
			show: 'mouseover',
			hide: 'mouseout',
			position: {
				corner: {
					target: 'topRight',
					tooltip: 'bottomLeft'
				}
			}
		});
	});
	
	$('.upload-grid').each(function()
	{
		$(this).qtip({
			content: '<img src="' + $(this).attr('src') + '" alt="image" />',
			position: {
				corner: {
					target: 'topRight',
					tooltip: 'bottomLeft'
				}
			}
		});
	});
	
	$('.upload-info').each(function()
	{
		$(this).qtip({
			content: {
				url: '/tips/uploads_tooltip.php',
				data: {
					id: $(this).attr('rel')
				}
			},
			style: {
				width: 400,
				padding: 5
			},
			show: 'mouseover',
			hide: 'mouseout',
			position: {
				corner: {
					target: 'topRight',
					tooltip: 'bottomLeft'
				}
			}
		});
	});
	
	$('#show-current').qtip({
		content: '<img src="' + $('#show-current').attr('href') + '" alt="' + $('#show-current').attr('href') + '" />',
		style: {
			width: 'auto',
			padding: 5
		},
		show: 'mouseover',
		hide: 'mouseout',
		position: {
			corner: {
				target: 'bottomRight',
				tooltip: 'topLeft'
			}
		}
	});
	
	$('#show-700-demo').click(function()
	{
		var demo = window.open(
			'/tips/kodak_700_series.php',
			'Choose Images',
			'status=0,toolbar=0,location=0,menubar=0,resizeable=allow,scrollbars=1,height=425,width=565'
		);
	});
	
	$('#show-current').click(function()
	{
		return false;
	});
	
	$('.confirm-delete-account').each(function(){
		$(this).click(function(){
			if (confirm('You are about to delete your account. Are you sure you want to continue?'))
				window.location = 'http://www.iotrak.com/account/delete';
		});
	});
	
	$('.admin-delete-account').each(function(){
		$(this).click(function(){
			if (confirm('You are about to delete your account. Are you sure you want to continue?'))
			{
				var strid = $(this).attr('id');
				var id = strid.replace('iotrak_user_', '');
				window.location = 'http://www.iotrak.com/admin/users/delete/' + id;
			}
		});
	});
	
	$('.news-articles').each(function(){
		$(this).tablesorter();
	});
	
	$('.sorttable').each(function(){
		$(this).tablesorter(); 
	});
	
	$('#article_display_yes').click(function(){
		$('input[name="access_level"]').attr('value', '0');
		$('input[name="access_level"]').attr('disabled', 'disabled');
	});
	
	$('#article_display_no').click(function(){
		$('input[name="access_level"]').removeAttr('disabled');
		$('input[name="access_level"]').attr('enabled', 'enabled');
	});
	
	$('#box_calc_inches').keyup(function()
	{
		var inches = parseInt( $(this).val() );
		
		if (inches != 0)
		{
			var boxes = Math.ceil( inches / 15 );
			var docs = Math.ceil( boxes * 2800 );
			
			$('#box_calc_containers').html(boxes);
			$('#box_calc_docs').html(docs);
		}
		else
		{
			$('#box_calc_containers').html('');
			$('#box_calc_docs').html('');
		}
	});
	
	$('#pickup_zip').keyup(function()
	{
		var zip = $(this).val();
		if (zip.length > 4)
		{
			$.get('/ajax/pickup.php', { zip: zip }, function(data)
			{
				if (data == '0')
				{
					$('#pickup_zone').html('N/A');
					$('#pickup_cost').html('N/A');
					alert('Sorry, we do not currently offer pickup services in your area.');
				}
				else if (data == 'A')
				{
					$('#pickup_zone').html('A');
					$('#pickup_cost').html('$5.00');
				}
				else if (data == 'B')
				{
					$('#pickup_zone').html('B');
					$('#pickup_cost').html('$7.50');
				}
				else if (data == 'C')
				{
					$('#pickup_zone').html('C');
					$('#pickup_cost').html('$12.00');
				}
				else if (data == 'D')
				{
					$('#pickup_zone').html('D');
					$('#pickup_cost').html('N/A');
					alert('Please contact i/oTrak for a pickup quote!');
				}
			});
		}
	});
	
	$('#storage_quantity').keyup(function()
	{
		calcStorageRequirements();
	});
	
	$('#storage_docsize').change(function()
	{
		calcStorageRequirements();
	});
	
	$('#storage_scanres').change(function()
	{
		calcStorageRequirements();
	});
	
	$('#storage_colordepth').change(function()
	{
		calcStorageRequirements();
	});
	
	$('#shredding_unit_cost').keyup(function()
	{
		calcShreddingCosts();
	});
	
	$('#shredding_avg_units').keyup(function()
	{
		calcShreddingCosts();
	});
	
	$('#shredding_rental_fees').keyup(function()
	{
		calcShreddingCosts();
	});
	
	$('#shredding_fuel_fees').keyup(function()
	{
		calcShreddingCosts();
	});
	
	$('#shredding_other_fees').keyup(function()
	{
		calcShreddingCosts();
	});
	
	$('#shredding_monthly_pounds').keyup(function()
	{
		calcShreddingCosts();
	});
	
	$('#green_shred_pounds').keyup(function()
	{
		calcGreenBenefits();
	});
	
	setInterval("changeSlide()", 8000);
	
	setInterval("changeBottomLeftService()", 7000);
	setInterval("changeBottomCenterService()", 12000);
	setInterval("changeBottomRightService()", 10000);
	
	$('.article_browse_images').click(function()
	{
		var browse = window.open(
			'/admin/popups/choose_images.php',
			'Choose Images',
			'status=0,toolbar=0,location=0,menubar=0,resizeable=allow,scrollbars=1,height=400,width=300'
		);
	});
	
	$('#article_choose_icon').click(function()
	{
		var browse = window.open(
			'/admin/popups/choose_icon.php',
			'Choose Images',
			'status=0,toolbar=0,location=0,menubar=0,resizeable=allow,scrollbars=1,height=400,width=300'
		);
	});

	$('.article-choose-icon').click(function()
	{
		var image = '<img src="' + $(this).attr('src') + '" alt="icon" style="height: 50px; width: 50px;" />';
		var input = '<input type="hidden" name="article_icon" value="' + $(this).attr('rel') + '" />';
		
		window.opener.$('#article-icon').html(image + input);
	});

	$('.confirm-delete').click(function()
	{
		return confirm('Are you sure you want to delete this item?');
	});
	
	$('.confirm-copy').click(function()
	{
		return confirm('Do you want to create a copy of this item?');
	});
	
	$('.lazyload').lazyload({
		placeholder: "/images/gray.gif",
		effect: "fadeIn"
	});

	if (typeof CKEDITOR != 'undefined')
	{
		if ($('#article_content').length)
		{
			CKEDITOR.replace('article_content',
			{
				height: 500,
				width: 950
			});
		}
		
		if ($('#article_description').length)
		{	
			CKEDITOR.replace('article_description',
			{
				height: 200,
				width: 950
			});
		}
		
		if ($('#upload_description').length)
		{
			CKEDITOR.replace('upload_description',
			{
				height: 160,
				toolbar:
				[
					['Source'],
					['Cut','Copy','Paste','PasteText','PasteFromWord'],
					['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],
					['NumberedList','BulletedList'],
					['Font','FontSize','-','TextColor','BGColor'],
					['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
					['Link','Unlink','Anchor'],
					['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'],
				],
				width: 560
			});
		}
	}

	$('.folder-nav ul li a').each(function()
	{
		$(this).click(function()
		{
			$('.folder-nav ul li').each(function()
			{
				$(this).removeClass();
			});
			
			$(this).parent().addClass('active');
			
			$('#admin-news-iotrak').css('display', 'none');
			$('#admin-news-leonides').css('display', 'none');
			$('#admin-news-mammologix').css('display', 'none');
			$('#admin-news-misc').css('display', 'none');
			$('#admin-news-photoscan').css('display', 'none');
			$('#admin-news-quotes').css('display', 'none');
			
			var found = true;
			switch( $(this).html() )
			{
				case 'i/oTrak':
					$('#admin-news-iotrak').css('display', 'block');
					break;
				case 'Leonides Coalition':
					$('#admin-news-leonides').css('display', 'block');
					break;
				case 'Mammologix':
					$('#admin-news-mammologix').css('display', 'block');
					break;
				case 'Misc':
					$('#admin-news-misc').css('display', 'block');
					break;
				case 'Photo Scanning':
					$('#admin-news-photoscan').css('display', 'block');
					break;
				case 'Quotes':
					$('#admin-news-quotes').css('display', 'block');
					break;
				default:
					found = false;
					break;
			}
			
			return !found;
		});
	});

	$('#kodak-nav ul li a').each(function()
	{
		$(this).click(function()
		{
			$('#kodak-nav ul li').each(function()
			{
				$(this).removeClass();
			});
			
			$(this).parent().addClass('active');
			
			$('#kodak-networked').css('display', 'none');
			$('#kodak-workgroup').css('display', 'none');
			$('#kodak-departmental').css('display', 'none');
			$('#kodak-production').css('display', 'none');
			$('#kodak-check-scanners').css('display', 'none');
			$('#kodak-software').css('display', 'none');
			
			$('#kodak-product-overview').css('display', 'none');
			$('#kodak-product-specs').css('display', 'none');
			$('#kodak-product-support').css('display', 'none');
			
			$('#kodak-wp-education').css('display', 'none');
			$('#kodak-wp-financial').css('display', 'none');
			$('#kodak-wp-government').css('display', 'none');
			$('#kodak-wp-healthcare').css('display', 'none');
			$('#kodak-wp-insurance').css('display', 'none');
			$('#kodak-wp-manufacturing').css('display', 'none');
			$('#kodak-wp-publishing').css('display', 'none');
			$('#kodak-wp-records').css('display', 'none');
			$('#kodak-wp-retail').css('display', 'none');
			$('#kodak-wp-service').css('display', 'none');
			$('#kodak-wp-transportation').css('display', 'none');
			$('#kodak-wp-position').css('display', 'none');
			$('#kodak-wp-funeral').css('display', 'none');
			
			switch( $(this).html() )
			{
				case 'Networked':
					$('#kodak-networked').css('display', 'block');
					break;
				
				case 'Workgroup':
					$('#kodak-workgroup').css('display', 'block');
					break;
				
				case 'Departmental':
					$('#kodak-departmental').css('display', 'block');
					break;
				
				case 'Production':
					$('#kodak-production').css('display', 'block');
					break;
				
				case 'Check Scanners':
					$('#kodak-check-scanners').css('display', 'block');
					break;
				
				case 'Software':
					$('#kodak-software').css('display', 'block');
					break;
				
				case 'Overview':
					$('#kodak-product-overview').css('display', 'block');
					break;
				
				case 'Specifications':
					$('#kodak-product-specs').css('display', 'block');
					break;
				
				case 'Support':
					$('#kodak-product-support').css('display', 'block');
					break;
				
				case 'Education':
					$('#kodak-wp-education').css('display', 'block');
					break;
				
				case 'Financial Services':
					$('#kodak-wp-financial').css('display', 'block');
					break;
				
				case 'Government':
					$('#kodak-wp-government').css('display', 'block');
					break;
				
				case 'Healthcare':
					$('#kodak-wp-healthcare').css('display', 'block');
					break;
				
				case 'Insurance':
					$('#kodak-wp-insurance').css('display', 'block');
					break;
				
				case 'Manufacturing':
					$('#kodak-wp-manufacturing').css('display', 'block');
					break;
				
				case 'Publishing':
					$('#kodak-wp-publishing').css('display', 'block');
					break;
				
				case 'Records Management':
					$('#kodak-wp-records').css('display', 'block');
					break;
				
				case 'Retail':
					$('#kodak-wp-retail').css('display', 'block');
					break;
				
				case 'Service Bureau':
					$('#kodak-wp-service').css('display', 'block');
					break;
				
				case 'Transportation':
					$('#kodak-wp-transportation').css('display', 'block');
					break;
				
				case 'Position Papers':
					$('#kodak-wp-position').css('display', 'block');
					break;
				
				case 'Funeral Homes':
					$('#kodak-wp-funeral').css('display', 'block');
					break;
			}
			
			return false;
		});
	});
});

function calcGreenBenefits()
{
	var pounds = ( $('#green_shred_pounds').val() == '' ? 0 : parseInt( $('#green_shred_pounds').val() ) );
	
	var trees = pounds * (0.00758988294314381);
	var landfill = pounds * (0.00133988294314381);
	var gallons = pounds * (3.13503344481605);
	var electricity = pounds * (1.83026755852843);
	var pollution = pounds * (0.0267558528428094);
	
	$('#green_trees_saved').html(trees);
	$('#green_landfill_saved').html(landfill);
	$('#green_gallons_saved').html(gallons);
	$('#green_electricity_saved').html(electricity);
	$('#green_pollution_saved').html(pollution);
}

function calcShreddingCosts()
{
	var unit_cost = ( $('#shredding_unit_cost').val() == '' ? 0 : parseFloat( $('#shredding_unit_cost').val() ) );
	var avg_units = ( $('#shredding_avg_units').val() == '' ? 0 : parseFloat( $('#shredding_avg_units').val() ) );
	var rental_fees = ( $('#shredding_rental_fees').val() == '' ? 0 : parseFloat( $('#shredding_rental_fees').val() ) );
	var fuel_fees = ( $('#shredding_fuel_fees').val() == '' ? 0 : parseFloat( $('#shredding_fuel_fees').val() ) );
	var other_fees = ( $('#shredding_other_fees').val() == '' ? 0 : parseFloat( $('#shredding_other_fees').val() ) );
	var pounds = ( $('#shredding_monthly_pounds').val() == '' ? 0 : parseFloat( $('#shredding_monthly_pounds').val() ) );
	
	if (pounds != 0)
		var their_cost = ((unit_cost * avg_units) + rental_fees + fuel_fees + other_fees);
	else
		var their_cost = 0;
	
	their_cost = Math.round(their_cost * Math.pow(10, 2)) / Math.pow(10, 2);
	
	var our_boxes = Math.floor( pounds / 35 );
	if (our_boxes < 1) our_boxes = 1;
	
	var our_cost = (our_boxes * 10);
	if (our_cost == 0)
		var our_delivery = 0;
	else
		var our_delivery = our_cost + 7.5;
	
	$('#monthly_shredding_cost').html('$' + their_cost);
	$('#ezshred_dropoff_cost').html('$' + our_cost);
	$('#ezshred_pickup_cost').html('$' + our_delivery);
}	
	
function calcStorageRequirements()
{
	var quantity = parseInt( $('#storage_quantity').val() );
	var size = parseInt( $('#storage_docsize').val() );
	var res = parseInt( $('#storage_scanres').val() );
	var color = parseInt( $('#storage_colordepth').val() );
	
	var totalsize = ((res * res * 8.5 * size * color) / 8) * quantity;
	var size5comp = totalsize * .95;
	var size10comp = totalsize * .9;
	var size15comp = totalsize * .85;
	
	$('#storage_requirements_nocomp').html(formatSize(totalsize));
	$('#storage_requirements_5comp').html(formatSize(size5comp));
	$('#storage_requirements_10comp').html(formatSize(size10comp));
	$('#storage_requirements_15comp').html(formatSize(size15comp));
}

function changeSlide()
{
	var shred_display = $("#shred_easy").css("display");
	var store_display = $("#store_easy").css("display");
	var scan_display = $("#scan_easy").css("display");
	var send_display = $("#send_easy").css("display");
	
	if (shred_display == 'inline')
	{
		$("#scan_easy").css("display", "none");
		$("#send_easy").css("display", "none");
		$("#shred_easy").fadeOut("slow", function()
		{
			$("#store_easy").fadeIn("slow");
		});
	}
	else if (store_display == 'inline')
	{
		$("#shred_easy").css("display", "none");
		$("#send_easy").css("display", "none");
		$("#store_easy").fadeOut("slow", function()
		{
			$("#scan_easy").fadeIn("slow");
		});
	}
	else if (scan_display == 'inline')
	{
		$("#shred_easy").css("display", "none");
		$("#store_easy").css("display", "none");
		$("#scan_easy").fadeOut("slow", function()
		{
			$("#send_easy").fadeIn("slow");
		});
	}
	else
	{
		$("#store_easy").css("display", "none");
		$("#scan_easy").css("display", "none");
		$("#send_easy").fadeOut("slow", function()
		{
			$("#shred_easy").fadeIn("slow");
		});
	}
}

function changeBottomLeftService()
{
	var slide1 = $("#bot-l-1");
	var slide2 = $("#bot-l-2");
	var slide3 = $("#bot-l-3");
	
	if (slide1.css("display") == "block")
	{
		slide1.fadeOut("slow", function()
		{
			slide2.fadeIn("slow");
		});
	}
	else if (slide2.css("display") == "block")
	{
		slide2.fadeOut("slow", function()
		{
			slide3.fadeIn("slow");
		});
	}
	else
	{
		slide3.fadeOut("slow", function()
		{
			slide1.fadeIn("slow");
		});
	}
}

function changeBottomCenterService()
{
	var slide1 = $("#bot-c-1");
	var slide2 = $("#bot-c-2");
	if (slide1.css("display") == "none")
	{
		slide2.fadeOut("slow", function()
		{
			slide1.fadeIn("slow");
		});
	}
	else
	{
		slide1.fadeOut("slow", function()
		{
			slide2.fadeIn("slow");
		});
	}
}

function changeBottomRightService()
{
	var slide1 = $("#bot-r-1");
	var slide2 = $("#bot-r-2");
	var slide3 = $("#bot-r-3");
	var slide4 = $("#bot-r-4");
	var slide5 = $("#bot-r-5");
	var slide6 = $("#bot-r-6");
	var slide7 = $("#bot-r-7");
	var slide8 = $("#bot-r-8");
	var slide9 = $("#bot-r-9");
	var slide10 = $("#bot-r-10");
	if (slide1.css("display") == "block")
	{
		slide1.fadeOut("slow", function()
		{
			slide2.fadeIn("slow");
		});
	}
	else if (slide2.css("display") == "block")
	{
		slide2.fadeOut("slow", function()
		{
			slide3.fadeIn("slow");
		});
	}
	else if (slide3.css("display") == "block")
	{
		slide3.fadeOut("slow", function()
		{
			slide4.fadeIn("slow");
		});
	}
	else if (slide4.css("display") == "block")
	{
		slide4.fadeOut("slow", function()
		{
			slide5.fadeIn("slow");
		});
	}
	else if (slide5.css("display") == "block")
	{
		slide5.fadeOut("slow", function()
		{
			slide6.fadeIn("slow");
		});
	}
	else if (slide6.css("display") == "block")
	{
		slide6.fadeOut("slow", function()
		{
			slide7.fadeIn("slow");
		});
	}
	else if (slide7.css("display") == "block")
	{
		slide7.fadeOut("slow", function()
		{
			slide8.fadeIn("slow");
		});
	}
	else if (slide8.css("display") == "block")
	{
		slide8.fadeOut("slow", function()
		{
			slide9.fadeIn("slow");
		});
	}
	else if (slide9.css("display") == "block")
	{
		slide9.fadeOut("slow", function()
		{
			slide10.fadeIn("slow");
		});
	}
	else
	{
		slide10.fadeOut("slow", function()
		{
			slide1.fadeIn("slow");
		});
	}
}

function formatSize(bytes)
{
	i = 0;
	while (bytes >= 1024)
	{
		if (i > 3) break;
		bytes = bytes / 1024;
		i++;
	}
	
	var roundBytes = Math.ceil(bytes);
	var size = 'Bytes';
	
	if (i == 1) size = 'KB';
	if (i == 2) size = 'MB';
	if (i == 3) size = 'GB';
	if (i > 3) size = 'TB';

	return roundBytes + ' ' + size;
}