// 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,email,twitter,favorites ,myspace, live,google, stumbleupon,delicious,more',
          services_exclude: 'amazonwishlist'
}


$(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").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();
		});
	}
	
});

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++;
						});
	});
});

function bindClickTale(){	
	$(".ClickTaleTag").click(function(){
		ClickTaleTag($(this).attr("id"));	
		ClickTaleExec('$(\'#' + this.id + '\').click()');		
	});
}