$(function(){ 
 
 	var zIndexNumber = 10000;
	$('div').each(function() {
		$(this).css('zIndex', zIndexNumber);
		zIndexNumber -= 10;
	});
	
	$('#modalWindow').css('zIndex',20000);
    $('#quote-bg').css('zIndex',10);
	
	$('#coin-slider').coinslider({ width: 575, height: 340, spw: 7, sph: 5, delay: 8000, sDelay: 30, navigation: true, links: true, hoverPause: true  });
	 
 	$('#rssfeedbox').rssfeed('http://www.tennistip.com/slycer/feed', {
    	limit: 5, 
		content: false, 
		date: false, 
		header: false,
		titletag: 'b'
  	});

// show all lessons grid
	//$("#grid_lessons tr:nth-child(even)").addClass("even");
	
	$("#grid_lessons").tablesorter({ 
        sortList: [[1,0]],
		headers: { 4: { sorter: false}, 5: {sorter: false} }
        }); 

	var opts =
	  // horizontal Dock with images expanding downwards in the vertical axis...
		{ align: 'bottom'
	  	// set the maximum minor axis (vertical) image dimension to 48px
		, size: 30 
		, source: function(i){ return (this.alt) ? false : this.src.replace(/(jpg|gif)$/,'png'); }  //default
                   
	  
	};

 	$('#social_buttons').jqDock(opts);


	$("ul.sf-menu").supersubs({ 
            minWidth: 6,  maxWidth: 18, extraWidth:  0
          }).superfish();  
                         
                          
   
	$("A.external").click( function() {
        window.open( $(this).attr('href'), 'external_w' );
        return false;
   });
	
 
		 
   $('.pop').cluetip({width: '250px', activation: 'hover', showTitle: false, positionBy: 'auto'  });
	 
	$('div.instructor_details').hide();
   $('a.instr-toggle').click(function() {
        $(this).next('div.instructor_details') // the details we want
				.toggle(400);
					
    });
	
	$('div.location_details').hide();
   $('a.loc-toggle').click(function() {
        $(this).next('div.location_details') // the details we want
				.toggle(400);
					
    });
 
    $('#ccvhelper').hide();
    $('a.slick-toggle').click(function() {
        $('div#ccvhelper') // the details we want
                  .toggle(400);
						return (false);
						   
    });
	
	
	
	 
	
	
    // link scroller 
    $('a[href*=#]:not([href=#])').click(function() {
	if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
	&& location.hostname == this.hostname) {
	  var $target = $(this.hash);
	  $target = $target.length && $target
	  || $('[name=' + this.hash.slice(1) +']');
	  if ($target.length) {
		var targetOffset = $target.offset().top;
		$('html,body')
		.animate({scrollTop: targetOffset}, 1000);
	   return false;
	  }
	}
  });
  
  //thickbox replacement
    var closeModal = function(hash)
    {
        var $modalWindow = $(hash.w);

        //$('#jqmContent').attr('src', 'blank.html');
        $modalWindow.fadeOut('2000', function()
        {
            hash.o.remove();
            //refresh parent

            if (hash.refreshAfterClose === 'true')
            {

                window.location.href = document.location.href;
            }
        });
    };
    var openInFrame = function(hash)
    {
        var $trigger = $(hash.t);
        var $modalWindow = $(hash.w);
        var $modalContainer = $('iframe', $modalWindow);
        var myUrl = $trigger.attr('href');
        var myTitle = $trigger.attr('title');
        var newWidth = 0, newHeight = 0, newLeft = 0, newTop = 0;
        $modalContainer.html('').attr('src', myUrl);
        $('#jqmTitleText').text(myTitle);
        myUrl = (myUrl.lastIndexOf("#") > -1) ? myUrl.slice(0, myUrl.lastIndexOf("#")) : myUrl;
        var queryString = (myUrl.indexOf("?") > -1) ? myUrl.substr(myUrl.indexOf("?") + 1) : null;

        if (queryString != null && typeof queryString != 'undefined')
        {
            var queryVarsArray = queryString.split("&");
            for (var i = 0; i < queryVarsArray.length; i++)
            {
                if (unescape(queryVarsArray[i].split("=")[0]) == 'width')
                {
                    var newWidth = queryVarsArray[i].split("=")[1];
                }
                if (escape(unescape(queryVarsArray[i].split("=")[0])) == 'height')
                {
                    var newHeight = queryVarsArray[i].split("=")[1];
                }
                if (escape(unescape(queryVarsArray[i].split("=")[0])) == 'jqmRefresh')
                {
                    // if true, launches a "refresh parent window" order after the modal is closed.

                    hash.refreshAfterClose = queryVarsArray[i].split("=")[1]
                } else
                {

                    hash.refreshAfterClose = false;
                }
            }
            // let's run through all possible values: 90%, nothing or a value in pixel
            if (newHeight != 0)
            {
                if (newHeight.indexOf('%') > -1)
                {

                    newHeight = Math.floor(parseInt($(window).height()) * (parseInt(newHeight) / 100));

                }
                var newTop = Math.floor(parseInt($(window).height() - newHeight) / 2);
            }
            else
            {
                newHeight = $modalWindow.height();
            }
            if (newWidth != 0)
            {
                if (newWidth.indexOf('%') > -1)
                {
                    newWidth = Math.floor(parseInt($(window).width() / 100) * parseInt(newWidth));
                }
                var newLeft = Math.floor(parseInt($(window).width() / 2) - parseInt(newWidth) / 2);

            }
            else
            {
                newWidth = $modalWindow.width();
            }

            // do the animation so that the windows stays on center of screen despite resizing
            $modalWindow.css({
                width: newWidth,
                height: newHeight,
                opacity: 0
            }).jqmShow().animate({
                width: newWidth,
                height: newHeight,
                top: newTop,
                left: newLeft,
                marginLeft: 0,
                opacity: 1
            }, 'slow');
        }
        else
        {
            // don't do animations
            $modalWindow.jqmShow();
        }

    }

    $('#modalWindow').jqm({
        overlay: 70,
        modal: true,
        trigger: 'a.thickbox',
        target: '#jqmContent',
        onHide: closeModal,
        onShow: openInFrame
    });

 
});
 