/* **************************************************
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(a,c,b){a=document.getElementById(a);if(a.value==c)a.value="";if(b&&a.className.match(b))newClass=a.className.replace(b,""),a.className=newClass};

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

/**
 *	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(a,b,c,d){var e;a=a.length>0?a[0]:a;e=a.value=a.value.replace(/^\s+|\s+$|^\++/g,""),a=e;if(a==null||a.length<2||b==a||a.search(/(?:[\W_]|^).?\*.?(?:[\W_]|$)/)>-1)return d==null&&alert(c),!1;return!0};

/* 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=820,resizable=yes,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=yes,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"};
	b.inDepth={name:"inDepth",features:"width=990,height=815,resizable=yes,titlebar=no,toolbar=yes,status=yes,menubar=yes,scrollbars=yes,location=yes,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}}
)();

/**
 * Timestamp Widget
 *
 * Usage:
 * alert( widgets.timeStamp() );
 */
(function(){
	window.widgets||(window.widgets={}); window.widgets.timeStamp = function(){ return new Date().getTime();}
})();

/* Copyright (c) 2009 Brandon Aaron (http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
 * Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
 *
 * Version: 3.0.2
 * 
 * Requires: 1.2.2+
 */
(function(c){var a=["DOMMouseScroll","mousewheel"];c.event.special.mousewheel={setup:function(){if(this.addEventListener){for(var d=a.length;d;){this.addEventListener(a[--d],b,false)}}else{this.onmousewheel=b}},teardown:function(){if(this.removeEventListener){for(var d=a.length;d;){this.removeEventListener(a[--d],b,false)}}else{this.onmousewheel=null}}};c.fn.extend({mousewheel:function(d){return d?this.bind("mousewheel",d):this.trigger("mousewheel")},unmousewheel:function(d){return this.unbind("mousewheel",d)}});function b(f){var d=[].slice.call(arguments,1),g=0,e=true;f=c.event.fix(f||window.event);f.type="mousewheel";if(f.wheelDelta){g=f.wheelDelta/120}if(f.detail){g=-f.detail/3}d.unshift(f,g);return c.event.handle.apply(this,d)}})(jQuery);
/**
 * jQuery Cookie plugin
 *
 * Copyright (c) 2010 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 * http://plugins.jquery.com/project/Cookie
 */
jQuery.cookie=function(key,value,options){if(arguments.length>1&&String(value)!=="[object Object]"){options=jQuery.extend({},options);if(value===null||value===undefined){options.expires=-1}if(typeof options.expires==='number'){var days=options.expires,t=options.expires=new Date();t.setDate(t.getDate()+days)}value=String(value);return(document.cookie=[encodeURIComponent(key),'=',options.raw?value:encodeURIComponent(value),options.expires?'; expires='+options.expires.toUTCString():'',options.path?'; path='+options.path:'',options.domain?'; domain='+options.domain:'',options.secure?'; secure':''].join(''))}options=value||{};var result,decode=options.raw?function(s){return s}:decodeURIComponent;return(result=new RegExp('(?:^|; )'+encodeURIComponent(key)+'=([^;]*)').exec(document.cookie))?decode(result[1]):null};

/**
 * Onload Bindings
 */
$(document).ready(function(){

	$("input.checkoutPref").live("change", function(){
		DWRHelper.addCheckoutPreference($(this).attr("name"), $('input:radio[name=' + $(this).attr("name") + ']:checked').val());
	});
	if(!!$.validator && !$.validator.methods.BHEmail){
		$.validator.addMethod("BHEmail", function(value) { 
			return /.+@{1}.+\.{1}.+/.test(value);
		});
	}
	//can we haz js?
	var $hazScript = $("#hazScript"); $hazScript.html("<img src='" + $hazScript.attr("href") + "' />");
	return;
});
// cookie function moved form utility.js
function getCookie(c_name)
	{
		if (document.cookie.length>0)
		{
			c_start = document.cookie.indexOf(c_name + "=");
			if( c_start != -1 )
			{ 
				c_start = c_start + c_name.length + 1; 
				c_end = document.cookie.indexOf( ";", c_start );
				if( c_end == -1 ) 
					c_end = document.cookie.length;
				return unescape( document.cookie.substring( c_start, c_end ) );
			} 
		}
		return "";
	}
	var cartCookie = getCookie('TopBarCart');
	var hasCartCookie = cartCookie != "null" && cartCookie != "";
	var cookieData = hasCartCookie ? cartCookie.split("|") : null;
//end cookie
// now the subWindow function moving from utility.js (please dont use this its only here so the old pages shouldn't break)
var newWindow;

function subWindow() { // some BML*.html //chart/*.html // js/newProductPage.js //some more look in xml
    if (arguments.length < 1) {
        alert("function subWindow called with " + arguments.length +
              " arguments, but it expects at least 1 arguments.");
        return null;
    }
    var heightval = arguments[1] != null ? arguments[1] : 480;
    var widthval = arguments[2] != null ? arguments[2] : 640;
    var filename = arguments[3] != null ? arguments[3] : "";
	var scrollbars = arguments[4] != null ? arguments[4] : 1;
	var LeftPosition = (screen.width) ? (screen.width-widthval)/2 : 0;
    var TopPosition = (screen.height) ? (screen.height-heightval)/2 : 0;
	
    var dimensions = "directory=0,height="+heightval+",width="+widthval+
                     ",left="+LeftPosition+",top="+TopPosition+",resizable=1,statusbar=0,hotkeys=0,menubar=0,scrollbars="+scrollbars+",status=0,toolbar=0";

    var newWindow = window.open(filename,arguments[0],dimensions);
	{newWindow.focus()}

    if (!filename) {
        newWindow.document.write("<title>B&H Photo-Video Pro Audio, The world's largest dealer of imaging equipment at discount prices</title>")
        newWindow.document.write("<center><font size=4 color='red'>Loading, please wait...</font></center>")
    }
    return;
}
//end subWindow

