jQuery(document).ready(function(){	

	$("ul#buckets").hide();

//Subnav Events
	$("#navigation ul li a.security-fencing-a").hover(function() {
		$("#dropdown-security-fencing").stop(true, true).slideToggle('medium'); },
	function() {
		$("#dropdropdown-security-fencingdown").stop(true, true).slideToggle('medium');
	});
	
	$("#dropdown-security-fencing").hover(function() {
		$(this).stop(true, true).show(); },
	function() {
		$(this).stop(true,true).slideUp('medium');
	}); 

	$("#navigation ul li a.gates-a").hover(function() {
		$("#dropdown-gates").stop(true, true).slideToggle('medium'); },
	function() {
		$("#dropdropdown-gates").stop(true, true).slideToggle('medium');
	});
	
	$("#dropdown-gates").hover(function() {
		$(this).stop(true, true).show(); },
	function() {
		$(this).stop(true,true).slideUp('medium');
	}); 
	
	$("#navigation a.nosubnav").hover(function() {
		$("#dropdown-gates").stop(true,true).slideUp('medium');
		$("#dropdown-security-fencing").stop(true,true).slideUp('medium');
	}); 

	// Fancybox

	$(".fancybox").fancybox({
		'transitionIn'	:	'fade',
		'transitionOut'	:	'fade',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	true
	});
	
		$("#sidebar #sidebar-gallery img").css("opacity","1");
		
		$("#sidebar #sidebar-gallery img").hover(function() {
		$(this).css("opacity","0.8"); },
		function() {
		$(this).css("opacity","1"); });
		
		
		
// Index Selector

	var servicesArray = {
		'fencing' : {
            'image'   : 'index-selector-fence.jpg',
            'options' : [
                {'value' : 'security-fencing.php', 'display' : 'Security Fencing'},
				{'value' : 'palisade-fencing.php', 'display' : 'Palisade Fencing'},
                {'value' : 'mesh-fencing.php', 'display' : 'Mesh Fencing'},
                {'value' : 'timber-fencing.php', 'display' : 'Timber Fencing'},
                {'value' : 'sports-ground-fencing.php', 'display' : 'Sports Ground Fencing'},
                {'value' : 'site-hoarding.php', 'display' : 'Site Hoarding'}
            ]
        },
		'gates'   : {
            'image'   : 'index-selector-gate.jpg',
            'options' : [
                {'value' : 'gates.php', 'display' : 'General Gates'},
				{'value' : 'metal-gates.php', 'display' : 'Metal Gates'},
                {'value' : 'security-gates.php', 'display' : 'Security Gates'}
            ]
        },
		'shelters'   : {
            'image'   : 'index-selector-shelter.jpg',
            'options' : [
                {'value' : 'shelters.php', 'display' : 'All Shelters'}
            ]
        },
		'handrails'  : {
            'image'   : 'index-selector-handrails.jpg',
            'options' : [
                {'value' : 'dda-handrails.php', 'display' : 'DDA Handrails'}
            ]
        }
	};
    
    /*
     * This fires when you change the accreditation type (top dropdown)
     */
    $('#viewSelector').change(function() {
        var value = $(this).val();
        if (value in servicesArray) {
            // Swap the images (hide, change, fade in)
            $('#index-selector-logo').hide()
                                     .attr('src', 'images/index-selector/' + servicesArray[value].image)
                                     .fadeIn(500);
            // Empty, then fill the <select> with <options>'s
            $('#viewSubSelector').empty();
            for (var i in servicesArray[value].options) {
                var optionData = servicesArray[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;
        }
        return false;
    });
				
	$("#starthere").hide().delay(500).fadeIn(1300);
	
});
