
/**
 * editContent() - opens the content html editor 
 **/ 
function editContent( args , contextPath ) { 
	openPopupWindow( contextPath + "/editor.htm?" + args ,'PageEditor',970,700);
}


/**
 * editPage() - opens the cds-page editor
 **/ 
function editPage( pageId  , contextPath) { 
 	 openPopupWindow(contextPath + "/editpage.htm?pageId=" + pageId, 'PageEditor',850,600);
}
 
/**
 * toggleSlider() - show hide div
 **/ 
function toggleSlider(imageId, divId, contextPath){  
	image = (jQuery('#'+divId).is(':visible')) ?  contextPath +"/images/button/rightArrow.gif" : contextPath + "/images/button/downArrow.gif";
	jQuery(document).find('#' + divId).slideToggle();  
	jQuery('#'+imageId).attr("src",image);
	
	
}

/**
 * toggleSlider() - show hide div
 **/ 
function openSlider(divId, contextPath){  
	imageId =  divId + '_img' ;
	sliderId = divId + '_slider' ; 
	image =  contextPath +"/images/button/downArrow.gif";
	jQuery(document).find('#' + sliderId).slideDown("normal");
	jQuery('#'+imageId).attr("src",image);
	 
	
}



/**
 * getPageContent() - loads content via an async ajax call 
 *
 **/  
function getPageContent( pageId, resourceType, divId, contextPath ) {

	jQuery.ajax({ 
	   type: "GET",
	   url:   contextPath + "/content.htm",
	   async: true, 
	   cache: false, 
	   data: "pageId=" + pageId + "&resourceType=" + resourceType  ,
	   success: function(html){ 
			jQuery("#" + divId).append(html);  
			jQuery("a[rel^='lightbox']").lightBox({fixedNavigation:true}); 
		}
	 }); 

}


/**
 * getPageContent() - loads content via an async ajax call and wraps the 
 * 					  reponse with the before/after html.  
 *
 **/ 
function getProductContent(queryString, divId, beforeHtmlFragement , afterHtmlFragement, contextPath  ) {

	getProductContent(queryString, divId, beforeHtmlFragement , afterHtmlFragement, false, contextPath);
}


/**
 * getProductContent() 
 *
 *
 **/  
function getProductContent(queryString, divId, beforeHtmlFragement , afterHtmlFragement, asyncFlag, contextPath ) {
	
	jQuery.ajax({ 
	   type: "GET",
	   url:  contextPath + "/content.htm",
	   async: asyncFlag, 
	   cache: false, 
	   data: queryString , 
	   success: function(html){ 
	   
	   		// response is empty 
	    	if ( (html==null) || (html.length < 1)) {
	    		return;
	    	}
	    
	    	beforeHtmlFragement=(beforeHtmlFragement!=null) ? beforeHtmlFragement : '';
	    	afterHtmlFragement = (afterHtmlFragement!=null) ? afterHtmlFragement : '';
			jQuery("#" + divId).append(beforeHtmlFragement + html + afterHtmlFragement);  
			
			initScrollingAnchors(divId,contextPath);
			
			
		} 
	 });  
}

function getProductContentWithoutSlider(queryString, asyncFlag, contextPath ) {
 
	jQuery.ajax({ 
		type: "GET",
		url:  contextPath + "/content.htm",
		async: asyncFlag, 
		cache: false, 
		data: queryString , 
		success: function(html) {
	   
			// response is empty 
			if (html == null || html.length < 1)
				return document.write('Not available.');
	    		
			document.write(html);
		}
	});  
}

function getParentSliderDivId(anchorName) {
	var sliderId= null;
	jQuery("a[name=" + anchorName +"]").parents().each(function() {
		if ( this.id=='extendeddetails' || this.id=='manufacturing' ||
			 this.id=='tolerances' || this.id=='details' ) {
			 sliderId=this.id;
			 return false;
		}
	 });

	 return sliderId;
	 
}


function initScrollingAnchors(divId,contextPath) {

	// initialize scrolling archors for links in the current  div only 
	jQuery("#" + divId + " a.scroll").click(function () {
	 
		elementClick = jQuery(this).attr("href");
		pos = elementClick.indexOf("#");
		if (pos < 0) { 
			return;
		}
		
		anchorName = elementClick.substr(pos+1);
		sliderDivId = getParentSliderDivId(anchorName);
		if (sliderDivId!=null) {
			openSlider(sliderDivId,contextPath );	
		}
		
		destination = jQuery("a[name=" + anchorName +"]").offset().top;
		jQuery("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1100 );
	  	return false;
	});
}

 
  
/**
 * getProductContent() 
 *
 *
 **/  
function renderIconIfImageExists(imagesPath, applicationPath, list, overlayContainerDiv, overlayDiv, imageClassDiv, showOverlay) {

	var t;

	if (showOverlay) {

		jQuery(overlayDiv).css('height', jQuery(list).height());
		
		jQuery(overlayContainerDiv).show();
	}
		
	var numberOfProductImages = jQuery(imageClassDiv).size();
	
	jQuery(imageClassDiv).each(function() {
	
		var mserveNumber = jQuery(this).attr('id');
			  	
		var xhr = jQuery.ajax({
			type:	"GET",
			url:	applicationPath + "/content.htm",
			async:	true,
			cache:	false, 
			data:	"excludeAssetContents=Y&mserveNumber=" + mserveNumber, 
			success: function(html) {
		   	
				assetId = xhr.getResponseHeader("assetId");
			  	
				if (assetId != null && assetId.length > 1) {
			  	
					var anchorImage = jQuery('<a id="a_' + mserveNumber + '" tabindex="-1"  href="' + applicationPath + '/content.htm?view=productSize&assetId=' + assetId  + '" ><img  height="24" id="img_' + mserveNumber + '" /></a>');
			  		
			  		// Add anchor and image to html
					jQuery("#ajax-loader_" + mserveNumber).replaceWith(anchorImage);
					// Set the image
					jQuery("#img_" + mserveNumber).attr('src', imagesPath + '/images/camera_unmount_48x48_autocroped.gif');
					// Add the lightbox effect to the image
					jQuery('#a_' + mserveNumber).lightBox({ fixedNavigation: true });
					
					numberOfProductImages = numberOfProductImages - 1;
					
					if (numberOfProductImages == 0 && showOverlay) {
					
						clearTimeout(t);

						jQuery(overlayContainerDiv).hide();
					}
			  	} else {
			  		
			  		// Remove children from the html
					jQuery("#" + mserveNumber).empty();
					
					numberOfProductImages = numberOfProductImages - 1;
					
					if (numberOfProductImages == 0 && showOverlay) {
					
						clearTimeout(t);

						jQuery(overlayContainerDiv).hide();
					}
				} 		
			} 
		});
	});
	
	if (showOverlay) {
	
		// set timeout to hide overlay after 1 minute
		// (the overlay may already be hidden if the AJAX calls have completed)
		t = setTimeout('removeLoaderIcon("' + imageClassDiv + '"); jQuery("' + overlayContainerDiv + '").hide();', (30 * 1000));
	}
}

function removeLoaderIcon(imageClassDiv) {

	jQuery(imageClassDiv).each(function() {

		var mserveNumber = jQuery(this).attr('id');
		
		if (jQuery("#ajax-loader_" + mserveNumber).length > 0) {
			
			// Remove children from the html
			jQuery("#" + mserveNumber).empty();		
		}
	});
}

/**
 * getItemStockAvailability() - loads item availability via an async ajax call 
 *
 **/  
function getItemStockAvailability( holeserveItemNumber, divId) {

	jQuery.ajax({ 
	   type: "GET",
	   url:   "/item_availability.htm",
	   async: true, 
	   cache: false, 
	   data: "holeserveItemNumber=" + holeserveItemNumber  ,
	   success: function(html){ 
			var available = html.split(":");
			var displayText = "";
			
			if (html.length < 100) {
				if (available[0] > 0) {
					displayText = "<span class='availablemessage'>" + "In Stock" + "</span>";
				} else {
					if (available[2] != undefined) {
						displayText = "<span class='etamessage'>" + available[2] + "</span>";
					}
				}
			}
			
			jQuery("#" + divId).append(displayText);
		}
	 }); 

}

/**
 * getAvailabilityStatus(itemId) - gets the Availability Status that was populated by the AJAX call 
 *
 **/  
function getAvailabilityStatus(itemId) {
	return jQuery("#availability_" + itemId).html().replace(/(<\/?[^>]+>|\s| )/gi," ").replace(/\s\s+/g," ").replace(/^.*; /g,"");
}
 

