// JavaScript Document

function templateSetup2(d, i) {
	var value = i.nameEvent;
	d.find('.nameEvent').text(value);
	var value = i.startDateEvent.format("dddd, mmmm d");	
	//end time
	var timespane = "";
	if( i.typeEvent == 'meeting' ){
		timespane = " at " + i.startDateEvent.format("h:MM TT") + " to " + i.endDateEvent.format("h:MM TT");
	}
	
	d.find('.dateEvent').text(value + timespane);
	return d;
}
function templateSetup(d, i,endDate) {
	var value = i.nameEvent;
	d.find('.nameEvent').text(value);
	var startDate = strDateTimeToDate(i.dateEvent,i.timeEvent);
	var value = startDate.format("dddd, mmmm d");	
	//end time
	var timespane = "";
	if( i.typeEvent == 'meeting' ){
		timespane = " at " + startDate.format("h:MM TT") + " to " + endDate.format("h:MM TT");
	}
	
	d.find('.dateEvent').text(value + timespane);
	return d;
}
function strDateTimeToDate(dateString,timeString){
			//2008-08-29
			var startIndex =0;
			var endIndex = dateString.indexOf("-");
			var yearStr = dateString.substring(startIndex,endIndex);
			var yearNumber = Number(yearStr);
			
			startIndex = endIndex+1;
			endIndex = dateString.indexOf("-",startIndex);
			var monthStr = dateString.substring(startIndex,endIndex);
			var monthNumber = Number(monthStr);
			monthNumber--;
			
			startIndex = endIndex+1;
			endIndex = dateString.length;
			var dayStr = dateString.substring(startIndex,endIndex);
			var dayNumber = Number(dayStr);
			//TIME
			
			startIndex =0;
			endIndex = timeString.indexOf(":");
			var hourStr = timeString.substring(startIndex,endIndex);
			
			startIndex = endIndex+1;
			endIndex = timeString.indexOf(":",startIndex);
			var minStr = timeString.substring(startIndex,endIndex);
			
			startIndex = endIndex+1;
			endIndex = timeString.length;
			var secStr = timeString.substring(startIndex,endIndex);
			
			var myDate = new Date(yearNumber,monthNumber,dayNumber,hourStr,minStr,secStr);
			return myDate;
			
}

function hijackLinks() {

	var title = $('#mainContent h1').html() || 'Hello!';
	$('#menudivphone h1').html(title);
	$('#mainContent h1').remove();
    $('#progress').remove();

    $('#container a').click(function(e){
		var url = e.target.href;
		if (url == undefined) {
			url = e.currentTarget.href;
			if (url == undefined) {
				return;
			}
		}
		
		if( url.match("pages/dads/golf/golfTournament.html") 
		    ){
			
		}else{
			if (url.match(/ahespta.com/) || url.match(/file:/) || url.match(/localhost/ ) ) {		
				e.preventDefault();
				loadPage(url);
			}
		}
    });//click
	
}

function loadPage(url) {
    scrollTo(0,0);
	toggleMenuHide();
		
	
    if (url == undefined) {
    } else {
		$('body').append('<div id="progress">Loading...</div>');

    	$('#mainContent').load(url + ' #mainContent', hijackLinks);
    }
}

$(document).ready(function(){
						   
	hijackLinks();						   
	
 	$('select').bind('change',function(){
		//alert( ' blur!');
		//$(this).blur()
		/* do something here */ 
		//Get the value of the selected item 
		var phComboValStr = $("#ComboBoxPhone").val();
		var phComboVal = parseInt(phComboValStr);		
		//alert( phComboVal + ' ComboBoxPhone value.');
		//$("#phCopy").removeAttr("id");
		$("#phExtraData").children().remove();
		switch(phComboVal)
		 {
		 case 1:
		   break;
		 case 2:
		 	$('#phExtraData').append( $("#board").clone(true).show() );
			//$("#board").clone(true).removeAttr("id") .attr("id", "phCopy" ).append("#phExtraData").show();
		   break;
		 case 3:
		 	//$('#phExtraData').append( $("#committee").clone(true).removeAttr("id").show() );
		 	$('#phExtraData').append( $("#committee").clone(true).show() );
		   break;
		 case 4:
		 	//$('#phExtraData').append( $("#quickLinks").clone(true).removeAttr("id").show() );
		 	$('#phExtraData').append( $("#quickLinks").clone(true).show() );
		   break;
		 case 5:
		 	$('#phExtraData').append( $("#coming").clone(true).show() );
		   break;
		 case 6:
		 	$('#phExtraData').append( $("#navigation").clone(true).show() );
		   break;
		 case 7:
			//alert( 'case 7');
		
			//$('#phExtraData').append("<div class='newbox'>I'm new box by prepend</div>");	   
		   break;
		 default:
		   //code to be executed if n is different from case 1 and 2
		 }
		
		});
						   
	
						   
	//alert($('.thrColFixHdr #sidebar1 #navigation a').length + ' elements!');

						  // .thrColFixHdr #sidebar1 #navigation a:hover
	//alert('Welcome to Anaheim Hills Elementary School PTA Web Site.');
	//$('#board tbody tr:even').css('background-color','#dddddd');
/*	
  $.getJSON('http://localhost/json/pta_events2011_12.json', 
    function(data) {
      alert('Fetched ' + data.length + ' items!');
    });
  
  $.getJSON('json/pta_events2011_12.json', function(data) {

var jqxhr =$.getJSON('http://localhost/json/celebs.json?callback=?' , function(data) {
  var items = [];

  $.each(data.allPTAEvents.PTAEvent, function() {
    items.push('<li >' + this._title + '</li>');
  });

  $('<ul/>', {
    'class': 'my-new-list',
    html: items.join('')
  }).appendTo('body');
})
.success(function() { alert("second success"); })
.error(function(jqXHR, textStatus, errorThrown) { 
				alert("error "  + textStatus);
        console.log("error " + textStatus); 
        console.log("incoming Text " + jqXHR.responseText); 
    }) 

.complete(function() { alert("complete"); });

// Set another completion function for the request above
jqxhr.complete(function(){ alert(" second complete"); });

  */
var curHost = "drive"; // localhost, live,  drive 
var url = "";
switch(curHost){
	case "drive":
		url = "json/pta_events2011_12.json";	
	break
	case "localhost":
		url = "http://localhost/json/pta_events2011_12.json";
	break;
	case "live":
		//url = "http://www.ahespta.com/json/pta_events2011_12.json";
		url = "http://www.ahespta.com/json/pta_events2011_12.json";
	break;
};

var jqxhr =$.getJSON(url , function(data) {
 var items = [];
	/*  **********************************  */
	var currentDate = new Date();
  $.each(data.allPTAEvents.PTAEvent, function() {
	//alert("data.allPTAEvents.PTAEvent " + this._title );
	var endDate = strDateTimeToDate(this._endDate,this._endTime);
	
	if( currentDate.getTime() < endDate.getTime() ){
		var startDate = strDateTimeToDate(this._startDate,this._startTime);
		var itemData = {
			nameEvent: this._title,
			startDateEvent: startDate,
			endDateEvent: endDate,
			typeEvent: this._type
			
			//address1: this.,
			//addressCity: this.,
			//addressState: this.,
			//addressZip: this.,
			//contact: this.,
			//emailEvent: this.,
			//details: this.
		};
		items.push(itemData);
	}//if 
  });//each
	
	items.sort(function(a, b)
	{
		// a and b will here be two objects from the array
		// thus a[1] and b[1] will equal the names
		
		// if they are equal, return 0 (no sorting)
		if (a.startDateEvent.getTime() == b.startDateEvent.getTime()) { return 0; }
		if (a.startDateEvent.getTime() > b.startDateEvent.getTime()){
			// if a should come after b, return 1
			return 1;
		}else{
			// if b should come after a, return -1
			return -1;
		}
	});//sort
//var len = items.length;
var len = 10;

for (x=0;x<len;x++){
		var newDiv = $('#comingEvents .template').clone().removeClass('template');
		templateSetup2(newDiv, items[x] )
			.appendTo('#comingEvents')
			.fadeIn();  
}
/*
  $.each(items, function() {
		var newDiv = $('#comingEvents .template').clone().removeClass('template');
		templateSetup2(newDiv, this )
			.appendTo('#comingEvents')
			.fadeIn();  
		
  });//each
  */
	/*  **********************************  
  $.each(data.allPTAEvents.PTAEvent, function() {
	//alert("data.allPTAEvents.PTAEvent " + this._title );
	
  	var newDiv = $('#comingEvents .template').clone().removeClass('template');
	var itemData = {
		nameEvent: this._title,
		dateEvent: this._startDate,
		timeEvent: this._startTime,
		dateEndEvent: this._endDate,
		timeEndEvent: this._endTime,
		typeEvent: this._type
		
		//address1: this.,
		//addressCity: this.,
		//addressState: this.,
		//addressZip: this.,
		//contact: this.,
		//emailEvent: this.,
		//details: this.
	};
  //templateSetup(newDiv, itemData);
	var startDate = strDateTimeToDate(itemData.dateEvent,itemData.timeEvent);
	var endDate = strDateTimeToDate(itemData.dateEndEvent,itemData.timeEndEvent);
	var currentDate = new Date();
	
	if( currentDate.getTime() < endDate.getTime() ){
		  templateSetup(newDiv, itemData, endDate )
			.appendTo('#comingEvents')
			.fadeIn();  
	}
											  
    //items.push('<li id="' +  '">' + this._title + '</li>');
  });//each
  
  */
/*
  $('<ul/>', {
    'class': 'my-new-list',
    html: items.join('')
  }).appendTo('body');
  */
})
//.success(function() { alert("second success"); })
.error(function(jqXHR, textStatus, errorThrown) { 
				alert("error "  + textStatus);
        console.log("error Status " + textStatus); 
        console.log("incoming Text " + jqXHR.responseText); 
    }) 

//.complete(function() { alert("complete"); })
;

// Set another completion function for the request above
//jqxhr.complete(function(){ alert(" second complete"); });

	
	$('.exinfo').css('display','none');
	$('#board tbody tr:even').addClass('zebra');
	//$('#committee tbody tr:even').css('background-color','#dddddd');
	$('#committee tbody tr:even').addClass('zebra');
	$('<input type="button" value="Show Committee Chairs" id="toggleButton">').insertBefore('#board');
	$('#committee').hide();
	//News
	$( '#bio > div' ).hide();
	// uncomment the next line if you'd like the first pane to be visible by default
	$('#bio > div:first').show();
	
	/*---------*/
	/* Add html */
/*	
	var strData = '    ';
	  $('<div>\
		<table   cellspacing="0" cellpadding="0">\
      <tr>\
        <th  scope="row">Date:</th>\
        <td> Tuesday at 6:30 to 7:30</td>\
      </tr>\
	  </table>\
		</div>')
    .attr('id', 'overlay')
    .css('opacity', 0.65)
    .hover(function(){
      $(this).addClass('active');
    }, function() {
      $(this).removeClass('active');
      setTimeout(function(){
        $('#overlay:not(.active)').slideUp(function(){
          $('a.cart-hover').removeClass('cart-hover');
        });
      }, 800);
    }).appendTo('#mainContent');
*/

	/*---------*/
	/* Live */
  $('.arrowR').live('click', function() {
      $(this).removeClass('arrowR');
      $(this).addClass('arrowD');
	  $(this).siblings(".exinfo").show();
	});
  
  $('.arrowD').live('click', function() {
      $(this).removeClass('arrowD');
      $(this).addClass('arrowR');
	  $(this).siblings(".exinfo").hide();
	});
	
	/*
  $('#overlayClose').live('click', function() {
    $('#overlay').removeClass('active');
	$('#overlay').slideUp('slow');
  });
  */
	/*---------*/
	/* Click */
   
 $('#overlayClose').click(function(e) {
    $('#overlay').removeClass('active');
	$('#overlay').slideUp('slow');
	});
 /* 
	$('.arrowR').click(function() {
      $(this).removeClass('arrowR');
      $(this).addClass('arrowD');
	});
	$('.arrowD').click(function() {
      $(this).removeClass('arrowD');
      $(this).addClass('arrowR');
	});
*/	
  $('#comingEvents a').click(function(e) {
	/*
	if(    $('#overlay:animated').length>0) { 
	//animating
		e.preventDefault();
	 	return; 
	} 
	*/
	if(    $('#overlay').is(':visible')) { 
		$('#overlay').removeClass('active');
		$('#overlay').slideUp('slow');
		e.preventDefault();
		return; 
	} 
	
    //var url = $(this).attr('href');
	//alert('The URL IS: ' + url + '');
	
	// var url = $(this).attr('href') + ' #mainContent';
	//$('#overlay').html('loading...').load(url);
	
    //$(this).addClass('cart-hover');
	var pdiv = $(this).parent();
	//$kids.first().css("color", "red");
	//item_title
	var $kids  =pdiv.children(".titleEvent");
	var value = $kids.first().text(); 
	$('#overlay').find('.item_title').text(value);
	
	//item_date
	$kids  =pdiv.children(".dateEvent");
	value = $kids.first().text(); 
	$('#overlay').find('.item_date').text(value);

	//exinfo
	var $exinfo  =pdiv.children(".exinfo");

	//item_address1
	$kids  =$exinfo.find(".address1");
	value = $kids.first().text(); 
	$('#overlay').find('.item_address1').text(value);
		
	//addressCity
	$kids  =$exinfo.find(".addressCity");
	value = $kids.first().text(); 
	$('#overlay').find('.item_addressCity').text(value);
	//addressState
	$kids  =$exinfo.find(".addressState");
	value = $kids.first().text(); 
	$('#overlay').find('.item_addressState').text(value);
	//addressZip
	$kids  =$exinfo.find(".addressZip");
	value = $kids.first().text(); 
	$('#overlay').find('.item_addressZip').text(value);
	//contact
	$kids  =$exinfo.find(".contact");
	value = $kids.first().text(); 
	$('#overlay').find('.item_contact').text(value);
	//email
	$kids  =$exinfo.find(".emailEvent");
	value = $kids.first().text(); 
	$('#overlay').find('.item_emailEvent').text(value);
	$('#overlay').find('.item_mailto').attr('href', 'mailto:' + value); 
	//details
	$kids  =$exinfo.find(".details");
	value = $kids.first().text(); 
	$('#overlay').find('.item_details').text(value);
	
	
	//alert( value + ' DATA!');

    $('#overlay:not(:animated)')
      .addClass('active')	 
      .slideDown();      
	
    //$('#biography').load(url);
    e.preventDefault();
  })

	$('#bio h3').click(function() {
		$( '#bio > div' ).hide();
		$(this).next().animate( 
			{'height':'toggle'}, 'slow', 'easeOutBounce'
		);
	});
	
	/*---------*/
	/*  hover  */
  $('#overlay').hover(function(){
    $(this).addClass('active');
  }, function(){
    $(this).removeClass('active');
      setTimeout(function(){
        $('#overlay:not(.active)').slideUp(function(){
        });
      }, 800);
	
  });

  $('#board tbody tr').hover(function(){
    $(this).addClass('zebraHover');
  }, function(){
    $(this).removeClass('zebraHover');
  });
  
  $('#committee tbody tr').hover(function(){
    $(this).addClass('zebraHover');
  }, function(){
    $(this).removeClass('zebraHover');
  });
  
  $('.thrColFixHdr #container #sidebar2 #coming #comingEvents div').hover(function(){
    $(this).addClass('zebraHoverDiv');
  }, function(){
    $(this).removeClass('zebraHoverDiv');
  });
	
	
  $('#toggleButton').click(function(){
    
    if($('#board').is(':visible')) {
      $(this).val('Show Board of Directors');
    	//$('#board').slideToggle('slow');
		/*
		$('#board').hide();
    	$('#committee').show();
		*/
		$('#board').fadeOut('slow', function(){
			$('#committee').fadeIn('slow');
		});

    } else {
      $(this).val('Show Committee Chairs');
	  /*
 		$('#board').show();
    	$('#committee').hide();
		*/
		$('#committee').fadeOut('slow', function(){
			$('#board').fadeIn('slow');
		});
		
   }//endif
  });
  
  
//Get the value of the selected item 
  var phComboVal = $("#ComboBoxPhone").val();
  //Get the text of the selected item
  testData =  $("#ComboBoxPhone option:selected").text();
  
  //Find out when the select value changed
  
   $("#ComboBoxPhone").change(function() { 

	});
	//Programmatically set the selected item.
	//$("#ComboBoxPhone").val(2);
	
	//Modify the list.
	//Clear the list:   $(“#ComboBoxPhone”).html(“”);
	//Add to the list: $(“<option value=’9’>Value 9</option>”).appendTo(“#ComboBox”);
	
});

  
