jQuery(document).ready(function(){
  

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

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

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

	});

});

jQuery(document).ready(function() {
 // hides the slickbox as soon as the DOM is ready
 // (a little sooner than page load)
  jQuery('#slip').hide();
  jQuery('#basichistory').hide();
  jQuery('#basicstats').hide();
  jQuery('#details').hide();
  
  bwidth = jQuery('body').css("width");
  
  marginWidth = bwidth.substring(0, 4) - 990;
  
  jQuery('#slip').css("left", bwidth.substring(0, 4) - (marginWidth/2) -248 );

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

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

jQuery(document).ready(function () {
  // hide all ULs inside LI.drawer except the first one
  jQuery('LI.drawer UL:not(:first)').hide(); 
  
  // apply the open class
  jQuery('LI.drawer UL:first').addClass('open');
  
  jQuery('H4.drawer-handle').click(function () {
    // hide the currently visible drawer contents
    jQuery('LI.drawer UL:visible').slideUp(200);
    
    // remove the open class from the currently open drawer
    jQuery('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:
    jQuery(this).next().slideDown(200);
    
    // set a class indicating on the H2 that the drawer is open
    jQuery(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}jQuery/,'')
                .replace(/\/jQuery/,'');
    }

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

// Toggle tr in table
jQuery(function() {
	jQuery('tr.parent')
		.css("cursor","pointer")
		//.attr("title","Click to expand/collapse")
		.click(function(){
			jQuery(this).siblings('.child-'+this.id).toggle();
		});
	//jQuery('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':
			jQuery.get(loc+"ajax/betslip/add_entry/"+id, function(text) {
				if (text.length > 0) {
					jQuery("#slip").html(text).fadeIn('slow');
					jQuery("#slipcount").html(parseInt(jQuery("#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':
			jQuery("#slip").load(loc+"ajax/betslip/exclude/"+id);
		
		break;
		
		case 'remove':
			jQuery("#slip").load(loc+"ajax/betslip/remove/"+id);
			jQuery("#slipcount").html((parseInt(jQuery("#slipcount").text())-1) ? (parseInt(jQuery("#slipcount").text())-1) : '0');
			
			break;
			
		case 'clear':
			jQuery("#slip").load(loc+"ajax/betslip/clear/", {}, function() {
				jQuery("#slipcount").html('0');
				jQuery('a#toggle_slip').trigger('click');
			});
			
			break;
		
		case 'viewall':
			jQuery("#slip").addClass("stick");
			jQuery("#slip").load(loc+"ajax/betslip/viewall/", {}, function() {
				// jQuery("#slipcount").html('0');
				// jQuery('a#toggle_slip').trigger('click');
			});
			break;
			
		case 'viewtop':
			jQuery("#slip").removeClass("stick"); 
			jQuery("#slip").load(loc+"ajax/betslip/viewtop/", {}, function() {
				// jQuery("#slipcount").html('0');
				// jQuery('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;
}
