/*
*******************************************************************************
*	Copyright © Neoco; www.neoco.com
*	$Id: general.js 961 2010-07-26 10:06:35Z alex $
*	purpose:  site-wide JavaScript
*	site: Neoco Website 2009
*
*******************************************************************************
*/

$(document).ready(function() {
	
	//-------------------------------------------------------------------------------
	// Generic form clear
	
	$('.formclear').focus( function() {		
		if (this.value == $(this).attr("defaultValue")){
			this.value = '';	
		}		
	});
	
	$('.formclear').blur( function() {	
		if (this.value == ''){
			this.value = $(this).attr("defaultValue");
		}		
	});		
	
	//-------------------------------------------------------------------------------
	// Newsletter signup
	
	$('.newsletter #newsletterForm').submit(function() {
		
		$('.newsletter #newsletterForm .error').hide();		
		$('.newsletter #newsletterForm .success').hide();	
		
		//---
		
		var name = $('.newsletter #newsletterForm #n-name');
		var email = $('.newsletter #newsletterForm #n-email');
		
		//---
		
		if ( name.val() == '' || name.val() == name.attr("defaultValue") ){
			$('.newsletter #newsletterForm .error').text('Please enter your name.').show();
			return false;
		} else if ( email.val() == '' || email.val() == email.attr("defaultValue") ){
			$('.newsletter #newsletterForm .error').text('Please enter your email address.').show();
			return false;
		}	
		
		//---
		
		$.getJSON("/newsletter/?ajax=1", { name: name.val(), email: email.val() }, function(json){
			if ( json.error == true ){
				$('.newsletter #newsletterForm .error').text( json.message ).show();	
			} else {
				$('.newsletter #newsletterForm .success').show();	
				
				name.val( name.attr("defaultValue") );
				email.val( email.attr("defaultValue") );
					
			}
		});			
		
		return false;
	});

	//-------------------------------------------------------------------------------
	// flash movie height generation
	
	// get the height of the wrapper and set the object to be that height
	// this needs to be only on video pages
	var wrapperHeight = $("div#wrapper").css('height');
	$("#flashBg").css('height', wrapperHeight);

	//-------------------------------------------------------------------------------
	// show the digital by neoco footer tooltip
	
	$("#digitalby").simpletip({
		content: '<p>We\'re a Social CRM agency<br/> developing ways for brands<br/> to better engage their<br/> audience</p><p class="phone"><span>Give Us A Buzz</span> 020 7025 6940</p><p class="email"><span>Ping Us An Email</span> <a href="mailto:hello@neoco.com">info@neoco.com</a></p><p class="web"><span>Check Our Site</span> <a href="http://www.neoco.com">www.neoco.com</a></p>',
		position: [-80, -215],
		onBeforeShow: function() {
	        $('#digitalby').css({'background-position' : '0 -49px'});
	    },
	    onHide: function() {
	    	$('#digitalby').css({'background-position' : '0 0'});
	    }
	});

	//-------------------------------------------------------------------------------	
	// home page link hover	
	
	$('body.home li.colThree a, body.home li.colFour a, body.work div#main td a').hover( function(){
		var closest = $(this).closest('li, td');
		$('a.imgContainer', closest).css('border-color', '#FF00B3');
		$('h3 a', closest).css('color', '#FF00B3');
	},
	function(){
		var closest = $(this).closest('li, td');
		$('a.imgContainer', closest).css('border-color', '#e5e5e5');
		$('h3 a', closest).css('color', '#000000');
	});
	
	// twitter	
	$("body.home div#main li.colTwo div.noJS").css("display","none");
	$("body.home div#main li.colTwo").tweet({
		join_text: null,
		username: "neoco",
		count: 1,
		loading_text: "loading tweets..."
	});
	
	// JQuery Flash Embed
	// This is set up for all youtube videos
	// http://jquery.lukelutman.com/plugins/flash/
	$('a[href^="http://www.youtube.com"]').flash(
        { height: 355, width: 425 },
        { version: 8 },
        function(htmlOptions) {
            $this = $(this);
            htmlOptions.src = $this.attr('href');
            htmlOptions.wmode = 'transparent';
            $this.before($.fn.flash.transform(htmlOptions));
            //$('a[href^="http://www.youtube.com"]').css('display', 'none')						
        }
    );

});

$(window).load(function() {
	
	//-------------------------------------------------------------------------------
	// Google Map initialisation
	
	// initialize the map once everything else is loaded
	// needs to work like this for google
	if($('body').hasClass('contact')){
		initialize();
	}
	
	//-------------------------------------------------------------------------------
	// after home page twitter hover	
	$("body.home div#main li.colTwo h3").hover( function(){
		$('body.home li.colTwo').css('background-position', '-234px 0');
	},
	function(){
		$('body.home li.colTwo').css('background-position', '0 0');
	});
	
	//-------------------------------------------------------------------------------
	// Detect if Flash is installed
	
	// http://www.featureblend.com/javascript-flash-detection-library.html
	if(!$('body').hasClass('blog')){
		if(!FlashDetect.installed){
			$('body.home div#wrapper').css('background', 'url(/images/backgrounds/home-bg-new.png) no-repeat top center');
			$('body.workpage div#wrapper').css('background', 'url(/images/backgrounds/bg2.jpg) no-repeat top center');
			$('body.workpage div#main h2').css('display', 'block');
			$('body.workpage div#main h3').css('display', 'block');
			$('body.workpage div#main p.sans').css('display', 'block');
			$('body.home div#flashBg, body.workpage div#flashBg').css('display', 'none');
		}else{
			$('body.home object, body.workpage object, body.learn.page object').css('display', 'block');
		}
	}
	
	//-------------------------------------------------------------------------------
	// PeerIndex Scraper
	
	$('#peerindextmp').load('http://www.neoco.com/curl.php dl.user_stats div.badges p.badge', function() {
		var $data = $('#peerindextmp .badge').text();
		$('#peerindex a').html($data);
	});
	
});

