// Core Javascript

/* Search box clear text */
$(document).ready(function () {

    var clearMePrevious = '';
    if ($(".txt-search")) {
        // clear input on focus

        $('.txt-search').focus(function () {
            if ($(this).val() == $(this).attr('title')) {
                $(this).val('');
            }
        });

        // if field is empty afterward, add text again

        $('.txt-search').blur(function () {
            if ($(this).val() == '') {
                $(this).val($(this).attr('title'));
            }
        });
    }
});

// What to add to images on rollover so test.jpg would become test-on.jpg
var overSuffix = '-on';
// Add this class to images to have them roll over
var rolloverClass = 'on';

// All the button rollovers - the rollover image must have an -on suffix and sit in the same folder as the normal state. eg   
    $('.' + rolloverClass).hover(
	    function ()	{	
			//alert('here');
		    $(this).attr('src',doOverImg($(this).attr('src')))
	    },	
	    function ()	{	
		    $(this).attr('src',doDefaultImg($(this).attr('src')))
	    }
    );


	    $(function () {
	        //$("img[alt=Opens in a new window]").remove();	
	        // casuing problems in IE
	        //$(".article tr:nth-child(2n)").css("background-color","none");





	        /*
	        // Old version
	        $("#panel").attr('aria-hidden',true);
		
	        $("#trigger").click(function(){
	        $("#panel").toggle("fast");
	        $(this).toggleClass("active");
	        if ($('#trigger.active')) {
	        $('#panel').attr('aria-hidden',false);}
	        return false;
	        });
	        */
	        if ($("#panel")) {

	            panelWidth = $('#panel').css('width');
	            panePaddingRight = $('#panel').css('padding-right');
	            panelMove = (parseInt(panePaddingRight) || 0) + (parseInt(panelWidth) || 0);

	            $("#panel").attr('aria-hidden', true);
	        }
	        if ($("#trigger")) {
	            $("#trigger").click(function () {

	                $("#panel").toggle(600);
	                $('body').toggleClass("panelFix");

	                if ($('#trigger.active')) {

	                    // C2 - I think IE7 needs to compare the actual bool value rather than a string. this would probably be safe for all browsers but I don't want to play with it 
	                    if (($('#panel').attr('aria-hidden') == 'true') || ($('#panel').attr('aria-hidden') == true)) {
	                        moveto = panelMove + 'px';
	                        panelHidden = false;
	                    } else {
	                        moveto = '1px';
	                        panelHidden = true;
	                    }

	                    $(this).animate({
	                        right: moveto
	                    }, 590, function () {
	                        //console.log(moveto + " , " + panelHidden);
	                        $('#panel').attr('aria-hidden', panelHidden);
	                    });
	                }

	                $(this).toggleClass("active");
	                return false;
	            });
	        }



	        if ($("#nav-main ul ul")) {

	            if ($("#nav-main ul ul").length > 0) {
	                $("#header").css("padding-bottom", "44px");
	            };

	            if (!$("#nav-main ul ul").length > 0) {

	                $("#nav-main li a.selected").css("border", "none");
	            };
	        }

	        if ($("#rss-link a")) {
	            if (!$("#rss-link a").length > 0) {
	                $("#rss-link").css("display", "none");
	            };
	        }
	        if ($("#nav-channel li:first-child")) {
	            $("#nav-channel li:first-child").addClass("first-child");
	        }
	        if ($("#nav-channel li:last-child")) {
	            $("#nav-channel li:last-child").addClass("last-child");
	        }
	        if ($(".pager li:not(:last)")) {
	            $(".pager li:not(:last)").append("|");
	        }


	        if ($("#frm-filter")) {
	            // Set value of fields	
	            $("#frm-filter .date").attr("value", "dd/mm/yyyy");
	            var text = "dd/mm/yyyy";

	            $("#frm-filter .date").focus(function () {
	                if ($(this).attr("value") == text) $(this).attr("value", "");
	            });

	            $("#frm-filter .date").blur(function () {
	                if ($(this).attr("value") == "") $(this).attr("value", text);
	            });

	        }


	        //$(".formHolder p.button	input").addClass("on");
	        if ($("#tabs, #ee-tabs")) {
	            $("#tabs, #ee-tabs").tabs();
	        }

	        if ($("#carousel-home")) {
	            $("#carousel-home").tabs({ fx: { opacity: "toggle"} }).tabs("rotate", 12000, true);
	            $("#carousel-home").hover(
				function () {
				    $("#carousel-home").tabs("rotate", 0, true);
				},
				function () {
				    $("#carousel-home").tabs("rotate", 12000, true);
				}
		);
	        }
	        if ($("#carousel")) {
	            $("#carousel").tabs({ fx: { opacity: "toggle"} }).tabs("rotate", 8000, true);
	            $("#carousel").hover(
			function () {
			    $("#carousel").tabs("rotate", 0, true);
			},
			function () {
			    $("#carousel").tabs("rotate", 8000, true);
			}
	);
	        }

	        if ($("#subs-holder")) {
	            $("#subs-holder h3").collapsible();
	        };

	        if ($("#sectors")) {
	            $('#sectors h3').collapsible();
	        };

	        //if("#resources-main") {
	        //		$('#resources-main h2').resources();
	        //	};

	        if ($("#frm-feedback")) {
	            $("#frm-feedback h2").collapsible();
	        };

	        if ($("#sector-dropdown")) {
	            $("#sector-dropdown h2").collapsible();
	        };

	        // Add pdf icons to pdf links
	        if ($("a[href$='.pdf']")) {
	            $("a[href$='.pdf']").addClass("pdf");
	        }
	        if ($("a[href$='.xls']")) {
	            // Add Excel icons to pdf links
	            $("a[href$='.xls']").addClass("xls");
	        }
	        if ($("a[href$='.doc']")) {
	            // Add txt icons to document links (doc, rtf, txt)
	            $("a[href$='.doc'], a[href$='.txt'], a[href$='.rft']").addClass("txt");
	        }
	        if ($("a[href$='.zip']")) {
	            // Add zip icons to Zip file links (zip, rar)
	            $("a[href$='.zip'], a[href$='.rar']").addClass("zip");
	        }
	        if ($("a[href^='mailto:']")) {
	            // Add email icons to email links
	            $("a[href^='mailto:']").addClass("email");
	        }
	        //Add external link icon to external links - 
	        /*
	        $('a').filter(function() {
	        //Compare the anchor tag's host name with location's host name
	        return this.hostname && this.hostname !== location.hostname;
	        }).addClass("external");
	        //You might also want to set the _target attribute to blank
	        */
	        if ($("a")) {
	            $('a').filter(function () {
	                //Compare the anchor tag's host name with location's host name
	                return this.hostname && this.hostname !== location.hostname;
	            }).addClass("external").attr("target", "_blank");
	        }
	        if ($(".case.captionfull")) {
	            // Sub Sector Rollovers
	            $('.case.captionfull').hover(function () {
	                $(".cover", this).stop().animate({ opacity: '1' }, { queue: false, duration: 160 });
	            }, function () {
	                $(".cover", this).stop().animate({ opacity: '0' }, { queue: false, duration: 160 });
	            });
	        }

	    });
          
// Roll over helpers
function doOverImg(path)	{
	start = path.substring(0, path.indexOf('.'))
	end = path.substring(path.indexOf('.'))
	overpath = (start + overSuffix + end);
	return overpath
}
function doDefaultImg(path)	{
	return path.replace(overSuffix,'');
}

/*
// C2 - This was breaking the home page. Don't think it's used any longer

jQuery(function($){
	$('#slider1').bxSlider({
		infiniteLoop: true,
		hideControlOnEnd: true,
		pager: false
	});
});
*/

// Roll over helpers
function doOverImg(path) {
	start = path.substring(0, path.indexOf('.'))
	end = path.substring(path.indexOf('.'))
	overpath = (start + overSuffix + end);
	return overpath
}
function doDefaultImg(path) {
	return path.replace(overSuffix, '');
}


$(function () {
    if ($("#carousel-home")) {
        $("#carousel-home").css('display', '')
    }
    if ($("#carousel")) {
        $("#carousel").css('display', '')
    }
});
//$(document).ready(function () {
//    $('#testlightBoxDivId').jqm({ ajax: '/lightbox-pages/lightboxtest.aspx', trigger: 'a.RAS' });

    // content page You can also use the line below to specify that the lightbox should take the url from the links href
    // <a href="test.htm" class="modalTrigger">Test the lightbox</a><br /><br />
    // $('#lightBoxDivId').jqm({ajax: '@href', trigger: 'a.modalTrigger'});
//});

