$(document).ready(function(){

	$("ul.topnav li a").click(function() { //When trigger is clicked...

		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click

		$(this).parent().hover(function() {
		}, function(){
			$(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
		});

	});

});

$(document).ready(function() {
 // hides the slickbox as soon as the DOM is ready
 // (a little sooner than page load)
  $('#slip').hide();
  $('#basichistory').hide();
  $('#basicstats').hide();
  $('#details').hide();

 // toggles the slickbox on clicking the noted link
  $('a#toggle_slip').click(function() {
	$('#slip').toggle(300);
	return false;
  });
  $('a#toggle_basichistory').click(function() {
	$('#basichistory').slideToggle(100);
	return false;
  });
  $('a#toggle_basicstats').click(function() {
	$('#basicstats').slideToggle(100);
	return false;
  });
  $('a#toggle_details').click(function() {
	$('#details').slideToggle(100);
	return false;
  });
});

$(document).ready(function() {
	// Attach jfav to a.addToFavorites
	// $('.addToFavorites').jFav();
	
	// Attach rel=facebox to facebox
	$('a[rel*=facebox]').facebox()
	$('a[class*=facebox]').facebox()
});

// List accordion
$(document).ready(function () {
  // hide all ULs inside LI.drawer except the first one
  $('LI.drawer UL:not(:first)').hide(); 
  
  // apply the open class
  $('LI.drawer UL:first').addClass('open');
  
  $('H4.drawer-handle').click(function () {
    // hide the currently visible drawer contents
    $('LI.drawer UL:visible').slideUp(200);
    
    // remove the open class from the currently open drawer
    $('H4.open').removeClass('open');
    
    // show the associated drawer content to 'this' (this is the current H2 element)
    // since the drawer content is the next element after the clicked H2, we find
    // it and show it using this:
    $(this).next().slideDown(200);
    
    // set a class indicating on the H2 that the drawer is open
    $(this).addClass('open');
  });
});

// Smooth scroll via http://www.zachstronaut.com/posts/2009/01/18/jquery-smooth-scroll-bugs.html
function enable_smooth_scroll() {
    function filterPath(string) {
        return string
                .replace(/^\//,'')
                .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
                .replace(/\/$/,'');
    }

    var locationPath = filterPath(location.pathname);
    
    var scrollElement = 'html, body';
    $('html, body').each(function () {
        var initScrollTop = $(this).attr('scrollTop');
        $(this).attr('scrollTop', initScrollTop + 1);
        if ($(this).attr('scrollTop') == initScrollTop + 1) {
            scrollElement = this.nodeName.toLowerCase();
            $(this).attr('scrollTop', initScrollTop);
            return false;
        }    
    });
    
    $('a[href*=#]').each(function() {
        var thisPath = filterPath(this.pathname) || locationPath;
        if  (   locationPath == thisPath
                && (location.hostname == this.hostname || !this.hostname)
                && this.hash.replace(/#/, '')
            ) {
                if ($(this.hash).length) {
                    $(this).click(function(event) {
                        var targetOffset = $(this.hash).offset().top;
                        var target = this.hash;
                        event.preventDefault();
                        $(scrollElement).animate(
                            {scrollTop: targetOffset},
                            500,
                            function() {
                                location.hash = target;
                        });
                    });
                }
        }
    });
}

// Toggle tr in table
$(function() {
	$('tr.parent')
		.css("cursor","pointer")
		//.attr("title","Click to expand/collapse")
		.click(function(){
			$(this).siblings('.child-'+this.id).toggle();
		});
	$('tr[@class^=child-]').hide().children('td');
});

// Betslip
function betslip (action,id) {
	var loc = window.location.protocol + "//" + window.location.host + "/";
	if (window.location.host == "localhost") { loc += "betscout/html/" };
	
	switch(action)
	{
		case 'add':
			$.get(loc+"ajax/betslip/add_entry/"+id, function(text) {
				if (text.length > 0) {
					$("#slip").html(text).fadeIn('slow');
					$("#slipcount").html(parseInt($("#slipcount").text())+1);
				} else {
					jQuery.facebox('<h1>Error adding bet</h1><p>Reasons could be:<ul><li>You are trying to add more than one bet from the same event</li><li>The bet you are trying to make has been changed</li><li>You are trying to add a bet for a finished event</li></ul></p>');
				};
			});

			break;
		
		case 'exclude':
			$("#slip").load(loc+"ajax/betslip/exclude/"+id);
		
		break;
		
		case 'remove':
			$("#slip").load(loc+"ajax/betslip/remove/"+id);
			$("#slipcount").html((parseInt($("#slipcount").text())-1) ? (parseInt($("#slipcount").text())-1) : '0');
			
			break;
			
		case 'clear':
			$("#slip").load(loc+"ajax/betslip/clear/", {}, function() {
				$("#slipcount").html('0');
				$('a#toggle_slip').trigger('click');
			});
			
			break;
		
		case 'viewall':
			$("#slip").load(loc+"ajax/betslip/viewall/", {}, function() {
				// $("#slipcount").html('0');
				// $('a#toggle_slip').trigger('click');
			});
			break;
			
		case 'viewtop':
			$("#slip").load(loc+"ajax/betslip/viewtop/", {}, function() {
				// $("#slipcount").html('0');
				// $('a#toggle_slip').trigger('click');
			});
			break;
			
		case 'load':
			jQuery.facebox(function() {
			  jQuery.get(id, function(data) {
			    jQuery.facebox(data, 'w550')
			  })
			});
			
			break;
			
		case 'login':
			jQuery.facebox(function() {
			  jQuery.get(id, function(data) {
			    jQuery.facebox(data)
			  })
			});

			break;
	}
	
	// return false to disable link
	return false;
};

function faqjump (to) {
	var loc = window.location.protocol + "//" + window.location.host + "/";
	if (window.location.host == "localhost") { loc += "betscout/html/" };
	
	location.href=loc + 'help#' + to;
}

function vidplayer (link) {
	jQuery.facebox(function() {
	  jQuery.get(link, function(data) {
	    jQuery.facebox(data)
	  })
	});
	
	return false;
}

function bsfacebox (link, my_class) {
	jQuery.facebox(function() {
	  jQuery.get(link, function(data) {
	    jQuery.facebox(data, my_class)
	  })
	});
	
	return false;
}