$(function(){
    
    /* Make touchscreen android and ios devices work correctly on touch events */
    var ua = navigator.userAgent.toLowerCase()
    
    if (ua.indexOf("android") > -1 || ua.indexOf("iphone") > -1) {
        $('#navigation ul li').bind("click touchstart",function(e){
            //alert(e.type);
            $(this).addClass('hover');
            $(this).siblings().removeClass('hover');
            if ($(this).hasClass('clicked')) {
                document.location($(this).children('a').attr('href'));
                return true;
            }
            if ($(this).children('ul').length > 0){
                //alert('has children');
                $(this).siblings().removeClass('clicked');
                $(this).addClass('clicked');

                return false;
            }
            return true;
        })
    }
    
    // update news
    $.ajax({
      url: '/uploads/news/update.php'
    });
    
    $('blockquote p').prepend('<span class="lquote">&ldquo;</span>');
    $('blockquote p').append('<span class="rquote">&rdquo;</span>');
    
    $('.content table h3').each(function(){
        $(this).parent('td').addClass('heading');
    });
    
    if ($('.imagespace img').length > 0) {
        $(window).load(function(){
            if ($('.imagespace img').height() > 100) {
                $('.imagespace img').height(100);
                $('.imagespace img').css('left',(215 - $('.imagespace img').width())/2);
            }
            $('.imagespace img').css('top',(109 - $('.imagespace img').height())/2);
        });
    }
    
    $('form.payment-form #submitForm, form.payment-form #cancelForm').parent('dt').css('float','left').css('clear','none').css('width','116px');
    $('form.payment-form #submitForm').css('margin-left','85px');
    $('form.payment-form #cancelForm').parent('dt').insertBefore($('form.payment-form #submitForm').parent('dt'));
    $('form.payment-form #cancelForm').val('Change Details');
    $('form.payment-form #submitForm, form.payment-form #cancelForm').parent('dt').removeClass('send_button');
    
    //$('div.payment-confirm').parent('.content').css('width','500px').css('margin','0 auto').css('float','none');
    
	jQuery.support.borderRadius = false;
	jQuery.each(['BorderRadius','MozBorderRadius','WebkitBorderRadius','OBorderRadius','KhtmlBorderRadius'], function() {
		if(document.body.style[this] !== undefined) {
			jQuery.support.borderRadius = true;
		}
	});
    
    $('#inductees table, .events table').each(function(){
        $(this).find('tr:even td').not('.heading').addClass('tiger');
    })
    
    $('.history h2').each(function(){
        $(this).append('<div class="pointer" />');
        $(this).addClass('expand');
    });
    
    $('.history h2.expand').live('click', function(){
        $('.history-content').not($(this).next('.history-content')).slideUp();
        $('h2.contract').removeClass('contract').addClass('expand');
        $(this).next('.history-content').slideDown();
        $(this).removeClass('expand');
        $(this).addClass('contract');
    })
    
    $('.history h2.contract').live('click', function(){
        $(this).next('.history-content').slideUp();
        $(this).removeClass('contract');
        $(this).addClass('expand');
    })
    
    $('.history table td').each(function(){
        $(this).children().not('h2').wrapAll('<div class="history-content" />');
    })
    $('.history-content').each(function(){
        $(this).slideUp();
    });
    
    $('#cancelForm').click(function(){
        history.back();
        return false;
    })
    
    $('#keywords').blur(function(){
        if ($(this).val() == "") {
            $(this).val("Search");
        }
    })
    $('#keywords').focus(function(){
        if ($(this).val() == "Search") {
            $(this).val("");
        }
    })
    
    
    /*
     * Pretty forms
     */
    $('form.prettyform input[type*=radio]').css('border','none');
    $('form.prettyform input[type*=checkbox]').css('border','none');
    if($.browser.mozilla) { prettyform(); }
    
    /*
     * LI hover fix for IE6
     * 
     */
    
    $('#navigation ul li').mouseover(function(){
        $(this).addClass('hover');
    })
    $('#navigation ul li').mouseleave(function(){
        $(this).removeClass('hover');
    })
    
    
    
    /*var config = {    
         over: function(){ $(this).addClass('hover'); }, // function = onMouseOver callback (REQUIRED)    
         timeout: 250, // number = milliseconds delay before onMouseOut    
         out: function(){ $(this).removeClass('hover'); } // function = onMouseOut callback (REQUIRED)    
    };
    $('#navigation ul li').each( function(){
        $(this).hoverIntent( config );
    });
	
	$('#navigation ul ul ul').each(function(){
		$(this).parent('li').addClass('submenu');
	});*/
    
    /*
     * Blur on focus
     */
    $('a').focus(function() { this.blur(); });
    $('input[type*=submit]').focus(function() { this.blur(); });
    $('input[type=text], input[type=password]').addClass("input");
    $('.required').append(" <span>*</span>"); 
    
    // Check for search box, use title as default if exists
    if( $("#searchText").length ){
    	$("#searchBox").emptyText($("#searchBox").attr('title'), "Defaulted");
    }
	
	// make whole block a link
	$('#page .sidebar .block').each(function(){
		$(this).wrapInner($(this).find('a').clone().empty());
		$(this).find('h1 a').replaceWith($(this).find('h1 a').contents()); // remove the original link
	});
	$('#page .sidebar .block img').unwrap(); // remove the extra <p> tag the CMS adds
	
	
	//console.log(sHome.count());
	var random = [], randomThree = [];
	
	for (var i in sHome) {
	    random.push(i);
	}
	for(var c=0;c < 3;c++){
	    var rand = Math.floor(Math.random() * random.length);
	    randomThree[c] = random[rand];
	    random.splice(rand,rand);
	}
	
	if ($('#home-slider').length > 0) {
    	$('#home-slider').crossSlide({
            fade: 0.5,
            variant: true
        }, 
            [sHome[randomThree[0]],
            sHome[randomThree[1]],
            sHome[randomThree[2]]]
          , function(idx, img, idxOut, imgOut) {
              if (idxOut == undefined)
              {
              }
              else
              {
                $('#home-slider-contents>div').eq(idx-1).animate({
                    opacity: 0
                }, 800, 'swing', function() {
                    // Animation complete.
                });
                $('#home-slider-contents>div').eq( ((idx)>=$('#home-slider img').length) ? 0 : (idx) ).delay(500).animate({
                    opacity: 1
                }, 800, 'swing', function() {
                    // Animation complete.
                });
              }
        });
    }
	
	
	/*
	$('#home-slider-wrap').mouseenter(function(){
	    $('#home-slider').cycle('pause');
	})
	$('#home-slider-wrap').mouseleave(function(){
        $('#home-slider').cycle('resume');
    })
    
	
	var firstSlide = true;
	
	$('#home-slider')
	.cycle({
		fx:     'fade', 
		speed:  'slow', 
		timeout: 5000,
		pause: 1,
		before:  onBefore, 
		after:   onAfter 
	});
	
	function onBefore(curr,next,opts) {
	    if (!firstSlide) {
    	    $('#home-slider-contents>div').eq(opts.currSlide).animate({
                opacity: 0
            }, 800, 'swing', function() {
                // Animation complete.
            });
            $('#home-slider-contents>div').eq( ((opts.currSlide+1)>=opts.slideCount) ? 0 : (opts.currSlide+1) ).delay(500).animate({
                opacity: 1
            }, 800, 'swing', function() {
                // Animation complete.
            });
        } else {
            firstSlide = false;
        }
    }
    function onAfter(curr,next,opts) {
    }
    */
	
	scraper('ul#listTicker1');	
	
	function scraper(ticker) {
		var interval;
		var first = 0;
		var speed = 700;
		var pause = 7000;
		
		$(ticker+' li').slice(2).hide();
		
		$(ticker).hover(
			function(){
				clearInterval(interval);
			},function(){
				interval = setInterval(removeFirst, pause);
			}
		);
		
		function removeFirst(){
			first = $(ticker+' li:first').html();
			$(ticker+' li').eq(1)
			.animate({'border':0}, speed)
			$(ticker+' li:first')
			.animate({opacity: 0,height:0,'padding-top':0,'padding-bottom':0,'border':0}, speed)
			.fadeOut('slow', function() {$(this).remove();});
			$(ticker+' li').eq(2)
			.animate({opacity: 1}, speed)
			.fadeIn('slow', function() {});
			addLast(first);
		}
		function addLast(first){
			last = ''+first+'';
			$(ticker).append("<li style='display:none'>"+last+"</li>")
		}
		interval = setInterval(removeFirst, pause);
	}
});


/*
 * Pretty forms
 */
function prettyform(){
  /*
   * Hide forms
   */
  $( 'form.prettyform' ).hide().end();
  /*
   * Label & list formatting
   */
  $( 'form.prettyform' ).find( 'label' ).not( '.noprettyform' ).each( function( i ){
    var labelContent = this.innerHTML;
    var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );
    var labelSpan = document.createElement( 'span' );
        labelSpan.style.display = 'block';
        labelSpan.style.width = labelWidth;
        labelSpan.innerHTML = labelContent;
    this.style.display = '-moz-inline-box';
    this.innerHTML = '';
    this.appendChild( labelSpan );
  } ).end();
  
  /*
   * Show forms
   */
  $( 'form.prettyform' ).show().end();
}
