// JavaScript Document
var aValue;
var nI = 0;
var aURLArguments = location.search.substring(1).split('&');
for (nI = 0; nI < aURLArguments.length; nI++) {
	aValue = aURLArguments[nI].split('=');
	if (aValue[0].toLowerCase() == 'srccode') {
		document.cookie = 'srccode=' + escape(aValue[1]) + ';domain=.bhphotovideo.com;expires=';
   	}
   	if (aValue[0].toLowerCase() == 'cpncode') {
		document.cookie = 'cpncode=' + escape(aValue[1]) + ';domain=.bhphotovideo.com;expires=';
    }
}

function addAccForm()
{
	var currentAccessory, currentQuantity;

	if(document.addAccessoriesToCart != null && document.addAccessoriesToCart.elements != null)
	{
		for(var i = 0; i<document.addAccessoriesToCart.elements.length; i++)
		{
			if((document.addAccessoriesToCart.elements[i].name.indexOf('cb_') > -1))
			{
				if(document.addAccessoriesToCart.elements[i].checked)
				{
					currentAccessory = document.addAccessoriesToCart.elements[i].name;
					currentQuantity = "qty" + currentAccessory.substring( currentAccessory.indexOf('_') );

					getElementRefs('hiddenAcc').innerHTML +=
					   '<input type=hidden name=' + currentAccessory + ' value=selected>' +
					   '<input type=hidden name=' + currentQuantity + ' value=' + getElementRefs(currentQuantity).value + '>';
				}
			}
			else if((document.addAccessoriesToCart.elements[i].name.indexOf('addedTroughType') > -1)) {
				getElementRefs('hiddenAcc').innerHTML += '<input type=hidden name=addedFromType value=' + document.addAccessoriesToCart.elements[i].value + '>';
			}
			else if((document.addAccessoriesToCart.elements[i].name.indexOf('addedTroughValue') > -1)) {
				getElementRefs('hiddenAcc').innerHTML += '<input type=hidden name=addedFromValue value=' + document.addAccessoriesToCart.elements[i].value + '>';
			}
		}
	}
	
	if( getElementRefs('hiddenAcc').innerHTML != "" )
		getElementRefs('hiddenAcc').innerHTML += '<input type=hidden name="addAccessories"value=""/>';
}


var origWidth, origHeight;
if (document.layers) {
	origWidth = window.innerWidth; origHeight = window.innerHeight;
	window.onresize = function() { if (window.innerWidth != origWidth || window.innerHeight != origHeight) history.go(0); }
}

var cur_lyr;
function swapLayers(id) {
  if (cur_lyr) hideLayer(cur_lyr);
  showLayer(id);
  cur_lyr = id;
}

function showLayer(id) {
  var lyr = getElemRefs(id);
  if (lyr && lyr.css) lyr.css.display = "block";
}

function hideLayer(id) {
var lyr = getElemRefs(id);
  if (lyr && lyr.css) lyr.css.display = "none";
}


function getElemRefs(id) {
	var el = (document.getElementById)? document.getElementById(id): (document.all)? document.all[id]: (document.layers)? document.layers[id]: null;
	if (el) el.css = (el.style)? el.style: el;
	return el;
}

var origWidth, origHeight;
if (document.layers) {
	origWidth = window.innerWidth; origHeight = window.innerHeight;
	window.onresize = function() { if (window.innerWidth != origWidth || window.innerHeight != origHeight) history.go(0); }
}

var cur_lyrNote;
function popLayersNote(id) {
  if (cur_lyrNote) hideLayerNote(cur_lyrNote);
  showLayerNote(id);
  cur_lyrNote = id;
}

function showLayerNote(id) {
  var lyrNote = getElemRefs(id);
  if (lyrNote && lyrNote.css) lyrNote.css.visibility = "visible";
}

function hideLayerNote(id) {
  var lyrNote = getElemRefs(id);
  if (lyrNote && lyrNote.css) lyrNote.css.visibility = "hidden";
}

function getElemRefs(id) {
	var el = (document.getElementById)? document.getElementById(id): (document.all)? document.all[id]: (document.layers)? document.layers[id]: null;
	if (el) el.css = (el.style)? el.style: el;
	return el;
}

/* ------------------------------------------------------------- */


function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

var currentFeaturesObj = null;
var currentSpecObj = null;
var currentIncludesObj = null;

function expandClose(id,sku,itemSource, kitDetails, sync) 
{
		if(kitDetails == 'kitFeatures' && currentFeaturesObj!=null){ //something's open
			document.getElementById( currentFeaturesObj ).style.display = 'none';
			document.getElementById( "img_" + currentFeaturesObj ).src = '/images/M_refinePlus.gif'; 
			if( currentFeaturesObj == id ) {
				currentFeaturesObj = null;
				return;
			}
		}
		
		if(kitDetails == 'kitSpecs' && currentSpecObj!=null){ //something's open
			document.getElementById( currentSpecObj ).style.display = 'none';
			document.getElementById( "img_" + currentSpecObj ).src = '/images/M_refinePlus.gif'; 
			if( currentSpecObj == id ) {
				currentSpecObj = null;
				return;
			}
		}
		
		if(kitDetails == 'kitIncludes' && currentIncludesObj!=null){ //something's open
			document.getElementById( currentIncludesObj ).style.display = 'none';
			document.getElementById( "img_" + currentIncludesObj ).src = '/images/M_refinePlus.gif'; 
			if( currentIncludesObj == id ) {
				currentIncludesObj = null;
				return;
			}
		}
		
		if( kitDetails == 'kitFeatures' )
			currentFeaturesObj = id;
		else if( kitDetails == 'kitSpecs' )
			currentSpecObj = id;
		else if( kitDetails == 'kitIncludes' )
			currentIncludesObj = id;
		
		var objID = document.getElementById(id);
		var objImg = document.getElementById("img_" + id );

	   objID.style.display = 'block';  
	   objImg.src = '/images/M_refineMinus.gif';
	   
	   if(objID.innerHTML.length < 50)
	   {
		    useLoadingImage('preloader_' + id);
			dwr.util.setEscapeHtml(false);
			if(sync)
				dwr.engine.setAsync(false);
			
			if(objID.childNodes.length <= 3)/*3 being the number that firefox display for objID.childNodes.length*/{
				if( id.indexOf("feature") != -1 )
					 DWRHelper.getFeatures( sku, itemSource,function(value){ dwr.util.setValue(id, value+ '<a name=' + kitDetails + '> </a>');   } );
					 
				else if( id.indexOf("spec") != -1 )
					 DWRHelper.getSpecifications( sku, itemSource, function( value ){ dwr.util.setValue(id, value+ '<a name=' + kitDetails + '> </a>');   } );
							
				else if( id.indexOf("include") != -1 )
					 DWRHelper.getIncludes( sku, itemSource, function( value ){ dwr.util.setValue(id, value+ '<a name=' + kitDetails + '> </a>');   } );
			}
		}
}
function useLoadingImage(placeHolder) {
  dwr.engine.setPreHook(function() {
      document.getElementById(placeHolder).style.display = 'block';
  });
  dwr.engine.setPostHook(function() {
     document.getElementById(placeHolder).style.display = 'none';
  });
}

function ShowLayer(id, action){
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = action;
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = action;
		}
		else { // IE 4
			document.all.id.style.display = action;
		}
	}
}

function showCountries(){
	ShowLayer('cooMore', 'inline');
	ShowLayer('cMore', 'none');
}

// configure add this
var addthis_config = {
          services_compact: 'facebook,favorites,twitter,live,myspace,stumbleupon,google,blogger,delicious,more',
          services_exclude: 'amazonwishlist,email,aolmail,gmail,hotmail,yahoomail,printfriendly,print'
}


$(document).ready(function(){
	$("a.itemIncludes").click(function (event) {
							$(".ajaxLayerCloseLink").click();
							 $("div.itemContainer").hide();
							 $(this).next().fadeIn("slow");
							 $(".current").hide('slow');
							 $("div.icFeatures").slideDown("slow").addClass('current');
							 $("a").removeClass('boldLink');
							 $("a.icFeaturesLink").addClass('boldLink');
	});
	$("a.closeIt").click(function (event) {
							 $("div.itemContainer").fadeOut("slow");
	});
	$("a.icFeaturesLink").click(function () {
			$(".current").hide('slow');
			$("div.icFeatures").slideDown("slow").addClass('current');
			$("a").removeClass('boldLink');
			$("a.icFeaturesLink").addClass('boldLink');
	});
	$("a.icSpecificationsLink").click(function () {
			$(".current").hide('slow');
			$("div.icSpecifications").slideDown("slow").addClass('current');
			$("a").removeClass('boldLink');
			$("a.icSpecificationsLink").addClass('boldLink');
	});
	$("a.icIncludesLink").click(function () {
			$(".current").hide('slow');
			$("div.icIncludes").slideDown("slow").addClass('current');
			$("a").removeClass('boldLink');
			$("a.icIncludesLink").addClass('boldLink');
	});
	
	if($.browser.msie){
		$(".atwLayerLoader, .atcLayerLoader, .nmLayerLoader").live("load", function(){
			layerTop = $(this).offset().top;
			layerBottom = layerTop + $(this).find(".ajaxLayer").height() + 45;
			$(".itemIncContainer , .itemLitContainer").each(function(){
				if($(this).offset().top < layerBottom && $(this).offset().top > layerTop){
					$(this).addClass("negativeZ");
				}else{
					$(this).removeClass("negativeZ");
				}
			});
			$(".alParent").each(function(){
				if($(this).offset().top < layerBottom && $(this).offset().top > layerTop){
					$(this).css("position", "static").addClass("staticDude");
				}
			});
		  	$(this).find(".ajaxLayerCloseLink").one("click", function(){
				 $(".itemIncContainer , .itemLitContainer").removeClass("negativeZ");
				 $(".staticDude").css("position", "relative").removeClass("staticDude");
			});
			$(".closeIt").click();
		});
	}
	
});

//power reviews
function goToReview () {
	$("#tab_read_reviews").click();
	window.location.hash = "#reviews";
}
function showCountries(){
	ShowLayer('cooMore', 'inline');
	ShowLayer('cMore', 'none');
}
$(document).ready(function(){
	
	$(".openInPopup").click(function(){
		//target=$(this).attr("target");
		subWindowNoScrollBars($(this).attr("target"));	
	});		
	var popupNumber = 1;
	$(".openInPopup:not([target])").each(function(){
		$(this).attr("target", function(){
							return "popUpNumber" + popupNumber++;
						});
	});
	
	// force tab-anchor links to work
	if(window.location.hash){
		setTimeout(function(){
			$("#productBottom .itemSpec a").each(function(){
				if($(this).attr("href") == window.location.hash){ 
					$(this).click();
					this.scrollIntoView();
					return false; //were done, break out
				}
			});
		}, 1000);
	}
});

//start Glossary 
function createGloss(targetDivID, defFile){
	targetCollection = $("#" + targetDivID + " *").not("a");  // cache jquery collection of items to add definitions to.
		$.get(defFile,  function(data){ // use ajax to get definitions file
			$(data).find("name").each(function(){ //run for all definition names
				 //case insensitive (i) and  global (g) regex (the parentheses give us a reference to the original string ($1).)
				var regex = new RegExp("\\b(" + $(this).text() + ")\\b", "ig");
				targetCollection.replaceText(regex , "<span class='dictDef'>$1</span>"); //jquery plugin does the replacing for us
			});
			
			$("#" + targetDivID + " span.dictDef").removeNested("span.dictDef"); // custom jquery plugin
			defItems = $("#" + targetDivID + " span.dictDef"); //cache all definition tags
			defItems.one("mouseover", function(){ // create all functionality on the first mouseover
				  var myText = $(data).find("name").textEquals($(this).text()); // custom plugin 
				  var defText = myText.siblings("desc").text();
				  $(this).append("<div class='gloss'><h5>" + myText.text() + "</h5><p>" + defText + "</p><span class='closeMe'>close</span></div>");
				  $(this).click(function(){
						$(".activeDefLink .closeMe").click();
						$(this).addClass("activeDefLink");
						$(this).find(".closeMe").one("click", function(){
							heli = 0; // stop the hover functionality
							$(this).parents(".dictDef").removeClass("activeDefLink");//console.log("closed " + $(this)[0]);
							return false; // stop click from bubbling up and activating parent click
						});
						$(this).find(".gloss").click(function(){return false});
						$(this).modalize();
						// if.. is IE    and  "Whats in the box" exists   and..       the layer would slip under "Whats in the box" ...
						if($.browser.msie && $(".witbTitle").length && ($(this).offset().left + 275) >= $(".itemIncContainer").offset().left){
							$(this).find(".gloss" ).css({left:"auto", right:0}); // flip the layer to the other side
						}
						return false;
					});
				  $(this).hover(function(){
						heli = 1;
						heliSelector = $(this);
						setTimeout(hoverDelay,1000);
					},function(){
						heli = 0;
					});
					heli = 1; heliSelector = $(this); setTimeout(hoverDelay,1000);
					$(this).one("mouseout", function(){heli = 0;});
			});				
		},"xml");
	
	
}
//custom plugin to recursively remove extra tags of the same type
$.fn.removeNested = function(selector){
	this.find(selector).each(function(){ // stop the madness - when the collection returned is empty the recursion stops
		$(this).removeNested(selector); // recursive call
		$(this).replaceWith($(this).text()); // doin my thing
	}); 
	return this;
}
$.fn.textEquals = function(text){
	this.each(function(){
		if($(this).text().myStandard() == text.myStandard()) rv = $(this);
	});
	return rv;
}
$.fn.modalize = function(){
	myModal = this;
	$(document).one("click", function(e){
		if(e.which && e.which == 3){ // stop right click in ff
			myModal.modalize(); 
			return true;
		}
		var clicked = $(e.target);
		if(clicked.is(myModal.find("closeMe"))) return true;
		if(clicked.is(myModal) || clicked.parents().is(myModal)) {
			myModal.modalize(); 
			return this;
		}
		myModal.find(".closeMe").click();
		return true;
	});
	return this;
}
String.prototype.myStandard = function(){
	return this.toLowerCase().replace( /[^a-z0-9]/g, "");
}

var heli;
var heliSelector;
function hoverDelay(){
	if(heli)
	heliSelector.click();
}
// End Glossary


function bindClickTale(){	
	$(".ClickTaleTag").click(function(){
		ClickTaleTag($(this).attr("id"));	
		ClickTaleExec('$(\'#' + this.id + '\').click()');		
	});
}
/*
 * jQuery replaceText - v1.1 - 11/21/2009
 * http://benalman.com/projects/jquery-replacetext-plugin/
 * 
 * Copyright (c) 2009 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
(function($){$.fn.replaceText=function(a,b,c){return this.each(function(){var e=this.firstChild,g,d,f=[];if(e){do{if(e.nodeType===3){g=e.nodeValue;d=g.replace(a,b);if(d!==g){if(!c&&/</.test(d)){$(e).before(d);f.push(e)}else{e.nodeValue=d}}}}while(e=e.nextSibling)}f.length&&$(f).remove()})}})(jQuery);