$(document).ready(function(){

//define shadowbox

if ( typeof(Shadowbox) != 'undefined')
{
Shadowbox.init({
    handleOversize:     "resize",
    continuous:         true,
    handleUnsupported:  "remove",
    autoplayMovies:     false
});
}



//detect Safari browser

var safari = false;

var userAgent = navigator.userAgent.toString().toLowerCase();
if ((userAgent.indexOf('safari') != -1) && !(userAgent.indexOf('chrome') != -1)) 
{
 
 if ( userAgent.indexOf('mac') != -1 ) safari = true;
 
} 

  /* ------------------------------- */
// Equal Heights
/* ------------------------------- */
function equalHeight(group) 
{
    tallest = 0;
    group.each(function() 
    {
       thisHeight = $(this).height();
       if(thisHeight > tallest) 
       {
        tallest = thisHeight;
      }
    });
    
    $("#vsep").height( tallest );
    
    
}

equalHeight($(".col"));

//make vertical seperator same height as lcol

//$("#vsep").height( $("#lcol").height() );

$('.featurebox:last').css('margin','0px');

$('.fboxvsep:last').remove();

$('.item:last').css('background', '#FFFFFF');

/*-------------------------------------- */
/* Navigation
/*-------------------------------------- */

  
 //$("ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)
	$("ul#nav li a").hover(function() { //When trigger is clicked...
		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click
		$(this).parent().hover(function() {
		}, function(){
			$(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
		});
	});
	
/*	$("ul#nav li ul.subnav li a").hover(function() { //When trigger is clicked...
		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).parent().find("ul.subnav").slideDown('fast').show();
		$(this).parent().find("ul.secondnav").slideDown('fast').show(); //Drop down the subnav on click
      
		$(this).parent().hover(function() {
		}, function(){
			$(this).parent().find("ul.secondnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
	  });
  }); */
	
	
	//homepage video
	if (  $("#player").length )
	{
	 flowplayer("player", baseURL+"web/flowplayer/flowplayer-3.2.7.swf",{
 
 clip: {
		scaling: 'fit',
		autoBuffering: true,
		autoPlay: false,
    baseUrl: baseURL+'programvideos/'
  }  
 
 
 });
 }
 
 
 		//Video playlist
		// setup player
 
 if ( $('#memberplayer').length )
 {
     
	flowplayer("memberplayer", baseURL+"web/flowplayer/flowplayer-3.2.7.swf",{
	
	 	
		clip: {
		scaling: 'fit',
		autoBuffering: false,
		autoPlay: false,
    baseUrl: baseURL+'programvideos/',
    
    // when clip starts, this function is called
		onStart: function(clip) {
		
  		//disbable overlay for Safari
  		if ( safari == false )
  		{
  		// Appending the overlay div
      $('body').append('<div id="fadeOverlay" style="opacity:0.80;display:none;position:fixed;left:0;top:0;width:100%;height:100%;z-index:9999;background:#000;"></div>'),
      // Apply fadeIn animation for the smoothing effect.
      $('#fadeOverlay').css({'filter' : 'alpha(opacity=80)'}).fadeIn();
      
      }
				
		},
		
		onResume: function(clip) {
    
    if ( safari == false )
    {
    // Appending the overlay div
    $('body').append('<div id="fadeOverlay" style="opacity:0.80;display:none;position:fixed;left:0;top:0;width:100%;height:100%;z-index:9999;background:#000;"></div>');
    // Apply fadeIn animation for the smoothing effect.
    $('#fadeOverlay').css({'filter' : 'alpha(opacity=80)'}).fadeIn();
    
    }
    
    },
		
		onPause: function(clip) {
		
		if ( safari == false )
    {
    
    // Remove the overlay div when clicking on the fade in area.
    $('#fadeOverlay').fadeOut("slow", function() {$(this).remove();});
    
    }
    
    },
		
		 // when clip ends, this function is called
		onFinish: function(clip) {
		
		if ( safari == false )
    {
		// Remove the overlay div when clicking on the fade in area.
    $('#fadeOverlay').fadeOut("slow", function() {$(this).remove();});
    
    }
    }
    
    }
    
    
    

	// playlist plugin 
	}).playlist("#videoscroller");
	
	}
	
	
	// Remove the overlay div when clicking on the fade in area.
    $('body').click(function() {
    
    if ( safari == false )
    {
    
       $('#fadeOverlay').fadeOut("slow", function() {$(this).remove();});
       
    }   
    });
 
 //spinner code
 if (  $('.spinnerfast').length )
 {
 
 $('.spinnerfast').spinner({ min: 1, max: 1000, increment: 'fast' });
 
 }
 
 
 //date picker code
 //date picker past dates only
 
 if (  $('.date-pick-past').length )
 {

 $('.date-pick-past').datepicker(

		{

		

		  dateFormat: 'dd/mm/yy',

			changeMonth: true,

			changeYear: true,

		  minDate: '-99y',

		  maxDate: Date(),

		  yearRange: '1920:nn'

    

    }

	);
 }
 
 if (  $('.editableta').length )
 {
 
 $('.editableta').editable({
   type:'textarea',
   editClass: 'editabletextarea',
   submit: 'Save',
   cancel: 'Cancel',
   onEdit: beginta,
   onSubmit: end
 });
 
 }
 
 if (  $('.editable').length )
 {
 
 
 $('.editable').editable({
   type:'text',
   editClass: 'editabletext',
   submit: 'Save',
   cancel: 'Cancel',
   onEdit: begin
 });
 
 
 }
 
  function beginta( content )
 {
 
  
  var iptxt = $(".editabletextarea").val();
   
   if ( iptxt == "Click here to edit this") $(".editabletextarea").val('');
 
 
 }
 
 function begin( content )
 {
 
   var txt = this.parent().prev().text();
   
   switch( txt )
   {
     case "Water:":
     
     case "Alcohol:":
  
     this.append('<br />Please enter quantity in <strong>ml</strong>.');   
   
     break;
     
     case "Time Taken:":
     
     this.append('<br />Please enter time in <strong>hrs</strong>.');
     
     break;
   
   } 
   
   var iptxt = $(".editabletext").val();
   
   if ( iptxt == "Click to add text" || iptxt == "Click to add time" ) $(".editabletext").val('');
   
  

  
 
 
 }
 
  function end() 
  {
 
    var fid = $("#selected_food_diary_id").val();
    
    var txt = $("#evening_5_7").html();
    
        
    if ( txt != '' && txt != 'Click here to edit this' )
    {
    
   		 $.ajaxq ("ajaxqueue", 
         {
                 
        url: baseURL+'loginarea/?action=showbutton&fid='+fid,
        cache: false,
        success: function(html) 
        {
        
        // $("#send_food_diary").show();
         
        }
    });
   
   }       
          
  }
 
 
   if ( $('#foodfrm').length )
   {
   
       
     $("#save_food").click(function () { 
      var fdv = $('#food_diary_val').html();
      
      $('#selected_food_diary').val( fdv );
      
      $('#submitaction').val( 'save_food' );
      
      $('#foodfrm').submit();
      
       
    });
    
    
     $("#send_food_diary").click(function () { 
      var fdv = $('#food_diary_val').html();
      
      $('#selected_food_diary').val( fdv );
      
      $('#submitaction').val( 'send_food_diary' );
      
      $('#foodfrm').submit();
      
       
    });


   }
   
   
   if ( $('#frmbig').length )
   {
   
       
     $("#save_monitor_reading").click(function () { 
            
      $('#submitaction').val( 'save_monitor_reading' );
      
      $('#frmbig').submit();
      
       
    });
    
    
     $("#send_monitor_reading").click(function () { 
            
      $('#submitaction').val( 'send_monitor_reading' );
      
      $('#frmbig').submit();
      
       
    });


   }
   
    $("#blog_link").click(function() {
 $('#comments_box').slideDown('slow');
});

$("#blogfrm").submit(function() {
  var msg = "Please enter the following fields:\n\n";
  
  var err = false;
   
   if ($("#blog_comments").val() == '' )
   {
    err = true;
    
    msg += "Comments\n";
   }
   
    if (err)
    {
     alert( msg );
     return false;
    }
    
    return true; 
      
    });
    
    
    //display the reply to comments box when the link is clicked.
    $(".replyto").click(function() {
    
    //close all the open reply-to divs
        $(".replytodiv").each(function (i) {
         
         $(this).hide();
         
         });

    
    var id = $(this).attr("id");
    
 //show only the current active reply-to div   
 $('#replydiv_'+id).slideDown('slow');
 
});
 
});

function doRedirect( action )
 {
  
  if ( action == '') return false;
  
  document.location.href = baseURL + action;
 
 
 }
 
 //function to count words
function cnt(w) {

var y=w.value;
var r = 0;
a=y.replace(/\s/g,' ');
a=a.split(' ');
for (z=0; z<a.length; z++) {if (a[z].length > 0) r++;}
var x = document.getElementById('cnt');
var maxwords = document.blogForm.maxwords.value;
if (maxwords == '') maxwords = '0';
maxwords = parseInt(maxwords);

var val = maxwords - r;
if (val <= 0) val = 0;
document.blogForm.wordCnt.value = r;
x.innerHTML = val;

if (r > maxwords) {
w.value = document.blogForm.commentsText.value;
return false;
} else {
document.blogForm.commentsText.value = y;
}
}

//function to count words in the reply-to
function replytocnt(w, id) {

var y=w.value;
var r = 0;
a=y.replace(/\s/g,' ');
a=a.split(' ');
for (z=0; z<a.length; z++) {if (a[z].length > 0) r++;}
var x = document.getElementById('cnt_'+id);
var maxwords = document.getElementById('maxwords_'+id).value;
if (maxwords == '') maxwords = '0';
maxwords = parseInt(maxwords);

var val = maxwords - r;
if (val <= 0) val = 0;
document.getElementById('wordCnt_'+id).value = r;
x.innerHTML = val;

if (r > maxwords) {
w.value = document.getElementById('commentsText_'+id).value;
return false;
} else {
document.getElementById('commentsText_'+id).value;
}
}

//function to check the reply-to form
function checkReplyTo( frm )
{
var msg = "Please enter the following fields:\n\n";
  
  var err = false;
  
  var id = frm.parent_id.value;
   
   if ($("#replycomments_"+id).val() == '' )
   {
    err = true;
    
    msg += "Reply\n";
   }
   
    if (err)
    {
     alert( msg );
     return false;
    }
    
    return true; 
}

//function to enable shadowbox click
function doClick( id )
{

  $("#"+id).click();

}

