// JavaScript Document
		
//		clear Text

 //___________________________________________________________________________________________________________________________________

function clearText(field) {
    if (field.defaultValue === field.value) { field.value = '';
	} else if (field.value === '') {field.value = field.defaultValue;
	}
}

//		Google Analytics

 //___________________________________________________________________________________________________________________________________



  
//		infodrop

 //___________________________________________________________________________________________________________________________________
 


$(function(){
var myTimeout = null;
$("#headGroup").hover(function(){
    if (myTimeout) clearTimeout(myTimeout);
    myTimeIn = setTimeout(function() {
        $(".aboutSmall").show('fast');
    }, 400);
}, 
	function(){
	if (myTimeIn) clearTimeout(myTimeIn);
    myTimeOut = setTimeout(function() {
        $(".aboutSmall").hide('fast');
    }, 400);
	

});
});


//		Email Validation
  
 //___________________________________________________________________________________________________________________________________

function echeck(str) {

		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)===-1){
		   alert("Please enter a valid email address");
		   return false;
		}

		if (str.indexOf(at)===-1 || str.indexOf(at)===0 || str.indexOf(at)===lstr){
		   alert("Please enter a valid email address");
		   return false;
		}

		if (str.indexOf(dot)===-1 || str.indexOf(dot)===0 || str.indexOf(dot)===lstr){
		    alert("Please enter a valid email address");
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!==-1){
		    alert("Please enter a valid email address");
		    return false;
		 }

		 if (str.substring(lat-1,lat)===dot || str.substring(lat+1,lat+2)===dot){
		    alert("Please enter a valid email address");
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))===-1){
		    alert("Please enter a valid email address");
		    return false;
		 }
		
		 if (str.indexOf(" ")!==-1){
		    alert("Please enter a valid email address");
		    return false;
		 }

		return true;					
	}
 
 function validateForm(){
	var emailID=document.contact.email;
	
	if ((emailID.value===null)||(emailID.value==="")){
		alert("Please Enter your Email address");
		emailID.focus();
		return false;
	}
	if (echeck(emailID.value)===false){
		emailID.value="";
		emailID.focus();
		return false;
	}

	return true;
 }
 
 
 //VIDEO
//___________________________________________________________________________________________________________________________________
$(document).ready(function() {
	$(".play").hide();		   
	
	$(".pause").click (					   
		function ()
		{
			$(".pause").hide();
			$(".play").show();
		}
	);
	$(".play").click (					   
		function ()
		{
			$(".play").hide();
			$(".pause").show();
		}
	);
});


 //DropDown
//___________________________________________________________________________________________________________________________________

$(function(){
var myTimeout = null;
$("ul.dropdown li").hover(function(){
    if (myTimeout) clearTimeout(myTimeout);
    var _thisRef = $(this);
    myTimeIn = setTimeout(function() {
        _thisRef.find("ul:first").show();
    }, 400);
}, function(){
	if (myTimeIn) clearTimeout(myTimeIn);
    var _thisRef = $(this);
    myTimeout = setTimeout(function() {
        $("ul.dropdown li ul").css('display', 'none');
    }, 400);
});

$("ul.dropdown li ul li:has(ul)").find("a:first").append("&raquo;");
});

 


 //		Scroll
 //___________________________________________________________________________________________________________________________________
function calcPos() {	
    imgpos = parseFloat($("ul.projectImages").css('left'));
    return imgpos;
}

function calcWidth() {
    var imgwidth = 0;
    $("ul.activeProject").find("img").each(function(){
        imgwidth += $(this).width();
    });
	imgwidth -= 980;
    return imgwidth;
}

function calcSpeedR() {
    imgwidthspeed = ((calcWidth() + 980) + calcPos()) * 3;
    return imgwidthspeed;
}

function calcSpeedL() {
    imgwidthspeed = ((0 - 1) * calcPos()) * 3;
    return imgwidthspeed;
}


$(function(){	
	$("#rightArrow").hover(function() {
		$('.projectImages').animate({left: '-' + calcWidth() +'px'}, calcSpeedR());
	},function() {
		$('.projectImages').stop();
	});
	
	$("#leftArrow").hover(function() {
		$('.projectImages').animate({left: '0px'}, calcSpeedL());
	},function() {
		$('.projectImages').stop();
	});
});



 //		workPosts
 //___________________________________________________________________________________________________________________________________

$(document).ready(function() {
    $('#projectsList li a').click(function(){
		
		id = $(this).attr('id');
        $('.activeProject').removeClass('activeProject');
		$('article.'+id).addClass('activeProject');
		$('ul.'+id).addClass('activeProject');
		$('ul.'+id).css('left', '0');
        $(this).addClass("activeProject");
    });
});

 //		Reveal
 //___________________________________________________________________________________________________________________________________


(function($) {

$('a[data-reveal-id]').live('click', function(e) {
	e.preventDefault();
	var modalLocation = $(this).attr('data-reveal-id');
	$('#'+modalLocation).reveal($(this).data());
});

$.fn.reveal = function(options) {

	var defaults = {  
		animation: 'fadeAndPop', //fade, fadeAndPop, none
		animationspeed: 300, //how fast animtions are
		closeonbackgroundclick: true, //if you click background will modal close?
		dismissmodalclass: 'close-reveal-modal' //the class of a button or element that will close an open modal
	}; 
    
	var options = $.extend({}, defaults, options); 
		
	return this.each(function() {

	var modal = $(this),
	topMeasure  = parseInt(modal.css('top')),
	topOffset = modal.height() + topMeasure,
	locked = false,
	modalBG = $('.reveal-modal-bg');


if(modalBG.length === 0) {
	modalBG = $('<div class="reveal-modal-bg" />').insertAfter(modal);
}

openModal();

var closeButton = $('.' + options.dismissmodalclass).bind('click.modalEvent',closeModal);

if(options.closeonbackgroundclick) {
	modalBG.css({"cursor":"pointer"});
	modalBG.bind('click.modalEvent',closeModal);
}

function openModal() {
	modalBG.unbind('click.modalEvent');
	$('.' + options.dismissmodalclass).unbind('click.modalEvent');
	if(!locked) {
		lockModal();
		if(options.animation === "fadeAndPop") {
			modal.css({'top': $(document).scrollTop()-topOffset, 'opacity' : 0, 'visibility' : 'visible'});
			modalBG.fadeIn(options.animationspeed/2);
			modal.delay(options.animationspeed/2).animate({
				"top": $(document).scrollTop()+topMeasure,
				"opacity" : 1
			}, options.animationspeed,unlockModal());
			//play video
		}
		if(options.animation === "fade") {
			modal.css({'opacity' : 0, 'visibility' : 'visible', 'top': $(document).scrollTop()+topMeasure});
			modalBG.fadeIn(options.animationspeed/2);
			modal.delay(options.animationspeed/2).animate({
				"opacity" : 1
			}, options.animationspeed,unlockModal());
		}
		if(options.animation === "none") {
			modal.css({'visibility' : 'visible', 'top':$(document).scrollTop()+topMeasure});
			modalBG.css({"display":"block"});
			unlockModal();
		}
	}
}

function closeModal() {
	if(!locked) {
		lockModal();
		if(options.animation === "fadeAndPop") {
					$("#videoPlaying").remove()
			modalBG.delay(options.animationspeed).fadeOut(options.animationspeed);
			modal.animate({
				"top":  $(document).scrollTop()-topOffset,
				"opacity" : 0
			}, options.animationspeed/2, function() {
				modal.css({'top':topMeasure, 'opacity' : 1, 'visibility' : 'hidden'});
				unlockModal();
			});
		}
		if(options.animation === "fade") {
			modalBG.delay(options.animationspeed).fadeOut(options.animationspeed);
			modal.animate({
				"opacity" : 0
			}, options.animationspeed, function() {
				modal.css({'opacity' : 1, 'visibility' : 'hidden', 'top' : topMeasure});
				unlockModal();
			});
		}
		if(options.animation === "none") {
			modal.css({'visibility' : 'hidden', 'top' : topMeasure});
			modalBG.css({'display' : 'none'});
		}
	}
}

function unlockModal() {
	locked = false;
}

function lockModal() {
	locked = true;
}
});//each call
};//orbit plugin call
})(jQuery);


//News Ticker
//------------------------------------------------------------------------------------------------


$(function() {

    var marquee = $("#stories"); 
    marquee.css({"overflow": "hidden", "width": "2500px"});

    marquee.wrapInner("<div>");
    marquee.find("div").css("width", "5000px");

    var reset = function() {
        $(this).css("margin-left", "940px");
        $(this).animate({ "margin-left": "-2000px" }, 59999, 'linear', reset);
    };

    reset.call(marquee.find("div"));

	$("#stories div").tweet({
		username: "blumilk",
		count: 3,
	});
	
});

// Maps
//-----------------------------------------------------------------------------------

var map; //the google map
var directionsService; //service that provides directions to get to our destination
var directionsDisplay; //rendeder that draws directions on map
var destinationName = "Media 5, Media Exchange, Coquet Street, Byker, Newcastle-Upon-Tyne"; //our destination. Set yours!

function initiate_geolocation(skipHTML5){

if (!skipHTML5 && navigator.geolocation) {
// HTML5 GeoLocation
function getLocation(position) {
document.getElementById("method").innerHTML = "Location obtained using HTML5";
showMapAndRoute({
"lat": position.coords.latitude,
"lng": position.coords.longitude
});
}
navigator.geolocation.getCurrentPosition(getLocation, error);
} else {
// Google AJAX API fallback GeoLocation
if (typeof google === 'object') {
var geocoder = new google.maps.Geocoder();
if (google.loader.ClientLocation) {
document.getElementById("method").innerHTML = "Location obtained using Google Geocoder";
showMapAndRoute({
"lat": google.loader.ClientLocation.latitude,
"lng": google.loader.ClientLocation.longitude
});
} else
{
alert("Google Geocoder was unable to get the client position");
}
}
}
}

function showMapAndRoute(l)
{
var latlng = new google.maps.LatLng(l.lat,l.lng);

var myOptions = {
zoom: 8,
mapTypeId: google.maps.MapTypeId.TERRAIN
};

map = new google.maps.Map(document.getElementById("map"), myOptions);
directionsDisplay = new google.maps.DirectionsRenderer();
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById("route"));

var request = {
origin: l.lat + ',' + l.lng ,
destination: destinationName,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService = new google.maps.DirectionsService();
directionsService.route(request, function(result, status) {
if (status === google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
}
});
}

function error(e)
{
switch(e.code)
{
case e.TIMEOUT:
alert ('Timeout');
break;
case e.POSITION_UNAVAILABLE:
alert ('Position unavailable');
break;
case e.PERMISSION_DENIED:
alert ('Permission denied');
break;
case e.UNKNOWN_ERROR:
alert ('Unknown error');
break;
}

//try to get location using Google Geocoder
initiate_geolocation(true);
}

        

