/*----------------------
[custom JS styles]

Project: If main site
Last change: 15.01.10
Assigned to: Felicity Evans
-----------------------*/

$(document).ready(function() {

    // Detect IE
    if (jQuery.browser.msie) {
        var ieX = (parseInt(jQuery.browser.version));
        if (ieX == 8) { jQuery('body').addClass('ie8'); }
        else if (ieX == 7) { jQuery('body').addClass('ie7'); }
        else if (ieX == 6) { jQuery('body').addClass('ie6'); };
    }

    //Modernizr feature detection
    if ($("html").hasClass("no-borderradius")) {
        $('.home .featureStrip').append('<div class="featureStripTopLeft"></div><div class="featureStripTopRight"></div><div class="featureStripBottomLeft"></div><div class="featureStripBottomRight"></div>');
        $('.mainNav').append('<div class="tl"></div><div class="tr"></div><div class="bl"></div><div class="br"></div>');
    }

    //Alternating backgrounds rows
    $('table tr:even').not('table.interactive tr:even,#telerik table tr:even,.RadComboBox table tr:even').addClass('even');
    $('.listing > li:even,.featureListing > li:even,.newsListing > li:even,.courseListing > li:even,.jobListing > li:even,.peopleListing > li:even').addClass('even');

    //Nth-child replacement
    $(".courseOverview dl dd:nth-child(4n)").addClass("even");

    //External links in a new window
    $("a.ext").click(function() {
        window.open($(this).attr('href'));
        return false;
    });

    //autofill search form
    $("#wrapper .siteSearch input.submit").attr("value", "GO");
    autoFill($("#wrapper .siteSearch input.text"), "Search here");

    // use title for value
    $(".useTitleForValue").useTitleForValue();

    // General focus enhancement for input styling
    $('input[type=text],input[type=password],textarea').focus(function() { jQuery(this).addClass('focus'); });
    $('input[type=text],input[type=password],textarea').blur(function() { jQuery(this).removeClass('focus'); });

    // Inline field tips
    $('.formTip').hide();
    $('input,select,textarea').focus(function() {
        if (jQuery(this).siblings('span').children('.formError').is(':visible')) {
            jQuery(this).siblings('.formTip').hide();
        } else {
            jQuery(this).siblings('.formTip').show();
        }
    });
    $('.telerik-input input').focus(function() {
        if (jQuery(this).closest('.telerik-input').siblings('span').children('.formError').is(':visible')) {
            jQuery(this).closest('.telerik-input').siblings('.formTip').hide();
        } else {
            jQuery(this).closest('.telerik-input').siblings('.formTip').show();
        }
    });
    $('select').change(function() {
        if (jQuery(this).siblings('span').children('.formError').is(':visible')) {
            jQuery(this).siblings('.formTip').hide();
        } else {
            jQuery(this).siblings('.formTip').show();
        }
    });
    $('input:not(.applyNow),select,textarea').blur(function() {
        jQuery(this).siblings('.formTip').hide();
    });
    $('input.applyNow').blur(function() {
        setTimeout(function() {
            $('input.applyNow').siblings('.formTip').hide();
        }, 500);
    });
    $('.telerik-input input').blur(function() {
        jQuery(this).closest('.telerik-input').siblings('.formTip').hide();
    });

    //Home page feature
    $('.homeFeature .tab').hover(function() {
        $('.homeFeature .tab').removeClass('activeTab');
        $('.homeFeature .feature').removeClass('activeFeature');
        $(this).addClass('activeTab');
        $(this).prev('.feature').addClass('activeFeature');
    });
    $(".homeFeature .tab").click(function() {
        window.location = $(this).find("a").attr("href"); return false;
    });

    var homeFeatureTallest = $(".homeFeature li .tab").tallestSize();
    $(".homeFeature li .tab").css('height', homeFeatureTallest);

    //Colorbox
    $('.colorbox').colorbox();
    $('.colorboxInPage').colorboxInPage();
    $('.colorboxInLine').colorboxInLine();
    $('.colorboxIFrame').colorboxIFrame();
    $('.colorboxIFrameSmall').colorboxIFrameSmall();


    //Slideshow
    var CarouSlideSChildren = $(".slideshow .CarouSlide .slider-holder").children().length;
    if (CarouSlideSChildren == 0) {
        $(".slideshow").remove();
    } else if (CarouSlideSChildren == 2) {
        $(".slideshow .CarouSlide").CarouSlide({
            animType: "slide",
            animInfinity: false,
            showSlideNav: false,
            showBackNext: true,
            animTime: 600
        });
    } else if (CarouSlideSChildren > 2) {
        $(".slideshow .CarouSlide").CarouSlide({
            animType: "slide",
            animInfinity: true,
            showSlideNav: false,
            showBackNext: true,
            animTime: 600
        });
    }

    // Event carousel
    var CarouSlideEChildren = $(".events .CarouSlide .slider-holder").children().length;
    if (CarouSlideEChildren == 0) {
        $(".events").remove();
    } else if (CarouSlideEChildren == 1) {
        $(".slider-nav-wrap").remove();
    } else if (CarouSlideEChildren == 2) {
        $(".events .CarouSlide").CarouSlide({
            animType: "slide",
            autoAnim: true,
            slideTime: 3500,
            animTime: 400,
            animInfinity: false,
            showBackNext: true
        });
    } else if (CarouSlideEChildren > 2) {
        $(".events .CarouSlide").CarouSlide({
            animType: "slide",
            autoAnim: true,
            slideTime: 3500,
            animTime: 400,
            animInfinity: true,
            showBackNext: true
        });
    }

    // Tool tip 
    function addToolTip(toolTipItems) {
        if (jQuery('html').attr('dir') == 'rtl') { yOffset = -0; }
        else { yOffset = -189; };
        xOffset = -21;
        toolTipItems.hover(function(e) {
            this.t = this.title;
            this.title = "";
            jQuery("body").append("<span class='toolTipC'><span>" + this.t + "</span></span>");
            jQuery(".toolTipC").css("top", (e.pageY - xOffset) + "px").css("left", (e.pageX + yOffset) + "px").fadeIn("fast");
        }, function() {
            this.title = this.t;
            jQuery(".toolTipC").remove();
        });
        toolTipItems.mousemove(function(e) {
            jQuery(".toolTipC").css("top", (e.pageY - xOffset) + "px").css("left", (e.pageX + yOffset) + "px");
        });
    };
    toolTipItems = jQuery(".toolTip");
    addToolTip(toolTipItems);

    // Show/hide list
    //Expand list item if class name is defined on LI
    $('.show-hideContent li:last-child').addClass('last-child');
    if ($('.show-hide li').hasClass('expanded')) {
        $('ul.show-hide li.expanded .button').show();
        $('.show-hideContent').not('.expanded .show-hideContent').hide();

    } else {
        $('ul.show-hide li:first-child').addClass('expanded');
        $('ul.show-hide li:first-child .button').show();
        $('.show-hideContent').not('ul.show-hide > li:first-child .show-hideContent').hide();

    }
    //Expand/contract content
    $('.show-hide .show-hideTitle h2').click(function() {
        if ($(this).closest('li').hasClass('expanded')) {
            $(this).closest('div').next('div').slideUp();
            $(this).closest('li').removeClass('expanded');
            $(this).next(".button").fadeToggle("fast");
        } else {
            $(this).closest('div').next('div').slideDown();
            $(this).closest('li').addClass('expanded');
            $(this).next(".button").fadeToggle("fast");
        }
    });

    //Spotlights equal height
    var spotlightTallest = $(".spotlight").tallestSize();
    $(".spotlight").css('height', spotlightTallest);

    // Promo boxes equal height
    var promoBoxTallest = $(".promoBox").tallestSize();
    $(".promoBox").css('height', promoBoxTallest);

    //Print button
    var printLink = '<li><a href="#" class="print" id="print" title="Before printing please ensure you check the \'Print background colours and images\' checkbox to ensure that it is ticked; within the \'Advanced\' tab.">Print</a></li>';
    $('#headerLinks li:last-child').removeClass('last');
    $('#headerLinks').append(printLink);
    $('#print,.printAction').click(function() {
        window.print();
        return false;
    });
    $('#headerLinks li:last-child').addClass('last');

});  //end document ready

/*
functions ------------------------------~~ */

//auto fill search field
function autoFill(id, v){
    $(id).attr({
        value: v
    }).focus(function(){
        if($(this).val()==v){
            $(this).val("").css({
                color: "#333"
            });
        }
    }).blur(function(){
        if($(this).val()==""){
            $(this).val(v);
        }
    });
}

// Submit specific button on Enter press
function clickButton(e, buttonid) {
    var evt = e ? e : window.event;
    var bt = document.getElementById(buttonid);

    if (bt) {
        if (evt.keyCode == 13) {
            bt.click();
            return false;
        }
    }
}

// Display character count left for textbox
function checkCount(txtid, spanid, n) {
    var txt = document.getElementById(txtid);
    var spn = document.getElementById(spanid);

    spn.innerHTML = n - txt.value.replace(/\r\n/g, "\n").replace(/\n/g, "\r\n").length;
}

// Fade toggle
jQuery.fn.fadeToggle = function(speed, easing, callback) {
   return this.animate({opacity: 'toggle'}, speed, easing, callback);
}; 

// jQuery useTitleForValue plugin
jQuery.fn.useTitleForValue=function(){this.each(function(){i=$(this);if(i.attr("title").length){i.addClass("inactive").attr({"value":i.attr("title")});i.focus(function(){if($(this).val()==$(this).attr("title")){$(this).removeClass("inactive").removeAttr("value");}}).blur(function(){if($(this).val()==""){$(this).addClass("inactive").attr({"value":$(this).attr("title")});}});}})
return this;};

// Events carousel
function mycarousel_initCallback(carousel)
	{
		jQuery('.carouselDots li a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        return false;
		});
		jQuery('#carouselNext').bind('click', function() {
			carousel.next();
			return false;
		});
		jQuery('#carouselPrevious').bind('click', function() {
			carousel.prev();
			return false;
		});
		// Disable autoscrolling if the user clicks the prev or next button.
		carousel.buttonNext.bind('click', function() {
			carousel.startAuto(0);
		});
		carousel.buttonPrev.bind('click', function() {
			carousel.startAuto(0);
		});
		// Pause autoscrolling if the user moves with the cursor over the clip.
		carousel.clip.hover(function() {
			carousel.stopAuto();
		}, function() {
			carousel.startAuto();
		});
	};
	function currItem(carousel,item,i,state)
	{$('.carouselDots li:nth-child('+ i +')').addClass('on');};
	function clearCurrItem(carousel,item,i,state)
	{$('.carouselDots li:nth-child('+ i +')').removeClass('on');};


	jQuery.fn.colorboxInPage = function() {
	    this.each(function() {
	        var popContents = $(this).attr('href') + ' #centreCol';
	        $(this).click(function() {
	            $(this).colorbox({
	                title: $(this).attr('title'),
	                href: popContents,
	                open: true,
	                width: 600,
	                maxHeight: 500,
	                onComplete: function() {
	                    $('.colorboxInPage').colorboxInPage();
	                    $('.colorboxInLine').colorboxInLine();
	                }
	            });
	            return false;
	        });
	    });
	};

	jQuery.fn.colorboxInLine = function() {
	    this.each(function() {
	        var popContents = $(this).attr('href');
	        $(this).click(function() {
	            $(this).colorbox({
	                href: popContents,
	                inline: true,
	                open: true,
	                width: 600,
	                maxHeight: 520
	            });
	            return false;
	        });
	    });
	};

	jQuery.fn.colorboxIFrame = function() {
	    this.each(function() {
	        $(this).click(function() {
	            $(this).colorbox({
	                iframe: true,
	                width: 600,
	                height: 500
	            });
	        });
	    })
	    //return this;
	};

	jQuery.fn.colorboxIFrameSmall = function() {
	    this.each(function() {
	        $(this).click(function() {
	            $(this).colorbox({
	                iframe: true,
	                width: 600,
	                height: 360
	            });
	        });
	    })
	    //return this;
	};
	

	
/*
Add this plugin ------------------------------~~ */
var addthis_config = {
	 ui_offset_top: -10,
	 ui_offset_left: -95
}
