var undefined;

(function($) {

	/**
	 * helper variables and function
	 */
	$.ifixpng = function(customPixel) {
		$.ifixpng.pixel = customPixel;
	};
	
	$.ifixpng.getPixel = function() {
		return $.ifixpng.pixel || '/pixel.gif';
	};
	
	var hack = {
		ltie7  : $.browser.msie && $.browser.version < 7,
		filter : function(src) {
			return "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src='"+src+"')";
		}
	};
	
	$.fn.ifixpng = hack.ltie7 ? function() {
    	return this.each(function() {
			var $$ = $(this);
			// in case rewriting urls
			var base = $('base').attr('href');
			if (base) {
				// remove anything after the last '/'
				base = base.replace(/\/[^\/]+$/,'/');
			}
			if ($$.is('img') || $$.is('input')) { // hack image tags present in dom
				if ($$.attr('src')) {
					if ($$.attr('src').match(/.*\.png([?].*)?$/i)) { // make sure it is png image
						// use source tag value if set 
						var source = (base && $$.attr('src').search(/^(\/|http:)/i)) ? base + $$.attr('src') : $$.attr('src');
						// apply filter
						$$.css({filter:hack.filter(source), width:$$.width(), height:$$.height()})
						  .attr({src:$.ifixpng.getPixel()})
						  .positionFix();
					}
				}
			} else { // hack png css properties present inside css
				var image = $$.css('backgroundImage');
				if (image.match(/^url\(["']?(.*\.png([?].*)?)["']?\)$/i)) {
					image = RegExp.$1;
					image = (base && image.substring(0,1)!='/') ? base + image : image;
					$$.css({backgroundImage:'none', filter:hack.filter(image)})
					  .children().children().positionFix();
				}
			}
		});
	} : function() { return this; };
	
	$.fn.iunfixpng = hack.ltie7 ? function() {
    	return this.each(function() {
			var $$ = $(this);
			var src = $$.css('filter');
			if (src.match(/src=["']?(.*\.png([?].*)?)["']?/i)) { // get img source from filter
				src = RegExp.$1;
				if ($$.is('img') || $$.is('input')) {
					$$.attr({src:src}).css({filter:''});
				} else {
					$$.css({filter:'', background:'url('+src+')'});
				}
			}
		});
	} : function() { return this; };
	
	$.fn.positionFix = function() {
		return this.each(function() {
			var $$ = $(this);
			var position = $$.css('position');
			if (position != 'absolute' && position != 'relative') {
				$$.css({position:'relative'});
			}
		});
	};

})(jQuery);

$(function(){
    
    $('img[@src$=.png]').ifixpng();
    
    var inputVals = {
        keyword: 'search wakeside',
        s: 'search wakeside connection',
        emailAddress: 'your email address'
    };
    
    if ($.browser.msie) {
        $('#header-top').css('height', '55px');
        
        // Hack to fix side bar top spacing
        $(".widget_categories, .widget_pages").css({
            "margin-top": "-20px"
        });
    }
    
    if ($.browser.safari) {
        $('#header-top').css('height', '55px');
    }
    
    $('#feature-fade-inner').innerfade({
        speed: 2000,
        timeout: 3000,
        type: 'sequence',
        containerheight: '23px'
    });
    
    $("a[rel='external']").filter("[href$='.mov'], [href$='.m4v'], [href$='.mp4']").attr('rel', 'shadowbox');
    
    Shadowbox.init();
    
    $('a[@rel="external"]').attr('target', '_blank');
    $('a[@rel="popup"]').each(function(){
        $(this).click(function(){
            window.open($(this).attr('href'), $(this).text(), "width=600,height=490,left=100,top=100,scrollbars=no,menubar=no,resizable=no,location=no,toolbar=no");
            return false;
        });
    });
    
    $('#header form').submit(function(){
        var kInput = $('input[name="keyword"]');
        return checkKeyword(kInput.get(0), inputVals[kInput.attr('name')]);
    });
    
    $('input[type="text"]')
        .focus(function(){
            $(this).removeClass('alt');
            if ($(this).val() == inputVals[$(this).attr('name')]) {
                $(this).val('');
            }
        })
        .blur(function(){
            if ($(this).val() == '') {
                $(this).addClass('alt'); 
                $(this).val(inputVals[$(this).attr('name')]);
            }
        })
        // Set initial input values
        .each(function(){
            if (inputVals[$(this).attr('name')] != undefined && ($(this).val() == '' || $(this).val() == inputVals[$(this).attr('name')])) {
                $(this).val(inputVals[$(this).attr('name')]).addClass('alt');
            }
        });
    
});

function checkKeyword(keyword, defaultText) {
  var minKeywordLength = 0;
  var maxKeywordLength = 0;
  minKeywordLength += 3;
  maxKeywordLength += 30;
  var errMsg = '';
  //alert("minKeywordLength " + minKeywordLength);
  var searchTerms = '';
  searchTerms = keyword.value;
  //alert("searchTerms.length " + searchTerms.length);

	var invalidSearchTermMessage = 'Non PCI-Compliant character found.'; // probably want to change this message! 
	var invalidChars = /[<>!]/; // add all illegal characters here. 
	var matches_array = searchTerms.match(invalidChars); 
	
  var noSearchTerm = 'Please enter a keyword or item number';
  var shortSearchTerm = 'Your keyword or item number must be at least 3 characters long';

  if (searchTerms == defaultText) {
      alert(noSearchTerm);
      return false;
  }
  if (searchTerms == '') {
      alert(noSearchTerm);
      return false;
  } else if ( searchTerms.length < minKeywordLength ) {
      alert(shortSearchTerm);
      return false;
	} else if (matches_array != null) { 
		// show the user a dialog if '<' or '>' or '!' are found in the keyword. 
		alert(invalidSearchTermMessage); 
		return false; 
  } else {
    return true;
  }
}

