﻿//jCarousel function

function mycarousel_initCallback(jc) {
    jQuery('#mycarousel-controls a').bind('click', function() {
        jc.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        return false;
    });
 }
   
//jCarousel - highlight link in focus
function mycarousel_itemFirstInCallback(carousel, item, idx, state) {
    jQuery('#mycarousel-control-' + idx).addClass("mycarousel-controls-on");
}
 
//jCarousel - de-highlight link no longer in focus
function mycarousel_itemFirstOutCallback(carousel, item, idx, state) {
    jQuery('#mycarousel-control-' + idx).removeClass("mycarousel-controls-on");
}
 
jQuery(document).ready(function() {
	//make external links open in a new window (inc. modification from http://snipplr.com/view/5449/check-if-a-url-contains-a-subdomain/)
	jQuery('a').filter(function() {
			currentdomain = this.hostname;
			targetdomain = location.hostname;
			// IF THERE, REMOVES 'http://', 'https://' or 'ftp://' FROM THE START
			currentdomain = currentdomain.replace(new RegExp(/^http\:\/\/|^https\:\/\/|^ftp\:\/\//i),"");
			targetdomain = targetdomain.replace(new RegExp(/^http\:\/\/|^https\:\/\/|^ftp\:\/\//i),"");
			// IF THERE, REMOVES 'www.' FROM THE START OF THE STRING
			currentdomain = currentdomain.replace(new RegExp(/^www\.|^es\.|^fr\.|^de\.|^stg\.|^z0\.|^local\./i),"");
			targetdomain = targetdomain.replace(new RegExp(/^www\.|^es\.|^fr\.|^de\.|^stg\.|^z0\.|^local\./i),"");
        	return currentdomain && currentdomain !== targetdomain;
    	}).addClass("external").attr({target: "_blank"});
    
    //Open eBooks in a new window
	jQuery('.hentry.category-ebooks h2 a').addClass("external").attr({target: "_blank"});
    	
    //validate webtolead form
//    jQuery(".wpcf7").validate();
    
    // initialise chatterbox
    chatterbox.init();
    
	// do tabs on services pages
	jQuery("#tabs").tabs().addClass('ui-tabs-vertical ui-helper-clearfix');
	jQuery("#tabs li").removeClass('ui-corner-top').addClass('ui-corner-left');

	//make external area links open in a new window - homepage image map
	jQuery('area').filter(function() {
        	return this.hostname && this.hostname !== location.hostname;
    	}).addClass("external").attr({target: "_blank"});

     	//make first link of footer nav have class strong
    	jQuery("#first .widget_pages ul li a").addClass("strong");
    	jQuery("#first .widget_pages ul li ul li a").removeClass("strong");
    	
    	//jCarousel  	
    	jQuery('#mycarousel').jcarousel({
	        // Configuration goes here
	        scroll: 1,
	        auto: 11,
	        buttonNextHTML: "<a title='Next' class='homeCarouselbuttonNext' href='#'>Next</a>",
	        buttonPrevHTML: "<a title='Previous' class='homeCarouselbuttonPrev' href='#'>Previous</a>",
	        itemFirstInCallback:  mycarousel_itemFirstInCallback,
	        itemFirstOutCallback: mycarousel_itemFirstOutCallback,
	        wrap:'both',
	        initCallback: function(jc, state) { // adapted from http://old.nabble.com/Howto:-Adding-a-pause-button-to-jcarousel-td22057133s27240.html
	        	mycarousel_initCallback(jc);
				if (state == 'init') { 
					// Insert a play/pause button between the prev/next buttons 
					jQuery('#mycarousel-controls li:last').after('<li class="jcarousel-toggle"><a title="Play/Pause" href="#">Play/Pause</a></li>');
					//jQuery('#homeCarousel').after('<div class="jcarousel-paused"><p>Carousel Paused</p></div>');
			
					/* Override the internal startAuto() method, which is called after 
					* animations complete, to prevent next/prev buttons from reactivating 
					* the timer while in the pause state. 
					*/ 
					jc.startAutoOrig = jc.startAuto; 
					jc.startAuto = function() { 
					if (!jc.paused) { 
						jc.startAutoOrig(); 
						} 
					} 
			
					jc.pause = function() { 
						jQuery('li.jcarousel-toggle') 
							.removeClass('jcarousel-play') 
							.addClass('jcarousel-pause'); 
						jc.paused = true; 
						jc.stopAuto(); 
						//console.log('stopped');
						//jQuery('.jcarousel-paused').show();
					}; 
			
					jc.play = function() { 
						jQuery('li.jcarousel-toggle') 
							.removeClass('jcarousel-pause') 
							.addClass('jcarousel-play'); 
						jc.paused = false; 
						jc.next();
						//console.log('started');
						//jQuery('.jcarousel-paused').hide();
					}; 
					
					// stop the default click to '#' when using next/prev
					jQuery('.homeCarouselbuttonNext').click(function(event){
						event.preventDefault();
					});
					jQuery('.homeCarouselbuttonPrev').click(function(event){
						event.preventDefault();
					});
			
					// Click event for playback toggle button, conditionally calls play/pause 
					jQuery('li.jcarousel-toggle a').click(function(){ 
						if (jc.paused) { 
							jc.play(); 
						} else { 
							jc.pause(); 
						} 
						return false;
					}); 
					jQuery('.vertical-response-signup-form input').focus(function(){ 
						jc.pause(); 
					}); 
					jQuery('.vertical-response-signup-form input').blur(function(){ 
						jc.play(); 
					}); 
				} 
				jc.play(); 
			} 
        });    	

        //Case Studies expansion
     	jQuery(".jsCaseStudyBody").hide();
        jQuery(".jsCaseStudyExpand").click(function() {
        	jQuery(this).find(".jsCaseStudyBody").slideToggle("fast");
        });
	
	//404 Page Rollover
	jQuery(".four04-link").hover(function() {
		jQuery(".four04-content").addClass("four04-roll")
		},
	function() {
		jQuery(".four04-content").removeClass("four04-roll")
		});
		
	//Add watermarks to Mailchimp newsletter sign-up forms EN
    jQuery(".site-en .gform_body .email input").watermark("Email", {className: 'bw-watermark'});
	jQuery(".site-en .gform_body .name input").watermark("Name", {className: 'bw-watermark'});

	//Add watermarks to Vertical Response newsletter sign-up forms DE
    jQuery(".site-de .gform_body .email input").watermark("E-mail", {className: 'bw-watermark'});
	jQuery(".site-de .gform_body .name input").watermark("Namen", {className: 'bw-watermark'});
	
	//Add watermarks to Vertical Response newsletter sign-up forms FR
    jQuery(".site-fr .gform_body .email input").watermark("Email", {className: 'bw-watermark'});
	jQuery(".site-fr .gform_body .name input").watermark("Nom", {className: 'bw-watermark'});
	
	//Add watermarks to Vertical Response newsletter sign-up forms ES
    jQuery(".site-es .gform_body .email input").watermark("Email", {className: 'bw-watermark'});
	jQuery(".site-es .gform_body .name input").watermark("Nombre", {className: 'bw-watermark'});
	
	//Custom Google Analytics Event Tracking
	
	//Newsletter Sign-up Submission Attempt - Sidebar (value = 1)
	jQuery("#primary .gform_footer input.button").click(function() {
		var userEmail = jQuery(".vertical-response-signup-sidebar .vr-email").val();
		//console.log('Newsletter Sign-up', 'Form Submission Attempt', userEmail, '1');
		if (_gaq.push) {
			_gaq.push(['_trackEvent', 'Newsletter Sign-up', 'Form Submission Attempt', userEmail]);	
		}
	})
	
	// Pricing page - choose starting currency
	if (jQuery("body").hasClass("site-en")) {
		jQuery("#pricing-currency-controls li a#gbp").addClass("active");
	} else {
		jQuery("#pricing-currency-controls li a#eur").addClass("active");	
	}
	
	// Pricing page currency swaps
	jQuery("#pricing-currency-controls li a").click(function(event) {
		event.preventDefault();
		// Get the new values
		currency = jQuery(this).attr("id");
		pitchMonthly = jQuery(".pitch-monthly-" + currency).text();
		proMonthly = jQuery(".pro-monthly-" + currency).text();
		enterpriseMonthly = jQuery(".enterprise-monthly-" + currency).text();
		pro20kMentions = jQuery(".pro-20k-mentions-" + currency).text();
		pro50kMentions = jQuery(".pro-50k-mentions-" + currency).text();
		entAddQueries = jQuery(".ent-additional-queries-" + currency).text();
		entWhiteLabel = jQuery(".ent-white-label-" + currency).text();
		// Swap the values
		jQuery(".pitch-monthly").text(pitchMonthly);
		jQuery(".pro-monthly").text(proMonthly);
		jQuery(".enterprise-monthly").text(enterpriseMonthly);
		jQuery(".pro-20k-mentions").text(pro20kMentions);
		jQuery(".pro-50k-mentions").text(pro50kMentions);
		jQuery(".ent-additional-queries").text(entAddQueries);
		jQuery(".ent-white-label").text(entWhiteLabel);
		// Mark the correct currency as in focus
		jQuery("#pricing-currency-controls li a").removeClass("active");
		jQuery(this).addClass("active");
	})	
	
	// Tweetcamp competition specials
	// First grab the referrer URL
	var referrer = document.referrer.toLowerCase();
	// If it's http://labs.brandwatch.com/tweetcamp/ then add the secret link
	var secreturl = "http://labs.brandwatch.com/tweetcamp/";
	var formurl = "http://www.brandwatch.com/foxy-lurve"
	if ( referrer == secreturl ) {
		//console.log('hello - ' +referrer);
		jQuery(".slug-homepage .socialThanks").addClass("tweetCampSpecial");
	}
	// If the link is clicked, take them to the secret form page
	jQuery(".tweetCampSpecial").click(function() {
		window.location = formurl;
	});
	
	// FAQs accordion
	jQuery("#accordion").accordion(({
		autoHeight: false,
		navigation: true,
		active: false,
		collapsible: true
	}));
});
