//*****Replace align attribute with class**************************************************//
function replaceAlign() {
  if (!document.getElementsByTagName('img')) return false;
  $('img[align="left"]').addClass('left').removeAttr('align');
  $('img[align="right"]').addClass('right').removeAttr('align');
  $('img[align="middle"]').addClass('middle').removeAttr('align');
}
//*****jQuery clear value from july 21st 2009 comment on http://www.joesak.com/2008/11/19/a-jquery-function-to-auto-fill-input-fields-and-clear-them-on-click*****//
function clearDefaultValue() {
  $(':input').focus(function() {
    if($(this).val() == $(this).attr('title')) {
      $(this).val('');
    }
  }).blur(function() {
    if($(this).val() == '') {
      $(this).val($(this).attr('title'));
    }
  });
  $('#btnNext').mousedown(function() {
	  $(':input').each(function (i) {
      if($(this).val() == $(this).attr('title')) {
        $(this).val('');
      }
    });
  });
}
function homePageRotation() {
  //*****jquery cycle from http://jquery.malsup.com/cycle/pager3.html**************************************************//
  $('#bannerImages').cycle({ 
    fx:               'fade', 
    speed:            1000, 
    timeout:          6000, 
    pager:            '#bannerNav',
    activePagerClass: 'selected',
    pagerAnchorBuilder: function(idx, slide) { 
      // return selector string for existing anchor 
      return '#bannerNav li:eq(' + idx + ') a';
    }
  });
  var size = $('ul#bannerNav li').length;
  $('ul#bannerNav').addClass('banners' + size);
  //*****jquery carousel from http://www.gmarwaha.com/jquery/jcarousellite/**************************************************//
  $('#calloutCarousel .callouts').jCarouselLite({
    btnNext:  '#calloutCarousel .next',
    btnPrev:  '#calloutCarousel .prev',
    visible:  4,
    circular: true
  });
}
//*****jquery carousel from http://www.gmarwaha.com/jquery/jcarousellite/**************************************************//
function jcarousel() {
  $('.carouselWrap .carousel').jCarouselLite({
    btnNext:  '.carouselWrap .next',
    btnPrev:  '.carouselWrap .prev',
    visible:  4,
    circular: true
  });
}
//*****prettyPhoto from *****//
function prettyPhoto() {
	$('a[rel^="prettyPhoto"]').prettyPhoto({
		animation_speed: 'fast', /* fast/slow/normal */
		slideshow: 5000, /* false OR interval time in ms */
		autoplay_slideshow: false, /* true/false */
		opacity: 0.80, /* Value between 0 and 1 */
		show_title: false, /* true/false */
		allow_resize: false, /* Resize the photos bigger than viewport. true/false */
		default_width: 1050,
		default_height: 525,
		counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
		theme: 'light_rounded', /* light_rounded / dark_rounded / light_square / dark_square / facebook */
		horizontal_padding: 5, /* The padding on each side of the picture */
		hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
		wmode: 'opaque', /* Set the flash wmode attribute */
		autoplay: true, /* Automatically start videos: True/False */
		modal: false, /* If set to true, only the close button will close the window */
		/* deeplinking: true, Allow prettyPhoto to update the url to enable deeplinking. */		
		overlay_gallery: true, /* If set to true, a gallery will overlay the fullscreen image on mouse over */
		keyboard_shortcuts: false, /* Set to false if you open forms inside prettyPhoto */
		changepicturecallback: function(){}, /* Called everytime an item is shown/changed */
		callback: function(){}, /* Called when prettyPhoto is closed */
		ie6_fallback: true,
		markup: '<div class="pp_pic_holder" style="width: 590px; height: 525px;"> \
				  <div class="ppt">&nbsp;</div> \
					<div class="pp_top"> \
						<div class="pp_left"></div> \
						<div class="pp_middle"></div> \
						<div class="pp_right"></div> \
					</div> \
					<div class="pp_content_container"> \
						<div class="pp_left"> \
						<div class="pp_right"> \
							<div class="pp_content"> \
							  <a class="pp_close" href="#">Close</a> \
								<div class="pp_loaderIcon"></div> \
								<div class="pp_fade"> \
									<a href="#" class="pp_expand" title="Expand the image">Expand</a> \
									<div class="pp_hoverContainer"> \
										<a class="pp_next" href="#">next</a> \
										<a class="pp_previous" href="#">previous</a> \
									</div> \
									<div id="pp_full_res"></div> \
									<div class="pp_details"> \
										<div class="pp_nav"> \
											<a href="#" class="pp_arrow_previous">Previous</a> \
											<p class="currentTextHolder">0/0</p> \
											<a href="#" class="pp_arrow_next">Next</a> \
										</div> \
										<p class="pp_description"></p> \
										{pp_social} \
									</div> \
								</div> \
							</div> \
						</div> \
						</div> \
					</div> \
					<div class="pp_bottom"> \
						<div class="pp_left"></div> \
						<div class="pp_middle"></div> \
						<div class="pp_right"></div> \
					</div> \
				</div> \
				<div class="pp_overlay"></div>',
		gallery_markup: '<div class="pp_gallery"> \
							<a href="#" class="pp_arrow_previous">Previous</a> \
							<div> \
								<ul> \
									{gallery} \
								</ul> \
							</div> \
							<a href="#" class="pp_arrow_next">Next</a> \
						</div>',
		image_markup: '<img id="fullResImage" src="{path}" />',
		flash_markup: '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="{width}" height="{height}"><param name="wmode" value="{wmode}" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="{path}" /><embed src="{path}" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="{width}" height="{height}" wmode="{wmode}"></embed></object>',
		quicktime_markup: '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" height="{height}" width="{width}"><param name="src" value="{path}"><param name="autoplay" value="{autoplay}"><param name="type" value="video/quicktime"><embed src="{path}" height="{height}" width="{width}" autoplay="{autoplay}" type="video/quicktime" pluginspage="http://www.apple.com/quicktime/download/"></embed></object>',
		iframe_markup: '<iframe src ="{path}" width="{width}" height="{height}" frameborder="no"></iframe>',
		inline_markup: '<div class="pp_inline">{content}</div>',
		custom_markup: '',
		social_tools: false, /* html or false to disable */
        deeplinking: false
	});
}
//**************************************************Toggle Question/Answer**********************************************************//
function toggle() {
  if (!$('ul.toggle').length > 0) return false;
	$('ul.toggle h3').addClass('inactive');
	$('ul.toggle div.toggle').hide();
  $('ul.toggle h3').click(function () {
		$(this).toggleClass('active');
		$(this).toggleClass('inactive');
    $($(this).next('div.toggle')).slideToggle();
  });
  $('.showHideAll').click(function(e) {
    e.preventDefault();
    if($(this).hasClass('hideAll')) {
      $(this).children('a').html($(this).children('a').html().replace('Hide all', 'Show all'));
		  $(this).next().find('h3').removeClass('active');
		  $(this).next().find('h3').addClass('inactive');
      $(this).next().find('div.toggle').show();
      $(this).next().find('div.toggle').slideUp();
      $(this).removeClass('hideAll');
    } else {
      $(this).children('a').html($(this).children('a').html().replace('Show all', 'Hide all'));
		  $(this).next().find('h3').addClass('active');
		  $(this).next().find('h3').removeClass('inactive');
      $(this).next().find('div.toggle').hide();
      $(this).next().find('div.toggle').slideDown();
      $(this).addClass('hideAll');
    }
  });
}
function cloneTableRow() {
  $('.addALine').click(function() {
    //the row to clone
    var $row = $(this).closest('tr').prev();
    //the cloned row
    var $clonedRow = $row.clone();
    //the amount of rows to append to end of the id and name
    var index = $(this).closest('tr').siblings().length + 1;
    $clonedRow.find('*').andSelf().filter('[id]').each( function(){
      id = this.id.slice(0,this.id.length - 1);
      this.id = id + index;
    });
    $clonedRow.find('*').andSelf().filter('[name]').each( function(){
      name = this.name.slice(0,this.name.length - 1);
      this.name = name + index;
    });
    $clonedRow.find('*').andSelf().filter('[value]').each( function(){
      this.value = '';
    });
    $clonedRow.find('span').remove();
    $clonedRow.find('input').removeAttr('class');
    $clonedRow.find('input').removeAttr('checked');
    $clonedRow.find('select').removeAttr('class');
    $clonedRow.insertAfter($row);
    $clonedRow.find('*').andSelf().filter('input').eq(0).focus();
    return false;
  });
}
//*****Load all functions**************************************************//
$(document).ready(function(){
  replaceAlign();
  clearDefaultValue();
  homePageRotation();
  jcarousel();
  prettyPhoto();
  toggle();
   cloneTableRow();
});
