// Core Javascript

/* Search box clear text */


$(document).ready(function () {




    $(".article p:first").addClass("first");
   
    var clearMePrevious = '';

    // clear input on focus
    $('.txt-search').focus(function () {
        if ($(this).val() == $(this).attr('value')) {
            $(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 () {
	    // moved to campaign page
	    //    var HideText = "' + HideText + '";
	    //    $("#camp-link").click(function () {
	    //        $("#campaign-form div").slideUp('normal');
	    //        if ($(this).next().is(':hidden') == true) {
	    //            $(this).next().slideDown('normal');
	    //            $("#campaign-form div div").slideDown('normal');
	    //            $("#campaign-form div div div").slideDown('normal');
	    //            $("#campaign-form div div div div").slideDown('normal');
	    //            $("#FormLink").text(HideText);

	    //        }
	    //        return false;
	    //    });

	    //    $("#campaign-form div").hide();

	    $('#slider1').bxSlider({
	        infiniteLoop: true,
	        hideControlOnEnd: true,
	        pager: false
	    });


	    $("#panel").attr('aria-hidden', true);



	    panelWidth = $('#panel').css('width');
	    panePaddingRight = $('#panel').css('padding-right');
	    panelMove = (parseInt(panePaddingRight) || 0) + (parseInt(panelWidth) || 0);

	    $("#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 () {
	                $('#panel').attr('aria-hidden', panelHidden);
	            });
	        }

	        $(this).toggleClass("active");
	        return false;
	    });



	    $("#nav-channel li:first-child").addClass("first-child");
	    $("#nav-channel li:last-child").addClass("last-child");
	    $(".pager li:not(:last)").append("|");
	    $("#puff-holder p.more a").wrapInner("<span></span>");



	    // Set value of fields
	    $("#frm-search input[type='text']").attr("value", "I am looking for ...");
	    var text = "I am looking for ...";

	    $("#frm-search input[type='text']").focus(function () {
	        if ($(this).attr("value") == text) $(this).attr("value", "");
	    });

	    $("#frm-search input[type='text']").blur(function () {
	        if ($(this).attr("value") == "") $(this).attr("value", text);
	    });

	    $("#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);
	    });

	    /*
	    // This has been moved to the news page as it the only place it is used

	    if ("#frm-filter") {
	    $("input.date").datepicker({
	    showOn: "button",
	    buttonImage: "/SDI/images/calendar.gif",
	    buttonText: 'Choose a Day',
	    buttonImageOnly: true,
	    dateFormat: 'dd/mm/yy'
	    });
	    };
	    */

	    $("#tabs, #ee-tabs").tabs();

	    if ("#carousel-home") {
	       
	        $("#carousel-home").tabs({ fx: { opacity: "toggle"} }).tabs("rotate", 12000, true);
	        $("#carousel").hover(
			function () {
			    $("#carousel-home").tabs("rotate", 0, true);
			},
			function () {
			    $("#carousel-home").tabs("rotate", 12000, true);
			}
		);
	    }
	   
	    $("#carousel, #carousel-sector, #carousel-channel").tabs({ fx: { opacity: "toggle"} }).tabs("rotate", 8000, true);
	    $("#carousel, #carousel-sector, #carousel-channel").hover(
		function () {
		    $("#carousel, #carousel-sector, #carousel-channel").tabs("rotate", 0, true);
		},
		function () {
		    $("#carousel, #carousel-sector, #carousel-channel").tabs("rotate", 8000, true);
		}
	);


	    //if ("#key-carousel") {
	    //	$("#key-carousel").tabs();
	    //}

	    //  $(".listing:eq(0)").addClass("first");

	    if ("#subs-holder") {
	        $("#subs-holder h3").collapsible();
	    };

	    if ("#sectors") {
	        $('#sectors h3').collapsible();
	    };


	    if ("#frm-feedback") {
	        $("#frm-feedback h2").collapsible();
	    };


	    /*if ("#sub-nav") {
	    $("#header").css("padding-bottom", "400px");
	    };*/

	    // Add pdf icons to pdf links
	    $("a[href$='.pdf']").addClass("pdf");

	    // Add Excel icons to pdf links
	    $("a[href$='.xls']").addClass("xls");

	    // Add txt icons to document links (doc, rtf, txt)
	    $("a[href$='.doc'], a[href$='.txt'], a[href$='.rft']").addClass("txt");

	    // Add zip icons to Zip file links (zip, rar)
	    $("a[href$='.zip'], a[href$='.rar']").addClass("zip");

	    // 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

	    //    $('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").attr("title", "Opens in new window");

	    // 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 });
	    });


	    /*
	    $('.slides').each(function() {
	    var $this = $(this), $ss = $this.closest('.slideshow');
	    var prev = $ss.find('a.prev'), next = $ss.find('a.next');
	    $this.cycle({
	    fx:     'scrollHorz',
	    speed:   400,
	    timeout: 0
	    });
	    });
	    */

	   


	});

// 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', '')
    }
    if ("#carousel-sector") {
        $("#carousel-sector").css('display', '')
    }
    if ("#carousel-channel") {
        $("#carousel-channel").css('display', '')
    }
    if (".diagTool") {
        $(".diagTool").css('display', '')
    }

});

//tables
$(function () {

    $('table tr:even').addClass('even');
    $('table tr:odd').addClass('odd');

});
