
if((jQuery(window).width() <= 480) || ( window.devicePixelRatio >= 2)) {
	jQuery('#screen_style').remove();	
} else {
	jQuery('#handheld_style').remove();
}

var hideUrlBar = function() {
	if (window.pageYOffset <= 0) {
		window.scrollTo(0, 1);
	}
};
/*
jQuery(window).live('load', function() {
	window.setTimeout(hideUrlBar, 0);
},
false);
*/

function load_mobile_init() {
	console.log('load_mobile_init()');
	jQuery(function($) {

		window.setTimeout(hideUrlBar, 0);

		manage_event_handlers();

		var whatpage = $('#body div:first-child').attr('class');
		whatpage = whatpage.split(/ +/);
		whatpage = whatpage[1];

		switch (whatpage) {

		case 'news-list':
			console.log('=> news-list');
			resizeNewsImages(320);
			addReadMoreNews();

			break;

		case 'current-projects':
			console.log('=> current-projects');

			resizeProjectListImages(320);
			getProjectListTitles();


			break;

		case 'case-study':
			console.log('=> case-study');
			resizeProjectImages(320);
			remWorkItemImageTitle();
			insertReturnToProjects();
			break;

		case 'info-page':
			console.log('=> info-page');
			resizeProjectImages(320);
			replaceInfoHeader();
			break;

		default:
			console.log('=> default / error');
			alert('error - should never happen.');

			break;

		}


		//this happens on all pages
		
		$('#header img').attr({
			'width': 23,
			'height': 20
		}).fadeIn(300);

		return false;

	});
}



function insertReturnToProjects() {
	jQuery(function($) {

		var insertHTML = '<a href="work.html" class="backlink">Back to Projects</a>';
				
		$(insertHTML).insertAfter('.case-study .section:last-child');

		return false;
	});
}



function addReadMoreNews() {
	jQuery(function($) {
		
		var readMoreHTML = '<span class="readmorelink">Read More</span>';
		
		$('.news-list .text-wrapper .body p:not(:first-child)').hide();
		
		$('.news-list .article').each(function(){
			var numParas = $(this).find('p').length;
			
			if (numParas > 1) {
				$(readMoreHTML).insertAfter($(this).find('p:first-child'));
			}
		});		
	
		return false;
	});
}



function replaceInfoHeader() {
	jQuery(function($) {
		var checkText = $('.body .section.side:first-child h2:first-child').text();
		
		$('.body .section.side:first-child h2:first-child').empty();
	
		if(checkText == 'Get in touch') {
			$('<img src="images/text/get_in_touch_mobile.png" alt="' + checkText + '" width="108" height="25" />').appendTo('.body .section.side:first-child h2:first-child');
		}

		if(checkText == 'We are Everywhere') {
			$('<img src="images/text/about_us_mobile.png" alt="' + checkText + '" width="185" height="25" />').appendTo('.body .section.side:first-child h2:first-child');
		}
		

	
		return false;
	});
}



function swapOutFirstItemHeader() {
	console.log('swapOutFirstItemHeader()');
	jQuery(function($) {

		$('.slideshow .header h2 img').hide();

		var temptesting = $('.slideshow .header h2 img').attr('src');
		temptesting = temptesting.slice(0, -9);
		temptesting = temptesting + 'mobile-title.png';

		var img = new Image();

		img.onload = function() {

			var newWidth = (this.width / this.height) * 25;

			$('.slideshow .header h2 img').attr({
				'src': temptesting,
				'height': 25,
				'width': newWidth
			}).show();

			$('.slideshow .header h2').show();

		};

		img.src = temptesting;

		$('.slideshow .header').insertAfter($('.slideshow ul'));

		return false;

	});
}



function getProjectListTitles() {
	console.log('getProjectListTitles()');
	jQuery(function($) {

		$('.slideshow ul li h3 img, .project-list ul li h3 img').hide();

		$('.slideshow ul li h3 img, .project-list ul li h3 img').each(function() {

			var $ourimage = $(this);

			var temptesting = $ourimage.attr('src');
			temptesting = temptesting.slice(0, -13);
			temptesting = temptesting + 'mobile-title.png';

			var img = new Image();

			img.onload = function() {

				var newWidth = (img.width / img.height) * 25;

				$ourimage.attr({
					'src': temptesting,
					'height': 25,
					'width': newWidth
				}).show();

			};

			img.src = temptesting;

		});

		$('.slideshow ul li h3 img, .project-list ul li h3 img').show();

		return false;

	});
}



function manage_event_handlers() {
	console.log('manage_event_handlers()');
	jQuery(function($) {

		$('.readmorelink').live('click', function() {
			$(this).hide();
			$(this).parent().find('p').fadeIn(600);
		});

		return false;

	});
}



function makeFirstlinkWrapImage() {
	console.log('makeFirstlinkWrapImage()');
	jQuery(function($) {

		var our_link = $('.header .slideshow .header a').attr('href');

		$('.header .slideshow .header a').hide();

		$('.header .slideshow li').bind('click', function() {
			window.location = our_link;
		});

		return false;

	});
}



function remWorkItemImageTitle() {
	console.log('remWorkItemImageTitle()');
	jQuery(function($) {

		var temptesting = $('.header h2').css('background-image');

		$ourimage = $('.header h2');

		temptesting = temptesting.replace(/"/g, "").replace(/url\(|\)$/ig, "");

		var img = new Image();

		img.onload = function() {

			$('.header h2').css({
				'background-image': 'none',
				'margin-left': 20
			}).show();

		};

		img.src = temptesting;

		var ratio = (img.width / img.height) * 25;

		var altText = $('.header h2').text();

		$('.header h2').empty();

		$('<img src="' + temptesting + '" height="25" width="' + ratio + '" alt="' + altText + '" />').appendTo('.header h2');

		return false;

	});
}



function resizeProjectListImages(size) {
	console.log('resizeProjectListImages(' + size + ')');
	jQuery(function($) {
		
		$('.slideshow ul li img, .project-list ul li img').not('h3 img').each(function() {

			var $currentImage = $(this);
			
			var newHeight = ($(this).height() / $(this).width()) * size;
			
			$(this).attr({
				'width': size,
				'height': newHeight
			}).hide();
			
			var temptesting = $(this).attr('src');
			temptesting = temptesting.slice(0, -19);
			temptesting = temptesting + 'overview_mobile.jpg';

			var img = new Image();

			img.onload = function() {
				var newHeight = (img.height / img.width) * size;
				
				$('<img src="' + temptesting + '" width="' + size + '" height="' + newHeight + '" />').appendTo($currentImage.parent()).hide().fadeIn(150);

			};

			img.src = temptesting;

			$currentImage.parent().parent().attr('style', 'width: ' + size + 'px; position: relative;').show();

			$currentImage.parent().parent().find('h3').insertAfter($currentImage.parent());

		});

		return false;
	});
}



function resizeProjectImages(size) {
	console.log('resizeProjectImages(' + size + ')');
	jQuery(function($) {

		$('.slideshow ul li img').each(function() {

			var obj_width = $(this).width();
			var obj_height = $(this).height();
			var modifier = obj_height / obj_width;

			$(this).attr({
				'width': size,
				'height': (size * modifier)
			}).show();

		});

		return false;

	});
}



function resizeNewsImages(size) {
	console.log('resizeNewsImages(' + size + ')');
	jQuery(function($) {

		$('.image-wrapper img.news_main').each(function() {

			var obj_width = $(this).width();
			var obj_height = $(this).height();
			var modifier = obj_height / obj_width;

			$(this).attr({
				'width': size,
				'height': (size * modifier)
			}).show();

		});
		$('.image-wrapper img.news_thumb').hide();
		return false;

	});

}

