jQuery(function($){
    $.fn.moreOrLess = function (options) {
        // The height of the content block when it's not expanded
        var defaults = {
            adjustHeight:240, 
            moreText: "+ More", 
            lessText: "- Less"
        };
        options = $.extend({}, defaults, options);

        return this.each(function() {
                
            var $this, $outerDIV, height;
            $this = $(this);
            height = $this.css('height').replace('px','');
            if (height > options.adjustHeight) {
                // Sets the .more-block div to the specified height and hides any content that overflows
                $this.css('height', options.adjustHeight).css('overflow', 'hidden').addClass('more-block');
                
                // The section added to the bottom of the "more-less" div
                $outerDIV = ('<div class="more-less" />');
                $this.wrap($outerDIV);                

                $this.parent().append('<div class="adjust">' + options.moreText + '</div>');
                
                //Set click properties
                $(".adjust", $this.parent()).toggle(function () {
                    var $this = $(this);
                    //More Clicked
                    $this.parents("div:first")
                    .find(".more-block")
                    .animate({height: height + 'px'},700, function() {
                        
                        $this.text(options.lessText).parents("div:first").find("span.continued").css('display', 'none');
                    });
                }, function () {
                    //Less Clicked
                    var $this = $(this);
                    $(this).parents("div:first").find(".more-block").animate({
                        height: options.adjustHeight
                        },700, function() {
                        $this.text(options.moreText).parents("div:first").find("span.continued").css('display', '');
                    });
                });
            }
        });
    };


    $('.expandContent').moreOrLess({
        'adjustHeight' : 233, 
        'moreText' : '+ Expand', 
        'lessText' : '- Hide'
    });
    $('.expandContentFR').moreOrLess({
        'adjustHeight' : 233, 
        'moreText' : '+ plus', 
        'lessText' : '- moins'
    });

    /* $('header .mega-panel').hide().removeClass('initial');
	$('header nav>ul>li').hover(function(){
		$(this).find('.mega-panel').fadeIn();
	},function(){
		$(this).find('.mega-panel').fadeOut();
	}) */


    $(".photos a[rel='latest-photos'], .gallery a[rel='gallery']").colorbox({
		transition:"fade",
		innerWidth: 978,
		innerHeight: 740,
//		initialHeight: 740,
//		initialWidth: 978,
		data: { ajax: true },
		iframe: true
//                ,
//		top: 0,
////                onOpen: function(){
////                    $('nav.cf').addClass('nav-on-top');
////                },
////                onComplete: function(){
////                    $('nav.cv').removeClass('nav-on-top');
////                }
////                ,
//		onOpen: function() { 
//			window.scrollTo(0,$('#wpadminbar').size()?22:0);
//                        //$('nav.cf').addClass('nav-on-top');
//		},
//		onComplete: function() {
//			$('#colorbox').css({
//				top: (175 + ($('#wpadminbar').size()?28:0)),
//                                width: 977
//			});
////                        $('nav.cv').removeClass('nav-on-top');
//		}
	}).attr('href',function(){
		return $(this).attr('href') + '?ajax=true';
	});
	
        
        $('#cboxNext, #cboxPrevious').css({'opacity': '0.65'});
        $('#cboxNext, #cboxPrevious').hover(
        function(){
            $(this).stop().animate({'opacity': '1'})},
        function(){
            $(this).stop().animate({'opacity': '0.65'})
        });
        
    $(".videos a[rel=video]").colorbox({
		transition: 'none',
		innerWidth: 978,
		innerHeight: 391,
//		initialWidth: 940,
//		initialheight: 391,
		data: { 
                    ajax: true 
                },
		iframe: true
//                ,
//		top: 245,
//		onOpen: function() { $('#cboxNext').css({'left' : '546px', 'right' : '0px'}) }
//		onComplete: function() {
//			$('#colorbox').css({
//				top: (245 + ($('#wpadminbar').size()?28:0))
//			});
//                        $('nav.cv').removeClass('nav-on-top');
//		}
	}).attr('href',function(){
		return $(this).attr('href') + '?ajax=true';
	});
	
    var athletes_link = (icl_lang == 'fr' ? 'equipe-canada' : 'team-canada');
    $('.first_fullwidth select[name=games]').after('<form class="search" method="get" action="' + icl_home + athletes_link + '/"><input type="text" name="s" class="search"><input type="submit" value="Search"></form>');                
        
    $('.first_fullwidth select').click(function(){
        $(this).parents('.fullwidth').addClass('is_hover');
    }).blur(function(){
        $(this).parents('.fullwidth').removeClass('is_hover');
    }).customStyle();
	
	
    $('.first_fullwidth select[name=sports], .first_fullwidth select[name=games]').change(function(){
        if ($.trim($(this).val())) {
            var str = $(this).val().split('/');
            code = str.shift();
            if (code.length == 2) {
                str.unshift(athletes_link);
            } else {
                str.unshift(athletes_link, code);
            }
            window.location = icl_home + str.join('/');
        }
    });
	
    $('.first_fullwidth form').submit(function(e){
        e.preventDefault();
        if ($.trim($(this).find('input[type=text]').val()))
            window.location = icl_home + athletes_link + '?s=' + $(this).find('input[type=text]').val();
    })

    $('.sc_tab').click(function() {
        $('.sc_tab').removeClass('sc_tab_active');
        $(this).addClass('sc_tab_active');
        var which = $(this).attr('title');
        $(this).parent().parent().find('.sc_tab_single_box').css('display','none');
        $(this).parent().parent().find('.sc_tab_single_box').eq(which).css('display','block');
    });

	jQuery(".toggle_body").hide();

	jQuery("h4.toggle").toggle(function(){
		jQuery(this).addClass("toggle_active");
		}, function () {
		jQuery(this).removeClass("toggle_active");
	});

	jQuery("h4.toggle").click(function(){
		jQuery(this).next(".toggle_body").slideToggle();

	});


    //JS for Athlete Sidebar filters

    $('div.filters select').customStyle();
        
    //$('div.filters > ul > li:first').after('<li><form class="search" method="get" action="' + icl_home + athletes_link + '/"><input type="text" name="s" class="search" value="Search Profiles"><input type="submit" value="Search"></form></li>');
	
    $('div.filters > ul > li form input[type=text]').click(function(){
        $(this).val('');
    });
    
    $('div.filters > ul > li form input[type=text]').blur(function(){
        $(this).val('Search Profiles');
    });        	
  
    var filterVals = new Array();
    $('div.filters select[name=sports-profile-filter]').change(function(){
       filterVals[0] = $(this).val();
       console.log(filterVals);
    });
    
    $('div.filters select[name=games-profile-filter]').change(function(){
       filterVals[1] = $(this).val();
       console.log(filterVals);
    });
    
    $('div.filters form button').click(function(event){
        event.preventDefault();
        var cleanVals = $.grep(filterVals,function(n){return(n);});
        
        var splitVals = cleanVals.join("/").split("/");
        console.log(splitVals);
        var newFilterVals = "";
        for (var i=0; i < splitVals.length; i++) {
            if (i != 2 && splitVals[i]) {
                newFilterVals += splitVals[i]+"/"
            }
        }
        //console.log(newFilterVals);
        var form = $('form#filter-profiles-form');
        form.attr('action', icl_home + athletes_link + "/" + newFilterVals).submit();        
    });
})


