// Slideshow

function slideSwitch() {
	
	var $active = $('#slideshow img.active');

	if ( $active.length == 0 ) $active = $('#slideshow img:last');

	var $next =  $active.next().length ? $active.next()
		: $('#slideshow img:first');

	$active.addClass('last-active');

	$next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 1000, function() {
			$active.removeClass('active last-active');
		});
}

// End slideshow

jQuery(document).ready(function(){
	
	
	
	$(".fancybox").fancybox({
	    "width"				: "90%",
	    "height"			: "90%",
	    "autoScale"			: false,
	    "transitionIn"		: "none",
	    "transitionOut"		: "none",
	    "type"				: "iframe"
	});
    
	
	
	$("#index-selector-js-backup").hide();
													
// Show hide top
	
	function hideNavs () {
		$("ul#subnav-quality-assurance").fadeOut('1500');
		$("ul#subnav-about-us").fadeOut('1500');
	};
	
	$("#top-navigation a.courses-a, #top-navigation a.contact-a").hover(function(){
		hideNavs();
	});
								
	$("#top-navigation a.quality-assurance-a").hover(function() {
		$("ul#subnav-quality-assurance").slideDown('1500');
		$("ul#subnav-about-us").fadeOut('1500');
	});
								
	$("#top-navigation a.about-us-a").hover(function() {
		$("ul#subnav-about-us").slideDown('1500');
		$("ul#subnav-quality-assurance").fadeOut('1500');
	});
	
	$("ul#subnav-quality-assurance").mouseleave(function(){
		hideNavs();
	});
	
	$("ul#subnav-about-us").mouseleave(function(){
		hideNavs();
	});
	
	

// CSS3 for cta-bottom
	$("#main #cta-bottom").css("-moz-border-radius","15px");
	$("#main #cta-bottom").css("border-radius","15px");
// End CSS3 for cta-bottom

// Left Nav (non-index)
	// Add tabs to sidebar navigation 
	$('<ul id="tab-menu"><li class="left-nav-category on">Category</li><li class="left-nav-accreditation">Accreditation</li></ul>').prependTo('#sidebar');
	// End add tabs

$(".slidedown").hide();	
$('#sidebar #nav-holder #left-nav-accreditation').hide();

$("#sidebar #left-nav h3").click(function () {
	$(this).toggleClass("open").next().slideToggle("1500");
});		


$('.left-nav-category').click(function(){
	$('#sidebar #nav-holder div').slideUp('1500');
	$('#sidebar #nav-holder #left-nav-category').slideDown('1500');	
	$('#sidebar #tab-menu .left-nav-category').addClass("on");			
	$('#sidebar #tab-menu .left-nav-accreditation').removeClass("on");
});

$('.left-nav-accreditation').click(function(){
	$('#sidebar #nav-holder div').slideUp('1500');
	$('#sidebar #nav-holder h3').removeClass('open');
	$('#sidebar #nav-holder #left-nav-accreditation').slideDown('1500');		
	$('#sidebar #tab-menu .left-nav-accreditation').addClass("on");		
	$('#sidebar #tab-menu .left-nav-category').removeClass("on");	
});


$("#index #index-sidebar #index-nav h3").click(function () {
	$(this).toggleClass("open").next().slideToggle("1500");
});		


$('<p class="learnmore"><span class="readmore">Show More &raquo;</span><span class="hideall">Hide &laquo;</span></p>').insertBefore('.belowlearnmore');

$('p.learnmore span.hideall').toggle();

$('p.learnmore').click(function(){
	$(this).next().slideToggle();
	$(this).children('.readmore').toggle();
	$(this).children('.hideall').toggle();
});


$('.belowlearnmore').hide();

// Slideshow run
								
$(function() {
	setInterval( "slideSwitch()", 3000 );
}); 

// End slideshow run

// Index Selector

	var trainingArray = {
		'hstltd' : {
            'image'   : 'index-selector-logo-hs.jpg',
            'options' : [
                {'value' : 'hstltd-courses/index.php', 'display' : 'All Health & Safety Training Ltd Courses'},
				{'value' : 'hstltd-courses/hstltd-risk-assessment-training.php', 'display' : 'Risk Assessment Training'},
                {'value' : 'hstltd-courses/hstltd-vehicle-banksman.php', 'display' : 'Vehicle Banksman'},
                {'value' : 'hstltd-courses/hstltd-fire-awareness-training.php', 'display' : 'Fire Awareness Training'},
                {'value' : 'hstltd-courses/hstltd-pedestrian-operated-lift-trucks.php', 'display' : 'Pedestrian Operated Lift Trucks'},
                {'value' : 'hstltd-courses/hstltd-pedestrian-operated-pallet-trucks.php', 'display' : 'Pedestrian Operated Pallet Trucks'},
                {'value' : 'hstltd-courses/hstltd-loading-shovel.php', 'display' : 'Loading Shovel (Tracked or Wheeled)'},
                {'value' : 'hstltd-courses/hstltd-nvq-plant-operations.php', 'display' : 'NVQ Plant Operations'}
            ]
        },
		'ipaf'   : {
            'image'   : 'index-selector-logo-ipaf.jpg',
            'options' : [
                {'value' : 'ipaf-courses/index.php', 'display' : 'All IPAF Courses'},
				{'value' : 'ipaf-courses/ipaf-mobile-elevated-working-platforms-cherry-picker.php', 'display' : 'MEWPs / Cherry Pickers'},
                {'value' : 'ipaf-courses/ipaf-safety-harness-use-inspection.php', 'display' : 'Safety Harness Use & Inspection'}
            ]
        },
		'iosh'   : {
            'image'   : 'index-selector-logo-iosh.jpg',
            'options' : [
                {'value' : 'iosh-courses/index.php', 'display' : 'All IOSH Courses'},
				{'value' : 'iosh-courses/iosh-managing-safely.php', 'display' : 'Managing Safely'},
                {'value' : 'iosh-courses/iosh-working-safely.php', 'display' : 'Working Safely'}
            ]
        },
		'npors'  : {
            'image'   : 'index-selector-logo-npors.jpg',
            'options' : [
                {'value' : 'npors-courses/index.php', 'display' : 'All NPORS Courses'},
				{'value' : 'npors-courses/npors-180-degree-backhoe-loader.php', 'display' : '180 Degree Excavator Backhoe Loader'},
                {'value' : 'npors-courses/npors-360-degree-excavator.php', 'display' : '360 Degree Excavator Training'},
                {'value' : 'npors-courses/npors-abrasive-wheels-cutting-discs.php', 'display' : 'Abrasive Wheels & Cutting Discs'},
                {'value' : 'npors-courses/npors-forward-tipping-dumper.php', 'display' : 'Forward Tipping Dumpers'},
                {'value' : 'npors-courses/npors-lorry-loader-hiab.php', 'display' : 'Lorry Loader (HIAB)'},
                {'value' : 'npors-courses/npors-manual-handling-training-course.php', 'display' : 'Manual Handling for Operatives'},
                {'value' : 'npors-courses/npors-overhead-gantry-crane.php', 'display' : 'Overhead Gantry Cranes (OHC)'},
                {'value' : 'npors-courses/npors-ride-on-road-roller.php', 'display' : 'Ride on Road Roller'},
                {'value' : 'npors-courses/npors-rough-terrain-forklifts.php', 'display' : 'Rough Terrain Forklift'},
                {'value' : 'npors-courses/npors-industrial-side-loader-forklifts.php', 'display' : 'Industrial Side Loader Forklift Truck'},
                {'value' : 'npors-courses/npors-slinger-signaller.php', 'display' : 'Slinger Signaller'},
                {'value' : 'npors-courses/npors-telescopic-handler-forklifts.php', 'display' : 'Telescopic Handler Forklift'}
            ]
        },
		'nuco'   : {
            'image'   : 'index-selector-logo-nuco.jpg',
            'options' : [
                {'value' : 'nuco-courses/index.php', 'display' : 'All NUCO Courses'},
				{'value' : 'nuco-courses/nuco-first-aid.php', 'display' : 'First Aid'},
				{'value' : 'nuco-courses/nuco-emergency-first-aid.php', 'display' : 'Emergency First Aid'},
                {'value' : 'nuco-courses/nuco-first-aid-at-work.php', 'display' : 'First Aid at Work'}
            ]
        },
		'pasma'  : {
            'image'   : 'index-selector-logo-pasma.jpg',
            'options' : [
				{'value' : 'pasma-courses/pasma-mobile-scaffold-tower.php', 'display' : 'Mobile Aluminium Scaffold Tower Course'}
            ]
        },
		'rtitb'  : {
            'image'   : 'index-selector-logo-rtitb.jpg',
            'options' : [
                {'value' : 'rtitb-courses/index.php', 'display' : 'All RTITB Courses'},
				{'value' : 'rtitb-courses/rtitb-industrial-counterbalance-forklift-trucks.php', 'display' : 'Industrial Counterbalance Forklift Trucks'},
                {'value' : 'rtitb-courses/rtitb-industrial-reach-forklift-trucks.php', 'display' : 'Industrial Reach Forklift Trucks'},
                {'value' : 'rtitb-courses/rtitb-forklift-instructor-course.php', 'display' : 'Forklift Instructor Training'}
            ]
        }
	};
    
    /*
     * This fires when you change the accreditation type (top dropdown)
     */
    $('#viewSelector').change(function() {
        var value = $(this).val();
        if (value in trainingArray) {
            // Swap the images (hide, change, fade in)
            $('#index-selector-logo').hide()
                                     .attr('src', 'images/index-selector/' + trainingArray[value].image)
                                     .fadeIn(500);
            // Empty, then fill the <select> with <options>'s
            $('#viewSubSelector').empty();
            for (var i in trainingArray[value].options) {
                var optionData = trainingArray[value].options[i];
				var appendString = '<option value=\"' + optionData.value + '\">' + optionData.display + '</option>/n';
		$('#viewSubSelector').append(appendString);
            }
            // Fade the sub elements in
            disableSubElements(false, 500);
        }
        else {
            // Fade the accreditation logo out
            $('#index-selector-logo').fadeOut(200);
            // Fade the sub elements out
            disableSubElements(true, 200);
            // Empty, the <select>
            $('#viewSubSelector').empty();
        }
    });
    
    /*
     * This gets called to disable the sub elements by the above script
     */
    function disableSubElements(disable, time) {
        time = typeof(time) != 'undefined' ? time : 1000;
        if (disable) {
            $('#index-selector-go, #viewSubSelector').attr('disabled','disabled');
            $('#index-selector-go, #selectorSpecifically, #viewSubSelector, #index-selector-phone').fadeTo(time,0.2);
        }
        else {
            $('#index-selector-go, #viewSubSelector').removeAttr('disabled');
            $('#index-selector-go, #selectorSpecifically, #viewSubSelector, #index-selector-phone').fadeTo(time,1);
        }
    }

    // Initially disable the sub elements
    if (!$('#viewSelector').val()) {
        disableSubElements(true, 0);
    }
    else {
        $('#viewSelector').trigger('change');
    }

    /*
     * This fires when you click the go button
     */
    $('#index-selector-go').click(function() {
        var href = $('#viewSubSelector').val();
		var qs = window.location.search.substring(1);
        if (href) {
            window.location = href + '?' + qs + '&tab=accreditation';
        }
        return false;
    });

	
	$("div#form form.formular fieldset").css("-moz-border-radius", "4px");
	$("div#form form.formular fieldset").css("border-radius", "4px");
	$("div#form form.formular fieldset").css("-webkit-border-radius", "4px");
	
// End Index Selector
				
});
