var counter = 1;
var android;

$(document).ready(function() {
	var img = new Image();
	img.src = 'resume/images/code.png';
	android = navigator.userAgent.match(/Android/i);
	if(android) {
		window.scrollTo(0,0);
		window.scrollTo(0,1);
	}
	
	$.ajax({
		url:"resume/options.php",
		success:function(opts){
			//console.log(data);
			$('.languages').after(opts);
			$.ajax({
				   url:"resume/controls.php",
				   success:function(controls) {
					   $('.options').after(controls);
					   
					   $('#scroll-up').click(function() {
		$('html,body').animate({scrollTop: 0}, 'fast');
	});
	$('#expand').click(function() {
		var html = $(this).html();
		if(html === '+') {
			$(this).html('&ndash;');
			$(this).attr('title', 'Click to collapse all');
			counter = $('.page-item').length;
			$('.page-item').each(function() {
				var target = $(this).parent();
				if($(this).css('display') === 'none') {
					$(target).find('.arrow').html('&ndash;');
					$(target).find('h4').attr('title', 'Click to collapse details');
					$(this).animate({
						height: 'toggle'
					}, 'fast', function() {
						//
					});
				}
			});
		} else {
			$(this).html('+');
			$(this).attr('title', 'Click to expand all');
			counter = 0;
			$('.page-item').each(function() {
				var target = $(this).parent();
				if($(this).css('display') === 'block') {
					$(target).find('.arrow').html('+');
					$(target).find('h4').attr('title', 'Click to expand details');
					$(this).animate({
						height: 'toggle'
					}, 'fast', function() {
						//
					});
				}
			});
		}
	});
	$('#scroll-down').click(function() {
		$('html,body').animate({scrollTop: $(document).height()}, 'fast');
	});
				}
			});
		}
	});
	
	$('#print').click(function() {
		window.print();
		return false;
	});
	
	$('.extras').find('#tab').click(function() {
		if($('.extras').offset().left == $(document).width() - $('.extras').width()) {
			$('.extras').animate({
			left: $(document).width() - $('#tab').width() + 32
			}, 'fast', function() {
				//
			});
		} else {
			$('.extras').animate({
			left: $(document).width() - $('.extras').width()
			}, 'fast', function() {
				//
			});
		}
	});
	//START
	$('.page-item').each(function() {
		var parent = $(this).parent();
		if($(parent).find('.arrow').html() != '+') {
			$(parent).find('h4').attr('title', 'Click to collapse details');
		} else {
			$(parent).find('h4').attr('title', 'Click to expand details');
		}
		$(parent).find('h4').click(function() {
		var html = $(parent).find('.arrow').html();
		if(html === '+') {
			$(parent).find('.arrow').html('&ndash;');
			$(parent).find('h4').attr('title', 'Click to collapse details');
			counter++;
			if(counter === $('.page-item').length) {
				$('#expand').html('&ndash;');
				$('#expand').attr('title', 'Click to collapse all');
			}
		} else {
			$(parent).find('.arrow').html('+');
			$(parent).find('h4').attr('title', 'Click to expand details');
			counter--;
			if(counter === 0) {
				$('#expand').html('+');
				$('#expand').attr('title', 'Click to expand all');
			}
		}
		$(parent).find('.page-item').animate({
			height: 'toggle'
			}, 'fast', function() {
				//
			});
		});
	});
	//END
});

$(window).bind('orientationchange', function(event) {
	if(android) {
		if(window.orientation === 90 || window.orientation === -90) {
			window.scrollTo(0,0);
		}
		window.scrollTo(0,1);
	}
});

function ob(value, id) {
	var string = value.join("");
	if(id === '#email') {
		$(id).attr('href', 'mailto:' + string);
		$(id).html(string);
	}
	if(id === '#cell' && android) {
		$(id).html('<a href="tel:' + string + '">' + string + '</a>');
	} else {
		$(id).html(string);
	}
  /*
  var string = value.join("");
  if(id === "#email") {
	  $(id).attr('href', 'mailto:' + string);
  }
  $(id).html(string);
  */
}
