var fadespeed = $.browser.mozilla ? 250 : 0;
var hash = 'update';

$(document).ready(function() {
	// NAV ROLLOVER
	$("#nav > li").hover(function() {
		$(this).find('img:first').attr('src',$(this).find('img').attr('src').replace(".png","_hover.png"));
	}, function() {
		$(this).find('img:first').attr('src',$(this).find('img').attr('src').replace("_hover",""));
	});
	// NAV SUBMENU
	$("#nav > li > ul").parent().hover(function() {
		$(this).find('ul:first').animate({height:'125px',padding:'0px 2px 2px 2px'},{queue:false, duration:300, easing: 'easeOutSine'});
	}, function() {
		$(this).find('ul:first').animate({height:'0px',padding:'0px 2px 0px 2px'},{queue:false, duration:300, easing: 'easeOutSine'});
	});
	// NAV SUBMENU ROLLOVER
	$("#nav > li > ul > li").hover(function() {
		$(this).css('background',"url('images/dark.png')");
	}, function() {
		$(this).css('background','transparent');
	});

	// NAV2 ROLLOVER
	$("#nav2 img").each(function() {
		$(this).parent().parent().css('background','url(\''+$(this).attr('src').replace('.png','_hover.png')+'\')');
	});
	$("#nav2 img").hover(function() {
		$(this).animate({opacity:'0'},{queue:false, duration:250});
	}, function() {
		$(this).animate({opacity:'1'},{queue:false, duration:250});
	});

	$(window).hashchange(function() { updateState(location.hash); });
	updateState(location.hash);

});

function updateState(nav) {
	if(nav!=hash) {
		hash=nav;
		var doc = hash.replace('#','');
		var tab;
		if(doc.indexOf('_')>-1) {
			tab=doc.split('_')[1];
			doc=doc.split('_')[0];
		}
		if(doc =='') doc = 'news';
		$('#loading').css('display','block').fadeIn(500,function(){
		$.post(doc+'.php', {refresh:'true'}, function(data) {
				$('#content').html(data);
				$('#panel1 table tr:odd td, #coaches tr:odd th').css('background','#1d1d1d');
				$('#panel1 table tr:even td, #coaches tr:even th').css('background','#000000');
				$('#panel1.gallery .chart a:odd').css('background','#1d1d1d');
				$('#panel1.gallery .chart a:even').css('background','#000000');
				/*$('#chart').serialScroll({
					target:'#charts',
					items:'li',
					axis:'x',
					navigation:'#chart_nav img',
					duration:250,
					force:true,
					stop:true,
					start:(parseInt(tab?tab:1)-1),
					onBefore:function( e, elem, $pane, $items, pos ){ e.preventDefault(); if(this.blur) this.blur(); },
					onAfter:function( elem ){}
				});*/
				$('#chart_nav img').click(function(){
					$('#chart_nav img').each(function(){
						$(this).attr('src',$(this).attr('src').replace('_hover',''));
					});
					$(this).attr('src',$(this).attr('src').replace('.png','_hover.png'));
					//alert('#'+$(this).attr('id').replace('tab','chart'));
					$('#charts').scrollTo('#'+$(this).attr('id').replace('tab','chart'), 250, {easing:'easeOutSine'})//parseInt($(this).attr('id').replace('tab',''))*545,0);
				});
				$('#chart #tab'+tab).click();
				// ROSTER SCROLLER
				/*$('#charts li.scrollable').each(function(){
					$(this).scrollTo(0);
					if($(this).find('tr').size()>13) {
						$('#'+$(this).attr('id').replace('_','_scroll_')).css('display','block');
					}
				});
				$('.scroll').toggle(function(){
					$(this).css('background-image','url(\'images/chart_uparrow.gif\')');
					$('#'+$(this).attr('id').replace('_scroll','')).scrollTo('100%', 1000, {easing:'easeOutSine'});
				},function(){
					$(this).css('background-image','url(\'images/chart_downarrow.gif\')');
					$('#'+$(this).attr('id').replace('_scroll','')).scrollTo(0, 1000, {easing:'easeOutSine'});
					});*/				$('#chartpane div.scrollable').jScrollPane();
				// COACHES SCROLLER
				$('#coaches tr').click(function(){
					$('#coaches').animate({opacity:'0'},{queue:true, duration:250}).scrollTo('#'+$(this).attr('title')).animate({opacity:'1'},{queue:true, duration:250});
					$('#coachnav').fadeIn();
				});
				$('#coaches').scrollTo(0);
				$('#coachlist').click(function(){
					$('#coaches').animate({opacity:'0'},{queue:true, duration:250}).scrollTo(0).animate({opacity:'1'},{queue:true, duration:250});
					$('#coachnav').fadeOut();
				});
				$('#coachprev').click(function(){
					if($('#coaches').scrollTop()>350) $('#coaches').scrollTo($('#coaches').scrollTop()-350, 500, {easing:'easeOutCirc'});
				});
				$('#coachnext').click(function(){
					if($('#coaches').scrollTop()<5600) $('#coaches').scrollTo($('#coaches').scrollTop()+350, 500, {easing:'easeOutCirc'});
				});
				// SUBMIT SIGNUP FORM
				/*$('#boosterForm').submit(function() {
					if(validate()) $.post($(this).attr('action'), $(this).serialize(),function(data){
						$('#boosterForm').fadeOut();
						$('#boosterSignup').fadeOut();
						$('#boosterThankyou').fadeIn();
					});
					return false;
				});*/
				$('a.flickr').click(function(){
					//return(false);
				});
				if(doc=='upcomingevents') if(tab) $('#upcomingevents').scrollTo('#'+tab);
				$('#loading').fadeOut(500);
				$('a').click(function(){ if($(this).attr('hash')) updateState($(this).attr('hash')); });
				$('.editable').dblclick(function(){
					$(this).parent('.jScrollPaneContainer').replaceWith($(this));
					var editablecss = $(this).getStyleObject();
					var editable = $('<form method="post" action="/save.php"><input type="hidden" name="hash" value="'+hash+'"/><textarea class="editing" name="'+$(this).attr('id')+'">'+rep($(this).html())+'</textarea></form>');
					editable.css({position:'relative',left:0,top:0,padding:0,margin:0,border:0,width:editablecss.width,height:editablecss.height,display:'block'});
					editable.find('textarea').css({width:editablecss.width,height:308});
					editable.dblclick(function(){editable.submit();});
					$(this).replaceWith(editable);
				});
				$('#coachescorner,#boosterclubnews,#formsandpdfs').jScrollPane();
			});
		});
	}
}

function rep(content) {
	content=content.replace(/<h2>/gi,'[');
	content=content.replace(/<\/h2>/gi,']\r\n');
	content=content.replace(/<p>/gi,'');
	content=content.replace(/<\/p>/gi,'');
	content=content.replace(/\t/gi,'');
	content=content.replace(/<br\/?>/gi,'\r\n');
	content=content.replace(/\r\n\r\n\r\n/gi,'\r\n\r\n');
	return(content);
}

$.fn.getStyleObject = function(){
    var dom = this.get(0);
    var style;
    var returns = {};
    if(window.getComputedStyle){
        var camelize = function(a,b){
            return b.toUpperCase();
        };
        style = window.getComputedStyle(dom, null);
        for(var i = 0, l = style.length; i < l; i++){
            var prop = style[i];
            var camel = prop.replace(/\-([a-z])/g, camelize);
            var val = style.getPropertyValue(prop);
            returns[camel] = val;
        };
        return returns;
    };
    if(style = dom.currentStyle){
        for(var prop in style){
            returns[prop] = style[prop];
        };
        return returns;
    };
    if(style = dom.style){
      for(var prop in style){
        if(typeof style[prop] != 'function'){
          returns[prop] = style[prop];
        };
      };
      return returns;
    };
    return returns;
}

$.fn.copyCSS = function(source){
    var dom = $(source).get(0);
    var style;
    var dest = {};
    if(window.getComputedStyle){
        var camelize = function(a,b){
            return b.toUpperCase();
        };
        style = window.getComputedStyle(dom, null);
        for(var i = 0, l = style.length; i < l; i++){
            var prop = style[i];
            var camel = prop.replace(/\-([a-z])/g, camelize);
            var val = style.getPropertyValue(prop);
            dest[camel] = val;
        };
        return this.css(dest);
    };
    if(style = dom.currentStyle){
        for(var prop in style){
            dest[prop] = style[prop];
        };
        return this.css(dest);
   };
   if(style = dom.style){
      for(var prop in style){
        if(typeof style[prop] != 'function'){
          dest[prop] = style[prop];
        };
      };
    };
    return this.css(dest);
};
