jQuery(document).ready(function($){

Cufon.replace('.albert',{hover: true});
Cufon.replace('div.post h2',{hover: true});
Cufon.replace('#nav ul li a',{hover: true});
Cufon.replace('#productNav ul li a',{hover: true});
Cufon.replace('#top-links ul li a',{hover: true});


	// ------------------------------------------------------------------------

	// Homepage Graphic Carousel

//	if ( $('#graphic-carousel').exists() ) var activeImg = $('#controls li.active-image').attr('id').substring(4);

	var activeImg = 1;

	var aniCar = setInterval(function()
	{
		curr = activeImg +1;
		if (curr > jQuery('#carouselImages').children().size()){
			curr = 1;
		}
		jQuery('#img-'+activeImg).removeClass('active-image');
		jQuery('#img-'+curr).css('opacity', 0.2);
		jQuery('#img-'+curr).addClass('active-image');
		jQuery('#img-'+curr).fadeTo('500', 1);
		
		jQuery('#control-'+activeImg).removeClass('activeControl');
		activeImg = curr;
		jQuery('#control-'+activeImg).addClass('activeControl');

	}, 5000);
	
	jQuery('.controller').mouseover(function () {
		clearInterval(aniCar);
		// get ID of clicked hero
		var curr = jQuery(this).attr('id').substring(8);

		// add/remove active class from slideshow images and fade in new one
		if (curr != activeImg) {
			jQuery('#img-'+activeImg).removeClass('active-image');
			jQuery('#img-'+curr).css('opacity', 0.2);
			jQuery('#img-'+curr).addClass('active-image');
			jQuery('#img-'+curr).fadeTo('500', 1);
			
			jQuery('#control-'+activeImg).removeClass('activeControl');
			activeImg = curr;
			jQuery('#control-'+activeImg).addClass('activeControl');
		}
	});

	jQuery('.controller a').click(function () {
		return false;
	});


	// ------------------------------------------------------------------------

	// Homepage Login tabs

	$('#demo-login').hide();	
	
	$('#live-login-link').click(function(){
		$('#live-login').show();
		$('#demo-login').hide();
		$(this).addClass('active');
		$('#demo-login-link').removeClass('active');
		return false;
	});

	$('#demo-login-link').click(function(){
		$('#live-login').hide();
		$('#demo-login').show();	
		$(this).addClass('active');
		$('#live-login-link').removeClass('active');
		return false;
	});

	// ------------------------------------------------------------------------

	// Homepage Login rollover state

	$('.secure-login-button').mouseover(function() {
		$(this).attr('src', '../images/ui/login-form-submit-hover.gif')
	});

	$('.secure-login-button').mouseleave(function() {
		$(this).attr('src', '../images/ui/login-form-submit.gif')
	});

	$('.demo-login-button').mouseover(function() {
		$(this).attr('src', '../images/ui/logindemo-form-submit-hover.gif')
	});

	$('.demo-login-button').mouseleave(function() {
		$(this).attr('src', '../images/ui/logindemo-form-submit.gif')
	});

	$('.live-login-button').mouseover(function() {
		$(this).attr('src', '../images/ui/loginlive-form-submit.gif')
	});

	$('.live-login-button').mouseleave(function() {
		$(this).attr('src', '../images/ui/loginlive-form-submit.gif')
	});
	// ------------------------------------------------------------------------

	// Sub Nav Slide up/down func

	$('#subNav').addClass('subNavHide');
	var toggler = 'closed';

	$('#subNavChoose').click(function () {
		if (toggler == 'open')	{
			$('#subNav').slideUp();
			toggler = 'closed';
		} else {
			$('#subNav').slideDown();
			toggler = 'open';		
		}
      });
	
	$('#subNav').mouseleave(function(){
			$('#subNav').slideUp();
			toggler = 'closed';
	});

	// ------------------------------------------------------------------------

	// Generic Graphic Dropdown List Slide up/down func
	
	$('.graphicDropdownList').addClass('graphicDropdownListHide');
	var toggler = 'closed';

	$('.graphicDropdown').click(function () {
		if (toggler == 'open')	{
			$('.graphicDropdownList').slideUp();
			toggler = 'closed';
			return false;
		} else {
			$('.graphicDropdownList').slideDown();
			toggler = 'open';		
			return false;
		}
      });
	

	$('.graphicDropdownList li a').click(function () {
			$('.graphicDropdownList').slideUp();
			toggler = 'closed';
    });

	$('.graphicDropdownList').mouseleave(function(){
			$('.graphicDropdownList').slideUp();
			toggler = 'closed';
	});

	// ------------------------------------------------------------------------

	// Glossary Func - Currently disabled

/*	$('a.gloss_link').click(function () {

		var letter = $(this).attr('id');

		$('div.gloss-sect').hide();
		$('#gloss_'+letter).show();
		return false;
	
	});

	$('#gloss_all').click(function () {

		$('div.gloss-sect').show();
		return false;
	
	}); */

	// ------------------------------------------------------------------------

	// Seminar Testimonials

	var li_item = $('ul.testimonial-list li').length; 
	if (li_item > 1) {
		var i = 1;
		$('ul.testimonial-list li').hide(); // li
		// SHOW FIRST LI
		$('#quote_'+i).show(); // show first li	


		var aniQuote = setInterval(function() // rotate through in a loop
		{
			$('#quote_'+i).hide();
			i += 1;
			if ( i > li_item ) i = 1; 
			$('#quote_'+i).fadeIn('slow');		
		}, 10500);

		$('a.next-quote').click(function() {
			clearInterval(aniQuote);
			$('#quote_'+i).hide();
			i += 1;
			if ( i > li_item ) i = 1; 
			$('#quote_'+i).show();		
		});
	}

	// ------------------------------------------------------------------------

	// Product Table Tabs
	
	$('.product').hide();
	$('.product-active').show();
	
	$('#productNav li a').click( function() {
	
		if ( $(this).parent().hasClass('active') )	{
			return false;
		} else {
			id = $(this).attr('id');
			id = id.split('_',1);
			id = id[0];
			
			// do the nav
			$('#productNav li.active').removeClass('active');
			$(this).parent().addClass('active');
			Cufon.replace('#productNav ul li a',{hover: true});
			
			// do the tables
			$('.product-active').removeClass('product-active');
			$('.product').hide();
			$('#'+id).show();
			$('#'+id).addClass('product-active');
			
			return false;
		}
	
	});
	
	// Deal with links to table from elsewhere in the site
	// Uses jQuery 'url' plugin
	var tabRequest = $.url.param('ltb');
	
	if ( tabRequest != '') {
		// do the nav
		$('#productNav li.active').removeClass('active');
		$('#' + tabRequest + '_link').parent().addClass('active');
		Cufon.replace('#productNav ul li a',{hover: true});
		
		// do the tables
		$('.product-active').removeClass('product-active');
		$('.product').hide();
		$('#'+ tabRequest).show();
		$('#'+ tabRequest).addClass('product-active');
	}	

	// ------------------------------------------------------------------------
	
	// VIDEOS - uses elements of FlowPlayer API

	// Single Video Play Button

	$('a.play-video-single').click( function() {
		// State 3 = playing
		var state = $f(0).getState();
		if (state == 3)	{
			$f(0).stop();
			$(this).removeClass('stop-video');
		} else {
			$(this).addClass('stop-video');
			$f(0).play();
		}
	});

	// Weekly Market Video List

	$('.flv-list li a').click( function() {	
		var vid = $(this).attr('href');
		$f(0).setClip({ url: vid });
		$f(0).play();
		$('a.play-video-single').addClass('stop-video');

		
		// set new title
		var title = $(this).html();
		$('#curr-vid-head').html(title);
		Cufon.replace('#curr-vid-head',{hover: true});
		return false;
	});


	// ------------------------------------------------------------------------
	
	// On-Site Forms

	// add/remove the highlight classes
	$('div.cs-site-form input, div.cs-site-form select, div.cs-site-form textarea').focus( function() {
		$(this).addClass('highlight');
		
		$(this).hasClass('inputError') ? $(this).prev('label').addClass('highlight-label-error') : $(this).prev('label').addClass('highlight-label');
//		$(this).prev('label').addClass('highlight-label');
	});	
	$('div.cs-site-form input, div.cs-site-form select, div.cs-site-form textarea').blur( function() {
		$(this).removeClass('highlight');
		$(this).hasClass('inputError') ? $(this).prev('label').removeClass('highlight-label-error') : $(this).prev('label').removeClass('highlight-label');
//		$(this).prev('label').removeClass('highlight-label');
	});	

	// Submit rollover state

	$('.cs-form-submit').mouseover(function() {
		$(this).attr('src', '../images/ui/button-form-submit-hover.gif');
	});

	$('.cs-form-submit').mouseleave(function() {
		$(this).attr('src', '../images/ui/button-form-submit-static.gif');
	});

	// ------------------------------------------------------------------------

	// Insert Code Block into Post 'the_content'

	var codeSnippet = $('#postCustomInsert').html();
	$('#postCustomInsert').remove();
	$('#postInsert').html(codeSnippet);


	// ------------------------------------------------------------------------

	// Striping for tables that need it
	// Odd-Even is fucked up because of [0] based indexing. Go figure.
	$('table.zebra tr:odd').addClass('even');


	// ------------------------------------------------------------------------

	// FAQ Behaviour

	$('.faq-text').hide();
	

	$('.faq-list li a.arrow-link').click(function() {
		
		if ( $(this).hasClass('active') )	{

			$('.faq-list li a.active').removeClass('active');
			$('.faq-text').hide();
			if ( $(this).hasClass('map-toggle') )	{
				$(this).html('View seminar location map');			
			}
			return false;
		
		} else {

			$('.faq-list li a.active').removeClass('active');
			$('.faq-text').hide();		
			$(this).addClass('active');
			if ( $(this).hasClass('map-toggle') )	{
				$(this).html('Hide seminar location map');
			}
			$(this).next('.faq-text').show();
			return false;
		}

	});

	// ------------------------------------------------------------------------

	// Seminar Single Map Open Links

	$('.map-open').click(function() {
		$('.faq-list li a').addClass('active').html('Hide seminar location map');
		$('.faq-text').show();
		return false;
	});


	// Deal with links that open map panel
	// Uses jQuery 'url' plugin
	var mapRequest = $.url.param('openMap');
	
	if ( mapRequest != '') {
		$('.faq-list li a').addClass('active').html('Hide seminar location map');
		$('.faq-text').show();
	}	



	// ------------------------------------------------------------------------

	// User Manual Behaviour


	$('.user-manual-content').hide();

	$('.user-manual-section-header').click( function() {	
		if ( $(this).hasClass('open') )	{
			$(this).next('.user-manual-content').hide();
			$(this).removeClass('open');
			return false;
		} else {
			$(this).next('.user-manual-content').show();
			$(this).addClass('open');
			return false;
		}
	});


	$('.close-man-page').click( function() {	
		$(this).parent().hide();
		$(this).parent().prev('.user-manual-section-header').removeClass('open');
		return false;
	});



	// ------------------------------------------------------------------------

	// run through page onload and add in tooltips
	// Tooltip text is taken from the TITLE attribute of the relevant form elements
	$('.text-tip').inputHintBox({div:$('.tip-box'),div_sub:'.box-wrapper',source:'attr',attr:'tooltip',incrementLeft:5,attachTo:$('div.cs-site-form')});
	$('.inputError').inputHintBox({div:$('.error-box'),div_sub:'.box-wrapper',source:'attr',attr:'tooltip',incrementLeft:5,attachTo:$('div.cs-site-form')});


	// ------------------------------------------------------------------------
	// Market Comment Height function
	
	$('#contentFrame').load( function() {
	    setHeight();
	});
	
	function setHeight() {
	    var newHeight = parseInt(location.hash.substring(1)) + 50;
	    $('#contentFrame').attr('height', newHeight);
	}


	
}); // ENDS Document Ready

// ------------------------------------------------------------------------



// Header login button animation
function showLogin()	{
	jQuery('.header-contact-dets').animate({
   width: '+=68'
    }, 500, function() {
		jQuery('.header-login-button').fadeIn();
  });	
}



// we need to run this function after YAV has added the error class to the relevant inputs
// attached to the form submit action as a callback
function addErrors()	{
	jQuery('.text-tip.inputError').inputHintBox({div:jQuery('.error-box'),div_sub:'.box-wrapper',source:'attr',attr:'tooltip',incrementLeft:5,attachTo:jQuery('div.cs-site-form')});
}


// Consoldiates all FlowPlayer calls and allows us to standardise Video Look & Feel across the site
// Basically nesting flowplayer function inside our own

function cc_do_flowplayer(target)	{

	flowplayer(
		target,
		"../swf/flowplayer-3.1.5.swf",
		{
			clip:		{
				autoPlay: false
			},
			plugins: 	{ 
				controls: {
			    	backgroundColor: '#202020',
			     	backgroundGradient: 'none',
			     	timeColor: '#043882'
			   	}
			}
		}
	);

}

function setListeners(){
    inputList = document.getElementsByTagName("INPUT");
    for(i=0;i<inputList.length;i++){
        inputList[i].attachEvent("onpropertychange",restoreStyles);
        inputList[i].style.backgroundColor = "";
    }
    selectList = document.getElementsByTagName("SELECT");
    for(i=0;i<selectList.length;i++){
        selectList[i].attachEvent("onpropertychange",restoreStyles);
        selectList[i].style.backgroundColor = "";
    }
}

function restoreStyles(){
    if(event.srcElement.style.backgroundColor != "")
        event.srcElement.style.backgroundColor = "";
}


/* ************************************************************ CLOCK ************************************** */
var dayarray    = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat")
var montharray  = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")

function getthedate(){
    var mydate=new Date()
    var year=mydate.getYear()
    if (year < 1000)
        year += 1900;
        var day   = mydate.getDay();
        var month = mydate.getMonth();
        var daym  = mydate.getDate();
        if (daym < 10)
            daym = "0" + daym
    var cdate = dayarray[day] + ", " + montharray[month] + " " + daym + ", " + year + " ";
    document.getElementById('date').innerHTML=cdate;
}

/* showClock() function extracts the current time hours, minutes and seconds and then displays them in the div with showText id from the BODY section */ 
function goforit() {
    var clock=new Date();
    var hours=clock.getHours();
    var minutes=clock.getMinutes();
    var seconds=clock.getSeconds();

    // for a nice disply we'll add a zero before the numbers between 0 and 9
    if (hours<10){
        hours="0" + hours;
    }

    if (minutes<10){
        minutes="0" + minutes;
    }
    
    if (seconds<10){
        seconds="0" + seconds;
    }
    
    document.getElementById('clock').innerHTML=hours+":"+minutes+":"+seconds;
    t = setTimeout('goforit()',500);
    /* setTimeout() JavaScript method is used to call showClock() every 1000 milliseconds (that means exactly 1 second) */
}

