// JavaScript Document
var http_request = false;

var calint;
var boxscoreint;

var currentDay;

function boxscoreHandler() {
	
	
	$(".players tr").hover(
		function () {
			$(this).addClass('hover');
		}, 
		function () {
			$(this).removeClass('hover');
		}
	);
	
	$(".players tr").toggle(function() {
		if ($(this).has('td').length > 0) {
			$(this).addClass('selected');
			$(this).after('<tr class="popOut"><td colspan="21">Loading...</td></tr>');
			var temp = $(this).next('tr');
			$.ajax({
				url: "http://www.fantasypuck.com/admin/getNHLPlayer/"+$(this).attr('playerid'),
				cache: false,
				success: function(html){
					$(temp).html(html);
				}
			});
		}

	}, function() {
		if ($(this).has('td').length > 0) {
			$(this).next('tr').remove();
			$(this).removeClass('selected');
		}
	});
}

function calendarHandler() {
	$('#calendarContainer td').click(function() {
		if ($(this).children('a').length > 0) {
			$('#'+currentDay).removeClass('curDay');
			$(this).addClass('curDay');
			currentDay = $(this).attr('id');
		}
	});
}

$(document).ready(function() {
	boxscoreHandler();
	calendarHandler();
});

var baseurl = 'http://www.fantasypuck.com/';



function getXMLHTTP() { //fuction to return the xml http object
	var xmlhttp=false;	
	try{
		xmlhttp=new XMLHttpRequest();
	}
	catch(e)	{		
		try{			
			xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e){
			try{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e1){
				xmlhttp=false;
			}
		}
	}
		
	return xmlhttp;
}

function createXMLHttpRequestObject()
{
  // xmlHttp will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // try to instantiate the native XMLHttpRequest object
  try
  {
    // create an XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    try
    {
      xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
    }
    catch(e) { }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}



function getNHLGamesDate(date) {		
	
	//document.getElementById('nhlGamesRight').innerHTML = '<h5>Loading...</h5><img src="'+baseurl+'static/images/loading.gif" />';
	$('#nhlGamesRight').fadeOut();
	
	//call_me_recursively();
	clearTimeout(calint);
	
	var strURL=baseurl+'admin/getNHLGamesDate/'+date;
	
	var req = createXMLHttpRequestObject();
	
	if (req) {
		req.open("GET", strURL, true);
		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				// only if "OK"
				if (req.status == 200) {						
					document.getElementById('nhlGamesRight').innerHTML=req.responseText;
					$('#nhlGamesRight').fadeIn('fast');
					if ($('#refresh').attr('title') == 1) {
						calint = setTimeout(function(){getNHLGamesDate(date)},30000);	
					}
				} else {
					//alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		}			
		
		req.send(null);
	}
	//alert('test');
	//var newdate = date;
	//window.setTimeout(getNHLGamesDate(date),10000);
}

function getNHLBoxscore(year,gameid,status) {		
	
	//document.getElementById('leftColumn').innerHTML = '<h5>Loading...</h5><img src="'+baseurl+'static/images/loading.gif" />';
	
	//call_me_recursively();
	clearTimeout(boxscoreint);
	//if (status != 'F') {
		var strURL=baseurl+'admin/getBoxscore/'+year+'/'+gameid;
		
		var req = createXMLHttpRequestObject();
		
		if (req) {
			req.open("GET", strURL, true);
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {
											
						$('#leftColumn').html(req.responseText);
						boxscoreHandler();
						var status2 = $('#boxscoreSummary').attr('title');
						//alert(status2);
						if (status2.substr(0,1) != 'F') {
							//alert(status2);
							boxscoreint = setTimeout(function(){getNHLBoxscore(year,gameid,status2)},30000);
						} else {
							
							$('#connectionStatus').html('');
						}
					} else {
						//alert("There was a problem while using XMLHTTP:\n" + req.statusText);
					}
				}	else if (req.readyState == 2) {
					$('#connectionStatus').html('Retreiving Data <img src="'+baseurl+'static/images/down.png" />');
					//document.getElementById('connectionStatus').innerHTML = 'Retreiving Data <img src="'+baseurl+'static/images/down.png" />';
					//$(this).delay(4000);
				}	else if (req.readyState == 3) {
					$('#connectionStatus').html('Displaying Data <img src="'+baseurl+'static/images/up.png" />');
					//document.getElementById('connectionStatus').innerHTML = 'Displaying Data <img src="'+baseurl+'static/images/up.png" />';
				}
			}			
			
			req.send(null);
		}
	//}
		
	//alert('test');
	//var newdate = date;
	//window.setTimeout(getNHLGamesDate(date),10000);
}

function getNHLGamesDateLive(date) {
	calint = setTimeout(function() {getNHLGamesDate(date)},30000);
}

function getBoxscoreLive(year,gameid,status) {
	//alert(year+' - '+gameid);
	//document.getElementById('connectionStatus').innerHTML = 'Connected';
	//d
	//if (status != 'F') {
		boxscoreint = setTimeout(function() {
				getNHLBoxscore(year,gameid,status);
				$('#connectionStatus').html('System Online <img src="'+baseurl+'static/images/connected.png" />');
			},5000);
	//}
}

(function loopsiloop(){
   setTimeout(function(){
       $.ajax({
           url: 'foo.htm',
           success: function( response ){
               // do something with the response
 
               loopsiloop(); // recurse
           },
           error: function(){
               // do some error handling.  you
               // should probably adjust the timeout
               // here.
 
               loopsiloop(); // recurse, if you'd like.
           }
       });
   }, 30000);
})();


function getCalendar(date) {		
	
	//document.getElementById('calendarContainer').innerHTML = '<h5>Loading...</h5><img src="'+baseurl+'static/images/loading.gif" />';
	
	var strURL=baseurl+'admin/getCalendar/'+date;
	var req = getXMLHTTP();
	
	if (req) {
		req.open("GET", strURL, true);
		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				// only if "OK"
				if (req.status == 200) {						
					document.getElementById('calendarContainer').innerHTML=req.responseText;						
				} else {
					alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		}			
		
		req.send(null);
	}
}
 
function get(obj) {
	var poststr = "userfile=" + encodeURI( document.getElementById("userfile").value );
	makePOSTRequest(baseurl+'admin/do_upload', poststr);
}
 
function fileUpload() {
	window.open("http://jpgreenhouses.com/admin/uploadPicture/", "File Upload", "status = 1, height = 600, width = 800, scrollbars = 1" );
}

function fileUploadPlant() {
	window.open("http://jpgreenhouses.com/admin/uploadPlantPicture/", "File Upload", "status = 1, height = 600, width = 800, scrollbars = 1" );
}

function setPicture(name) {
	document.getElementById('picture').value = name;
	document.getElementById('placeHolder').style.background = 'url("http://jpgreenhouses.com/static/images/news/'+name+'")';
}

function setPlantPicture(name,thumbName) {
	document.getElementById('picture').value = name;
	document.getElementById('thumb').value = thumbName;
	document.getElementById('insertPlantPic').src = 'http://jpgreenhouses.com/static/images/plants/'+name;
}

function clearPicture(message) {
	alert(message);
	//document.getElementById('picture').value = 'bum';
	//document.getElementById('thumb').value = '';
	//document.getElementById('insertPlantPic').src = 'http://jpgreenhouses.com/static/images/plants/noImage.jpg';
}

function setContent(id,value)
{
	document.getElementById(id).innerHTML = value;
}

function showDesigner(value)
{
	if (value == 0) {
		document.getElementById('contDesigner').style.display = 'block';
	} else if (value == 1) {
		document.getElementById('contDesigner').style.display = 'none';
	}
}

function updateCart(rowid,qty) {		
	
	document.getElementById(rowid+'_update').innerHTML = '<img src="http://jpgreenhouses.com/static/images/loading.gif" />';
	
	var strURL="http://jpgreenhouses.com/co/updateCart/"+rowid+"/"+qty;
	var req = getXMLHTTP();
	
	if (req) {
		
		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				// only if "OK"
				if (req.status == 200) {
					if (req.responseText == 'false') {
						document.getElementById(rowid+'_update').innerHTML = 'Quantity must be greater than 0';
						//setTimeout(document.getElementById(rowid+'_update').innerHTML = '',3000);
					} else if (req.responseText == 'true') {
						location.reload(true);
					}						
				} else {
					alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		}			
		req.open("GET", strURL, true);
		req.send(null);
	}		
}
function clearInnerHtml(elementid) {
	document.getElementById(elementid).innerHTML = '';
}



function redirectUrl(elementId)
{
	window.location = "http://jpgreenhouses.com/co/fromSize/"+document.getElementById(elementId).options[document.getElementById(elementId).selectedIndex].value;
}

/*function showimage(selectId,imageId)
{
	document.getElementById(imageId).src = document.getElementById(selectId).options[getElementById(selectId).selectedIndex].value+'_thumb.jpg';
	
		
if (!document.images)
return
document.images.pictures.src=
document.mygallery.picture.options[document.mygallery.picture.selectedIndex].value
}*/
