/* YouthHockey.com Application.js */

// $('element').scrollTo()
// $('element').scrollTo(speed)
	$.fn.scrollTo = function(speed) {
		var offset = $(this).offset().top - 30
		$('html,body').animate({scrollTop: offset}, speed || 1000)
		return this
	};

	

// Behaviors START
$(document).ready(function(){





	//Done loading, show the content and remove the loading animation
	$('#hidder').removeClass('hide');
	$('#page_loader').hide();
	
	$('#rightloading').fadeIn('fast');
	$('#rightloader').hide();	


/*
   $(".schedule_table").tablesorter({widthFixed: false, headers: {0:{sorter:false},1:{sorter:false}} })   
	$(".schedule_table").bind("sortStart",function() {
		$("#page_loader").show();
	}).bind("sortEnd",function() {
		$("#page_loader").hide();
	});
*/


	$("#checkboxall").click(function()				
	{
		var checked_status = this.checked;
		$("input[name=event_ids[]]").each(function()
		{
			this.checked = checked_status;
		});
	});





	// Target IE6 and below
	//if ($.browser.msie && $.browser.version <= 6 ) $("#browserUpgrade").css("display", "block" );


	$("#accordion").accordion({
		collapsible: true,
		active: false,
		animated: false
	});


	// Accordion for matching UI
	$('ul#teams').accordion({
	active: false,
	navigation: true,
	header: 'div.teamHandle',
	selectedClass: 'teamHandleSelect',
	alwaysOpen: false,
	animated: false,
	autoHeight: false
	});


	$('td.detail_link a').click(function() {
		$('body').scrollTo();
	});





	// Collapse Left Column
	$('#collapse_btn').live("click", function(){
		$(this).hide();
		$('#collapse_btn_expand').show();
		$('#right').addClass("right60");
		$('#left').addClass("left40");
		$.cookie('leftCol', 'collapsed');
		return false;
	});
	
	
	
	
	// Collapse Left Column
	$('#collapse_btn_expand').live("click", function(){
		$(this).hide();
		$('#collapse_btn').show();
		$('#right').removeClass("right60");
		$('#left').removeClass("left40");
		$.cookie('leftCol', 'expanded');
		return false;
	});	


/* ------------------------------------------------------------------------------------------------ */

	// COOKIES
    // Left column state
	var leftCol = $.cookie('leftCol');



	if (leftCol == 'collapsed') {
		$('#collapse_btn').hide();
		$('#collapse_btn_expand').show();    
		$('#right.calendarview').addClass("right60");
		$('#left.calendarview').addClass("left40");
	};
	
	if (leftCol == 'expanded') {
		$('#collapse_btn_expand').hide();
		$('#collapse_btn').show();    
		$('#right.calendarview').removeClass("right60");
		$('#left.calendarview').removeClass("left40");
	};
	   
	
 



	// Accordion for game picking
    $('#ms_iceslot_container').accordion({
    		active: false,
    		navigation: true, 
            header: 'div.iceslot_row_header',
            selectedClass: 'teamHandleSelect',
    		alwaysOpen: false, 
   			animated: false, 
    		autoHeight: false          
        });


		//This gets destroyed when large calendar is paged - it is replaced by a function in cal_sm_nav_monthly_schedule
		$(".datepicker").datepicker({
			duration: 0,
			showButtonPanel: true,
			showOtherMonths: true,
			minDate: +1			
		});
		
		$(".multi_datepicker").datepicker({
			duration: 0,
			showButtonPanel: true,
			showOtherMonths: true,
			minDate: +1
		});	
		
		
		var currentDate = $('#dateValue').attr('value');
		
		

		
		$(".day_picker").datepicker({
			defaultDate: new Date(currentDate),
			duration: 0,
			dateFormat: 'yy/mm/dd',
			altFormat: 'M dd, yy',
			altField: '#listDate',			
			changeMonth: true,
			numberOfMonths: 2,
			showButtonPanel: true,
			showOtherMonths: true,
			//minDate: -5,
			onSelect:function(theDate) {
				var listUrlpre = $("#dayChanger").attr('value');
				var urlExtend = $('#urladdition').attr('value');
				var listUrl = (listUrlpre + theDate);
				var theUrlextended = (listUrlpre + theDate + urlExtend);
					{
						location.href=theUrlextended;
					}
				}

			
		});				






		
   
      $('input#listDate').focus(function() {this.blur();});




/*
		$(".jumpmenu2").change(function() {
		  var val = $(this).selectedValues();
		  if (val != '') {
		      location.href=val;
		  }
		});
*/


	$("#multi_event_creation_forms").tabs();






	// Radio Buttons - view all organization toggle
	$(".orgswitch").click(function(){
			if($("input[name='orgswitch']").is(":checked")){
		        var val = $(this).attr('value');
		        if (val != '') {
		            location.href=val;   
		        }				 
			}else{
				return false;
			}
	});


	
	$(".weekendcheck").click(function(){
			if($("input[name='weekendcheck']").is(":checked")){
		        var val = $(this).attr('value');
		        if (val != '') {
		            location.href=val;   
		        }				 
			}else{
				return false;
			}
	});	

	
    $(".jumpmenu").change(function() {    	  
        var val = $(this).selectedValues();
        if (val != "") {
        		$('#page_loader').show();
            location.href=val;
        }
    });


	
	$('#reloadBtn').click(function() {
    		location.reload();
    		return false; 
	}); 



	// TOOLTIPS

	$('a.caltoolBtn').tipsy({gravity: 's'});
	$('a.button_tooltip').tipsy({gravity: 'w'})
	$('.tooltip_s').tipsy({gravity: 's'})
	$('.tooltip_w').tipsy({gravity: 'w'})	
	
	
	$('a.tooly').tooltip({ showURL: false, positionLeft: true });
	

	$('a.toolyCal') 
    .livequery(function(){ 
	 	$(this).tooltip({ 
				track: true, 
				delay: 0, 
				showURL: false, 
				showBody: " - ", 
				extraClass: "pretty", 
				fixPNG: true, 
				opacity: 0.95, 
				left: 20 
		});					 			        
    });		

//Toggle form values
	$("input[name='name']").toggleVal();
	$(".toggleVal").toggleVal({
    populateFrom: "name",
    removeLabels: true
  });
  
  
//hide all list items initially (except the first one)
	$("ul.sm_menu").hide();
	
	//this function handles the clicking of the 'menu' list item
	$("a.sm_menu_btn").click(function(){
		//check the visibility of the menu to see if we should hide or show it
		if($(this).next("ul.sm_menu").is(":visible")){
			//hide it!
			$(this).next("ul.sm_menu").fadeOut("fast");
		}else{
			//show it!
			$(this).next("ul.sm_menu").fadeIn("fast");
		}
		return false
	})


	

  // new conversation scroller
  $('.jump_to_new_message').click(function() {
    $('#messageForm').scrollTo().queue(function() {
      $('#message_subject').focus()
    })
    return false
  })

	// Tertiary Tabs
	$("#tertiary_tabs1 ul").idTabs();
	$("#softtabs ul").idTabs();



	$("#event_creation_forms").tabs();


	
  // FaceBox
	$('a[rel*=facebox]').facebox(); 


	$('a#multi_event_toggle').click(function(){
 			//$("#multi_event_creation_forms").toggle();
 			$("#event_creation_forms").hide();
	});	


	$('a.new_event') 
    .livequery(function(){ 
        $(this).click(function(){
				if($("#event_creation_forms").is(":visible")){
					//populate date field
			  		var date = $(this).attr('title');
			  		$(".datetextfield").attr({value: date}).effect("highlight", {color: '#FFC700'}, 1000);
			  		return false;
				}else{
		 			$("#event_creation_forms").show();
			  		var date = $(this).attr('title');
			  		$(".datetextfield").attr({value: date}).effect("highlight", {color: '#FFC700'}, 1000);
			  		return false; 
				} 			
 			});         
    });
    
       
	$('a.hide_event_forms') 
    .livequery(function(){ 
        $(this).click(function(){
				if($("#event_creation_forms").is(":visible")){
					$("#event_creation_forms").hide();
			  		return false;
				}else{
			  		return false; 
				} 			
 			});         
    });    
   
 
 
   

	$('td.event > span') 
    .livequery(function(){ 
        $(this).click(function(){
				if($("#event_creation_forms").is(":visible")){
					//populate date field
			  		var date = $(this).attr('class');
			  		$(".datetextfield").attr({value: date}).effect("highlight", {color: '#FFC700'}, 1000); 			
		 			return false; 
				}else{
		 			$("#event_creation_forms").show();
			  		var date = $(this).attr('class');
			  		$(".datetextfield").attr({value: date}).effect("highlight", {color: '#FFC700'}, 1000); 			
		 			return false; 
				} 			
 			});          
    });


	$('a#multi_random_home_btn') 
    .livequery(function(){ 
        $(this).click(function(){
 			$("#multi_random_home").toggle();
 			//$("#multi_event_creation_forms").hide();
	  		var date = $(this).attr('title');
	  		$(".datetextfield").attr({value: date});
	  		return false; 			
 			});         
    });     
   
    
    
	$('a.timeEdit') 
    .livequery(function(){ 
        $(this).click(function(){
 			$(".timeEdit-block").toggle();
 			return false; 			
 			});         
    });    


	// Searching for games dialog - after clicking on a yellow open date.
	$('a.open').live('click', function() {
		$('#page_loader').show();
		$('body').prepend("<div class='flash_notice flash_success'><span>We are searching for matching teams, this may take a moment.</span></div>");
	});

   

   // Close the create event forms
	$('.new_event_cancel').click(function(){
 			$("#event_creation_forms").toggle();			
 	});  
    


	$(".confirm_and_send").hide();

	
	// Utility tip
/*
	$('.schedule_table tr').hover(function() {
	  $(this).css('cursor', 'pointer');
	  $('.table_tool', this).css('display', 'inline');
		}, 
	function() {
	  $('.table_tool', this).css('display', 'none');
	});
	
*/
	
	// Utility tip
	$('.utility_tip').hover(function() {
	  $(this).css('cursor', 'pointer');
	  $('.utility_tip_btn').css('display', 'inline');
		}, 
	function() {
	  $('.utility_tip_btn').css('display', 'none');
	});	
	



	
//	  $(".schedule_table tr.child-row").addClass("hide-child-row");
	  $(".schedule_table tr.child-row").hide();
	  $(".schedule_table td.row-click").click(function() { 			 			
 			if($(this).parent().next("tr.child-row").hasClass("child-row-visible")){
		   	 $(this).parent().next("tr.child-row").hide();
		   	 $(this).parent().removeClass("detail-visible");
		   	 $(this).parent().next("tr.child-row").removeClass("child-row-visible"); 				 
			}else{
				$(this).parent().addClass("detail-visible");
				$(this).parent().next("tr.child-row").show();
	     	 	$(this).parent().next("tr.child-row").addClass("child-row-visible"); 
			}
   
	  });	
 
 
 
 	  $(".close-child-row").click(function() {
 			if($(this).parent().parent("tr.child-row").hasClass("child-row-visible")){
		   	$(this).parent().parent("tr.child-row").hide();
		   	$(this).parent().parent("tr.child-row").prev().removeClass("detail-visible");
		   	$(this).parent().parent("tr.child-row").removeClass("child-row-visible"); 				 
			}else{

			}
   
	  });	
 
 



	
	
	
	// Utility tip
	//$('.schedule_table tr').click(function() {
	  //$(this).css('cursor', 'pointer');
	  //$(this).toggleClass('selected_tr');
	  //$(this).next('.child-row').toggle();
	  
	//});	
	


	// Toggle from team to organization view
	$('input#orgToggle').iToggle({
		type: 'checkbox',
		keepLabel: false,
		speed: 300,
		onClick: function(){},
		onClickOn: function(){
		  var val = $("input#orgToggle").attr('value');
		  if (val != '') {
		      location.href=val;   
		  };
		},
		onClickOff: function(){	
		  var val = $("input#orgToggle").attr('value');
		  if (val != '') {
		      location.href=val;   
		  };
		},
		onSlide: function(){},
		onSlideOn: function(){},
		onSlideOff: function(){}
	});


	
	// Utility tip
	$('li.pop_menu_btn').click(function() {		
		$(this).css('cursor', 'pointer').toggleClass('caltoolBtn_active');
		$('.pop_menu').toggleClass('hide');
	});
	
	$('.pop_menu_btn li > a').click(function() {		
		$('.pop_menu_btn').removeClass('caltoolBtn_active');
		$('.pop_menu').toggleClass('hide');
	});		
	
	
    $("li.pop_menu_btn").hover(
      function () {
        $(this).css('cursor', 'pointer').addClass('caltoolBtn_active');
        $('.pop_menu').removeClass('hide');
      }, 
      function () {
        $(this).css('cursor', 'pointer').removeClass('caltoolBtn_active');
        $('.pop_menu').addClass('hide');
      }
    );





	 $("#teamsAccordion").accordion({
			active: false,
			navigation: true, 
			header: 'div.teamHandle',
			selectedClass: 'teamHandleSelect',
			alwaysOpen: false, 
			animated: false, 
			autoHeight: false          
	  });








	    	 




	//grab all the anchor tag with rel set to shareit
	$('a.context-Menu').click(function() {		
		
		var eventMenu = $(this).next("div.theuniquemenu").attr("id");
		
		
		//get the height, top and calculate the left value for the sharebox
		//var height = $(this).height();
		//var top = $(this).offset().top;
		
		//get the left and find the center value
		//var left = $(this).offset().left + ($(this).width() /2) - ($(eventMenu).width() / 2);		
		
		//grab the href value and explode the bar symbol to grab the url and title
		//the content should be in this format url|title
		//var value = $(this).attr('href').split('|');
		
		//assign the value to variables and encode it to url friendly
		//var field = value[0];
		//var url = encodeURIComponent(value[0]);
		//var title = encodeURIComponent(value[1]);
		
		//assign the height for the header, so that the link is cover
		//$('#shareit-header').height(height);
		
		//display the box
		$('#' + eventMenu).show();
		
		//set the position, the box should appear under the link and centered
		$('#' + eventMenu).css({'top': '10px', 'left':'10px'});
		
		
	});
 
	//onmouse out hide the shareit box
	$('div.theuniquemenu').mouseleave(function () {
		$('#shareit-field').val('');
		$(this).hide();
	});
	
	//hightlight the textfield on click event
	$('div.theuniquemenu').click(function () {
		$(this).select();
	});













	//Done doing stuff, show the content
	$('#home_whole_wrapper').removeClass('home_cycle_hide');
	$('#container').removeClass('home_loading');



	// Initiate Datepickers
	$("#ui-datepickerHome").datepicker();
	$(".date_picker").datepicker();	


 	// Hide all schedule forms
 	$("a.hideBtn").click(function(){
 		$(".homeForm").hide();
 		$(".travelForm").hide();
 		
 	});

    
	// Calendar color legend togglarrrrrrr
/*
	$(".legendBtn").click(function(){
		$("#calLegendWrap").slideToggle("fast");
	});
*/

 	
	//success notification timeout
	if ($('div.success').length) {
		//    var t = setTimeout("$('div.success').fadeOut('slow')",2500);
		$('div.hide_this').css('cursor', 'pointer');
		$('div.hide_this').click(function(){
			$('div.success').slideUp();
		});
	}
   
   
   
   //error notification timeout Flash Notice
    if ($('.flash_notice').length) {
        var t = setTimeout("$('.flash_notice').fadeOut('slow')",2500);
    }    


	$('.calTable td.day') 
    .livequery(function(){ 
    // use the helper function hover to bind a mouseover and mouseout event 
        $(this) 
            .hover(function() { 
                $(this).addClass('dayHover'); 
            }, function() { 
                $(this).removeClass('dayHover'); 
            }); 
    }, function() { 
        // unbind the mouseover and mouseout events 
        $(this) 
            .unbind('mouseover') 
            .unbind('mouseout'); 
    });	


});
//end behaviours



	function hideSingleForms() {
		if($("#event_creation_forms").is(":visible")){
			$("#event_creation_forms").hide();
				return false;
			}else{
			  	return false; 
		} 
	} 






(function($){
    $.fn.jExpand = function(){
        var element = this;

        $(element).find("tr.child-row").addClass("hide-child-row");
        $(element).find("tr.hide-child-row").hide();
        //$(element).find("tr:first-child").show();

        $(element).find("tr:not(.hide-child-row)").click(function() {
            $(this).next("tr").toggle();
        });
        
    }    
})(jQuery); 





(function($) {
    $.fn.tipsy = function(opts) {

        opts = $.extend({fade: false, gravity: 'n'}, opts || {});
        var tip = null, cancelHide = false;

        this.hover(function() {
            
            $.data(this, 'cancel.tipsy', true);

            var tip = $.data(this, 'active.tipsy');
            if (!tip) {
                tip = $('<div class="tipsy"><div class="tipsy-inner">' + $(this).attr('title') + '</div></div>');
                tip.css({position: 'absolute', zIndex: 100000});
                $(this).attr('title', '');
                $.data(this, 'active.tipsy', tip);
            }
            
            var pos = $.extend({}, $(this).offset(), {width: this.offsetWidth, height: this.offsetHeight});
            tip.remove().css({top: 0, left: 0, visibility: 'hidden', display: 'block'}).appendTo(document.body);
            var actualWidth = tip[0].offsetWidth, actualHeight = tip[0].offsetHeight;
            
            switch (opts.gravity.charAt(0)) {
                case 'n':
                    tip.css({top: pos.top + pos.height, left: pos.left + pos.width / 2 - actualWidth / 2}).addClass('tipsy-north');
                    break;
                case 's':
                    tip.css({top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2}).addClass('tipsy-south');
                    break;
                case 'e':
                    tip.css({top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth}).addClass('tipsy-east');
                    break;
                case 'w':
                    tip.css({top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width}).addClass('tipsy-west');
                    break;
            }

            if (opts.fade) {
                tip.css({opacity: 0, display: 'block', visibility: 'visible'}).animate({opacity: 1});
            } else {
                tip.css({visibility: 'visible'});
            }

        }, function() {
            $.data(this, 'cancel.tipsy', false);
            var self = this;
            setTimeout(function() {
                if ($.data(this, 'cancel.tipsy')) return;
                var tip = $.data(self, 'active.tipsy');
                if (opts.fade) {
                    tip.stop().fadeOut(function() { $(this).remove(); });
                } else {
                    tip.remove();
                }
            }, 100);
            
        });

    };
})(jQuery);
