// include sorttable.js for PCs 
/*
<script language="javascript1.1" type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js">
<script language="javascript1.1" type="text/javascript" src="js/menu-collapsed.js">
*/
/******************* FOR OLD SELECTION PAGE **************************/

// include yui/build/yahoo/yahoo-min.js
//		yui/build/event/event-min.js
/*
<script type="text/javascript" src="yui/build/yahoo/yahoo.js"></script><!-- Dependency source files -->
<script type="text/javascript" src="yui/build/event/event.js"></script><!-- Source file -->
<script src="yui/build/connection/connection-min.js"></script>
<script type="text/javascript" src="yui/build/dom/dom.js"></script><!-- Container source file -->
<script type="text/javascript" src="yui/build/yahoo-dom-event/yahoo-dom-event.js" ></script> 
<script type="text/javascript" src="yui/build/dragdrop/dragdrop-min.js" ></script> 
<script type="text/javascript" src="includes/DDPlayer.js" ></script>
*/

function setupCluetips() {
	$('.playerlink').cluetip( 
		{	
			activation: 'click',
			width: 430, 
			sticky: true, 
			arrows: true,
			cursor: 'pointer',
			attribute : 'href',
			cluetipClass : 'cdffl',
			mouseOutClose : true,
			closeText : '',
			showTitle: false
//			,
//			hoverIntent: {
//				sensitivity:  3,
//				interval:     250,
//				timeout:      750    
//			}
		} 
	);
}

function dropLetterA( string ) {
	$('#headline_content').hide().html(string).animate( {width: 'show'}, "slow" );
}
function dropLetter( string ) {
	// get the letter
	var first = string.substr(0,1);
//	if( string.length > 1 ) {
		string = string.substr(1);
//	}
	$('#headline_content').html( $('#headline_content').html() + first );
//	alert( string );
	if( string.length > 0 ) {
		string = string.replace( /\n/g, "" );
		string = string.replace( /"/g, "\\\"" );
//		alert( string );
		setTimeout( "dropLetter( \"" + string + "\")", 75 );
	} else {
		$('#red').addClass( 'blink' );
	}
}

function rotateHeadline( data ) {
	var currentCount = $('.headline').length;
	var lastId = 'headline' + currentCount;
	var newId = 'headline' + (currentCount + 1);
	var newHeadlineDiv = $('#' + lastId).clone().attr( 'id', newId );
	newHeadlineDiv.html( data );
	$('#' + lastId).after( newHeadlineDiv );
	var moveAmt = ( currentCount * -3 ) + 'em';
//	$('.headline').animate( {top: moveAmt}, 2000 );
	$('#' + lastId).animate( {top: moveAmt, opacity: 0}, 2000 );
	$('#' + newId).css('opacity','0').animate( {top: moveAmt, opacity: 1}, 2000 );
	
}

function displayHeadline() {
	$.get(
		'ajax_headline.php',
		{},
		function(data) { rotateHeadline(data); }
	);
}

function setupHeadlines() {
	displayHeadline();
	$('#headline_content').everyTime( 15000, function() {
		displayHeadline();
	}); 
}

$( function() {
	setupHeadlines();
	setupCluetips();

	$('button.playerlistname').each( function() {
			var playerid = this.getAttribute("playerid");
			this.setAttribute( "tipref", "main.php?do=playercard&player=" + playerid );
		}).cluetip( 
		{	
//			activation: 'click',
			width: 430, 
			sticky: true, 
			arrows: true,
			cursor: 'pointer',
			attribute : 'tipref',
			cluetipClass : 'cdffl',
			mouseOutClose : true,
			closeText : '',
			showTitle: false,
			hoverIntent: {
				sensitivity:  3,
				interval:     1000,
				timeout:      750    
			}
		}); 

	$('.playerrow').each( function() {
			this.removeAttribute( "onclick" );
			this.removeAttribute( "title" );
			var playerid = /^\d+/.exec( this.id )[0];
			this.setAttribute( "tipref", "main.php?do=playercard&player=" + playerid );
		}).cluetip( 
		{	
			activation: 'click',
			width: 430, 
			sticky: true, 
			arrows: true,
			cursor: 'pointer',
			attribute : 'tipref',
			cluetipClass : 'cdffl',
			mouseOutClose : true,
			closeText : '',
			showTitle: false
//			,
//			hoverIntent: {
//				sensitivity:  3,
//				interval:     250,
//				timeout:      750    
//			}
		}); 
	});

function setOpacity(o, opacity) {
		o.style.filter = "alpha(style=0,opacity=" + opacity + ")";	// IE
//		o.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + opacity + ')';
		o.style.KHTMLOpacity = opacity / 100;				// Konqueror
		o.style.MozOpacity = opacity / 100;					// Mozilla (old)
		o.style.opacity = opacity / 100;					// Mozilla (new)
}

function removeChildrenFromNode(node) {
   if(node == undefined || node === null) {
      return;
   }
	try {
		while (node.hasChildNodes()) {
		  node.removeChild(node.firstChild);
		}
	} catch( err ) {
		
	}
}

function statsRefresh( pickweek ) {
	var position;
	// we also need to look at the page to determine
	// whether to do week or season.  Then ...
	var week = document.getElementById("radioweek");
	var source="season";
	if( week.checked ) {
		source = "week";
	}
	var nodes = getElementsByClass( "depressed", document, "td" );
	for( var pos in nodes ) {
		// remove _button from the end of the id, and use as position
		position = nodes[pos].id;
		position = position.replace(/_button/,"");
	}
	synchronousXmlhttpPost( "ajax.php", "ajax=select&position=" + position + "&week=" + pickweek + "&source=" + source  );
	sortables_init();
	YAHOO.example.DDMapPlayers(position); 
}

function synchronousXmlhttpPost(strURL, strQueryString ) {
	document.body.style.cursor="wait";
	var div = "result";
	if( arguments.length > 2 ) {
		div = arguments[2];
	}
	var target =document.getElementById(div);
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, false );
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

//		removeChildrenFromNode(target);
//	while(target.hasChildNodes()){
//		target.removeChild(target.lastChild);
//	}

    self.xmlHttpReq.send( strQueryString );
//    var serverResponse = xmlHttpReq.responseText;
	var root = document.createElement("div");
    root.innerHTML = self.xmlHttpReq.responseText;
//    alert( "Appending content to target " + div );
//    alert( root.innerHTML );
	removeChildrenFromNode(target);
    target.appendChild( root );
	document.body.style.cursor="auto";
}

function xmlhttpPost(strURL, strQueryString ) {
//	alert( strURL );
//	alert( strQueryString );
//	alert( asynchronous );
	var div = "result";
//	alert( target.innerHTML );
	if( arguments.length > 2 ) {
		div = arguments[2];
	}
	var target =document.getElementById(div);
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true );
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
        	// create new DIV for content; then move content to target DIV?

			removeChildrenFromNode(target);

			var root = document.createElement("div");
            root.innerHTML = self.xmlHttpReq.responseText;
//            alert( "Appending content to target " + div );
 //           alert( root.innerHTML );
            target.appendChild( root );



//			document.getElementById(div).innerHTML = self.xmlHttpReq.responseText;
        }
    }

//		removeChildrenFromNode(target);
//	while(target.hasChildNodes()){
//		target.removeChild(target.lastChild);
//	}

    self.xmlHttpReq.send( strQueryString );
}

