﻿/*---------------------------------------
   Global behaviour
-------------------------------------- */

/*
Copyright 2007 by Marco van Hylckama Vlieg
Free for non-commercial use
*/

    //To restrict Share Options list
    var addthis_options = 'bebo, digg, facebook, hotmail, stumbleupon, yammer, delicious, email, google, myspace, twitter';

jQuery.fn.initAccordion = function() {

    this.each(function(){

    $('.accordionInner',this).hide();
    $('.accordionInner:first',this).show();
    $('.accordionItem:first',this).addClass('on');
    
    $('.accordionItem h3 a',this).click(function() {
        if( $(this).parent().parent().hasClass('on') ) {
            $(this).parent().parent().removeClass('on');
            $(this).parent().parent().find(".accordionInner").slideUp('normal');
        } else {
            $(this).parent().parent().siblings().removeClass('on');
            $(this).parent().parent().addClass('on');
        
            var checkElement = $(this).parents(".accordionItem").find(".accordionInner");
            // slideUp not working in FF because accordionInner container needs clear
            $(this).parents(".accordion").find(".accordionInner:visible").slideUp('normal');
            checkElement.slideDown('normal');
        }
        return false;
    });
    
    });
    return this;
  };

function autoFill(id, v){
    $(id).attr({
        value: v
    }).focus(function(){
        if($(this).val()==v){
            $(this).val("").css({
                color: "#000"
            });
        }
    }).blur(function(){
        if($(this).val()===""){
            $(this).val(v);
        }
    });
}

function domainRef(){
    var domainRefVal = "false";
    
    if (location.href.substring(7,15) == "www.qcda"){
        domainRefVal = "www.qcda";
    }else if (location.href.substring(7,18) == "content2010"){
        domainRefVal = "content2010";
    }else if (location.href.substring(7,15) == "qcda2010"){
        domainRefVal = "qcda2010";
    } 
return domainRefVal;
}


function markExternalLinks(){
    
    if (jQuery.browser.msie) {
        $("#mainContent a[href*='https://'], #secondaryContent a[href*='https://, #featureSecondaryContent a[href*='https://']").addClass("external").attr("target", "_blank").attr("title", function(){
            return this.title + " (Link to an external website)";  
        });
        
        $("#mainContent a[href*='http://'], #secondaryContent a[href*='http://'], #featureSecondaryContent a[href*='http://'],").not(".openBtn, [href*='" + location.hostname + "']").addClass("external").attr("target", "_blank").attr("title", function(){
            return this.title + " (Link to an external website)";
        });
        
    }
    else {
    
        $('#mainContent a, #secondaryContent a, #featureSecondaryContent a').filter(function(){
            return this.hostname && this.hostname !== location.hostname;
            
        }).addClass("external").attr("target", "_blank").attr("title", function(){
            return this.title + " (Link to an external website)"; 
        });
        
    }
}

/* produce captions with HTML instead
jQuery.fn.createCaption = function(){
    this.each(function(){
        //get caption text
        var caption = $(this).attr("alt");
        //strip out existing css
        $(this).css({'padding': '0','margin': '0'});
        //set width
        
        //this hack needs refactored into something nicer. 
        if (jQuery.browser.msie || jQuery.browser.mozilla) {
            width = $(this).width();
            //width = $(this).attr("width");
                if ($(this).hasClass("right")) {
                
                    $(this).wrap("<p class='captionRightWrapper' style='width:"+ width +"px'></p>")
                }
                else if ($(this).hasClass("left")) {
                    $(this).wrap("<p class='captionLeftWrapper' style='width:"+ width +"px'></p>")
                }
                $(this).after("<span>" + caption + "</span>")
        }
        else {
            this.onload = function(){
                width = $(this).width();
            if ($(this).hasClass("right")) {
                $(this).wrap("<p class='captionRightWrapper' style='width:"+ width +"px'></p>")
            }
            else if ($(this).hasClass("left")) {
                $(this).wrap("<p class='captionLeftWrapper' style='width:"+ width +"px'></p>")
            }
            $(this).after("<span>" + caption + "</span>").css({"width": "'+ width +'px"})
            }
        }    
        })
}
*/
jQuery.fn.createNotes = function(){
    $(this).each(function(){
        var linkID = $(this).attr("href");
        linkID = linkID.substring(1);
        $(this).bind('click', function() {
            $(".definitionBox a").each(function(){
                if($(this).attr("name") == linkID){
                    $(this).parent().show();
                    $(this).parent().append("<a href='#' class='close'>Close explantory notes</a>").bind('click', function() {
                        $(this).hide();return false;
                    });
                }
            });
            return false;
        });
    });
};
    
jQuery.fn.showDefinitionBoxes = function(){
    $(this).bind('click', function() {
        $(".definitionBox").show();
        $(".definitionBox").append("<a href='#' class='close'>Close explantory notes</a>").bind('click', function() {
            $(this).hide(); return false;
             });
        return false;
    });
};

$(document).ready( function(){

    $(".definitionBox").hide();
    $(".definitionLink").createNotes();
    $(".showAll").showDefinitionBoxes();
    markExternalLinks();

    // show that we have javascript
    $('body').addClass('js');
    
    //$(".jsHide").hide();
    
    //auto fill search field
    autoFill($("#frmSiteSearch #searchString"), $("#frmSiteSearch fieldset label").text());
    
    // add 'print' link
    $('ul.pageTools').append('<li><a href="javascript:window.print();"><img alt="print page" src="/images/interface/icnPrint.png"/></a></li>');

    // add 'last' class to last-child items
    $('.polls #mainContent ul.pollListing li:last-child').addClass('last');

    $(".accordion").initAccordion();
    
    // form validation
    $('.form div.required').each(function() {
        $(this).find('input, textarea').blur(function() {
            if($(this).attr('value') === "") {
                $(this).parent().find('strong.error').remove();
                var fieldName = $(this).parent().find('label').text();
                fieldName = fieldName.replace(/\ Required/, '');
                $(this).parent().append('<strong class="error">' + fieldName + ' is a required field</strong>');
                $(this).parent().addClass('error');
            } else {
                $(this).parent().find('strong.error').remove();
                $(this).parent().removeClass('error');
            }
        });
    });
    $('.form div.required .emailAddress').each(function() {
        $(this).blur(function() {
            var validEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
            var enteredEmail = $(this).attr('value');
            if (validEmail.test(enteredEmail)) {
                $(this).parent().find('strong.error').remove();
                $(this).parent().removeClass('error');
            } else {
                $(this).parent().find('strong.error').remove();
                $(this).parent().append('<strong class="error">Please enter a valid email address</strong>');
                $(this).parent().addClass('error');
            }
        });
    });
    
    // publications search
        // hide all options, turn headers 'off'
        $('.frmFilter fieldset.options .panel').hide();
        $('.frmFilter fieldset.options h2.hdr').addClass('off');
        // show first two options
        $('.frmFilter fieldset.options .panel:eq(1), .frmFilter fieldset.options .panel:eq(2)').slideDown();
        $('.frmFilter fieldset.options h2.hdr:eq(1), .frmFilter fieldset.options h2.hdr:eq(2)').removeClass('off');
        // toggle options and 'off' class on click
        $('.frmFilter fieldset.options h2').click(function() {
            $(this).toggleClass('off');
            $(this).parent().find('.panel').slideToggle();
        });
        // topics overlay
        $('#topicFilter ul ul').hide();
        $('#topicFilter a.hdr').click(function() {
            $(this).toggleClass('off');
            $(this).parent().find('ul').slideToggle();
            return false;
        });
    
    
    //if ($('#mainFeature').size() > 1) {
        //new cycle animation for slideshow
        $('#mainFeature').cycle({
            fx: 'scrollHorz',
            timeout: 0,
            speed: 300,
            startingSlide: 0
        });
   // }

    $(".titles li:eq(0)").addClass("on");
    $(".titles li a").each(function(index){
        $(this).bind('click', function() {
            $('#mainFeature').cycle(index);
            $(".titles li").removeClass("on");
            $(this).parent().addClass("on");
            return false;
        });
    });

    // make images on 'spotlights' links
    $('.spotlight').hover(function() {
        $(this).addClass('hover');
    }, function() {
        $(this).removeClass('hover');
    }).click(function() {
        window.location=$(this).find('a').attr('href');
    });
    // make images on landing pages links
    $('.list .row img, .list .item img').hover(function() {
        $(this).parent().addClass('hover');
    }, function() {
        $(this).parent().removeClass('hover');
    }).click(function() {
        window.location=$(this).parent().find('a').attr('href');
    });
    // make blog images on homepage a link
    $('#secondaryContent .section.blog img, #secondaryContent .section.blog p.label').hover(function() {
        $(this).addClass('hover');
    }, function() {
        $(this).removeClass('hover');
    }).click(function() {
        window.location=$(this).parent().parent().find('h3 a').attr('href');
    });
    $('.mailingListPromo').hover(function() {
        $(this).addClass('hover');
    }, function() {
        $(this).removeClass('hover');
    }).click(function() {
        window.location=$(this).find('a').attr('href');
    });
    $('.blogs #mainContent .blogList .imgBlock').hover(function() {
        $(this).addClass('hover');
    }, function() {
        $(this).removeClass('hover');
    }).click(function() {
        window.location=$(this).parent().find('h3 a').attr('href');
    });

    // launch publications topic selector
    $('#topicFilter .launchList').click(function() {
        $('#topicFilter').addClass('on');
        $('#topicFilter .filter').slideDown();
        return false;
    });
    
    $('#topicFilter .closeList').live("click", function(){
        $('#topicFilter').removeClass('on');
        $('#topicFilter .filter').slideUp();
        return false;
    });
    
    /*$('#topicFilter .closeList').click(function() {
        $('#topicFilter').removeClass('on');
        $('#topicFilter .filter').slideUp();
        return false;
    });*/
    
    // events on homepage
    $('.events .month').hide();
    $('.events .month').filter(".active").show();
    $('.events a.next').click(function() {
        $(this).parent().parent().parent().parent().fadeOut('slow', function() {
            $(this).next().fadeIn();
        });
        return false;
    });
    $('.events a.prev').click(function() {
        $(this).parent().parent().parent().parent().fadeOut('slow', function() {
            $(this).prev().fadeIn();
        });
        return false;
    });
    
    // show/hide news on homepage
    $('.news ul.items li').hide();
    $('.news ul.items li:eq(0), .news ul.items li:eq(1)').show();
    $('.news .filter ul li a.more').click(function() {
        $(this).parent().parent().parent().parent().find('ul.items li:hidden:first').show();
        return false;
    }); // show more items
    $('.news .filter ul li a.less').click(function() {
        $(this).parent().parent().parent().parent().find('ul.items li:visible:last').hide();
        return false;
    }); // show less items
    
    $("a[rel='colorbox']").colorbox();

    //add caption to images with caption class - remove from js
    //$("#mainContent img.caption").createCaption();



    
});