var store_old_bg;
var current_tab ;

function openTab(i) {
	
	
	
	// find out which tab number
	var tab_num = (i.index());
	
	// mark with a class
	var this_tab = $("#campaign_left_buttons").find('div.campaign_left_button:eq('+tab_num+')');
	
	
	this_tab.addClass('currentbutton');
	
	// then open equivalent caption
	$('#campaign_captions').find('div.caption_section:eq('+tab_num+') h2').show('slide',500);
	var new_bg = $('#campaign_images').find('div.campaign_image:eq('+tab_num+') img').attr('src');
	$('#campaign').css('background-image',"url('/"+new_bg+"')"); 
	
}

function shutTab(i) {
	i.removeClass('currentbutton');
	// find out which tab number
	var tab_num = (i.index());
	$('#campaign_captions').find('div.caption_section:eq('+tab_num+') h2').stop(true,true).hide();
}

$(document).ready(function() {
	// remove the no javascript warning!
	$('#noscript_warning').remove();
	
	$('#campaign_captions').css('visibility','visible');
	$('#campaign_captions div.caption_section div.caption_container h2').hide();
	$('.campaign_image img').hide();
	$('.campaign_left_button').hover(function(){
		// HOVER mousein	
	if (current_tab instanceof jQuery ) {shutTab(current_tab);};
	openTab($(this));
	current_tab = $(this);
	},
		function(){
		// HOVER mouseout
	//shutTab($(this));
	}
	);

	$('.campaign_left_button, .caption_container h2').click(function(){
		// click through		
		var new_url = $(current_tab).find('a').attr('href');
		window.location=new_url;
	}
	);
	
	/* Fix IE7 caption width bug*/
	$(".ie7_html div.caption_section .caption_container h2").each(function(){
		var foo=$(this).width();
		$(this).width(foo+35);
	});
	
	// Superfish menu
	
	/* Bring menus online*/
	$('.hide_until_domready').show();
	
	$('.main_dropdown_nav ul').addClass('sf-menu'); // apply demo's CSS
      	$('.sf-menu').superfish({ 
            delay:       600,                            // one second delay on mouseout 
            speed:       'fast',                          // faster animation speed 
            autoArrows:  false,                           // disable generation of arrow mark-up 
            dropShadows: false                            // disable drop shadows 
        }
		
	);
	
	/* Form field clearing - superceded
	$("form .text").focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});


*/

	/* Add nyroModal class to any image links marked class video */
	$(".video .image_container a").addClass('nyroModal');
	

	/* and replace mootools lightbox */
	$('a[rel="lightbox"]').addClass('nyroModal');
	
	/* Do nyromodals*/
	
	$('.nyroModal').nyroModal();
	
	$.nmObj({sizes: { initW: 300, initH: 300 }});
	
		
	// Fixed position "get-started" button
	
	var el = $('#get-started');
	if (el.length != 0) {
		var elpos_original = el.offset().top;

		$(window).scroll(function(){
		var elpos = el.offset().top;
		var windowpos = $(window).scrollTop();
		var finaldestination = windowpos;
		if(windowpos<elpos_original) {
			finaldestination = elpos_original;
			el.stop().animate({'top':400},500);
		} else {
			el.stop().animate({'top':windowpos},500);
		}
		});
	}
	// Client gallery image rollovers and clickthrus
	$(".client_gallery tr.even img").hover(function() {
		$(this).parent().parent().parent().next().find('img').show();
		$(this).hide();
		
	}, function() {
		
	});
	
	$(".client_gallery tr.odd img").hover(function() {
				
	}, function() {
		$(this).parent().parent().parent().prev().find('img').show();
		$(this).hide();
	});

	$(".client_gallery tr.odd img").click(function() {
		var url_stem='/'; /* Change this if you want to redirect to a subdirectory,
		eg. /customers/coca-cola.html
		*/
		var alt_text=$(this).attr('alt');
		window.location=url_stem+alt_text+".html";
	});
	
	/* Accordions for any divs classed "expandable_text" */
	$(".expandable_text").hide ().before("<p class='expand_link'><a href='#'>[Click to expand]</a></p>");
	$(".expand_link").click(function(){
		$(this).next('.expandable_text').show('slow'); 
		$(this).hide('slow');
		return false;
	}
	);
	
	
	/* Landing page form behaviour */
	// Form label behaviour
		$(" .ce_form input.text, .ce_form input.captcha, .ce_form textarea").each(function(index){
			if ($(this).attr("value").length == 0) {
				labelcopy=$(this).prev("label").clone(); // copy label
				labelcopy.find('span').remove(); // remove any spannage from the middle
				var foo=labelcopy.text().trim(); // grab the label text
				this.defaultValue=foo;
				this.value = this.defaultValue; // change value and default value to match label
			}
		}
		);
		
		$(" .ce_form input.text, .ce_form input.captcha, .ce_form textarea, .mod_search input.text ").focus(function() {
			var el = this;
			if( el.value == el.defaultValue ) {
			// clear field effect on focus
			el.value = "";
			}
		});
		$(".ce_form input.text, .ce_form input.captcha, .ce_form textarea, .mod_search input.text  ").blur(function() {
			var el = this;
			if( !el.value.length ) {
				el.value = el.defaultValue;
			}
		});
		
		$(".ce_form").submit(function(){
			$(this).find("input.text").each(function(index){
				var fieldVal=$(this).attr("value");
				labelcopy=$(this).prev("label").clone(); // need label again
				labelcopy.find('span').remove(); // remove any spannage from the middle
				var fieldLabel=labelcopy.text().trim(); // grab the label text
				// if field is left as default label or a field mandatory error, empty it 
				if ((fieldVal == fieldLabel) || (fieldVal.indexOf('must not be empty') > -1)) 
				{	
					$(this).attr("value","");
				}
			});
		})
		
		// Move form errors to fields
		
		$(".ce_form p.error").each(function(){
			var errortext=$(this).text();
			$(this).next("input").attr("value",errortext).addClass("error").focus(function(){
				$(this).removeClass("error");
			}
			);
			/* scroll into view (need this cause mootools probably won't work here)*/
			var foo_offset=$(this).offset();
			$(this).hide();
 			window.scrollTo(null, (foo_offset.top - 20));
			
		}
		);
	
	
 });

// Cufon webfonts:
Cufon.replace('#campaign_captions h2');
Cufon.replace('.campaign_left_button');
Cufon.replace('h2.page_title');

