/**
 * --------------------------------------------------------------------
 * jQuery-Plugin "pngFix"
 * Version: 1.2, 09.03.2009
 * by Andreas Eberhard, andreas.eberhard@gmail.com
 * --------------------------------------------------------------------
 */

(function($) {

jQuery.fn.pngFix = function(settings) {

	// Settings
	settings = jQuery.extend({
		blankgif: '/assets/images/interface/blank.gif'
	}, settings);

	var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
	var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);

	if (jQuery.browser.msie && (ie55 || ie6)) {

		//fix images with png-source
		jQuery(this).find("img[src$=.png]").each(function() {

			jQuery(this).attr('width',jQuery(this).width());
			jQuery(this).attr('height',jQuery(this).height());

			var prevStyle = '';
			var strNewHTML = '';
			var imgId = (jQuery(this).attr('id')) ? 'id="' + jQuery(this).attr('id') + '" ' : '';
			var imgClass = (jQuery(this).attr('class')) ? 'class="' + jQuery(this).attr('class') + '" ' : '';
			var imgTitle = (jQuery(this).attr('title')) ? 'title="' + jQuery(this).attr('title') + '" ' : '';
			var imgAlt = (jQuery(this).attr('alt')) ? 'alt="' + jQuery(this).attr('alt') + '" ' : '';
			var imgAlign = (jQuery(this).attr('align')) ? 'float:' + jQuery(this).attr('align') + ';' : '';
			var imgHand = (jQuery(this).parent().attr('href')) ? 'cursor:hand;' : '';
			if (this.style.border) {
				prevStyle += 'border:'+this.style.border+';';
				this.style.border = '';
			}
			if (this.style.padding) {
				prevStyle += 'padding:'+this.style.padding+';';
				this.style.padding = '';
			}
			if (this.style.margin) {
				prevStyle += 'margin:'+this.style.margin+';';
				this.style.margin = '';
			}
			var imgStyle = (this.style.cssText);

			strNewHTML += '<span '+imgId+imgClass+imgTitle+imgAlt;
			strNewHTML += 'style="position:relative;white-space:pre-line;display:inline-block;background:transparent;'+imgAlign+imgHand;
			strNewHTML += 'width:' + jQuery(this).width() + 'px;' + 'height:' + jQuery(this).height() + 'px;';
			strNewHTML += 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + jQuery(this).attr('src') + '\', sizingMethod=\'scale\');';
			strNewHTML += imgStyle+'"></span>';
			if (prevStyle != ''){
				strNewHTML = '<span style="position:relative;display:inline-block;'+prevStyle+imgHand+'width:' + jQuery(this).width() + 'px;' + 'height:' + jQuery(this).height() + 'px;'+'">' + strNewHTML + '</span>';
			}

			jQuery(this).hide();
			jQuery(this).after(strNewHTML);

		});

		// fix css background pngs
		jQuery(this).find("*").each(function(){
			var bgIMG = jQuery(this).css('background-image');
			if(bgIMG.indexOf(".png")!=-1){
				var iebg = bgIMG.split('url("')[1].split('")')[0];
				jQuery(this).css('background-image', 'none');
				jQuery(this).get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iebg + "',sizingMethod='scale')";
			}
		});
		
		//fix input with png-source
		jQuery(this).find("input[src$=.png]").each(function() {
			var bgIMG = jQuery(this).attr('src');
			jQuery(this).get(0).runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + bgIMG + '\', sizingMethod=\'scale\');';
   		jQuery(this).attr('src', settings.blankgif)
		});
	
	}
	
	return jQuery;

};

})(jQuery);


/* Function:    fancy_form_field
 * Description: Adds a listener to clear field value onfocus and 
 *              returns the values onblur if the field is empty.
 * Parameters:  field_id, value
 * Returns:     null
 */
function fancy_form_field (field_id,default_value) {
  $(field_id).focus(function(){
    if($(field_id).val() == default_value) {
      $(field_id).val('');
      $(field_id).css('color','#333')
    }
  }).blur(function(){
    if($(field_id).val() == '') {
      $(field_id).val(default_value);
      $(field_id).css('color','#bbb')
    }
  })
}


/* Function:    ajax_form
 * Description: binds a FreeForm Form to ajax submit and does all 
 *              work to update the user of an error or to submit.
 * Parameters:  form_id
 * Returns:     null
 */

function ajax_form (form_id) {
  var options = {
  }

  $(form_id).ajaxForm ({              //bind ajax submit to the form
    target:       "#form_response",       //response from submission
/*     beforeSubmit: quick_validate, */
    success:      function(response){     //callback function for success  
      if(response == "success"){
        $(form_id).fadeOut('fast');
        $('#form_response').html('Thank you for your comments/questions.  We will be in touch soon.').addClass('success').fadeIn();
        $(form_id).resetForm()      //clear the form
      }else{
        $('#form_response').addClass('error').html($('#form_response #content ul').html()).fadeIn();
      }
    }
  });
}


function quick_validate() {
  if($('#name').val() == 'Name') {$('#name').val('');}
  if($('#email').val() == 'Email') {$('#email').val('');}
  if($('#comment').val() == "Question? Comment? We've got an answer.") {$('#comment').val('');}
  alert('submitting...')
  return true
}



// We only want these styles applied when javascript is enabled
			$('div.navigation').css({'width' : '300px', 'float' : 'left'});
			$('div.content').css('display', 'block');

			// Initially set opacity on thumbs and add
			// additional styling for hover effect on thumbs
			var onMouseOutOpacity = 0.67;
			$('#thumbs ul.thumbs li').css('opacity', onMouseOutOpacity)
				.hover(
					function () {
						$(this).not('.selected').fadeTo('fast', 1.0);
					}, 
					function () {
						$(this).not('.selected').fadeTo('fast', onMouseOutOpacity);
					}
				);



//on page load functions
$(document).ready(function(){

  //make the contact form fields act all fancy
  fancy_form_field('#name', 'Name');
  fancy_form_field('#email', 'Email');
  fancy_form_field('#comment', "Question? Comment? We've got an answer.");
  
  //setup the form to submit via ajax
  ajax_form('#contact_us');
  

  //fix the footer menu
  $('#footer_col1 li:odd').addClass('last');

  //fix stupid IE6
  $(document).pngFix();
  
  //load photogallery
	// We only want these styles applied when javascript is enabled
	$('div.navigation').css({'width' : '300px', 'float' : 'left'});
	$('div.photogallery').css('display', 'block');

  $('.photogallery').galleriffic('#thumbs', {
    delay:                  2000,
		numThumbs:              12,
		preloadAhead:           10,
		enableTopPager:         true,
		enableBottomPager:      true,
		imageContainerSel:      '#slideshow',
		controlsContainerSel:   '#controls',
		captionContainerSel:    '#caption',
		loadingContainerSel:    '#loading',
		renderSSControls:       true,
		renderNavControls:      true,
		playLinkText:           'Start Slideshow',
		pauseLinkText:          'Pause Slideshow',
		prevLinkText:           '&laquo; Previous',
		nextLinkText:           'Next &raquo;',
		nextPageLinkText:       'Next &raquo;',
		prevPageLinkText:       '&laquo; Prev',
		enableHistory:          true,
		autoStart:              false,
		onChange:               function(prevIndex, nextIndex) {
			$('#thumbs ul.thumbs').children()
				.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
				.eq(nextIndex).fadeTo('fast', 1.0);
		},
		onTransitionOut:        function(callback) {
			$('#caption').fadeTo('fast', 0.0);
			$('#slideshow').fadeTo('fast', 0.0, callback);
		},
		onTransitionIn:         function() {
			$('#slideshow').fadeTo('fast', 1.0);
			$('#caption').fadeTo('fast', 1.0);
		},
		onPageTransitionOut:    function(callback) {
			$('#thumbs ul.thumbs').fadeTo('fast', 0.0, callback);
		},
		onPageTransitionIn:     function() {
			$('#thumbs ul.thumbs').fadeTo('fast', 1.0);
		}
  });
/*   $('#gallery').galleriffic('#thumbs', options) */
  
})