$(document).ready(function(){
	
	$('#work-case .grid_4').hover(function(){
		$(this).find('.roll').animate({opacity:1},200);
	},function(){
		$(this).find('.roll').animate({opacity:0},200);
	});
	

	
	function animateOpacity(element) {
		$(element).animate({'opacity' : 1}, 200);
	}
	
	$('#twitter,#youtube,#gowalla,#facebook').css('opacity','0.2');
	
	$('#twitter, #youtube, #gowalla, #facebook').hover(function(){
		$(this).animate({opacity: 1},200);							
	}, function(){
		$(this).animate({opacity: 0.2},200);	
	});
	
	
	$('input:text, textarea').focusin(function(){
		$(this).css('border','#99cc00 solid 1px');							 
	});
	
	$('input:text, textarea').focusout(function(){
		$(this).css('border','#161616 solid 1px');								  
	});
	
	$('.map_icon .map a').click(function(e) {
		$('.map_icon .map a').removeClass('pick');
		$(this).addClass('pick');
		var category = $(this).attr('rel');
		$(this).animate({'opacity' : 1}, 200);
		if ($(this).hasClass('actionbutton')) {
			$('.map_icon .map a').animate({'opacity' : 1}, 200);	
		} else {
			$('.map_icon .map a').not('.actionbutton, .pick').animate({'opacity' : 0.3}, 200);
		}
		maps(category);
		e.preventDefault();
		return false;
	});
	
	$('.map_icon .map a').mouseover(function() {
		$(this).animate({'opacity' : 1}, {'duration' : 200, 'queue': false});
	});
	
	$('.map_icon .map a').mouseout(function() {
		if ($('.map_icon .actionbutton').hasClass('pick') == false) {
			$(this).not('.actionbutton, .pick').animate({'opacity' : 0.3}, {'duration' : 200, 'queue' : false});
		}
	});
	
	$('.html5').each(function() {
		if (checkIfCanPlayHtml5() == false) {
			var v_id = $(this).parent('div').attr('id');
			var height = $(this).attr('height');
			var width = $(this).attr('width');
			var flvlink = $('.html5').find('source:first').attr('src');
			var control = "/video/mojoVideoULTRA-20100514.swf?nocache=" + (new Date()).getTime();
  	
			var params = {
				allowfullscreen: "true",
				allowscriptaccess: "always",
            	wmode: "transparent" 
  			};
		
  			var flashvars = {
				fileName: flvlink
  			};
		
			swfobject.embedSWF(control, v_id, width, height, "8.0.0", "", flashvars, params);
		}
	});

	function checkIfCanPlayHtml5() {
		
		var v = document.createElement("video");
		
		if ( !v.play ) {
			return false;	
		} else {
			return true;	
		}
	}
	
	$('.team_separate').hover(function() {
		$(this).animate({'opacity' : 0}, {'duration' : 200, 'queue' : false});
		$('.team_full').animate({'opacity' : 1}, {'duration': 200, 'queue' : false});
	}, function() {
		$(this).animate({'opacity' : 1}, {'duration' : 200, 'queue' : false});
		$('.team_full').animate({'opacity' : 0}, {'duration': 200, 'queue' : false});		
	});
	
	function imageRotator(mainelement){
	
		var currentImage = 0;
		var viewerContainer = $(mainelement+'.image_viewer');
		var viewerImages = $(mainelement+'.media_item,'+mainelement+' .html5');
		var listSize = $(mainelement+'.image_queue li').length-1;	
		var animatingImage = false;		
		var height = '';		
		
		// add controls if js is enabled
		viewerContainer.prepend('<div class="image_overlay"></div><div class="image_next"><img src="/images/image_next.png"/></div><div class="image_prev"><img src="/images/image_prev.png" /></div>');	
			
		
		// function constructor, load first image and set container height
		viewerContainer.append($(viewerImages.eq(currentImage))); // add image to container													
		height = $(viewerImages.eq(currentImage)).attr('height'); // get image height			
		//viewerContainer.css({height: (height+'px')}); // set height		
		viewerContainer.animate({ height : (height+'px') }, 800, 'swing',function(){
			//$(mainelement).prepend('<img src="images/close.png" alt="Close" class="projects_view" />');	
			//$('.projects_view').fadeIn(400);
			// lookup title tags
			if(viewerImages.eq(currentImage).attr('title')) {					
				$(mainelement+'.image_overlay').html(viewerImages.eq(currentImage).attr('title')); // add title tags to overlay
				$(mainelement+'.image_overlay').css({display:'block'}).animate({ height : '30px' }, 200, 'swing').addClass('active');  // animate
			}
		});	// animate set height
		$(viewerImages.eq(currentImage)).css({display: 'none'}).fadeIn(400); 
			
				
			
		
		$(mainelement+'.image_next').click(function(){		
			if(!animatingImage) {
				animatingImage = true;							
				nextImage = (currentImage < listSize)? currentImage + 1  : 0 ;	// if next image is last in queue current image is first
				// info div, animate and set next content				
				$(mainelement+'.image_overlay').animate({ height : '0px' }, 200, 'swing',function(){
					$(this).css({display:'none'});	 
					$(this).removeClass('active');
				});							
				viewerContainer.prepend(viewerImages.eq(nextImage).css({display:'block'}));	// add image to container							
				height = viewerImages.eq(nextImage).height(); // get and animate container height (for position absolute)					
				viewerContainer.animate({ height : (height+'px') }, 400, 'swing');	
				// remove last image						
				viewerImages.eq(currentImage).fadeOut(400, function(){
					$(this).remove();
					animatingImage = false;				
				});			
				currentImage = nextImage;
				// show overlay info	
				if(viewerImages.eq(currentImage).attr('title')) {			
					$(mainelement+'.image_overlay').html(viewerImages.eq(currentImage).attr('title'));
					$(mainelement+'.image_overlay').css({display:'block'}).animate({ height : '30px' }, 200, 'swing').addClass('active');
				}		
			}			
		});	
		
		
		// prev button
		$(mainelement+'.image_prev').click(function(){					
			if(!animatingImage) {
				animatingImage = true;
				prevImage = (currentImage > 0)? currentImage - 1  : listSize ;					
				$(mainelement+'.image_overlay').animate({ height : '0px' }, 200, 'swing',function(){
					$(this).css({display:'none'});	 
					$(this).removeClass('active');
				});					
				viewerContainer.prepend(viewerImages.eq(prevImage).css({display:'block'}));				
				height = viewerImages.eq(prevImage).height();			
				viewerContainer.animate({ height : (height+'px') }, 400, 'swing');			
				viewerImages.eq(currentImage).fadeOut(400, function(){
					$(this).remove();	
					animatingImage = false;
				});			
				currentImage = prevImage;			
				if(viewerImages.eq(currentImage).attr('title')) {			
					$(mainelement+'.image_overlay').html(viewerImages.eq(currentImage).attr('title'));
					$(mainelement+'.image_overlay').css({display:'block'}).animate({ height : '30px' }, 200, 'swing').addClass('active');
				}					
			}		
		});	
		
		// hide and show next and prev buttons
		$(mainelement+".image_next, "+mainelement+".image_prev").mouseenter(function(ev){			
			$(this).animate({'opacity' : 1}, 200,function(){ $(this).addClass('activeNav'); });			
		}).mouseleave(function() {		
			$(this).animate({'opacity' : 0}, 200,function(){ $(this).removeClass('activeNav'); });		
		});
		
		// move next and prev buttons using mouse position
		$(mainelement+".image_viewer").mousemove(function(e){	
			var offset = ($(mainelement).offset().top)? $(mainelement).offset().top : $(mainelement)[0].offsetTop ;
			var mpos = (e.pageY-offset);
			if (mpos < -24 || mpos > (height - 59))
			{
				$(mainelement+".image_next img, "+mainelement+".image_prev img").css('display', 'none');	
			}
			else
			{
				$(mainelement+".image_next img, "+mainelement+".image_prev img").css('display', 'inline');
			}
			$(mainelement+".image_next img, "+mainelement+".image_prev img").css({ top : (mpos+'px') });			
		});	
		
	}
	var rotator1 = imageRotator('#media_1 ');
	var rotator2 = imageRotator('#media_2 ');
	var rotator3 = imageRotator('#media_3 ');
	var rotator4 = imageRotator('#media_4 ');
	var rotator5 = imageRotator('#media_5 ');
	var rotator6 = imageRotator('#media_6 ');
	var rotator7 = imageRotator('#media_7 ');
});

function replaceAnchor(text) {
  var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
  return text.replace(exp,"<a href='$1'>$1</a>"); 
}

function maps(category)
{
//http://maps.google.co.uk/maps?f=q&source=s_q&hl=en&geocode=&q=mojofuel&sll=53.476528,-2.235503&sspn=0.008901,0.01929&g=53.476723,-2.236966&ie=UTF8&ll=53.475583,-2.235932&spn=0.008901,0.01929&z=16&iwloc=A
	marker = [];
	contentstring = [];
	if (category == null || category == '' || category == undefined) {
		category = '';	
	}
	var myLatlng = new google.maps.LatLng(53.476528,-2.235503);
	var data = {};
	$.ajax({url: 'data.php?service=map&filter='+category, async:false,dataType:'json',success: function(result){data = result;}});
	var myOptions = {
		zoom: 15,
		center: myLatlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	var map = new google.maps.Map(document.getElementById("map_section"), myOptions);
	for (i=0; i < data.length; i++) {
		latlng = new google.maps.LatLng (data[i].latitude, data[i].longitude);
		createMarker (data[i].name,
					    i,
						data[i].description,
						map,
						latlng, 
						data[i].pinImage, 
						data[i].category)
	}

}

var infowindow = new google.maps.InfoWindow({
	maxWidth:478
});

function createMarker(name, detail, description, map, latlng, pinImage, category) {
		contentstring[detail] = '<div class="info"><p class="rotator77">'+name+'</p><p class="rotator57">'+description+'</p></div>';
		marker[detail] = new google.maps.Marker({
			position: latlng,
			map: map,
			icon: pinImage,
			animation: google.maps.Animation.DROP
			});
		google.maps.event.addListener(marker[detail], 'click', function() {
			infowindow.setContent(contentstring[detail]);
			infowindow.set("isdomready",false);
			infowindow.open(map,marker[detail]);
			});
}
function accessmarker(d){
			infowindow.setContent(contentString[d]);
			infowindow.set("isdomready",false);
			infowindow.open(map,marker[d]);
}
