$(function() {

	// homepage content fader
	if ($("#slides").length) {
		$('.arrow').hide();
		$("#slides-control").tabs("#slides > div", {
			effect: 'fade',
			fadeOutSpeed: "slow",
			rotate: true
		}).slideshow({
			interval: 6000,
        	autoplay: true
		});
		
		var timer;
		$('#slides').hover(
		  function () {
		    $('.arrow').fadeIn(200);
		    clearTimeout(timer);
		  },
		  function () {
		    timer = setTimeout("$('.arrow').fadeOut(500);", 300);
		  }
		);
		
		$('.arrow').mouseenter(function(){
		    clearTimeout(timer);
		});
		
		//video overlay
		$("a[rel]").overlay({
		
		    mask: {
		    	color: '#000',
		    	opacity: 0.6
		    },
		    effect: 'apple',
		
		    onBeforeLoad: function() {
		    	var url = this.getTrigger().attr("href");	
		    	$('#overlay-iframe').attr('src', url);
		    },
		    
		    onClose: function() {	
		    	$('#overlay-iframe').attr('src', '');
		    }
		
		});
		
		
		//twitter feed
		$(".tweets").tweet({
		  username: "Engage_Minds",
      	  count: 1,
      	  refresh_interval: 10,
      	  template: function(info) {                // [function] template used to construct each tweet <li>
	        return info["text"];
	      }
      	});
      	
      	//$("ul.tweet_list").liScroll();
	}
	
	// filterable video gallery
	if ($("#gallery").length) {
	
		var $gallery = $('#gallery');
    			
    	$gallery.imagesLoaded( function(){
			$(window).hashchange();
      	});
      	
      	$(window).hashchange( function(){
			var selector = location.hash.substring(1);
			if ($('.option-set a').hasClass('selected') ) {
				$('.option-set').find('.selected').removeClass('selected');
			}
			if (selector == 'ceo') {
			    $('#narrative').slideDown();
			} else {
			    $('#narrative').slideUp();
			}
			if (selector !== '') {
				$('a[data-filter="\\.' + selector + '"]').addClass('selected');
			    $gallery.isotope({filter: '.' + selector, itemSelector : '.video'});
			} else {
				$('a[data-filter="\\*"]').addClass('selected');
			    $gallery.isotope({filter: '*', itemSelector : '.video'});
			}
		})
      	
      	// video overlay
      	$("#gallery div a[rel]").overlay({
		
			mask: {
				color: '#000',
				opacity: 0.6
			},
			effect: 'apple',
		
			onBeforeLoad: function() {
				var url = this.getTrigger().attr("href");	
				$('#overlay-iframe').attr('src', url);
			},
			
			onClose: function() {	
				$('#overlay-iframe').attr('src', '');
			}
		
		});
    }
    
    // google map
    if ($("#map").length) {
    	 var centre = new google.maps.LatLng(51.5188187,-0.1342686);
  	   var home = new google.maps.LatLng(51.5180187,-0.1342686);
  	   
  	   var myOptions = {
  	     zoom: 16,
  	     center: centre,
  	     mapTypeId: google.maps.MapTypeId.ROADMAP
  	   }
  	   
  	   var infowindow = new google.maps.InfoWindow({
	   	content: $('#map').html(),
	  	maxWidth: 400
	   });
	   
	   var map = new google.maps.Map(document.getElementById("map"), myOptions);
  	   
  	   var marker = new google.maps.Marker({
  	      position: home, 
  	      map: map, 
  	      title:"Connected Pictures"
  	  });
	  infowindow.open(map, marker);
    }
    
    // contact form
    if ($("#contact-form").length) {
    	$("#contact-form").validationEngine('attach', {
    		promptPosition: "bottomRight",
        	onValidationComplete: function(form, status){
        		if (status) {
        			$.post("inc/form.php", $(form).serialize(), function(data) {
        				$(form).html(data);
        			});
        		}
        		return false;
			}  
        })
    }

    // client video galleries
    
	if ($(".client-tabs").length) {
		$(".client-tabs").tabs(".client-panes > div", {
			effect: 'fade'
		});
		
		// video overlay
      	$(".gallery div a[rel]").overlay({
		
			mask: {
				color: '#000',
				opacity: 0.6
			},
			effect: 'apple',
		
			onBeforeLoad: function() {
				var url = this.getTrigger().attr("href");	
				$('#overlay-iframe').attr('src', url);
			},
			
			onClose: function() {	
				$('#overlay-iframe').attr('src', '');
			}
		
		});
	}
    
});
