/* **************************************************
Copyright (c) 2008, B & H Foto & Electronics Corp. All rights reserved. http://wwww.bhphotovideo.com - Author: Andres Vidal 
************************************************** */

/**
 * jQuery-Plugin "Placeholder"
 * 
 * @version: 1.1.0, 01.19.2011
 * 
 * @author: Andres Vidal
 *          code@andresvidal.com
 *          http://www.andresvidal.com
 *
 * Instructions: Call $(selector).placeholder(options) on an input element with an attribute placeholder.
 * @example: $('input#search').placeholder(); // <input id="search" name="q" placeholder="Search in books">
 *
 * @param (string or false)   onFocus - Class to apply when input  is focused. Default is focus.
 * @param (string or false)   onBlur - Class to appy when input is blurred. Default is false.
 * @param (string or false)   onChange - Class to apply if input value has changed after blurred. Default is false.
 * @param (true or false)     placeholderSupport - By default, the plugin will check for placeholder attribute browser support.
 * 
 */
(function(a){a.fn.placeholder=function(d){var b=jQuery.extend({onFocus:"focused",onChange:false,onBlur:false,placeholderSupport:"placeholder"in document.createElement("input")},d),c=function(){return!(a.trim(a(this).val())==""||a(this).val()==a(this).attr("placeholder"))};this.each(function(){b.onBlur&&a(this).addClass(b.onBlur);!b.placeholderSupport&&!c.call(this)&&a(this).val(a(this).attr("placeholder"));b.onChange&&c.call(this)&&a(this).addClass(b.onChange);a(this).bind("focus",function(){!b.placeholderSupport&&a(this).val()==a(this).attr("placeholder")&&a(this).val("");b.onFocus&&a(this).addClass(b.onFocus);b.onBlur&&a(this).removeClass(b.onBlur)}).bind("blur",function(){!b.placeholderSupport&&!c.call(this)&&a(this).val(a(this).attr("placeholder"));b.onFocus&&a(this).removeClass(b.onFocus);b.onBlur&&a(this).addClass(b.onBlur);b.onChange&&c.call(this)?a(this).addClass(b.onChange):a(this).removeClass(b.onChange)})});return this}})(jQuery);



/**
*	Clear Fields
*	The 2 functions will check its parameters against the input field to (clear it and remove the class) or (restore and add the class)
*	USAGE: Usage: <input id="someID" class="default" type="text" value="email address" onclick="clickclear('someID', 'email address', 'default');" onblur="clickrecall('someID', 'email address', 'default');" />
*
*	@library none
*	@onload false
*/

// @variables: element, default text
// @optional: toggleclass = element class
function clickclear(el, defaulttext, class_name) {
	el = document.getElementById(el);
	if (el.value == defaulttext) {
		el.value = "";
	}
	if ( class_name && el.className.match(class_name) ){	
		newClass = el.className.replace(class_name, '');
		el.className = newClass;
	}
}

// @variables: element, default text
// @optional: class name
function clickrecall(el, defaulttext, class_name) {
	el = document.getElementById(el);
	if (el.value == "") {
		el.value = defaulttext;
		if (class_name && !el.className.match(class_name) ){
			el.className = el.className + ' ' + class_name;
		}
	}
}

/**
 *	Validate Search
 *	This function will trim the value of the DOM input value and return true or false(with alert). It should be returned onSubmit. It checks for several allowed conditions. 
 *	USAGE: 	Usage: <form action="" method="" onSubmit="return validSearch(DOM input, default, errmsg);"><input type="text" class="input searchStr" /> ... </form>
 *
 *	@domObject		DOM Object	The DOM input object to be validated
 *	@defaultString	String		This is typically the HTML placeholder text (needed when hacked for HTML5 non-compliant browsers)
 *	@errmsg			String		The alert error message text to display.
 *	@noAlert		Boolean		Flag to suppress alert error message. Default: null	
 */

function validSearch ( domObject, defaultString, errmsg, noAlert )
{	
	var el = ( domObject.length > 0 ) ? domObject[0] : domObject,
		searchString = el.value = el.value.replace(/^\s+|\s+$|^\++/g, ""); //replace value with trimmed value (hack);
	
	if( searchString == null || searchString.length < 2 || defaultString == searchString || searchString.search(/(?:[\W_]|^).?\*.?(?:[\W_]|$)/) > -1 )
	{
		if ( noAlert == null )
		{
			alert(errmsg);
		}
		return false;
	}
	return true;
};

/* Popup Window Widget with Profile Support.
 * This widget extends the default functionality of window.open() with the use of powerful profiles.
 *
 * @version: 1.0.0, 08.04.2010 
 * @author: Andres Vidal
 *          code@andresvidal.com
 *          http://www.andresvidal.com/labs
 *
 * @arg	   url(mixed)       The popup url or object containing an href. Example: Use object [this] or string 'http://www.google.com'
 * @arg	   name(string)     The popup name or profile name (prefixed with "profiles.") Example: 'profiles.video'
 * @arg	   features(string) The features as a comma separated key=value list
 * @arg    debug(bool)      Set to "true" to disable opening the popup thru "window.open()" while testing
 * @return false
 *
 * @example	<a onclick="return widgets.popup(this, 'profiles.video', 'width=100px,height=100px,tango=120px');" href="http://www.video.bhphotovideo.com">B&H Videos</a>
 */

(function(){ 
	// popup profiles are in JSON: url, name, features
	var b={};
	b.basic={url:"",name:"popup",features:"width=800,height=600,resizable=yes,titlebar=no,toolbar=no,status=no,menubar=no,scrollbars=yes,location=no,directories=no,fullscreen=no"};
	b.videos={name:"videos",features:"width=980,height=740,resizable=no,titlebar=no,toolbar=no,status=no,menubar=no,scrollbars=yes,location=no,directories=no,fullscreen=no"};
	b.insights={name:"insights",features:"width=990,height=815,resizable=no,titlebar=no,toolbar=yes,status=yes,menubar=yes,scrollbars=yes,location=yes,directories=no,fullscreen=no"};
	b.BML={name:"bml",features:"width=985,height=560,resizable=yes,titlebar=no,toolbar=no,status=no,menubar=no,scrollbars=yes,location=no,directories=no,fullscreen=no"};
	
	window.widgets||(window.widgets={});window.widgets.popup||(window.widgets.popup={});widgets.popup=function(c,d,a,e){if(typeof c=="object")c=c.href;if(typeof d=="string"&&(profile=b[d.replace(/^profile(s)?\./i,"")])){d=profile.name;if(a){for(a=a+","+profile.features;(temp=a.replace(/\b([a-z]+)(=.*)\b\1=[^,]*,?/,"$1$2"))!=a;)a=temp;a=a}else a=profile.features;a=a}if(!c)c=b.basic.url;if(!d)d=b.basic.name;if(!a)a=b.basic.features;e||window.open(c,d.replace(/\s|\./,"_"),a);return false}}
)();

$(document).ready(function(){
	$("input.checkoutPref").live("change", function(){
		DWRHelper.addCheckoutPreference($(this).attr("name"), $('input:radio[name=' + $(this).attr("name") + ']:checked').val());
	});
});
