// Jquery scripts

//Binds the referenced functions to be executed whenever the DOM is ready to be traversed and manipulated.
$(document).ready(function() {
 showHide();
 showHide_dd();
 menuTree();
 siteMapTree();
 showNews();
 showHide2();
 multiShowHide();
 stripeTable();
 tableSorter();
 showOrderRow();
 subCalc(); //To do price calculations in seed order form 
});
 
 
// Show/hide a div
function showHide() {
  $('div.showhide> div').hide();
  $('div.showhide> a').click(function() {
	$(this).next().slideToggle('slow');
  });
};

function showHide_dd() {
  $('dl.showdd dd').hide();
  $('dl.showdd dt').click(function() {
  $('dl.showdd dd').slideUp('fast');
 $('dl.showdd dt').removeClass('highlight');
 $(this).addClass('highlight');
  if ($(this).next().css('display') == 'none') {
	$(this).next().slideDown('slow');
	}
	else {
	$(this).next().slideUp('fast');
	$(this).removeClass('highlight');
	};
  });
};

 // Show/hide the next row (seed order) Work in progress 10-25-10
function showOrderRow() {
  $('tr.orderRow').addClass('row_hide');
  $('tr.orderRow td a.addRow').addClass('more_rows');
//  $('tr.orderRow td a.addRow').addClass('row'); //test
  $('tr.orderRow:first').removeClass('row_hide').addClass('row_show');
  $('tr.orderRow td#barcode input[value!=""]').parent().parent().removeClass('row_hide').addClass('row_show'); //make sure if data is present row is visible (works but need to change icon)
//  $('tr.orderRow td#barcode input[value!=""]').parent().parent('a.addRow').removeClass('row').addClass('more_rows'); //test
  $('tr.orderRow td a.addRow').click(function() {
 	 if($(this).parent().parent().next('tr.orderRow')) {
 	 $(this).removeClass('more_rows').addClass('row');
 	 $(this).parent().parent().next('tr').removeClass('row_hide').addClass('row_show');
 	 };
  });
};


// Used for expandable list tree on help page menues (uses jquery.treeview.js)
function menuTree() {
$("#navigationtree").treeview({
	animated: "fast",
	persist: "location",
	collapsed: true,
	//unique: true
	control: "#treecontrol"
});
};

// Used for expandable list tree at sitemap.php (uses jquery.treeview.js)
function siteMapTree() {
$("#globalcontroltree").treeview({
	animated: "fast",
	collapsed: true,
	persist: "cookie",
	control: "#treecontrol"
});
};


// Show a single news item on mouseover (based on boagworld.com)
function showNews() {
 $("#showdd dt").mouseover(
 function () {
 if ($(this).next().css("display") == "none") {
 $("#showdd dt").removeClass("highlight");
 $(this).addClass("highlight");
 $("#showdd dd").slideUp("slow");
 $(this).next().slideDown("slow");
 };
 });
  $("#showdd dt").click(
 function () {
  $(this).next().slideUp("slow");
  $(this).removeClass("highlight");
});
};

// Show one div and hide another (for FAQ and Tour) 
function showHide2() {
  $('div.showhide2> div').show(); //this is a change; formerly defaulted to 'hide' 8-12-10
  $('div.showhide2> a').click(function() {
    $('div.showhide2> div').slideUp('fast');
	$(this).next().slideDown('slow');
  });
  
  $('a.showAll').click(function(){
  $('div.showhide2> div').slideDown('fast');
  return false;
  });
  
   $('a.hideAll').click(function(){
  $('div.showhide2> div').slideUp('fast');
  return false;
  });
};

/*for multiple hidden divs per page (AcDs)*/
function multiShowHide(){
	$('div.hide:eq(0)> div').hide(); 
    $('div.hide2:eq(0)> div').hide(); 
    $('div.hide3:eq(0)> div').hide(); 
    $('div.hide4:eq(0)> div').hide(); 
    $('div.hide5:eq(0)> div').hide(); 
    $('div.hide6:eq(0)> div').hide(); 
	$(".btn-slide").click(function(){
	  $("#panel").slideToggle("fast");
	  $(this).toggleClass("active");
	});

	$(".btn-slide2").click(function(){
	  $("#panel2").slideToggle("fast");
	  $(this).toggleClass("active");
	});
	$(".btn-slide3").click(function(){
	  $("#panel3").slideToggle("fast");
	  $(this).toggleClass("active");
	});
	$(".btn-slide4").click(function(){
	  $("#panel4").slideToggle("fast");
	  $(this).toggleClass("active");
	});
	$(".btn-slide5").click(function(){
	  $("#panel5").slideToggle("fast");
	  $(this).toggleClass("active");
	});
	$(".btn-slide6").click(function(){
	  $("#panel6").slideToggle("fast");
	  $(this).toggleClass("active");
	});
};

/*for striping tables other than tablesort*/
function stripeTable() {
  $('table.striped tbody tr:odd').addClass('odd');
  $('table.striped tbody tr:even').addClass('even');
   $('table.striped tr').mouseover(
    function () {
 $('table.striped tr').removeClass('highlight');
 $(this).addClass('highlight');
 });
};

/*sortable table columns using jquery.tablesorter.js. Added highlight function from stripeTable*/
function tableSorter() {
$("#myTable").tablesorter( { widgets: ['zebra']} ); /*sortList: [[0,0], [1,0]],*/
$('#myTable tr').mouseover(
    function () {
 $('#myTable tr').removeClass('highlight');
 $(this).addClass('highlight');
 });
};

/*Calculate subtotal and other fields using jquery.calculation.js and jquery.field.js*/
function subCalc()	{
 // bind the recalc function to the quantity fields
 $("input[name^=quantity]").bind("keyup", recalc);
 
 //bind the recalc function to change in Academic/Industry pricing
 $("input[class=unitprice]").bind("click",recalc); 

 // run the calculation function now
 recalc();
};

function recalc(){
$("[input[class^=price]").calc(
			// the equation to use for the calculation
			"qty * price",
			// define the variables used in the equation, these can be a jQuery object
			{
				qty: $("input[name^=quantity]"),
				price: $("input[class^=unitprice]:checked").val()
			},
			// define the formatting callback, the results of the calculation are passed to this function
			function (s){
				// return the number as a dollar amount
				return s.toFixed(2);
			},
			// define the finish callback, this runs after the calculation has been complete
			function ($this){
				// sum the prices
				var sum = $this.sum();
				
				$("input[name=subtotal]").text(
					// round the results to 2 digits
					sum.toFixed(2)
				);
			}
		);
 //Calculate subtotal when price changes
 $("input[class^=price]").sum("keyup", "#subtotal");
 //Calculate Grand total when subtotal or phytosanitary_shipment or expedited_shipping changes 
 $("input[class=subtotal]").sum("change", "#grand_total");
};

// #### JQuery based function to support video launch in a dialog window; it passes movieid as cgi parameter

//NOTE: the scripts below should be synchronized with default_xgdb.js
$(function() {
        $('.video-button').css('cursor','pointer');
        $('.video-button').attr('src', '/images/qtvideo.png');
        $('.video-button').mouseover(function(){ this.src =  '/images/qtvideo_hover.png'; });
        $('.video-button').mouseout(function(){ this.src = '/images/qtvideo.png'; });
        $('.video-button').click(function(event) {
                $.get('/help/av/movie.php',
                        { 'movieid[]': [this.id, this.title] },
                        function(html) {
                                $('#video_dialog').html(html);
                        }
                );
                $('#video_dialog').dialog('open');
        });
        $('#video_dialog').dialog({bgiframe:true,autoOpen:false,height:560,width:622,modal:true});
});


//For FLASH video, hosted  on  vimeo
$(function() {
        $('.flvideo-button').css('cursor','pointer');
        $('.flvideo-button').attr('src', '/images/flvideo.png');
        $('.flvideo-button').mouseover(function(){ this.src =  '/images/flvideo_hover.png'; });
        $('.flvideo-button').mouseout(function(){ this.src = '/images/flvideo.png'; });
        $('.flvideo-button').click(function(event) {
                $.get('/help/av/flmovie.php',
                        { 'movieid[]': [this.id, this.title] },
                        function(html) {
                                $('#video_dialog').html(html);
                        }
                );
                $('#video_dialog').dialog('open');
        });
        $('#video_dialog').dialog({bgiframe:true,autoOpen:false,height:560,width:622,modal:true});
});

//For help images e.g. track color codes that appear as a dialog box. See css style a.image_dialog_link for anchor and hover styling.
$(function() {
        $('.image-button').css('cursor','pointer');
        $('.image-button').click(function(event) {
                $.get('/help/image.php',
                        { 'imageid[]': [this.id, this.title] },
                        function(html) {
                                $('#image_dialog').html(html);
                        }
                );
                $('#image_dialog').dialog('open');
        });
        $('#image_dialog').dialog({bgiframe:true,autoOpen:false,height:640,width:480,modal:true});
});

// #### JQuery based function to support a context-sensitive help system where context provides the filename and title for an include file. A similar system is in place for XGDB help (see /XGDB/javascripts/default_xgdb.js)

$(function() {
        $('.help-button').css('cursor','help');
        $('.help-button').attr('src', '/images/help-icon.png');
        $('.help-button').mouseover(function(){ this.src = '/images/help-icon_hover.png'; });
        $('.help-button').mouseout(function(){ this.src = '/images/help-icon.png'; });
        $('.help-button').click(function(event) {
                $.get(  '/help/plantgdb_help.php',
                        { 'context[]': [this.id, this.title] },
                        function(html) {
                                $('#help_dialog').html(html);
                        }
                );
                $('#help_dialog').dialog('open');
        });
        $('#help_dialog').dialog({bgiframe:true,autoOpen:false,height:700,width:600,modal:false});
});

